Task: | Distances |
Sender: | jenkinsser |
Submission time: | 2021-01-31 14:45:52 +0200 |
Language: | C++ (C++17) |
Status: | READY |
Result: | 29 |
group | verdict | score |
---|---|---|
#1 | ACCEPTED | 29 |
#2 | TIME LIMIT EXCEEDED | 0 |
test | verdict | time | group | |
---|---|---|---|---|
#1 | ACCEPTED | 0.01 s | 1, 2 | details |
#2 | TIME LIMIT EXCEEDED | -- | 2 | details |
Code
#include <bits/stdc++.h>#define pb push_back#define mp make_pair#define st first#define nd second#define pii pair<int,int>#define N 2000005#define INF 1e9+5#define sp " "#define nl "\n"#define all(x) x.begin(),x.end()#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);#define fori(i,l,r) for(int i=l;i<=r;i++)#define forf(i,r) fori(i,1,r)#define rof(i,r,l) for(int i=r;i>=l;i--)#define ll long long#define int llusing namespace std;void solve(){int n;cin >> n;vector<int> vec(n);iota(vec.begin(),vec.end(),1);vector<int> g[n+1];int dist[n+1][n+1];for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){if(i==j)dist[i][j]=0;elsedist[i][j]=INF;}}for(int i=1;i<n;i++){int x,y;cin >> x >> y;dist[x][y]=1;dist[y][x]=1;}for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){for(int k=1;k<=n;k++){if(dist[i][j]>dist[i][k]+dist[k][j]){dist[i][j]=dist[i][k]+dist[k][j];dist[j][i]=dist[i][j];}}}}do{bool flag=true;for(int i=0;i<n-1;i++){if(dist[vec[i]][vec[i+1]]>3){flag=false;}}if(flag){for(int i:vec)cout << i << sp;cout << nl;return;}}while(next_permutation(vec.begin(),vec.end()));cout << -1 << nl;}int32_t main(){fastio()int t=1;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 3 2 4 7 5 6 8 1 3 2 8 7 5 4 6 1 3 2 5 7 8 6 4 1 2 3 4 5 6 7 8 1 3 2 4 6 8 7 5 ... Truncated |
Test 2
Group: 2
Verdict: TIME LIMIT EXCEEDED
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) |