-
Notifications
You must be signed in to change notification settings - Fork 0
/
EvenTree.cpp
46 lines (40 loc) · 1 KB
/
EvenTree.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
#include <iostream>
#include <fstream>
#include <map>
#include <deque>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
int countchildren (vector<int> *v , int* children , int offset) {
int totalSize = 0;
if(!v[offset].size())
children[offset] = 1;
else if(!children[offset]) {
for(int i = 0; i < v[offset].size() ; i++)
totalSize += countchildren(v, children, v[offset][i]);
}
children[offset] = totalSize + 1;
return children[offset];
}
int main() {
int N, M;
int res = 0;
cin >> N >> M;
int children[N];
memset(children, 0, sizeof(children));
vector<int> temp[N]; // convert to an array of values
for(int i = 0; i < M ; i++) {
int e1, e2;
cin >> e1 >> e2;
e1--; e2--;
temp[e2].push_back(e1);
}
countchildren(temp, children, 0);
for(int i = 1; i < N; i++) {
if(!((children[i]) % 2))
res++;
}
cout << res << endl;
return 0;
}