-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11709.cpp
More file actions
97 lines (90 loc) · 2 KB
/
11709.cpp
File metadata and controls
97 lines (90 loc) · 2 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
/**
* ____
* ____ ___ ____ ________ __/ __/
* / __ `__ \/ __ `/ ___/ / / / /_
* / / / / / / /_/ / / / /_/ / __/
* /_/ /_/ /_/\__,_/_/ \__,_/_/
*
* @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 1010
vector<int> e[NN];
vector<int> te[NN];
map<string, int> mp;
int color[NN];
vector<int> arr;
void dfs1(int u) {
color[u] = 1;
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;
}
int scc(int n) {
int i;
mem(color, 0);
for (i = 1; i <= n; i++) {
if (!color[i]) dfs1(i);
}
mem(color, 0);
int cnt = 0;
int r = 0;
reverse(all(arr));
for (i = 0; i < arr.size(); i++) {
if (!color[arr[i]]) {
dfs2(arr[i], ++r);
cnt++;
}
}
return cnt;
}
string s, ss;
char a[100];
int main() {
// ios_base::sync_with_stdio(false);
int t = 0, tc;
int n, m, i, j, l, k;
// cin>>tc;
while (cin >> n >> m) {
if (n == 0 && m == 0) return 0;
getchar();
int r = 1;
for (i = 0; i < n; i++) {
gets(a);
s.assign(a);
mp[s] = r++;
}
for (i = 0; i < m; i++) {
gets(a);
s.assign(a);
gets(a);
ss.assign(a);
e[mp[s]].pb(mp[ss]);
te[mp[ss]].pb(mp[s]);
}
int cnt = scc(n);
printf("%d\n", cnt);
for (i = 0; i <= n; i++) e[i].clear(), te[i].clear();
arr.clear();
mp.clear();
}
return 0;
}