-
Notifications
You must be signed in to change notification settings - Fork 1
/
EvenTree.cpp
78 lines (61 loc) · 1.08 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
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
// problem: "https://www.hackerrank.com/challenges/even-tree/problem"
// Even Tree
#include<iostream>
#include<vector>
using namespace std;
vector<int>N(101,-1);
vector<int>V(101,-1);
vector<int>V2(101,-1);
int nodes(vector<int>G[],int i)
{
if(N[i] == -1)
{
V[i] = 1;
int c = 0;
int j;
for(j=0;j!=G[i].size();j++)
{
if(V[G[i][j]]!=1)
{
V[G[i][j]] = 1;
c = c+1+nodes(G,G[i][j]);
}
}
N[i] = c;
}
return N[i];
}
int countCut(vector<int>G[],int i)
{
V2[i] = 1;
int j,c;
c = 0;
for(j=0;j!=G[i].size();j++)
{
if(V2[G[i][j]]==-1)
{
if(N[G[i][j]]%2)
{
c++;
}
c += countCut(G,G[i][j]);
}
}
return c;
}
int main()
{
int n,m;
cin>>n>>m;
vector<int>G[n+1];
while(m--)
{
int x,y;
cin>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
N[1] = nodes(G,1);
cout<<countCut(G,1);
return 0;
}