Original title Link:
Https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=4&page=show_problem &problem=183
Main topic:
Output each person in the phone ring.
Any person in the phone ring can call directly or indirectly to any other person in the circle.
Note the output: ', ' there are spaces after each of the two sets of test data in an empty row.
See the code:
#include <iostream> #include <cstring> #include <string> #include <map> #include <vector>
using namespace Std;
const int N = 25 + 1;
int g[n][n];
BOOL vis[n];//Record has accessed int k,n,m;
vector<string>name;//the name of the corresponding order map<string, int>peo;//the relationship between the name and order void Init () {memset (g, 0, sizeof (g));
memset (Vis, 0, sizeof (VIS));
Name.clear ();
Peo.clear ();
k = 0;
} void DFS (int u) {Vis[u] = true; for (int v = 0; v < n; v++) {if (G[u][v]&&g[u][v]==g[v][u]&&!vis[v])//Two people can contact each other and have not accessed {Co
UT << "," << name[v];
DFS (v);
}}} int main () {int kase=1; while (CIN >> n >> m&& (n| |
m)//n==0&&m==0 terminate loop {init ();
string u, v;
for (int i = 0; i < m; i++) {cin >> u >> v;
if (!peo.count (U)) {Peo[u] = k++;
Name.push_back (U);
} if (!peo.count (v)) {peo[v] = k++;
Name.push_back (v);
} G[peo[u]][peo[v]] = 1; } for (int h = 0; h < n; h+ +)//floyd transitive closure processing, so that the indirect contact can be directly linked to {for (int i = 0; i < n; i++) {if (G[i][h]) {(int j = 0; J & Lt
n;j++) if (G[h][j]) g[i][j] = 1;
}}} if (kase>1) cout << Endl;
cout << "Calling circles for data set" << kase++ << ":" << Endl;
for (int i = 0; i < n; i++) {if (!vis[i]) {cout << name[i];
DFS (i);
cout << Endl;
}}} return 0; }