CSES - Datatähti Open 2021 - Results
Submission details
Task:Distances
Sender:teehandsome
Submission time:2021-01-31 17:51:53 +0200
Language:C++ (C++11)
Status:READY
Result:29
Feedback
groupverdictscore
#1ACCEPTED29
#20
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2details
#2--2details

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:78:9: warning: label 'newT' defined but not used [-Wunused-label]
         newT:;
         ^~~~

Code

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define INF 1e9+7
#define all(x) x.begin(),x.end()
using namespace std;
using namespace __gnu_pbds;
using ll=long long;
using pii=pair<int,int>;
using ppi=pair<int,pii>;
using oset=tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>;

template<typename T>
void _print(vector<T> x) {cerr<<"{"; for(auto e:x) cerr<<e<<","; cerr<<"}";}
void _print(pii x) {cerr<<"{"<<x.first<<","<<x.second<<"}";}
template<typename T>
void _print(T x) {cerr<<x;}

void dbg() {cerr<<endl;}
template<typename Head,typename... Tail>
void dbg(Head H,Tail... T) {
    _print(H);
    if(sizeof...(T)) cerr<<",";
    else cerr<<"\"]";
    dbg(T...);
}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:[\"",dbg(__VA_ARGS__)

int dist[101][101];
vector<bool> flag;
int n;
bool ok;

void dfs(int u,int cnt,vector<bool> &flag,vector<int> &path) {
    if(ok) return;
    if(cnt==n) {
        for(auto e:path) {
            cout<<e<<" ";
        }
        cout<<endl;
        ok=true; return;
    }
    for(int i=1;i<=n;i++) {
        if(flag[i]) continue;
        if(dist[u][i]>3) continue;
        flag[i]=true; path.push_back(i);
        dfs(i,cnt+1,flag,path);
        flag[i]=false; path.pop_back();
    }
}

int main () {
    ios::sync_with_stdio(false); cin.tie(0);
    int t; cin>>t;
    while(t--) {
        cin>>n;
        for(int i=1;i<=n;i++) {
            for(int j=1;j<=n;j++) {
                if(i==j) dist[i][j]=0;
                else dist[i][j]=INF;
            }
        }
        for(int i=1;i<n;i++) {
            int u,v; cin>>u>>v;
            dist[u][v]=dist[v][u]=1;
        }
        for(int i=1;i<=n;i++) {
            for(int j=1;j<=n;j++) {
                for(int k=1;k<=n;k++) {
                    dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
                }
            }
        }
        flag.resize(n+1); fill(all(flag),false);
        vector<int> path; path.push_back(1); flag[1]=true;
        ok=false; dfs(1,1,flag,path);
        newT:;
    }
}

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