n Person (n<=25), play M-call, if A to B and B to a, then a, a, a, a, in the same telephone circle. Find all the phone rings and output the same circle owner.
n is very small, building an adjacency matrix, Map[i][j] indicates that the person numbered I called the person numbered J.
Run through the Floyd to find the delivery closure.
Finally, we use and check the set to group.
Purple Book has another method of grouping, that is, a new diagram, the same phone circle of people interconnect an edge, and then output each of the connected components of the owner.
Attached code
#include <stdio.h> #include <string> #include <string.h> #include <iostream> #include <map
> #include <vector> #include <set> #define MAXN using namespace std;
int n,m;
BOOL MAP[MAXN][MAXN];
int cnt;
map<string,int>int;
map<int,string>str;
Vector<set<int> >vec;
int P[MAXN];
int DONE[MAXN];
void Init () {memset (map,0,sizeof (MAP));
Int.clear ();
Str.clear ();
cnt=0;
for (int i=0;i<maxn;i++) p[i]=i;
Vec.clear ();
memset (done,0,sizeof (done));
set<int>s;
Vec.push_back (s);
} void Get () {string s1,s2;
for (int i=0;i<m;i++) {cin>>s1>>s2; if (!int[s1]) {int[s1]=++cnt;
STR[CNT]=S1;} if (!int[s2]) {int[s2]=++cnt;
STR[CNT]=S2;}
Map[int[s1]][int[s2]]=true; }} void Floyd () {for (int k=1;k<=cnt;k++) for (int. i=1;i<=cnt;i++) for (int j=1;j<=cnt;j+ +) map[i][j]=map[i][j]| | (map[i][k]&&mAP[K][J]);
} int find (int x) {return p[x]==x?x:p[x]=find (p[x]);}
void Pre () {Floyd ();
for (int i=1;i<=cnt;i++) for (int j=1;j<=cnt;j++) if (I!=j&&map[i][j]&&map[j][i])
{int xx=find (i);
int Yy=find (j);
if (xx!=yy) p[xx]=yy;
} for (int i=1;i<=cnt;i++) {int id=find (i);
if (!done[id]) {done[id]=vec.size ();
Set<int> s;
S.insert (i);
Vec.push_back (s);
} else Vec[done[id]].insert (i);
} for (unsigned int i=1;i<vec.size (); i++) {set<int>& s=vec[i];
Set<int>::iterator It=s.begin ();
for (; It!=s.end (); it++) {if (It!=s.begin ()) cout<< ",";
cout<<str[*it];
} puts ("");
}} int Kase; int main () {while (scanf ("%d%d", &n,&m), n| | m) {if (Kase)Puts ("");
printf ("Calling circles for data set%d:\n", ++kase);
Init ();
Get ();
Pre ();
}
}