Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<queue>
- #include <vector>
- using namespace std;
- int main()
- {
- int k;
- cin >> k;
- vector<int> graph[k + 1];
- vector<int>v1;
- for(int i = 0; i < k - 1; i++) {
- int a, b;
- cin >> a >> b;
- graph[a].push_back(b);
- graph[b].push_back(a);
- }
- queue<int>q;
- bool niza[k+1];
- for(int y=0; y<k; y++){
- niza[y]=false;
- }
- q.push(0);
- while(!q.empty()){
- int c=q.front();
- q.pop();
- cout<<c<<" ";
- niza[0]=true;
- for(int k1=0; k1<graph[c].size(); k1++){
- int cc=graph[c][k1];
- if(niza[cc]==false){
- q.push(cc);
- niza[cc]=true;
- }
- }
- }
- q.push(k);
- while(!q.empty()){
- int c=q.front();
- q.pop();
- v1.push_back(c);
- niza[k]=true;
- for(int k1=0; k1<graph[c].size(); k1++){
- int cc=graph[c][k1];
- if(niza[cc]==false){
- q.push(cc);
- niza[cc]=true;
- }
- }
- }
- for(int m=v1.size()-1; m>=0; m--){
- cout<<v1[m]<<" ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement