#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#include "bits_stdc++.h"
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%lld", &x);
#define input2(x, y) scanf("%lld%lld", &x, &y);
#define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a);
#define print(x, y) printf("%lld%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
using namespace std;
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
ll n, x, y, minval, maxval = 0, label = -1;
vector<ll> adj[100005];
ll minc[100005], maxc[100005], size1[100005], depth[100005], in[100005], conv[100005];
void dfs1(ll p, ll from)
{
size1[from] = 1;
for (ll u: adj[from]) if(p!=u) {dfs1(from, u); size1[from]+=size1[u];}
if (p!=-1 && minc[from]==from)
{
minc[p] = from;
minc[from] = p;
}
}
ll find_centroid(ll p, ll from)
{
for (ll u: adj[from])
{
if (u==p) continue;
if (size1[u]>n/2) return find_centroid(from, u);
}
return from;
}
void dfs2(ll p, ll from)
{
in[from]= ++label;
conv[label] = from;
maxval += depth[from]*2;
for (ll u: adj[from])
{
if (u==p) continue;
depth[u] = depth[from]+1;
dfs2(from, u);
}
}
int main()
{
input(n);
for (ll i=1; i<=n; ++i) minc[i] = maxc[i] = i;
for (ll i=0; i<n-1; ++i)
{
input2(x, y);
adj[x].pb(y);
adj[y].pb(x);
}
// Minimum:
// ans = ((n+1)/2)*2
minval = ((n+1)/2)*2;
dfs1(-1, 1);
if (minc[1]==1) swap(minc[adj[1][0]], minc[1]); // odd n lead to 1 without paur
// Maximum:
// Find centroid, ensure that all ppl change to house that is out of its centroid subtree
// i to be paired with (i+ (n)/2)%n
// As all |subtree| < n/2, his ensure that both are from different subtree
// This is optimal.
ll centroid = find_centroid(-1, 1);
depth[centroid] = 0;
dfs2(-1, centroid);
for (ll i=1; i<=n; ++i)
{
ll in1 = in[i];
ll in2 = (in1 + n/2)%n;
maxc[conv[in2]] = i;
}
// output
cout << minval << ' ' << maxval << '\n';
for (ll i=1; i<=n; ++i) print(minc[i], ' ');
cout << '\n';
for (ll i=1; i<=n; ++i) print(maxc[i], ' ');
return 0;
}
Compilation message
Village.cpp: In function 'int main()':
Village.cpp:11:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
11 | #define input(x) scanf("%lld", &x);
| ~~~~~^~~~~~~~~~~~
Village.cpp:68:2: note: in expansion of macro 'input'
68 | input(n);
| ^~~~~
Village.cpp:12:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | #define input2(x, y) scanf("%lld%lld", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
Village.cpp:72:3: note: in expansion of macro 'input2'
72 | input2(x, y);
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
7260 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
3 |
Correct |
1 ms |
7260 KB |
Output is correct |
4 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
5 |
Partially correct |
2 ms |
7256 KB |
Partially correct |
6 |
Partially correct |
1 ms |
7260 KB |
Partially correct |
7 |
Partially correct |
1 ms |
7300 KB |
Partially correct |
8 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
9 |
Partially correct |
2 ms |
7304 KB |
Partially correct |
10 |
Correct |
2 ms |
7260 KB |
Output is correct |
11 |
Correct |
2 ms |
7260 KB |
Output is correct |
12 |
Correct |
1 ms |
7256 KB |
Output is correct |
13 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
14 |
Partially correct |
1 ms |
7300 KB |
Partially correct |
15 |
Partially correct |
1 ms |
7260 KB |
Partially correct |
16 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
17 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
2 |
Partially correct |
2 ms |
7332 KB |
Partially correct |
3 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
4 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
5 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
6 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
7 |
Correct |
2 ms |
7260 KB |
Output is correct |
8 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
9 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
10 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
11 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
12 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
13 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
14 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
15 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
16 |
Partially correct |
3 ms |
7256 KB |
Partially correct |
17 |
Partially correct |
2 ms |
7300 KB |
Partially correct |
18 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
19 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
20 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
21 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
22 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
23 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
24 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
25 |
Partially correct |
2 ms |
7256 KB |
Partially correct |
26 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
27 |
Partially correct |
2 ms |
7296 KB |
Partially correct |
28 |
Partially correct |
2 ms |
7256 KB |
Partially correct |
29 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
30 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
7260 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
3 |
Correct |
1 ms |
7260 KB |
Output is correct |
4 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
5 |
Partially correct |
2 ms |
7256 KB |
Partially correct |
6 |
Partially correct |
1 ms |
7260 KB |
Partially correct |
7 |
Partially correct |
1 ms |
7300 KB |
Partially correct |
8 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
9 |
Partially correct |
2 ms |
7304 KB |
Partially correct |
10 |
Correct |
2 ms |
7260 KB |
Output is correct |
11 |
Correct |
2 ms |
7260 KB |
Output is correct |
12 |
Correct |
1 ms |
7256 KB |
Output is correct |
13 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
14 |
Partially correct |
1 ms |
7300 KB |
Partially correct |
15 |
Partially correct |
1 ms |
7260 KB |
Partially correct |
16 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
17 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
18 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
19 |
Partially correct |
2 ms |
7332 KB |
Partially correct |
20 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
21 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
22 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
23 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
24 |
Correct |
2 ms |
7260 KB |
Output is correct |
25 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
26 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
27 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
28 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
29 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
30 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
31 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
32 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
33 |
Partially correct |
3 ms |
7256 KB |
Partially correct |
34 |
Partially correct |
2 ms |
7300 KB |
Partially correct |
35 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
36 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
37 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
38 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
39 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
40 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
41 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
42 |
Partially correct |
2 ms |
7256 KB |
Partially correct |
43 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
44 |
Partially correct |
2 ms |
7296 KB |
Partially correct |
45 |
Partially correct |
2 ms |
7256 KB |
Partially correct |
46 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
47 |
Partially correct |
2 ms |
7260 KB |
Partially correct |
48 |
Partially correct |
46 ms |
12512 KB |
Partially correct |
49 |
Partially correct |
52 ms |
13148 KB |
Partially correct |
50 |
Partially correct |
51 ms |
13140 KB |
Partially correct |
51 |
Partially correct |
50 ms |
11856 KB |
Partially correct |
52 |
Partially correct |
51 ms |
13136 KB |
Partially correct |
53 |
Partially correct |
45 ms |
12628 KB |
Partially correct |
54 |
Correct |
36 ms |
12880 KB |
Output is correct |
55 |
Correct |
57 ms |
18528 KB |
Output is correct |
56 |
Partially correct |
69 ms |
15804 KB |
Partially correct |
57 |
Partially correct |
62 ms |
14932 KB |
Partially correct |
58 |
Partially correct |
53 ms |
13904 KB |
Partially correct |
59 |
Partially correct |
49 ms |
13064 KB |
Partially correct |
60 |
Partially correct |
46 ms |
13252 KB |
Partially correct |
61 |
Partially correct |
44 ms |
13120 KB |
Partially correct |
62 |
Partially correct |
52 ms |
13336 KB |
Partially correct |
63 |
Partially correct |
42 ms |
13112 KB |
Partially correct |
64 |
Partially correct |
45 ms |
13520 KB |
Partially correct |
65 |
Partially correct |
44 ms |
13672 KB |
Partially correct |
66 |
Partially correct |
44 ms |
13148 KB |
Partially correct |
67 |
Partially correct |
46 ms |
12040 KB |
Partially correct |
68 |
Partially correct |
45 ms |
12624 KB |
Partially correct |
69 |
Partially correct |
45 ms |
13744 KB |
Partially correct |
70 |
Partially correct |
43 ms |
13136 KB |
Partially correct |
71 |
Partially correct |
30 ms |
11860 KB |
Partially correct |
72 |
Partially correct |
34 ms |
12372 KB |
Partially correct |
73 |
Partially correct |
63 ms |
13732 KB |
Partially correct |
74 |
Partially correct |
39 ms |
13140 KB |
Partially correct |
75 |
Partially correct |
49 ms |
13652 KB |
Partially correct |
76 |
Partially correct |
50 ms |
13056 KB |
Partially correct |
77 |
Partially correct |
47 ms |
13396 KB |
Partially correct |
78 |
Partially correct |
31 ms |
11356 KB |
Partially correct |
79 |
Partially correct |
37 ms |
12112 KB |
Partially correct |
80 |
Partially correct |
59 ms |
13648 KB |
Partially correct |
81 |
Partially correct |
48 ms |
13764 KB |
Partially correct |
82 |
Partially correct |
45 ms |
13188 KB |
Partially correct |