Created
January 18, 2017 16:54
-
-
Save divanshArora/24a6010a10cf708e16f9df8baa51347c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <queue> | |
// https://www.hackerrank.com/challenges/bfsshortreach | |
using namespace std; | |
long long int dist[1001]={0}; | |
int visited[1001]={0}; | |
queue<int> mq; | |
void bfs(vector< vector< int > > &graph,int s) | |
{ | |
mq.push(s); | |
dist[s]= 0; | |
visited[s]=true; | |
while(!mq.empty()) | |
{ | |
int current = mq.front(); | |
mq.pop(); | |
for(int i=0;i<graph[current].size();i++) | |
{ | |
if(visited[ graph[current][i] ] == false) | |
{ | |
mq.push(graph[current][i]); | |
dist[graph[current][i]]=dist[current]+1; | |
visited[graph[current][i]]=true; | |
} | |
} | |
} | |
} | |
int main() | |
{ | |
ios_base::sync_with_stdio(false); | |
int n,e,k; | |
cin>>n>>e>>k; | |
vector<vector< int > > graph(n+1); | |
for(int i=0;i<e;i++) | |
{ | |
int a,b; | |
cin>>a>>b; | |
a--;b--; | |
graph[a].push_back(b); | |
graph[b].push_back(a); | |
} | |
for(int i=0;i<n;i++) | |
{ | |
for(int j=0;j<n;j++){ | |
visited[j] = false; | |
dist[j]=-1; | |
} | |
bfs(graph,i); | |
long long int ans =0; | |
for(int j=0;j<n;j++){ | |
if(dist[j]<=k && dist[j]!=-1 && dist[j]>1 && visited[j]==true) | |
{ | |
ans++; | |
} | |
} | |
cout<<ans<<endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment