Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- const ll N=1000000;
- vector<ll>adj[N+10];
- bool vis[N+100];
- ll n,m;
- ll level[N+1];
- void BFS(ll s)
- {
- vis[s]=true;
- level[s]=0;
- queue<ll>q;
- q.push(s);
- while(!q.empty())
- {
- ll u=q.front();
- q.pop();
- for(ll i=0;i<adj[u].size();i++)
- {
- if(vis[adj[u][i]]==false)
- {
- level[adj[u][i]]=level[u]+1;
- vis[adj[u][i]]=true;
- q.push(adj[u][i]);
- }
- }
- }
- }
- int main()
- {
- cin>>n;
- ll r,c,size_of_cum;
- for(ll i=1;i<=n;i++)
- {
- cin>>r;
- cin>>size_of_cum;
- while(size_of_cum--)
- {
- cin>>c;
- adj[r].push_back(c);
- }
- }
- BFS(1);
- for(ll i=1;i<=n;i++)
- {
- if(level[i]!=0 or i==1)cout<<i<<" "<<level[i]<<endl;
- else cout<<i<<" "<<-1<<endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement