CSES - Datatähti Open 2021 - Results
Submission details
Task:Distances
Sender:knightron0
Submission time:2021-01-30 18:06:01 +0200
Language:C++17
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
Test results
testverdicttimegroup
#10.01 s1, 2details
#20.01 s2details

Code

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define fr first
#define sc second
#define clr(a, x) memset(a, x, sizeof(a))
#define dbg(x) cout<<"("<<#x<<"): "<<x<<endl;
#define printvector(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout<<*it<<" "; cout<<endl;
#define all(v) v.begin(), v.end()
#define lcm(a, b) (a * b)/__gcd(a, b)
#define int long long int
#define printvecpairs(vec) for(auto it: vec) cout<<it.fr<<' '<<it.sc<<endl;
#define endl '\n'
#define float long double

const int MOD = 1e9 + 7;
const int INF = 2e15;
const int MAXN = 1e5 + 5;

vector<int> adj[MAXN];
vector<pair<int, int>> mem[2];
bool vis[MAXN];

void dfs(int s, int curr, int dist, int p){
    vis[s] = true;
    mem[curr].pb({dist, s});
    for(auto u: adj[s]){
        if(!vis[u] && u != p){
            dfs(u, curr^1, dist+1, s);
        }
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    #endif
    int t;
    cin>>t;
    while(t--){
    	int n;
    	cin>>n;
    	for(int i= 0;i<=n+1;i++){
    		adj[i].clear();
            vis[i] = 0;
    	}
    	for(int i= 0;i<n-1;i++){
    		int t1, t2;
    		cin>>t1>>t2;
    		adj[t1].pb(t2);
    		adj[t2].pb(t1);
    	}
        cout<<1<<' ';
        for(int v: adj[1]){
            mem[0].clear();
            mem[1].clear();
            dfs(v, 0, 0, 1);
            sort(all(mem[0]));
            reverse(all(mem[0]));
            sort(all(mem[1]));
            for(auto it: mem[1]){
                cout<<it.sc<<' ';
            }
            for(auto it: mem[0]){
                cout<<it.sc<<' ';
            }
        }
        cout<<endl;
    }

    return 0;
}


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

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 12 68 81 93 13 24 41 4...