-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path247.cpp
More file actions
108 lines (102 loc) · 2.48 KB
/
247.cpp
File metadata and controls
108 lines (102 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* ____
* ____ ___ ____ ________ __/ __/
* / __ `__ \/ __ `/ ___/ / / / /_
* / / / / / / /_/ / / / /_/ / __/
* /_/ /_/ /_/\__,_/_/ \__,_/_/
*
* @link : https://the-redback.com
*/
#include <bits/stdc++.h>
using namespace std;
#define ppb pop_back
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define inf 1000000000
#define eps 1e-9
#define NN 100
vector<int> arr;
vector<int> e[NN];
vector<int> te[NN];
vector<string> ans[NN];
vector<string> vs;
map<string, int> mp;
int color[NN];
void dfs1(int u) {
color[u] = true;
for (int i = 0; i < e[u].size(); i++) {
int v = e[u][i];
if (!color[v]) dfs1(v);
}
arr.pb(u);
return;
}
void dfs2(int u, int flag) {
color[u] = flag;
for (int i = 0; i < te[u].size(); i++) {
int v = te[u][i];
if (!color[v]) dfs2(v, flag);
}
return;
}
string s, ss;
int main() {
// ios_base::sync_with_stdio(false);
int t = 0, tc;
int n, m, i, j, l;
// cin>>tc;
while (cin >> n >> m) {
if (n == 0 && m == 0) return 0;
int r = 0;
for (i = 0; i < m; i++) {
cin >> s >> ss;
if (mp.find(s) == mp.end()) {
mp[s] = r++;
vs.pb(s);
}
if (mp.find(ss) == mp.end()) {
mp[ss] = r++;
vs.pb(ss);
}
e[mp[s]].pb(mp[ss]);
te[mp[ss]].pb(mp[s]);
}
mem(color, 0);
for (i = 0; i < n; i++) {
if (!color[i]) dfs1(i);
}
reverse(all(arr));
int mx = -inf;
r = 1;
mem(color, 0);
for (i = 0; i < arr.size(); i++) {
if (!color[arr[i]]) {
dfs2(arr[i], r++);
mx = max(mx, r);
}
}
for (i = 0; i < n; i++) {
int u = color[i];
ans[u].pb(vs[i]);
}
if (t) puts("");
printf("Calling circles for data set %d:\n", ++t);
for (i = 1; i < mx; i++) {
for (j = 0; j < ans[i].size(); j++) {
if (j) printf(", ");
cout << ans[i][j];
}
puts("");
}
mp.clear();
vs.clear();
arr.clear();
for (i = 0; i <= n; i++) {
e[i].clear();
te[i].clear();
ans[i].clear();
}
}
return 0;
}