Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <algorithm>
- #include <queue>
- using namespace std;
- ifstream fin("iepuri1.in");
- ofstream fout("iepuri1.out");
- const int MOD = 30011;
- const int DIM = 105;
- vector <int> G[DIM];
- int n,k,Root,x,y,Nr[DIM],Dist[DIM];
- void Read()
- {
- fin>>n>>k;
- for(int i=1;i<=n-1;i++)
- {
- fin>>x>>y;
- G[x].push_back(y);
- Nr[y]++;
- }
- }
- void GetRoot()
- {
- for(int i=1;i<=n;i++)
- {
- if(!Nr[i])
- {
- Root=i;
- break;
- }
- }
- }
- queue <int> Q;
- void BFS(int node)
- {
- Q.push(node);
- Dist[node]=1;
- while(!Q.empty)
- {
- int nod=Q.front();
- vector <int> ::iterator it;
- for(it=G[nod].begin();it!=G[nod].end();it++)
- {
- if(!Dist[*it])
- {
- Dist[*it]=Dist[node]+1;
- Q.push(*it);
- }
- }
- Q.pop();
- }
- }
- int main()
- {
- Read();
- GetRoot();
- BFS(Root);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement