Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #include<vector>
- using namespace std;
- int n;
- vector<int>adj[1000];
- bool visited[1000];
- bool initialize()
- {
- for(int i=0;i<=n;i++)
- visited[i]=false;
- }
- void bfs(int x)
- {
- initialize();
- int cnt=0;
- queue<int>q;
- q.push(x);
- while(!q.empty())
- {
- int u;
- u=q.front();
- q.pop();
- for(int i=0;i<adj[u].size();i++)
- {
- int v=adj[u][i];
- // cout<<"vis["<<v<<"]= "<<visited[v]<<endl;
- if(!visited[v])
- {
- cnt++;
- visited[v]=true;
- q.push(v);
- }
- }
- }
- cout<<n-cnt;
- for(int i=1;i<=n;i++)
- {
- if(!visited[i])
- cout<<" "<<i;
- }
- cout<<endl;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- while(cin>>n && n)
- {
- int u,v;
- while(cin>>u && u)
- {
- adj[u].clear();
- while(cin>>v && v)
- {
- adj[u].push_back(v);
- }
- }
- int m;
- cin>>m;
- while(m--)
- {
- int x;
- cin>>x;
- bfs(x);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement