Test instructions: Give the n person's M-call, ask how many rings are finally formed, find out all the rings
Oneself think is: Use map to store the name of the person, each of the names mapped into a number, then use and check set, find out how many blocks connected block, output
But the map is not familiar, can't write, and use and check the output when the feeling seems very troublesome
And then use the transitive closure, but judging to d[i][j]==1 and d[j][i]==1, how to output the path?
So I looked at the LRJ code = =
Using an ID (string s) function to give the name number, and the fifth chapter of the collection stack computer the same way
Then use the DFS output path = = (This should be good----good--learn)
Finally there is the time to initialize the d[i][i]=1, a person itself is a unicom block, so should be initialized to 1
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <stack>6#include <vector>7#include <map>8#include <Set>9#include <queue>Ten#include <algorithm> One #definemod=1e9+7; A using namespacestd; - -typedefLong LongLL; the Const intINF =0x7fffffff; - -vector<string>names; - strings1,s2; + intvis[1005],d[ the][ the]; - intn,m; + A at intID (string&s) { - for(intI=0; I<names.size (); i++){ - if(names[i]==s)returni; - } - Names.push_back (s); - returnNames.size ()-1; in } - to voidDfsintu) { +vis[u]=1; - for(intv=0; v<n;v++){ the if(!vis[v]&&d[u][v]&&D[v][u]) { *printf",%s", Names[v].c_str ()); $ Dfs (v);Panax Notoginseng } - } the } + A intMain () { the intI,j,k,kase=0; + while(SCANF ("%d%d", &n,&m)!=eof&&N) { -memset (D,0,sizeof(d)); $memset (Vis,0,sizeof(Vis)); $ names.clear (); - - for(i=0; i<n;i++) d[i][i]=1; the - for(i=0; i<m;i++){WuyiCin>>s1>>S2; theD[id (S1)][id (S2)]=1; - } Wu - for(k=0; k<n;k++) About for(i=0; i<n;i++) $ for(j=0; j<n;j++) -d[i][j]=d[i][j]| | (d[i][k]&&d[k][j]); - - if(Kase >0) printf ("\ n"); Aprintf"calling circles for data set%d:\n", ++Kase); + the for(i=0; i<n;i++){ - if(!Vis[i]) { $printf"%s", Names[i].c_str ()); the DFS (i); theprintf"\ n"); the } the } - } in return 0; the}
View Code
Go---Go---go----go---------
UVa 247 Calling Circles "delivery closure"