题目连接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=183
1 #include<cstdio> 2 #include<cstring> 3 #include<vector> 4 #include<map> 5 #include<string> 6 #include<iostream> 7 using namespace std; 8 const int maxn=30; 9 map<string,int> namecode; 10 string codename[maxn]; 11 int p[maxn][maxn]; 12 int vis[maxn]; 13 int n,m; 14 void floyd() 15 { 16 for(int k=1;k<=n;k++) 17 for(int i=1;i<=n;i++) 18 if(p[i][k]) for(int j=1;j<=n;j++) 19 if(p[k][j]) p[i][j]=1; 20 } 21 22 void dfs(int u) 23 { 24 vis[u]=1; 25 for(int i=1;i<=n;i++) 26 if(!vis[i]&&p[u][i]&&p[i][u]) 27 { 28 cout<<", "<<codename[i]; 29 dfs(i); 30 } 31 } 32 int main() 33 { 34 35 int cas=0; 36 string a,b; 37 while(scanf("%d%d",&n,&m)&&(n||m)) 38 { 39 memset(p,0,sizeof(p)); 40 memset(vis,0,sizeof(vis)); 41 namecode.clear(); 42 int id=0; 43 while(m--) 44 { 45 cin>>a>>b; 46 if(!namecode.count(a)) { namecode[a]=++id;codename[id]=a; } 47 if(!namecode.count(b)) { namecode[b]=++id;codename[id]=b; } 48 int u=namecode[a]; 49 int v=namecode[b]; 50 p[u][v]=1; 51 } 52 if(cas>1) puts(""); 53 printf("Calling circles for data set %d:\n",++cas); 54 floyd(); 55 for(int i=1;i<=n;i++) if(!vis[i]) 56 { 57 cout<<codename[i]; 58 dfs(i); 59 puts(""); 60 } 61 62 } 63 }
时间: 2024-11-09 03:56:33