-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoj1470b.cpp
More file actions
106 lines (84 loc) · 2.13 KB
/
poj1470b.cpp
File metadata and controls
106 lines (84 loc) · 2.13 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
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define MAXN 1000
#define DEG 15
struct edge {
int to, nxt;
} E[MAXN*1000], QRY[MAXN*1000];
int Ehead[MAXN];
int QRYhead[MAXN];
int parent[MAXN];
bool visited[MAXN];
int eidx, qidx;
bool isChild[MAXN];
int result[MAXN];
void init() {
eidx = qidx = 0;
memset(isChild, false, sizeof(isChild));
memset(Ehead, -1, sizeof(Ehead));
memset(QRYhead, -1, sizeof(QRYhead));
memset(result, 0, sizeof(result));
memset(visited, false, sizeof(visited));
}
void add_edge(int from, int to) {
E[eidx].to = to;
E[eidx].nxt = Ehead[from];
Ehead[from] = eidx++;
}
void add_query(int from, int to) {
QRY[qidx].to = to;
QRY[qidx].nxt = QRYhead[from];
QRYhead[from] = qidx++;
QRY[qidx].to = from;
QRY[qidx].nxt = QRYhead[to];
QRYhead[to] = qidx++;
}
int find_parent(int x) {
if(parent[x] != x)
parent[x] = find_parent(parent[x]);
return parent[x];
}
void Tarjan(int u) {
parent[u] = u;
for(int i = Ehead[u]; i != -1; i = E[i].nxt) {
int v = E[i].to;
Tarjan(v);
parent[v] = u;
}
visited[u] = true;
for(int i = QRYhead[u]; i != -1; i = QRY[i].nxt) {
int v = QRY[i].to;
if(!visited[v]) continue;
result[find_parent(v)]++;
}
}
int main() {
// freopen("input.txt", "r", stdin);
int n, m, curNode, toNode, ntoNode;
while(scanf("%d", &n) != EOF) {
init();
for(int i = 0; i < n ;++i) {
scanf("%d:(%d)", &curNode, &ntoNode);
for(int i2 = 0; i2 < ntoNode; ++i2) {
scanf("%d", &toNode);
add_edge(curNode, toNode);
isChild[toNode] = true;
}
}
scanf("%d\n", &m);
for(int i = 0; i < m; ++i) {
scanf("(%d %d) ", &curNode, &toNode);
add_query(curNode, toNode);
}
for(int i = 1; i <= n; ++i)
if(!isChild[i])
Tarjan(i);
for(int i = 1; i <= n; ++i)
if(result[i])
printf("%d:%d\n", i, result[i]);
}
return 0;
}