// aarifshuvo ``CSEJU #include <bits/stdc++.h> #define ll long long #define pb push_back #define SZ(x) ((int)(x).size()) #define scl(x) scanf("%lld", &x) #define scll(x,y) scanf("%lld %lld", &x, &y) #define all(x) (x).begin(),(x).end() #define mem(a,d) memset(a,d,sizeof a) #define REP(i,n) for(int i=0;i<n;i++) #define REV(i,n) for(int i=n-1;i>=0;i--) #define FOR(i,a,b) for(int i=a;i<=b;i++) #define pri(a) cout<<a<<endl #define prii(a,b) cout<<a<<" "<<b<<endl using namespace std; #define inf 123456789 vector<int> adj[1000007]; int dis[1000007]; int vis[1000007]; void bfs(int st) { queue<int> bfsq; bfsq.push(st); dis[st] = 0; vis[st] = 1; while(SZ(bfsq)) { int u = bfsq.front(); bfsq.pop(); REP(i,SZ(adj[u])) { int v = adj[u][i]; if(vis[v]==0) { vis[v] = 1; dis[v] = dis[u] + 1; bfsq.push(v) ; } } } } int main() { int u,v,n,e,st; cin>>n>>e>>st; FOR(i,1,e) { cin>>u>>v; adj[u].pb(v); } FOR(i,1,n) dis[i] = inf; bfs(st); FOR(i,1,n) { if(dis[i]==inf) printf("-1 "); else printf("%d ", dis[i]); } puts(""); return 0; }
~Be glad of life because it gives you the chance to love and to work and to play and to look at the stars.~
Wednesday, May 31, 2017
CSAcademy breadth_first_search
Subscribe to:
Comments (Atom)