-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path191C.cpp
More file actions
89 lines (82 loc) · 1.46 KB
/
Copy path191C.cpp
File metadata and controls
89 lines (82 loc) · 1.46 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
#include<bits/stdc++.h>
using namespace std;
int n, m;
int dep[100009], lca[100009][25];
int d[100009];
struct edge{
int x, y;
}a[100009];
struct Edge{
int to, nxt;
}e[200009];
int head[100009], tot;
void add(int x, int y){
e[++tot].to = y;
e[tot].nxt = head[x];
head[x] = tot;
}
void dfs(int x, int fa){
dep[x] = dep[fa] + 1;
for(int i = head[x]; i; i = e[i].nxt){
int y = e[i].to;
if(y == fa) continue;
lca[y][0] = x;
dfs(y, x);
}
}
void init_lca(){
for(int i = 1; i < 20; i++){
for(int j = 1; j <= n; j++){
lca[j][i] = lca[lca[j][i - 1]][i - 1];
}
}
}
int glca(int x, int y){
if(dep[x] < dep[y]) swap(x, y);
for(int i = 19; i >= 0; i--){
if(dep[lca[x][i]] >= dep[y]){
x = lca[x][i];
}
}
if(x == y) return x;
for(int i = 19; i >= 0; i--){
if(lca[x][i] != lca[y][i]){
x = lca[x][i];
y = lca[y][i];
}
}
return lca[x][0];
}
void dfs2(int x, int fa){
for(int i = head[x]; i; i = e[i].nxt){
int y = e[i].to;
if(y == fa) continue;
dfs2(y, x);
d[x] += d[y];
}
}
int main(){
scanf("%d", &n);
for(int i = 1; i <= n - 1; i++){
scanf("%d%d", &a[i].x, &a[i].y);
add(a[i].x, a[i].y);
add(a[i].y, a[i].x);
}
dfs(1, 0);
init_lca();
scanf("%d", &m);
for(int i = 1; i <= m; i++){
int x, y;
scanf("%d%d", &x, &y);
int lcaab = glca(x, y);
d[x]++, d[y]++;
d[lcaab] -= 2;
}
dfs2(1, 0);
for(int i = 1; i <= n - 1; i++){
int x = a[i].x, y = a[i].y;
if(dep[x] < dep[y]) swap(x, y);
printf("%d ", d[x]);
}
return 0;
}