CSES - Datatähti Open 2021 - Results
Submission details
Task:Distances
Sender:vishesh312
Submission time:2021-01-31 22:50:29 +0200
Language:C++ (C++17)
Status:READY
Result:29
Feedback
groupverdictscore
#1ACCEPTED29
#20
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2details
#2--2details

Code

#include "bits/stdc++.h"
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
*/

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

typedef long long ll;
const int mod = 1e9+7;
vector<vector<int>> adj, dist;
int cur;

void dfs(int u, int par) {
    if (par != -1) dist[cur][u] = dist[cur][par] + 1;
    for (int v : adj[u]) {
        if (v != par) {
            dfs(v, u);
        }
    }
}

void solve(int tc) {
    int n;
    cin >> n;
    adj.clear(), adj.resize(n);
    dist.clear(), dist.resize(n);
    for (auto &x : dist) x.resize(n);
    for (int i = 0; i < n-1; ++i) {
        int u, va;
        cin >> u >> va;
        --u, --va;
        adj[u].push_back(va);
        adj[va].push_back(u);
    }
    for (int i = 0; i < n; ++i) {
        cur = i;
        dfs(i, -1);
    }
    vector<int> v;
    for (int i = 0; i < n; ++i) v.push_back(i);
    do {
        bool can = true;
        for (int i = 0; i < n-1; ++i) {
            if (dist[v[i]][v[i+1]] > 3) {
                can = false;
                break;
            }
        }
        if (can) {
            for (int x : v) cout << x+1 << " ";
            cout << '\n';
            return;
        }
    } while (next_permutation(v.begin(), v.end()));
}

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int tc = 1;
    cin >> tc;
    for (int i = 1; i <= tc; ++i) solve(i);
    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 3 2 4 7 5 6 8 
1 3 2 8 7 5 4 6 
1 3 2 5 7 8 6 4 
1 2 3 4 5 6 7 8 
1 3 2 4 6 8 7 5 
...
Truncated

Test 2

Group: 2

Verdict:

input
100
100
37 59
81 37
44 81
...

correct output
1 99 82 81 59 5 71 55 17 24 13...

user output
(empty)