CSES - Datatähti Open 2021 - Results
Submission details
Task:Distances
Sender:koosaga
Submission time:2021-01-30 10:07:13 +0200
Language: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 'void solve()':
input/code.cpp:37:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
  ~~~~~^~~~~~~~~
input/code.cpp:39:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int s, e; scanf("%d %d",&s,&e);
             ~~~~~^~~~~~~~~~~~~~~

Code

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105;


int n, s, t, par[MAXN], chk[MAXN];
vector<int> gph[MAXN], ans;

void init(){
	memset(par, 0, sizeof(par));
	memset(chk, 0, sizeof(chk));
	ans.clear();
	for(int i=0; i<MAXN; i++) gph[i].clear();
	n = s = t = 0;
}

void dfs(int x, int p){
	par[x] = p;
	for(auto &i : gph[x]){
		if(i != p){
			dfs(i, x);
		}
	}
}

void dfs2(int x, int p, int d){
	if(!d) ans.push_back(x);
	for(auto &i : gph[x]){
		if(i != p && !chk[i]){
			dfs2(i, x, 1 - d);
		}
	}
	if(d) ans.push_back(x);
}

void solve(){ init();
	scanf("%d",&n);
	for(int i=1; i<n; i++){
		int s, e; scanf("%d %d",&s,&e);
		gph[s].push_back(e);
		gph[e].push_back(s);
	}
	s = 1, t = n;
	dfs(s, -1);
	for(int j=t; j!=-1; j=par[j]){
		chk[j] = 1;
	}
	for(int j=t; j!=-1; j=par[j]){
		dfs2(j, -1, j != t);
	}
	reverse(ans.begin(), ans.end());
	for(auto &i : ans) printf("%d ", i);
	puts("");
}

int main(){
	int t; cin >> t;
	while(t--) solve();
}

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

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
1 30 45 22 99 60 82 12 93 47 1...