CSES - Datatähti Open 2021 - Results
Submission details
Task:Distances
Sender:sandoval
Submission time:2021-01-30 04:10:24 +0200
Language:C++ (C++17)
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED29
#2ACCEPTED71
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2details
#2ACCEPTED0.01 s2details

Code

#include <bits/stdc++.h>
using namespace std;
constexpr int MAXN = 5+100;
vector<int> t[MAXN];
int pa[MAXN];
void dfs1(int u) {
for (int v : t[u]) {
if (v == pa[u]) continue;
pa[v] = u;
dfs1(v);
}
}
void dfs2(int u) {
cout << u << ' ';
for (int v : t[u]) {
for (int w : t[v]) {
dfs2(w);
}
cout << v << ' ';
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int tc; cin >> tc;
while (tc--) {
int n; cin >> n;
for (int i = 0; i < MAXN; ++i) {
t[i].clear();
pa[i] = -1;
}
for (int i = 0; i < n-1; ++i) {
int a,b; cin >> a >> b;
t[a].push_back(b);
t[b].push_back(a);
}
dfs1(1);
for (int i = 2; i <= n; ++i) {
t[i].erase(find(t[i].begin(), t[i].end(), pa[i]));
}
dfs2(1);
cout << '\n';
}
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