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

Compiler report

input/code.cpp: In function 'std::array<int, 2> dfs(int, int)':
input/code.cpp:16: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, dep;
array<int, 2> dfs(int v, int p = -1) {
	if(g[v].size() == 1 && p != -1) return {v, v};
	vector<array<int, 2>> chi;
	for(auto &i : g[v]) if(i != p) {
		dep[i] = dep[v]+1;
		chi.push_back(dfs(i, v));
	}
	for(int i = 1; i < chi.size(); i++) {
		nxt[chi[i-1][1]] = chi[i][0];
	}
	nxt[chi.back()[1]] = v;
	return {chi[0][0], v};
}
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);
		dep = vector<int>(n+1, 0);
		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];
		do {
			cout << n << " ";
		} while((n=nxt[n])!=-1);
		cout << '\n';
	}
}

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

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
5 55 71 17 59 24 52 32 100 39 ...
Truncated