Task: | Distances |
Sender: | flash_0408 |
Submission time: | 2021-01-31 18:37:07 +0200 |
Language: | C++ (C++17) |
Status: | READY |
Result: | 100 |
group | verdict | score |
---|---|---|
#1 | ACCEPTED | 29 |
#2 | ACCEPTED | 71 |
test | verdict | time | group | |
---|---|---|---|---|
#1 | ACCEPTED | 0.01 s | 1, 2 | details |
#2 | ACCEPTED | 0.01 s | 2 | details |
Code
#include <bits/stdc++.h> using namespace std; vector<vector<int>> adj; vector<int> depth; vector<int> leaf; void dfs(int node, int par){ depth[node] = depth[par] + 1; for(int v : adj[node]){ if(v == par) continue; dfs(v, node); } if(adj[node].size() == 1 && node != 0) leaf[node] = 1; } vector<int> ans; void dfs2(int node, int par){ if(leaf[node] == 1 || (depth[node] & 1) == 0) ans.push_back(node); for(int v : adj[node]){ if(v == par) continue; dfs2(v, node); } if((depth[node]&1) && !leaf[node]) ans.push_back(node); } void solve(){ adj.clear(); ans.clear(); int n; cin >> n; adj.resize(n); for(int i = 0; i < n-1; i++){ int u, v; cin >> u >> v; --u, --v; adj[u].push_back(v); adj[v].push_back(u); } depth.assign(n, 0); depth[0] = -1; leaf.assign(n, 0); dfs(0, 0); dfs2(0, 0); for(int v : ans) cout << v+1 << ' '; cout << '\n'; } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; for(int tc = 0; tc < t; tc++) solve(); return 0; }
Test details
Test 1
Group: 1, 2
Verdict: ACCEPTED
input |
---|
100 8 5 2 2 3 3 7 ... |
correct output |
---|
1 8 2 5 6 7 3 4 1 7 2 8 3 6 4 5 1 4 6 2 7 5 8 3 1 8 3 2 4 7 6 5 1 6 4 7 5 2 3 8 ... |
user output |
---|
1 8 2 5 6 7 3 4 1 7 2 8 3 6 4 5 1 4 6 2 7 5 8 3 1 8 3 2 4 7 6 5 1 6 4 7 5 2 3 8 ... Truncated |
Test 2
Group: 2
Verdict: ACCEPTED
input |
---|
100 100 37 59 81 37 44 81 ... |
correct output |
---|
1 99 82 81 59 5 71 55 17 24 13... |
user output |
---|
1 99 82 81 59 5 71 55 17 24 13... Truncated |