Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<bits/stdc++.h>
- using namespace std;
- vector<int>adj[100];
- bool visited[100];
- void bfs(int s)
- {
- queue<int>q;
- q.push(s);
- visited[s]=true;
- while(!q.empty())
- {
- int u=q.front();
- q.pop();
- cout<<u<<" ";
- for(int i=0;i<adj[u].size();i++)
- {
- if(visited[adj[u][i]]==false)
- {
- int v=adj[u][i];
- visited[v]=true;
- q.push(v);
- }
- }
- }
- cout<<endl;
- }
- int main()
- {
- int n,e,i,x,y;
- scanf("%d %d",&n,&e);
- for(i=0;i<e;i++)
- {
- cin>>x>>y;
- adj[x].push_back(y);
- }
- for(i=0;i<7;i++){
- bfs(i);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment