CSES - Datatähti Open 2021 - Results
Submission details
Task:Distances
Sender:kostia244
Submission time:2021-01-30 20:14:30 +0200
Language:C++ (C++17)
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED29
#2ACCEPTED71
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2details
#2ACCEPTED0.01 s2details

Compiler report

input/code.cpp: In function 'std::array<int, 2> dfs(int, int, int)':
input/code.cpp:14:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 1; i < chi.size(); i++) {
                 ~~^~~~~~~~~~~~

Code

#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
const int maxn = 1<<18;
int n;
vector<vector<int>> g;
vector<int> nxt;
array<int, 2> dfs(int v, int p, int type) {
	vector<array<int, 2>> chi;
	for(auto &i : g[v]) if(i != p) {
		chi.push_back(dfs(i, v, type^1));
	}
	for(int i = 1; i < chi.size(); i++) {
		nxt[chi[i-1][1]] = chi[i][0];
	}
	if(chi.empty()) return {v, v};
	if(type) {
		nxt[chi.back()[1]] = v;
		return {chi[0][0], v};
	}
	nxt[v] = chi[0][0];
	return {v, chi.back()[1]};
}
int main() {
	cin.tie(0)->sync_with_stdio(0);
	int t;cin >> t;
	while(t--) {
		cin >> n;
		g = vector<vector<int>>(n+1);
		nxt = vector<int>(n+1, -1);
		for(int f, t, i = 1; i < n; i++) {
			cin >> f >> t;
			g[f].push_back(t);
			g[t].push_back(f);
		}
		n = dfs(1, 0, 1)[0];
		do {
			cout << n << " ";
		} while((n=nxt[n])!=-1);
		cout << '\n';
	}
}

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

Test 2

Group: 2

Verdict: ACCEPTED

input
100
100
37 59
81 37
44 81
...

correct output
1 99 82 81 59 5 71 55 17 24 13...

user output
22 60 44 37 17 5 55 71 59 24 3...
Truncated