-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUVa-11709-Trust-groups.cpp
93 lines (71 loc) · 1.77 KB
/
UVa-11709-Trust-groups.cpp
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
#pragma GCC optimize ("Ofast")
#include <bits/stdc++.h>
#define endl '\n'
#define Min(a, b) (((a) < (b)) ? (a) : (b))
using namespace std;
typedef int64_t ll;
void Fast() {
cin.sync_with_stdio(0);
cin.tie(0);cout.tie(0);
}
void File() {
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
}
const int N = 1e5 + 9, M = 2e6 + 9, oo = 0x3f3f3f3f;
ll INF = 0x3f3f3f3f3f3f3f3f;
int Head[N], Next[M], To[M], dfs_num[N], dfs_low[N], ne, n, m, tax, dfs_timer, component;
int Stack[N], top;
bool in_stack[N];
string u, v;
map <string, int> inv;
void addEdge(int from, int to) {
Next[++ne] = Head[from];
Head[from] = ne;
To[ne] = to;
}
void _clear() {
memset(Head, 0, sizeof(Head[0]) * (n + 2));
memset(dfs_num, 0, sizeof(dfs_num[0]) * (n + 2));
ne = dfs_timer = top = component = 0;
inv.clear();
}
void Tarjan(int node)
{
dfs_num[node] = dfs_low[node] = ++dfs_timer;
in_stack[Stack[top++] = node] = true;
for(int i = Head[node]; i; i = Next[i]) {
if(dfs_num[To[i]] == 0)
Tarjan(To[i]);
if(in_stack[To[i]])
dfs_low[node] = Min(dfs_low[node], dfs_low[To[i]]);
}
if(dfs_num[node] == dfs_low[node]) {
++component;
for(int cur = -1; cur ^ node; in_stack[cur = Stack[--top]] = false) {}
}
}
void Solve()
{
_clear();
cin.ignore();
for(int i = 1; i <= n; ++i) {
getline(cin, u);
inv[u] = i;
}
while(m--) {
getline(cin, u);
getline(cin, v);
addEdge(inv[u], inv[v]);
}
for(int i = 1; i <= n; ++i) if(!dfs_num[i])
Tarjan(i);
cout << component << endl;
}
int main()
{
Fast();
int tc = 1;
for(int i = 1; (cin >> n >> m) && (n || m); ++i)
Solve();
}