CSES - Datatähti Open 2021 - Results
Submission details
Task:Distances
Sender:taulant
Submission time:2021-01-31 17:35:00 +0200
Language:C++11
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED29
#2ACCEPTED71
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2details
#2ACCEPTED0.01 s2details

Code

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
typedef pair<int, int> pii;

int32_t main(){
 ios::sync_with_stdio(0); cin.tie(0);
 int t; cin >> t;
 while(t--){
  int n; cin >> n;
  vector<vector<int>> g(n);
  for(int i=1; i<n; ++i){
   int a, b; cin >> a >> b;
   --a, --b;
   g[a].push_back(b);
   g[b].push_back(a);
  }
  vector<int> dist(n, 10000);
  dist[0] = 0;
  queue<int> q;
  q.push(0);
  while(!q.empty()){
   int i = q.front(); q.pop();
   for(int j:g[i]){
    if(dist[j] > dist[i]) dist[j] = dist[i] + 1, q.push(j);
   }
  }
  int i = 0;
  cout << 1 << " ";
  vector<int> vis(n);
  vis[0] = 1;
  for(int _=1; _<n; ++_){
   queue<int> q;
   vector<int> v(n, 10000);
   q.push(i);
   v[i]=0;
   i = 0;
   while(!q.empty()){
    int y = q.front(); q.pop();
    if(!vis[y] && dist[y] > dist[i]) i = y;
    for(int j:g[y]){
     if(v[j] > v[y] && v[y] < 3) v[j] = v[y] + 1, q.push(j);
    }
   }
   cout << i+1 << " ";
   vis[i] = 1;
  }
  cout << endl;
 }
}

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

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 60 81 17 55 71 5 59 39 52 32...