#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
typedef pair<int32_t, int32_t> pi;
typedef vector <int> vi;
typedef vector <pi> vpi;
typedef pair<pi, ll> pii;
typedef set <ll> si;
typedef long double ld;
#define f first
#define s second
#define mp make_pair
#define FOR(i,s,e) for(int i=s;i<=int(e);++i)
#define DEC(i,s,e) for(int i=s;i>=int(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define maxn 100010
#define INF (ll)1e9+10
#define MOD 1000000007
typedef pair <vi, int> pvi;
typedef pair <int,pi> ipi;
typedef vector <pii> vpii;
int N,a,b,c;
vi adj[maxn];
int maxans[maxn];
vi A[maxn];
int sz[maxn], depth[maxn];
void dfs(int x,int p){
sz[x] = 1;
aFOR(i,adj[x]) if (i != p){
depth[i] = depth[x] + 1;
dfs(i,x);
sz[x] += sz[i];
}
}
int minans[maxn], minval = 0;
void getmin(int x,int p){
aFOR(i,adj[x]) if (i != p){
getmin(i,x);
if (minans[i] == i){
minval += 2; swap(minans[i], minans[x]);
}
}
}
int get_cent(int x,int p){
aFOR(i,adj[x]) if (i != p && sz[i] > N/2) return get_cent(i,x);
return x;
}
vi v;
void get_set(int x,int p,int y){
v.pb(x);
aFOR(i,adj[x]) if (i != p) get_set(i,x,y);
}
int32_t main(){
fast;
cin >> N;
FOR(i,1,N-1){
cin >> a >> b; adj[a].pb(b); adj[b].pb(a);
}
dfs(1,-1);
FOR(i,1,N) minans[i] = i;
getmin(1,-1);
if (minans[1] == 1){
minval += 2;
swap(minans[1], minans[adj[1].back()]);
}
int cent = get_cent(1,-1);
depth[cent] = 0;
dfs(cent,-1);
int maxval = 0;
FOR(i,1,N) maxval += 2*min(sz[i], N - sz[i]);
int mxsz = 1;
aFOR(i,adj[cent]){
get_set(i, cent, i); mxsz = max(mxsz, sz[i]);
}
v.pb(cent);
vi v2;
aFOR(i,v) v2.pb(i);
rotate(v2.begin(), v2.begin() + mxsz, v2.end());
FOR(i,0,v.size()-1) maxans[v[i]] = v2[i];
cout << minval << ' ' << maxval << '\n';
FOR(i,1,N) cout << minans[i] << ' '; cout << '\n';
FOR(i,1,N) cout << maxans[i] << ' ';
}
Compilation message
Village.cpp: In function 'int32_t main()':
Village.cpp:15:20: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
15 | #define FOR(i,s,e) for(int i=s;i<=int(e);++i)
| ^~~
Village.cpp:92:5: note: in expansion of macro 'FOR'
92 | FOR(i,1,N) cout << minans[i] << ' '; cout << '\n';
| ^~~
Village.cpp:92:42: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
92 | FOR(i,1,N) cout << minans[i] << ' '; cout << '\n';
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
4 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
5 ms |
5076 KB |
Output is correct |
11 |
Correct |
4 ms |
5076 KB |
Output is correct |
12 |
Correct |
4 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5204 KB |
Output is correct |
20 |
Correct |
3 ms |
5076 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
3 ms |
5144 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5076 KB |
Output is correct |
30 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
3 ms |
5076 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
4 ms |
5076 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
5 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5076 KB |
Output is correct |
30 |
Correct |
3 ms |
5076 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
3 ms |
5076 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5076 KB |
Output is correct |
36 |
Correct |
3 ms |
5204 KB |
Output is correct |
37 |
Correct |
3 ms |
5076 KB |
Output is correct |
38 |
Correct |
3 ms |
5076 KB |
Output is correct |
39 |
Correct |
3 ms |
5144 KB |
Output is correct |
40 |
Correct |
4 ms |
5076 KB |
Output is correct |
41 |
Correct |
3 ms |
5076 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 ms |
5076 KB |
Output is correct |
44 |
Correct |
3 ms |
5076 KB |
Output is correct |
45 |
Correct |
3 ms |
5076 KB |
Output is correct |
46 |
Correct |
4 ms |
5076 KB |
Output is correct |
47 |
Correct |
3 ms |
5076 KB |
Output is correct |
48 |
Correct |
83 ms |
13896 KB |
Output is correct |
49 |
Correct |
94 ms |
14800 KB |
Output is correct |
50 |
Correct |
74 ms |
14780 KB |
Output is correct |
51 |
Correct |
70 ms |
12596 KB |
Output is correct |
52 |
Correct |
88 ms |
14656 KB |
Output is correct |
53 |
Correct |
72 ms |
13660 KB |
Output is correct |
54 |
Correct |
43 ms |
12376 KB |
Output is correct |
55 |
Correct |
130 ms |
20156 KB |
Output is correct |
56 |
Correct |
132 ms |
17428 KB |
Output is correct |
57 |
Correct |
91 ms |
16456 KB |
Output is correct |
58 |
Correct |
98 ms |
15632 KB |
Output is correct |
59 |
Correct |
84 ms |
14860 KB |
Output is correct |
60 |
Correct |
60 ms |
14880 KB |
Output is correct |
61 |
Correct |
62 ms |
14856 KB |
Output is correct |
62 |
Correct |
67 ms |
15140 KB |
Output is correct |
63 |
Correct |
53 ms |
14528 KB |
Output is correct |
64 |
Correct |
66 ms |
15112 KB |
Output is correct |
65 |
Correct |
58 ms |
15204 KB |
Output is correct |
66 |
Correct |
61 ms |
14480 KB |
Output is correct |
67 |
Correct |
42 ms |
12352 KB |
Output is correct |
68 |
Correct |
48 ms |
13652 KB |
Output is correct |
69 |
Correct |
66 ms |
15368 KB |
Output is correct |
70 |
Correct |
69 ms |
14704 KB |
Output is correct |
71 |
Correct |
40 ms |
12224 KB |
Output is correct |
72 |
Correct |
68 ms |
13056 KB |
Output is correct |
73 |
Correct |
84 ms |
15280 KB |
Output is correct |
74 |
Correct |
58 ms |
14412 KB |
Output is correct |
75 |
Correct |
104 ms |
15196 KB |
Output is correct |
76 |
Correct |
85 ms |
14576 KB |
Output is correct |
77 |
Correct |
75 ms |
14984 KB |
Output is correct |
78 |
Correct |
46 ms |
11776 KB |
Output is correct |
79 |
Correct |
68 ms |
12736 KB |
Output is correct |
80 |
Correct |
86 ms |
15240 KB |
Output is correct |
81 |
Correct |
70 ms |
15104 KB |
Output is correct |
82 |
Correct |
71 ms |
14844 KB |
Output is correct |