Skip to content

Instantly share code, notes, and snippets.

@1604078-MEHEDI
Created February 15, 2019 10:27
Show Gist options
  • Save 1604078-MEHEDI/f22f7f28e126a1410469c422afac7f3c to your computer and use it in GitHub Desktop.
Save 1604078-MEHEDI/f22f7f28e126a1410469c422afac7f3c to your computer and use it in GitHub Desktop.
// Level wise node count
#include <bits/stdc++.h>
using namespace std;
#define INF 1<<30
#define MAX 100005
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
typedef long long ll;
vector<int>graph[MAX];
map<int,int>mp;
int level[MAX];
bool visit[MAX];
int bfs(int s, int x)
{
queue<int>Q;
Q.push(s);
level[s] = 0;
visit[s] = true;
mp[level[s]]++;
while(!Q.empty())
{
int u = Q.front();
Q.pop();
for(int i = 0; i < graph[u].size(); i++){
int v = graph[u][i];
if(visit[v] == false){
level[v] = level[u]+1;
mp[level[v]]++;
Q.push(v);
visit[v] = true;
}
}
}
return mp[x-1];
}
int main()
{
FASTIO
///*
//double start_time = clock();
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
//*/
int n;
int a,b;
cin >> n;
for(int i = 0; i < n-1; i++){
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
int x;
cin >> x;
cerr << a << endl;
cout << bfs(1, x)<<endl;
//for(int i = 1; i <= 20; i++){
// cerr << mp[i] << endl;
//}
//double end_time = clock();
//printf( "Time = %lf ms\n", ( (end_time - start_time) / CLOCKS_PER_SEC)*1000);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment