-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApple Tree.cpp
More file actions
65 lines (45 loc) · 1.14 KB
/
Copy pathApple Tree.cpp
File metadata and controls
65 lines (45 loc) · 1.14 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
#include <bits/stdc++.h>
using namespace std;
vector<vector<long long> > matriz;
vector<bool> visitado;
vector<long long> dp;
void DFS(long long actual){
visitado[actual] = true;
long long ady = 0;
for(long long i = 0; i < matriz[actual].size(); i ++){
if(visitado[matriz[actual][i]] == false){
DFS(matriz[actual][i]);
ady += dp[matriz[actual][i]];
}
}
if(ady == 0) dp[actual] = 1;
else dp[actual] = ady;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long casos, nodos, e1, e2, querys;
cin >> casos;
while(casos --){
cin >> nodos;
matriz.clear();
visitado.clear();
dp.clear();
matriz.resize(nodos + 1);
visitado.resize(nodos + 1);
dp.resize(nodos + 1);
for(long long i = 1; i < nodos; i ++){
cin >> e1 >> e2;
matriz[e1].push_back(e2);
matriz[e2].push_back(e1);
}
DFS(1);
cin >> querys;
while(querys --){
cin >> e1 >> e2;
cout << dp[e1] * dp[e2] << "\n";
}
}
return 0;
}