#include<bits/stdc++.h>
using namespace std;
#define int long long
#define maxn 100050
#define f first
#define s second
vector<vector<int> > adj(maxn);
vector<int> subtree_size(maxn,0);
vector<int> pos(maxn,0);
int currcnt = 1;
int n,input1,input2;
int centroid = 0;
void dfs1(int node,int parent){//used to find subtree size
subtree_size[node] = 1;
pos[node] = currcnt;
currcnt++;
for(auto k:adj[node]){
if(k==parent){
continue;
}
dfs1(k,node);
subtree_size[node] += subtree_size[k];
}
return;
}
vector<int> reroute(maxn,1);
int mindist = 0;
void dfs2(int node,int parent){//used to find optimal ordering
for(auto k:adj[node]){
if(k==parent){
continue;
}
dfs2(k,node);
}
if(reroute[node]==node&&parent!=-1){
swap(reroute[node],reroute[parent]);
mindist++;
}
if(reroute[node]==node&&parent==-1){
swap(reroute[node],reroute[adj[node][0]]);
mindist++;
}
return;
}
vector<int> dist(maxn,0);
void dfs3(int node,int parent,int distt){
dist[node] = distt;
for(auto k:adj[node]){
if(k!=parent){
dfs3(k,node,distt+1);
}
}
}
void dfs4(int node,int parent){
bool can = 1;
for(auto k:adj[node]){
if(k!=parent){
dfs4(k,node);
if(subtree_size[k]>(n/2)){
can = 0;
}
}
}
if(n-subtree_size[node]>(n/2)){
can = 0;
}
if(can){
centroid = node;
}
}
int maxdist = 0;
void dfs5(int node,int parent){
for(auto k:adj[node]){
if(k!=parent){
dfs5(k,node);
maxdist += subtree_size[k];
}
}
}
vector<int> nodes(maxn);
vector<int> ipos(maxn*2);
int position = 0;
void dfs6(int node,int parent){
ipos[position] = node;
ipos[position+n] = node;
nodes[node] = position;
position++;
for(auto k:adj[node]){
if(k!=parent){
dfs6(k,node);
}
}
}
vector<int> arrangement(maxn,0);
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
for(int i=0;i<maxn;i++){
reroute[i] = i;
}
cin >> n;
for(int i=0;i<n-1;i++){
cin >> input1 >> input2;
adj[input1].push_back(input2);
adj[input2].push_back(input1);
}
int root = -1;
for(int i=1;i<=n;i++){
if(adj[i].size()==(int)1){
root = i;
}
}
dfs1(root,-1);
dfs2(root,-1);
dfs4(root,-1);
dfs1(centroid,-1);
dfs5(centroid,-1);
int tt = 0;
dfs6(centroid,-1);
for(int i=1;i<=n;i++){
arrangement[ipos[nodes[i]+(n/2)]] = i;
}
cout << mindist*2 << " " << maxdist*2 << "\n";
for(int i=1;i<=n;i++){
cout << reroute[i] << " ";
//cout << i << " ";
}
cout << "\n";
for(int i=1;i<=n;i++){
//cout << 1 << " ";
cout << arrangement[i] << ' ';
//cout << i << " ";
}
}
Compilation message
Village.cpp: In function 'int32_t main()':
Village.cpp:118:9: warning: unused variable 'tt' [-Wunused-variable]
int tt = 0;
^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8960 KB |
Output is correct |
2 |
Correct |
5 ms |
8960 KB |
Output is correct |
3 |
Correct |
5 ms |
8960 KB |
Output is correct |
4 |
Correct |
5 ms |
8960 KB |
Output is correct |
5 |
Correct |
5 ms |
8960 KB |
Output is correct |
6 |
Correct |
5 ms |
8960 KB |
Output is correct |
7 |
Correct |
5 ms |
8960 KB |
Output is correct |
8 |
Correct |
5 ms |
8960 KB |
Output is correct |
9 |
Correct |
5 ms |
8960 KB |
Output is correct |
10 |
Correct |
5 ms |
8960 KB |
Output is correct |
11 |
Correct |
5 ms |
8960 KB |
Output is correct |
12 |
Correct |
5 ms |
8960 KB |
Output is correct |
13 |
Correct |
5 ms |
8960 KB |
Output is correct |
14 |
Correct |
5 ms |
8960 KB |
Output is correct |
15 |
Correct |
5 ms |
8960 KB |
Output is correct |
16 |
Correct |
5 ms |
8960 KB |
Output is correct |
17 |
Correct |
5 ms |
8960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8960 KB |
Output is correct |
2 |
Correct |
5 ms |
8960 KB |
Output is correct |
3 |
Correct |
6 ms |
8960 KB |
Output is correct |
4 |
Correct |
6 ms |
8988 KB |
Output is correct |
5 |
Correct |
6 ms |
8960 KB |
Output is correct |
6 |
Correct |
6 ms |
8960 KB |
Output is correct |
7 |
Correct |
6 ms |
9088 KB |
Output is correct |
8 |
Correct |
6 ms |
8960 KB |
Output is correct |
9 |
Correct |
6 ms |
8960 KB |
Output is correct |
10 |
Correct |
6 ms |
9088 KB |
Output is correct |
11 |
Correct |
5 ms |
9088 KB |
Output is correct |
12 |
Correct |
6 ms |
8960 KB |
Output is correct |
13 |
Correct |
8 ms |
8960 KB |
Output is correct |
14 |
Correct |
8 ms |
8960 KB |
Output is correct |
15 |
Correct |
8 ms |
8960 KB |
Output is correct |
16 |
Correct |
5 ms |
8960 KB |
Output is correct |
17 |
Correct |
6 ms |
8960 KB |
Output is correct |
18 |
Correct |
5 ms |
8960 KB |
Output is correct |
19 |
Correct |
5 ms |
8960 KB |
Output is correct |
20 |
Correct |
5 ms |
8960 KB |
Output is correct |
21 |
Correct |
6 ms |
8960 KB |
Output is correct |
22 |
Correct |
5 ms |
9088 KB |
Output is correct |
23 |
Correct |
6 ms |
8960 KB |
Output is correct |
24 |
Correct |
6 ms |
8960 KB |
Output is correct |
25 |
Correct |
5 ms |
8960 KB |
Output is correct |
26 |
Correct |
6 ms |
8960 KB |
Output is correct |
27 |
Correct |
5 ms |
8960 KB |
Output is correct |
28 |
Correct |
6 ms |
8960 KB |
Output is correct |
29 |
Correct |
5 ms |
8960 KB |
Output is correct |
30 |
Correct |
5 ms |
8960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8960 KB |
Output is correct |
2 |
Correct |
5 ms |
8960 KB |
Output is correct |
3 |
Correct |
5 ms |
8960 KB |
Output is correct |
4 |
Correct |
5 ms |
8960 KB |
Output is correct |
5 |
Correct |
5 ms |
8960 KB |
Output is correct |
6 |
Correct |
5 ms |
8960 KB |
Output is correct |
7 |
Correct |
5 ms |
8960 KB |
Output is correct |
8 |
Correct |
5 ms |
8960 KB |
Output is correct |
9 |
Correct |
5 ms |
8960 KB |
Output is correct |
10 |
Correct |
5 ms |
8960 KB |
Output is correct |
11 |
Correct |
5 ms |
8960 KB |
Output is correct |
12 |
Correct |
5 ms |
8960 KB |
Output is correct |
13 |
Correct |
5 ms |
8960 KB |
Output is correct |
14 |
Correct |
5 ms |
8960 KB |
Output is correct |
15 |
Correct |
5 ms |
8960 KB |
Output is correct |
16 |
Correct |
5 ms |
8960 KB |
Output is correct |
17 |
Correct |
5 ms |
8960 KB |
Output is correct |
18 |
Correct |
5 ms |
8960 KB |
Output is correct |
19 |
Correct |
5 ms |
8960 KB |
Output is correct |
20 |
Correct |
6 ms |
8960 KB |
Output is correct |
21 |
Correct |
6 ms |
8988 KB |
Output is correct |
22 |
Correct |
6 ms |
8960 KB |
Output is correct |
23 |
Correct |
6 ms |
8960 KB |
Output is correct |
24 |
Correct |
6 ms |
9088 KB |
Output is correct |
25 |
Correct |
6 ms |
8960 KB |
Output is correct |
26 |
Correct |
6 ms |
8960 KB |
Output is correct |
27 |
Correct |
6 ms |
9088 KB |
Output is correct |
28 |
Correct |
5 ms |
9088 KB |
Output is correct |
29 |
Correct |
6 ms |
8960 KB |
Output is correct |
30 |
Correct |
8 ms |
8960 KB |
Output is correct |
31 |
Correct |
8 ms |
8960 KB |
Output is correct |
32 |
Correct |
8 ms |
8960 KB |
Output is correct |
33 |
Correct |
5 ms |
8960 KB |
Output is correct |
34 |
Correct |
6 ms |
8960 KB |
Output is correct |
35 |
Correct |
5 ms |
8960 KB |
Output is correct |
36 |
Correct |
5 ms |
8960 KB |
Output is correct |
37 |
Correct |
5 ms |
8960 KB |
Output is correct |
38 |
Correct |
6 ms |
8960 KB |
Output is correct |
39 |
Correct |
5 ms |
9088 KB |
Output is correct |
40 |
Correct |
6 ms |
8960 KB |
Output is correct |
41 |
Correct |
6 ms |
8960 KB |
Output is correct |
42 |
Correct |
5 ms |
8960 KB |
Output is correct |
43 |
Correct |
6 ms |
8960 KB |
Output is correct |
44 |
Correct |
5 ms |
8960 KB |
Output is correct |
45 |
Correct |
6 ms |
8960 KB |
Output is correct |
46 |
Correct |
5 ms |
8960 KB |
Output is correct |
47 |
Correct |
5 ms |
8960 KB |
Output is correct |
48 |
Correct |
90 ms |
13560 KB |
Output is correct |
49 |
Correct |
110 ms |
14072 KB |
Output is correct |
50 |
Correct |
107 ms |
13944 KB |
Output is correct |
51 |
Correct |
71 ms |
12792 KB |
Output is correct |
52 |
Correct |
109 ms |
13944 KB |
Output is correct |
53 |
Correct |
88 ms |
13432 KB |
Output is correct |
54 |
Correct |
52 ms |
15096 KB |
Output is correct |
55 |
Correct |
184 ms |
21156 KB |
Output is correct |
56 |
Correct |
143 ms |
17400 KB |
Output is correct |
57 |
Correct |
132 ms |
15588 KB |
Output is correct |
58 |
Correct |
174 ms |
14584 KB |
Output is correct |
59 |
Correct |
154 ms |
14072 KB |
Output is correct |
60 |
Correct |
63 ms |
14060 KB |
Output is correct |
61 |
Correct |
71 ms |
14056 KB |
Output is correct |
62 |
Correct |
82 ms |
14328 KB |
Output is correct |
63 |
Correct |
73 ms |
13944 KB |
Output is correct |
64 |
Correct |
95 ms |
14328 KB |
Output is correct |
65 |
Correct |
82 ms |
14456 KB |
Output is correct |
66 |
Correct |
72 ms |
13948 KB |
Output is correct |
67 |
Correct |
50 ms |
12788 KB |
Output is correct |
68 |
Correct |
72 ms |
13688 KB |
Output is correct |
69 |
Correct |
78 ms |
14456 KB |
Output is correct |
70 |
Correct |
70 ms |
14072 KB |
Output is correct |
71 |
Correct |
51 ms |
12792 KB |
Output is correct |
72 |
Correct |
63 ms |
13304 KB |
Output is correct |
73 |
Correct |
85 ms |
14456 KB |
Output is correct |
74 |
Correct |
70 ms |
13944 KB |
Output is correct |
75 |
Correct |
138 ms |
14456 KB |
Output is correct |
76 |
Correct |
124 ms |
13816 KB |
Output is correct |
77 |
Correct |
122 ms |
14456 KB |
Output is correct |
78 |
Correct |
81 ms |
12408 KB |
Output is correct |
79 |
Correct |
67 ms |
13000 KB |
Output is correct |
80 |
Correct |
107 ms |
14456 KB |
Output is correct |
81 |
Correct |
86 ms |
14584 KB |
Output is correct |
82 |
Correct |
88 ms |
14072 KB |
Output is correct |