-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevelNodes.cpp
More file actions
149 lines (96 loc) · 2.01 KB
/
LevelNodes.cpp
File metadata and controls
149 lines (96 loc) · 2.01 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <bits/stdc++.h>
using namespace std;
int mx = INT_MAX;
const int arr_limit = 1e7+10;
//int arr[arr_limit];
const int graph_v_limit = 1e5+10;
vector<int> g[graph_v_limit];
bool visited[graph_v_limit];
int level[graph_v_limit];
queue<int> child_queue;
void dfs(int vertex){
cout << vertex<< endl;
visited[vertex] = 1;
for(auto child : g[vertex]){
if(visited[child]) continue;
dfs(child);
}
}
void bfs_r(int vertex){
// root node condtion
if(child_queue.empty() && !visited[vertex]){
visited[vertex] = 1;
cout << vertex << endl;
}
for(auto child : g[vertex]){
if(visited[child]) continue;
visited[child] =1;
cout<<child<<endl;
child_queue.push(child);
}
while(!child_queue.empty()){
int f = child_queue.front();
child_queue.pop();
bfs_r(f);
}
}
void bfs(int vertex){
// root node condtion
if(child_queue.empty() && !visited[vertex]){
visited[vertex] = 1;
level[vertex]=1;
child_queue.push(vertex);
}
while(!child_queue.empty()){
int f = child_queue.front();
child_queue.pop();
// cout<< f<<" level:"<<level[f]<< endl;
for(auto child : g[f]){
if(visited[child]) continue;
visited[child] =1;
level[child] = level[f]+1;
child_queue.push(child);
}
}
}
bool has_cycle(int vertex){
cout << vertex<< endl;
visited[vertex] = 1;
for(auto child : g[vertex]){
if(visited[child]){
cout<<" making cycle";
return true;
}
has_cycle(child);
}
cout << " oustside for";
return false;
}
// int count_element(*a,ele){
// int count =0;
// for(auto i : a){
// if(i==ele) count++;
// }
// return count;
// }
int main(){
// int l = sizeof(g)/sizeof(g[0]);
// cout<< "hello world " << l << typeid(g[0]).name() << endl;
// cout<< mx;
int v;
cin >> v;
for(int i=0; i<v-1;++i){
int v1,v2;
cin >> v1 >>v2;
g[v1].push_back(v2);
g[v2].push_back(v1);
}
int lev;
cin>>lev;
// visited[1] = 1;
// bfs_r(1);
bfs(1);
cout << count(level,level+sizeof(level[0])*v+1,lev);
// cout << has_cycle(0) << endl;
return 0;
}