Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n;
- vector<int> adj[100001];
- queue<int> q;
- bool visited[100001];
- int father[100001];
- int main(){
- //freopen("father.in", "r", stdin);
- cin >> n;
- int a, b;
- while(cin >> a){
- cin >> b;
- adj[a].push_back(b);
- adj[b].push_back(a);
- //cout << a << " " << b << '\n';
- }
- for(int i=1; i<=n; i++){
- sort(adj[i].begin(), adj[i].end());
- }
- q.push(1);
- visited[1] = true;
- while(!q.empty()){
- int cur = q.front();
- q.pop();
- for(auto v: adj[cur]){
- if(!visited[v]){
- visited[v] = true;
- father[v] = cur;
- q.push(v);
- }
- }
- }
- for(int i=2; i<=n; i++){
- cout << father[i] << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement