-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount_nodes_on_same_level.cpp
89 lines (71 loc) · 1.67 KB
/
count_nodes_on_same_level.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
82
83
84
85
86
87
88
89
//https://www.hackerearth.com/practice/algorithms/graphs/breadth-first-search/tutorial/
#include<bits/stdc++.h>
#define pf printf
#define sf scanf
#define db double
#define z long long int
using namespace std ;
#define MAX 1000001
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
void bfs(vector<z> v[],z si,z s,z lev[],z xx)
{
z le[1000000]={0};
z i;
bool vis[si];
memset(vis,false,si);
queue<z> q;
q.push(s);
lev[s]=1;
vis[s]=true;
map<z,z> m;
while(!q.empty())
{
z x = q.front();
// cout << x << endl;
// cout << lev[x] << endl;
le[lev[x]]++;
m[lev[x]]++;
//cout << le[lev[x]] <<endl;
// cout << le[] ;
q.pop();
for(i=0;i<v[x].size();i++)
{
if(!vis[v[x][i]])
{
q.push(v[x][i]);
vis[v[x][i]]=true;
lev[v[x][i]]=lev[x]+1;
// le[lev[x]]++;
//le[ lev[v[x][i]] ]++;
}
}
}
// for(i=0;i<10;i++)
// cout << le[i] <<endl;
// for(i=0;i<10;i++)
// cout << i << " " << m[i] <<endl;
cout << m[xx];
}
int main()
{
fast;
z n;
cin>>n;
//n--;
vector<z> v[n];
z lev[n]={0};
z xx ;
z k=n-1;
while(k--)
{
//pf("enter edges (stop -1 -1)");
z x,y;
cin>>x>>y;
x--;y--;
//if(x==-1 && y==-1) break;
v[x].push_back(y);
v[y].push_back(x);
}
cin>>xx;
bfs(v,n,0,lev,xx);
}