-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoj1470a.cpp
More file actions
108 lines (89 loc) · 2.37 KB
/
poj1470a.cpp
File metadata and controls
108 lines (89 loc) · 2.37 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
#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 deep[MAXN], father[MAXN][DEG];
int eidx;
bool isChild[MAXN];
int result[MAXN];
void init() {
eidx = 0;
memset(isChild, false, sizeof(isChild));
memset(Ehead, -1, sizeof(Ehead));
memset(result, 0, sizeof(result));
}
void add_edge(int from, int to) {
E[eidx].to = to;
E[eidx].nxt = Ehead[from];
Ehead[from] = eidx++;
}
void BFS(int root) {
queue<int> Q;
deep[root] = 0;
father[root][0] = root;
Q.push(root);
while(!Q.empty()) {
int u = Q.front(); Q.pop();
for(int i = 1; i < DEG; ++i)
father[u][i] = father[father[u][i-1]][i-1];
for(int i = Ehead[u]; i != -1; i = E[i].nxt) {
int v = E[i].to;
deep[v] = deep[u] + 1;
father[v][0] = u;
Q.push(v);
}
}
}
// v is deeper
int LCA(int u, int v) {
if(deep[u] > deep[v]) swap(u, v);
int hv = deep[v], hu = deep[u];
int tu = u, tv = v;
// raise v to same deep as u
for(int diff = hv - hu, i = 0; diff; ++i, diff>>=1)
if(diff&1) // 5 = 101 = 2^0 + 2^2
tv = father[tv][i];
if(tu==tv) return tu;
// raise both u and v and find the intersect point
for(int i = DEG-1; i >= 0; --i) {
if(father[tu][i] == father[tv][i]) continue;
tu = father[tu][i];
tv = father[tv][i];
}
return father[tu][0];
}
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;
}
}
for(int i = 1; i <= n; ++i)
if(!isChild[i])
BFS(i);
scanf("%d\n", &m);
for(int i = 0; i < m; ++i) {
scanf("(%d %d) ", &curNode, &toNode);
int lca = LCA(curNode, toNode);
result[lca]++;
}
for(int i = 1; i <= n; ++i)
if(result[i])
printf("%d:%d\n", i, result[i]);
}
return 0;
}