CSES - Datatähti Open 2021 - Results
Submission details
Task:Distances
Sender:mofk
Submission time:2021-01-30 17:28:53 +0200
Language:C++17
Status:READY
Result:29
Feedback
groupverdictscore
#1ACCEPTED29
#20
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2details
#20.02 s2details

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:45:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i = 1; i < ord.size(); ++i) {
                         ~~^~~~~~~~~~~~
input/code.cpp:47:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (int j = 0; j <= ans.size(); ++j) {
                             ~~^~~~~~~~~~~~~
input/code.cpp:50:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 if (j < ans.size() && d[u][ans[j]] > 3) ok = false;
                     ~~^~~~~~~~~~~~

Code

#include <bits/stdc++.h>

using namespace std;

int n;
vector<int> g[105];
int d[105][105];

int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int ntest;
    cin >> ntest;
    while (ntest--) {
        cin >> n;
        for (int i = 1; i <= n; ++i) g[i].clear();
        for (int i = 1; i < n; ++i) {
            int u, v;
            cin >> u >> v;
            g[u].push_back(v);
            g[v].push_back(u);
        }

        memset(d, -1, sizeof d);
        for (int i = 1; i <= n; ++i) {
            queue<int> q;
            q.push(i);
            d[i][i] = 0;
            while (!q.empty()) {
                int u = q.front(); q.pop();
                for (auto v: g[u]) if (d[i][v] == -1) {
                    d[i][v] = d[i][u] + 1;
                    q.push(v);
                }
            }
        }

        vector<int> ord(n, 0);
        iota(ord.begin(), ord.end(), 1);
        sort(ord.begin(), ord.end(), [](int x, int y) {
                return d[1][x] < d[1][y];
             });
        vector<int> ans;
        ans.push_back(ord[0]);
        for (int i = 1; i < ord.size(); ++i) {
            int u = ord[i];
            for (int j = 0; j <= ans.size(); ++j) {
                bool ok = true;
                if (j && d[u][ans[j - 1]] > 3) ok = false;
                if (j < ans.size() && d[u][ans[j]] > 3) ok = false;
                if (ok) {
                    ans.insert(ans.begin() + j, u);
                    break;
                }
            }
        }

        for (auto x: ans) cout << x << ' ';
        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
6 5 7 2 3 8 4 1 
6 4 5 7 2 8 3 1 
7 5 2 8 6 4 3 1 
3 8 7 4 2 6 5 1 
5 2 7 3 4 8 6 1 
...

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
47 11 93 12 44 41 40 76 66 8 2...