-
Notifications
You must be signed in to change notification settings - Fork 138
/
Solution.cpp
81 lines (63 loc) · 1.49 KB
/
Solution.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
#include<bits/stdc++.h>
using namespace std;
#define MAX 100000
#define D(x) cout << #x " = " << (x) << endl
vector<int> edge[MAX+7];
int csum[MAX+7], st[MAX+7], ed[MAX+7], parent[MAX+7], indx;
void dfs(int idx, int par = -1)
{
parent[idx] = par;
indx++;
st[idx] = indx;
for(auto x : edge[idx]){
if(x == par) continue;
dfs(x, idx);
}
ed[idx] = indx;
}
int main()
{
int i, j, k, t, cs, n, u, v, q;
int valid;
scanf("%d", &t);
while(t--)
{
memset(csum, 0, sizeof(csum));
valid = indx = 0;
scanf("%d", &n);
for(i = 1; i < n; i++)
{
scanf("%d %d", &u, &v);
edge[u].push_back(v);
edge[v].push_back(u);
}
int root = 1;
dfs(root);
scanf("%d %d", &q, &k);
for(i = 1; i <= q; i++)
{
scanf("%d %d", &u, &v);
if(parent[v] == u){
csum[st[root]]++;
csum[ed[root]+1]--;
csum[st[v]]--;
csum[ed[v]+1]++;
}
else{
csum[st[u]]++;
csum[ed[u]+1]--;
}
}
for(i = st[root]; i <= ed[root]; i++)
{
csum[i] += csum[i - 1];
if(csum[i] >= k)
valid++;
}
int g = __gcd(valid, n);
printf("%d/%d\n", valid / g, n / g);
for(i = 1; i <= n; i++)
edge[i].clear();
}
return 0;
}