include iostream include cstdio include vector include cmath include a

 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
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
vector <vector <int> > g;
vector <bool> used;
vector <int> tr_s;
vector <int> ord;
int ind = 0;
int DFS(int v){
used[v] = true;
ord[v] = ind;
ind++;
for (int i = 0; i < g[v].size(); ++i){
if (!used[g[v][i]]){
tr_s[v] += DFS(g[v][i]);
}
}
return tr_s[v];
}
int main(){
int n;
cin >> n;
g.resize(n);
used.assign(n, false);
tr_s.assign(n, 1);
ord.assign(n, -1);
for (int i = 0; i < n; ++i){
int a, b;
cin >> a >> b;
a--; b--;
g[a].push_back(b);
g[b].push_back(a);
}
DFS(0);
return 0;
}