CSES - Datatähti Open 2021 - Results
Submission details
Task:Distances
Sender:Victor
Submission time:2021-01-30 17:01:49 +0200
Language: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;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, a, b) for (int i = a - 1; i >= (b); --i)
#define trav(a, x) for (auto &a : x)

#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define pb push_back

#define umap unordered_map
#define uset unordered_set
#define INF 1000000000

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long long ll;

vi graph[101];
int n, tc, ans[101], indx, dist;

void solve(int u, int p) {
    bool add = true;
    if(dist==3){
        add=false;
        ans[indx++]=u;
        dist=0;
    }
    trav(v, graph[u]) {
        if (v == p) continue;
        ++dist;
        solve(v, u);
    }

    if (add) {
        ans[indx++] = u;
        dist = 0;
    }
    ++dist;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL);

    cin >> tc;
    int u, v;
    while (tc--) {
        fill(graph, graph + 101, vi());
        cin >> n;
        rep(i, 0, n-1) {
            cin >> u >> v;
            graph[u].pb(v);
            graph[v].pb(u);
        }
        indx = dist = 0;
        solve(1, 0);
        rep(i,0,n)cout<<ans[i]<<' ';
        cout<<endl;
    }
    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
3 5 6 2 7 8 4 1 
8 2 7 3 6 4 5 1 
4 6 7 2 5 8 3 1 
3 8 2 4 7 6 5 1 
6 4 2 5 7 3 8 1 
...

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
60 81 17 5 55 71 59 24 39 52 3...