#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define mp make_pair
#define xx first
#define yy second
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(x) x.begin(),x.end()
#define ff(i,a,b) for (int i = a; i < b; i++)
#define fff(i,a,b) for (int i = a; i <= b; i++)
#define bff(i,a,b) for (int i = b-1; i >= a; i--)
#define bfff(i,a,b) for (int i = b; i >= a; i--)
using namespace std;
long double typedef ld;
unsigned int typedef ui;
long long int typedef li;
pair<int,int> typedef pii;
pair<li,li> typedef pli;
pair<ld,ld> typedef pld;
vector<vector<int>> typedef graph;
unsigned long long int typedef ull;
//const int mod = 998244353;
const int mod = 1000000007;
//Note to self: Check for overflow
graph g(100005);
int tin[100005],tout[100005];
int dub[100005],up[20][100005],siz[100005];
int VREME=0;
void dfs(int p, int q)
{
tin[p]=++VREME,dub[p]=dub[q]+1,up[0][p]=q,siz[p]=1;
for (auto it : g[p]) if (it!=q) dfs(it,p),siz[p]+=siz[it];
tout[p]=VREME;
}
void build(int n)
{
ff(k,1,20) fff(i,1,n) up[k][i]=up[k-1][up[k-1][i]];
}
bool anc(int a, int b)
{
if (a==0) return true; if (b==0) return false;
return tin[a]<=tin[b] && tout[a]>=tout[b];
}
int Lca(int a, int b)
{
bff(k,0,20) if (!anc(up[k][a],b)) a=up[k][a];
if (!anc(a,b)) a=up[0][a]; return a;
}
int dist(int a, int b)
{
return dub[a]+dub[b]-2*dub[Lca(a,b)];
}
int find_centroid(int p, int q, int n)
{
int c=p;
for (auto it : g[p]) if (it!=q)
{
int tc=find_centroid(it,p,n);
if (tc!=-1) return tc;
if (2*siz[it]>n) c=-1;
}
if (2*(n-siz[p])>n) c=-1;
return c;
}
void poseti(int p, int q, vector<int> &dodaj)
{
dodaj.pb(p);
for (auto it : g[p])
if (it!=q) poseti(it,p, dodaj);
}
///ans arrays
int vel[100005];
int mal[100005];
void mfs(int p, int q)
{
for (auto it : g[p]) if (it!=q) mfs(it,p);
vector<int> spajam;
for (auto it : g[p]) if (it!=q) if (siz[it]%2==1) spajam.pb(it);
if (siz[p]%2==0) spajam.pb(p);
for (int i=1;i<(int)spajam.size();i+=2)
mal[spajam[i]]=spajam[i-1],mal[spajam[i-1]]=spajam[i];
}
int main()
{
FAST;
int n; cin>>n;
ff(i,1,n)
{
int u,v; cin>>u>>v;
g[u].pb(v),g[v].pb(u);
}
dfs(1,0),tout[1]=VREME,build(n);
int c=find_centroid(1,0,n);
///veliki
vector<int> cor,big;
for (auto it : g[c])
{
if (2*max(siz[it],n-siz[it])<n) poseti(it,c,cor);
else poseti(it,c,big);
}
vector<int> border;
border.pb(c);
for (auto it : cor) border.pb(it);
for (auto it : big) border.pb(it);
ff(i,0,n) vel[border[i]]=border[(i+n/2)%n];
///mali
mfs(1,0);
if (n%2==1)
{
int dje=g[1].front();
mal[1]=dje;
mal[mal[dje]]=1;
}
///answer
li malans=0,velans=0;
fff(i,1,n) malans+=dist(i,mal[i]);
fff(i,1,n) velans+=dist(i,vel[i]);
cout<<malans<<" "<<velans<<"\n";
fff(i,1,n) cout<<mal[i]<<" "; cout<<"\n";
fff(i,1,n) cout<<vel[i]<<" "; cout<<"\n";
}
//Note to self: Check for overflow
Compilation message
Village.cpp: In function 'bool anc(int, int)':
Village.cpp:56:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
56 | if (a==0) return true; if (b==0) return false;
| ^~
Village.cpp:56:28: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
56 | if (a==0) return true; if (b==0) return false;
| ^~
Village.cpp: In function 'int Lca(int, int)':
Village.cpp:63:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
63 | if (!anc(a,b)) a=up[0][a]; return a;
| ^~
Village.cpp:63:32: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
63 | if (!anc(a,b)) a=up[0][a]; return a;
| ^~~~~~
Village.cpp: In function 'int main()':
Village.cpp:13:20: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
13 | #define fff(i,a,b) for (int i = a; i <= b; i++)
| ^~~
Village.cpp:151:5: note: in expansion of macro 'fff'
151 | fff(i,1,n) cout<<mal[i]<<" "; cout<<"\n";
| ^~~
Village.cpp:151:35: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
151 | fff(i,1,n) cout<<mal[i]<<" "; cout<<"\n";
| ^~~~
Village.cpp:13:20: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
13 | #define fff(i,a,b) for (int i = a; i <= b; i++)
| ^~~
Village.cpp:152:5: note: in expansion of macro 'fff'
152 | fff(i,1,n) cout<<vel[i]<<" "; cout<<"\n";
| ^~~
Village.cpp:152:35: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
152 | fff(i,1,n) cout<<vel[i]<<" "; cout<<"\n";
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
1 ms |
2772 KB |
Output is correct |
3 |
Correct |
2 ms |
2692 KB |
Output is correct |
4 |
Correct |
2 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Partially correct |
2 ms |
2784 KB |
Partially correct |
7 |
Correct |
3 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2684 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
2 ms |
2692 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Partially correct |
2 ms |
2772 KB |
Partially correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Partially correct |
2 ms |
2688 KB |
Partially correct |
16 |
Partially correct |
2 ms |
2688 KB |
Partially correct |
17 |
Partially correct |
2 ms |
2772 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
2 ms |
2772 KB |
Partially correct |
2 |
Partially correct |
2 ms |
2772 KB |
Partially correct |
3 |
Partially correct |
2 ms |
2816 KB |
Partially correct |
4 |
Partially correct |
3 ms |
2900 KB |
Partially correct |
5 |
Partially correct |
3 ms |
2900 KB |
Partially correct |
6 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
7 |
Correct |
3 ms |
3028 KB |
Output is correct |
8 |
Partially correct |
3 ms |
2900 KB |
Partially correct |
9 |
Partially correct |
3 ms |
2944 KB |
Partially correct |
10 |
Partially correct |
3 ms |
2900 KB |
Partially correct |
11 |
Partially correct |
3 ms |
2900 KB |
Partially correct |
12 |
Correct |
2 ms |
2948 KB |
Output is correct |
13 |
Correct |
2 ms |
2900 KB |
Output is correct |
14 |
Partially correct |
3 ms |
2900 KB |
Partially correct |
15 |
Partially correct |
2 ms |
2952 KB |
Partially correct |
16 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
17 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
18 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
19 |
Partially correct |
2 ms |
2944 KB |
Partially correct |
20 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
21 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
22 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
23 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
24 |
Partially correct |
3 ms |
2900 KB |
Partially correct |
25 |
Partially correct |
2 ms |
2816 KB |
Partially correct |
26 |
Partially correct |
3 ms |
2900 KB |
Partially correct |
27 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
28 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
29 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
30 |
Partially correct |
2 ms |
2944 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
1 ms |
2772 KB |
Output is correct |
3 |
Correct |
2 ms |
2692 KB |
Output is correct |
4 |
Correct |
2 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Partially correct |
2 ms |
2784 KB |
Partially correct |
7 |
Correct |
3 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2684 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
2 ms |
2692 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Partially correct |
2 ms |
2772 KB |
Partially correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Partially correct |
2 ms |
2688 KB |
Partially correct |
16 |
Partially correct |
2 ms |
2688 KB |
Partially correct |
17 |
Partially correct |
2 ms |
2772 KB |
Partially correct |
18 |
Partially correct |
2 ms |
2772 KB |
Partially correct |
19 |
Partially correct |
2 ms |
2772 KB |
Partially correct |
20 |
Partially correct |
2 ms |
2816 KB |
Partially correct |
21 |
Partially correct |
3 ms |
2900 KB |
Partially correct |
22 |
Partially correct |
3 ms |
2900 KB |
Partially correct |
23 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
24 |
Correct |
3 ms |
3028 KB |
Output is correct |
25 |
Partially correct |
3 ms |
2900 KB |
Partially correct |
26 |
Partially correct |
3 ms |
2944 KB |
Partially correct |
27 |
Partially correct |
3 ms |
2900 KB |
Partially correct |
28 |
Partially correct |
3 ms |
2900 KB |
Partially correct |
29 |
Correct |
2 ms |
2948 KB |
Output is correct |
30 |
Correct |
2 ms |
2900 KB |
Output is correct |
31 |
Partially correct |
3 ms |
2900 KB |
Partially correct |
32 |
Partially correct |
2 ms |
2952 KB |
Partially correct |
33 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
34 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
35 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
36 |
Partially correct |
2 ms |
2944 KB |
Partially correct |
37 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
38 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
39 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
40 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
41 |
Partially correct |
3 ms |
2900 KB |
Partially correct |
42 |
Partially correct |
2 ms |
2816 KB |
Partially correct |
43 |
Partially correct |
3 ms |
2900 KB |
Partially correct |
44 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
45 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
46 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
47 |
Partially correct |
2 ms |
2944 KB |
Partially correct |
48 |
Partially correct |
102 ms |
17284 KB |
Partially correct |
49 |
Partially correct |
114 ms |
18640 KB |
Partially correct |
50 |
Partially correct |
106 ms |
18592 KB |
Partially correct |
51 |
Partially correct |
81 ms |
15304 KB |
Partially correct |
52 |
Partially correct |
105 ms |
18548 KB |
Partially correct |
53 |
Partially correct |
90 ms |
16976 KB |
Partially correct |
54 |
Correct |
71 ms |
16512 KB |
Output is correct |
55 |
Correct |
203 ms |
30604 KB |
Output is correct |
56 |
Partially correct |
193 ms |
24336 KB |
Partially correct |
57 |
Partially correct |
188 ms |
22344 KB |
Partially correct |
58 |
Partially correct |
177 ms |
20556 KB |
Partially correct |
59 |
Partially correct |
123 ms |
18764 KB |
Partially correct |
60 |
Correct |
69 ms |
18828 KB |
Output is correct |
61 |
Partially correct |
77 ms |
18972 KB |
Partially correct |
62 |
Partially correct |
85 ms |
19020 KB |
Partially correct |
63 |
Partially correct |
77 ms |
18096 KB |
Partially correct |
64 |
Partially correct |
89 ms |
18896 KB |
Partially correct |
65 |
Partially correct |
87 ms |
19060 KB |
Partially correct |
66 |
Partially correct |
71 ms |
18116 KB |
Partially correct |
67 |
Partially correct |
53 ms |
14956 KB |
Partially correct |
68 |
Partially correct |
68 ms |
16676 KB |
Partially correct |
69 |
Partially correct |
82 ms |
19208 KB |
Partially correct |
70 |
Partially correct |
74 ms |
18120 KB |
Partially correct |
71 |
Partially correct |
67 ms |
14280 KB |
Partially correct |
72 |
Partially correct |
71 ms |
15832 KB |
Partially correct |
73 |
Partially correct |
90 ms |
19056 KB |
Partially correct |
74 |
Partially correct |
72 ms |
17736 KB |
Partially correct |
75 |
Partially correct |
113 ms |
18588 KB |
Partially correct |
76 |
Correct |
96 ms |
18432 KB |
Output is correct |
77 |
Partially correct |
86 ms |
18236 KB |
Partially correct |
78 |
Partially correct |
53 ms |
13644 KB |
Partially correct |
79 |
Partially correct |
69 ms |
15424 KB |
Partially correct |
80 |
Partially correct |
105 ms |
18508 KB |
Partially correct |
81 |
Partially correct |
99 ms |
18400 KB |
Partially correct |
82 |
Partially correct |
80 ms |
18840 KB |
Partially correct |