CSES - Datatähti Open 2021 - Results
Submission details
Task:Distances
Sender:flash_0408
Submission time:2021-01-31 17:54:25 +0200
Language:C++ (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;

vector<vector<int>> adj; 

struct LCA{
	vector<vector<int>> up; 
	int l;
	vector<int> tin, tout; 
	int timer; 
	vector<int> depth; 
	LCA(int n){
		l = 32 - __builtin_clz(n); 
		up.resize(l+1, vector<int>(n)); 
		tin.resize(n); 
		tout.resize(n); 
		depth.resize(n); 
		timer = 0;
		depth[0] = -1; 
		dfs(0, 0);  
	}

	void dfs(int node, int par){
		tin[node] = timer++; 
		depth[node] = depth[par] + 1;
		up[node][0] = par;
		for(int i = 1; i <= l; i++)
			up[i][node] = up[i-1][up[node][i-1]]; 

		for(int v : adj[node]){
			if(v == par)
				continue; 
			dfs(v, node); 
		}

		tout[node] = timer++; 
	}

	bool is_ancestor(int u, int v){
		return tin[u] <= tin[v] && tout[u] >= tout[v]; 
	}

	int lca(int u, int v){
		if(is_ancestor(u, v))
			return u; 
		if(is_ancestor(v, u))
			return v;

		for(int i = l; i >= 0; --i){
			if(!is_ancestor(up[i][u], v))
				u = up[i][u]; 
		}

		return up[0][u]; 
	}

	int distance(int u, int v){
		return depth[u] + depth[v] - 2*depth[lca(u, v)];
	}
}; 
void solve(){
	adj.clear(); 
	int n;
	cin >> n; 

	adj.resize(n); 
	for(int i = 0; i < n-1; i++){
		int u, v;
		cin >> u >> v;
		--u, --v;
		adj[u].push_back(v);
		adj[v].push_back(u); 
	}

	LCA d(n);

	vector<int> a(n); 
	for(int i = 0; i < n; i++)
		a[i] = i;

	do{
		bool f = true;
		for(int i = 0; i < n-1; i++){
			if(d.distance(a[i], a[i+1]) > 3){
				f = false; 
				break;
			}
		}
		if(f){
			for(int i = 0; i < n; i++)
				cout << a[i] + 1 << ' '; 

			cout << '\n';
			return; 
		}
	}while(next_permutation(a.begin(), a.end())); 
}
int32_t main(){
	ios_base::sync_with_stdio(false); 
	cin.tie(NULL); 
	
	int t;
	cin >> t; 

	for(int tc = 0; tc < t; tc++)
		solve(); 
	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
(empty)

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)