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

Code

//distances.cpp created at 01/29/21 21:18:59

#include <bits/stdc++.h>

using namespace std;

const int MN = 101;

int N;
set<int> adj[MN];

int ans[MN], idx;

void dfs(const int u, const int par, const bool early) {
    if (early) {
        ans[idx++] = u;
    }

    for (const int v: adj[u]) {
        if (v == par) continue;
        dfs(v, u, !early);
    }

    if (!early) {
        ans[idx++] = u;
    }
}

void solve() {
    cin >> N;
    for (int i=0; i<N; ++i) {
        adj[i].clear();
    }

    for (int i=0; i<N-1; ++i) {
        int u, v;
        cin >> u >> v;
        --u; --v;
        adj[u].insert(v);
        adj[v].insert(u);
    }

    idx = 0;
    dfs(0, -1, false);

    for (int i=0; i<N; ++i) {
        if (i) cout << ' ';
        cout << ans[i] + 1;
    }
    cout << '\n';
}

int main() {
    cin.tie(0)->sync_with_stdio(0);

    int T;
    cin >> T;

    for (int t=1; t<=T; ++t) {
        solve();
    }
}

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
4 3 5 6 2 7 8 1
3 8 2 7 5 4 6 1
3 8 7 2 5 6 4 1
5 6 2 4 7 3 8 1
6 8 3 2 5 7 4 1
...
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
22 60 19 12 11 47 93 44 78 68 ...
Truncated