CSES - Datatähti Open 2021 - Results
Submission details
Task:Distances
Sender:pln
Submission time:2021-01-30 21:53:24 +0200
Language:C++ (C++11)
Status:READY
Result:29
Feedback
groupverdictscore
#1ACCEPTED29
#20
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2details
#20.01 s2details

Code

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define ff first
#define ss second
#define pb push_back

const int N = 2e5 + 5, inf = 1e18 + 100;

int n, d[15][15], a[15];
vector < int > g[15];

void dickstra(int s)
{
    for (int i = 1; i <= n; ++i)
        d[s][i] = inf;
    d[s][s] = 0;

    set < pair < int, int > > q;
    q.insert({0, s});
    while (q.size())
    {
        int v = q.begin() -> ss;
        q.erase(q.begin());
        for (int to : g[v])
        {
            if (d[s][v] + 1 < d[s][to])
            {
                q.erase({d[s][to], to});
                d[s][to] = d[s][v] + 1;
                q.insert({d[s][to], to});
            }
        }
    }
}

void solve()
{
    cin >> n;

    for (int i = 1; i <= n; ++i)
    {
        g[i].clear();
        for (int j = 1; j <= n; ++j)
            d[i][j] = 0;
    }

    for (int i = 1; i < n; ++i)
    {
        int u, v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }

    for (int i = 1; i <= n; ++i)
        dickstra(i), a[i] = n - i + 1;

    int k = 1;
    for (int i = 2; i <= n; ++i)
        k *= i;

    while (k > 0)
    {
        next_permutation(a + 1, a + n + 1);
        k--;

        int ok = 1;
        for (int i = 2; i <= n; ++i)
            if (d[a[i - 1]][a[i]] > 3) {ok = 0; break;}

        if (ok)
        {
            for (int i = 1; i <= n; ++i)
                cout << a[i] << ' ';
            cout << '\n';
            return;
        }
    }
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int q;
    cin >> q;
    while (q--)
        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 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)