CSES - Datatähti Open 2021 - Results
Submission details
Task:Distances
Sender:pidddgy
Submission time:2021-01-31 20:59:23 +0200
Language:C++11
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
Test results
testverdicttimegroup
#10.01 s1, 2details
#20.03 s2details

Code

#include <bits/stdc++.h>
using namespace std;
#define cerr if(false) cerr
#define watch(x) cerr << (#x) << " is " << (x) << endl;
#define endl '\n'
#define ld long double
#define int long long
#define pii pair<int, int>
#define fi first
#define se second
#define sz(a) (int)(a).size()
#define y1 lsdjkfhshfdsighoihweogihewoghi
#define all(x) (x).begin(), (x).end()

const int maxn = 200;

int n;
int dis[maxn][maxn];
vector<int> G[maxn];

// graph split by parity
vector<int> Gpar[maxn];

void bfs(int st) {
    queue<int> Q;
    Q.push(st);
    dis[st][st] = 0;

    while(!Q.empty()) {
        int S = Q.front(); Q.pop();
        for(int to: G[S]) {
            if(dis[st][to] > dis[st][S]+1) {
                Q.push(to);
                dis[st][to] = dis[st][S]+1;
            }
        }
    }
}

bool vis[maxn];

void solve() {
    for(int i = 0; i < maxn; i++) {
        for(int j = 0; j < maxn; j++) {
            dis[i][j] = 1e18;
        }
        G[i].clear();
        Gpar[i].clear();
        vis[i] = false;
    } 

    cin >> n;

    for(int i = 1; i <= n-1; i++) {
        int u, v;
        cin >> u >> v;

        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }

    for(int i = 1; i <= n; i++) bfs(i);

    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            if(dis[1][i]%2 == dis[1][j]%2 and dis[i][j] <= 3) {
                Gpar[i].emplace_back(j);
                Gpar[j].emplace_back(i);
            }
        }
    }

    // do all with even distance from 1
    vector<int> v;

    stack<int> Q;
    Q.push(1);
    vis[1] = true;

    while(!Q.empty()) {
        int S = Q.top(); Q.pop();
        v.emplace_back(S);
        for(int to: Gpar[S]) {
            if(!vis[to]) {
                vis[to] = true;
                Q.push(to);
            }
        }

        if(Q.empty()) {
            for(int to: G[S]) {
            if(!vis[to]) {
                vis[to] = true;
                Q.push(to);
            }
        }
        }
    }

    for(int x: v) cerr << x << " ";
    cerr << endl; 

    int st = -1;

    for(int to: G[v.back()]) {
        if(dis[1][to] % 2 == 1) {
            st = to;
            break;
        }
    }

    if(st != -1) {
        Q.push(st);
        vis[st] = true;

        while(!Q.empty()) {
            int S = Q.top(); Q.pop();
            v.emplace_back(S);
            for(int to: Gpar[S]) {
                if(!vis[to]) {
                    vis[to] = true;
                    Q.push(to);
                }
            }
        }
    }

    for(int x: v) {
        cout << x << " ";
    }
    cout << endl;
}

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

    int t;
    cin >> t;

    while(t--) solve();
}

/*

*/

// Did you read the bounds?
// Did you make typos?
// Are there edge cases (N=1?)
// Are array sizes proper?
// Integer overflow?
// DS reset properly between test cases?
// Is using long longs causing TLE?
// Are you using floating points?

Test details

Test 1

Group: 1, 2

Verdict:

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 8 7 2 6 3 4 5 
1 7 2 6 4 5 3 8 
1 6 5 2 7 8 3 4 
1 8 7 4 2 6 5 3 
1 4 7 5 2 3 8 6 
...

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
1 99 82 93 47 81 98 95 50 31 8...