Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <queue>
- #include <vector>
- using namespace std;
- #define si(a) scanf("%d",&a)
- #define f first
- #define s second
- #define mp(a,b) make_pair(a,b)
- #define MAX 1005
- int dist[MAX];
- vector<int> graph[MAX];
- void bfs(int st)
- {
- queue<pair<int,int> > Q;
- Q.push(mp(st,0));
- memset(dist,-1,sizeof(dist));
- while(!Q.empty()){
- pair<int,int> now=Q.front();
- Q.pop();
- if(dist[now.f]!=-1)
- continue;
- dist[now.f]=now.s;
- //cout<<now.f+1<<" "<<now.s<<endl;
- for(int i=0;i<graph[now.f].size();i++)
- Q.push(mp(graph[now.f][i],now.s+1));
- }
- return ;
- }
- int main()
- {
- //freopen("input","r",stdin);
- int n,m,i;
- si(n);si(m);
- for(i=0;i<m;i++){
- int u,v;
- si(u);si(v);
- u--;v--;
- graph[u].push_back(v);
- graph[v].push_back(u);
- }
- bfs(0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement