Created
July 3, 2019 05:49
-
-
Save surinoel/aaf9c97dfe48b02b8016b04b4b14d0a9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <queue> | |
#include <vector> | |
#include <cstring> | |
#include <iostream> | |
using namespace std; | |
vector<int> graph[100001]; | |
int p[100001]; | |
int main(void) { | |
ios_base::sync_with_stdio(false); | |
cin.tie(nullptr); | |
int n; | |
cin >> n; | |
for (int i = 0; i < n - 1; i++) { | |
int x, y; | |
cin >> x >> y; | |
graph[x].push_back(y); | |
graph[y].push_back(x); | |
} | |
memset(p, -1, sizeof(p)); | |
queue<int> q; | |
p[1] = 0; | |
q.push(1); | |
while (!q.empty()) { | |
int x = q.front(); | |
q.pop(); | |
for (auto k : graph[x]) { | |
if (p[k] == -1) { | |
p[k] = x; | |
q.push(k); | |
} | |
} | |
} | |
for (int i = 2; i <= n; i++) { | |
cout << p[i] << '\n'; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment