floyd+DFS
Time Limit: 3000MS     64bit IO Format: %lld & %llu

 

 

map存人名,floyd传递闭包,DFS查询。

输出答案的逗号后面还有个空格,被坑到了233

 1 /*by SilverN*/
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 #include<map>
 8 using namespace std;
 9 const int mxn=101;
10 map<string,int>mp;
11 int id;
12 char s[mxn][mxn];
13 int e[mxn][mxn];
14 bool vis[mxn];
15 int n,m;
16 void DFS(int u){
17     for(int i=1;i<=n;i++){
18         if(!vis[i] && e[u][i] && e[i][u]){
19             vis[i]=1;
20             printf(", %s",s[i]);
21             DFS(i);
22         }
23     }
24     return;
25 }
26 int main(){
27     int i,j;
28     int cas=0;
29     while(scanf("%d%d",&n,&m)){
30         if(!n && !m)break;
31         mp.clear();id=0;
32         memset(e,0,sizeof e);
33         char u[mxn],v[mxn];
34         for(i=1;i<=m;i++){
35             scanf("%s%s",u,v);
36             if(!mp[u]){mp[u]=++id;memcpy(s[mp[u]],u,sizeof u);}
37             if(!mp[v]){mp[v]=++id;memcpy(s[mp[v]],v,sizeof v);}
38             e[mp[u]][mp[v]]=1;
39         }
40         for(int k=1;k<=n;k++)
41          for(i=1;i<=n;i++)
42           for(j=1;j<=n;j++){
43               e[i][j]|=e[i][k]&e[k][j];
44           }
45         memset(vis,0,sizeof vis);
46         if(cas)printf("\n");
47         printf("Calling circles for data set %d:\n",++cas);
48         for(i=1;i<=n;i++){
49             if(!vis[i]){
50                 vis[i]=1;
51                 printf("%s",s[i]);
52                 DFS(i);
53                 printf("\n");
54             }
55         }
56     }
57     return 0;
58 }