#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
vi adjList[100000];
int parent[100000],size[100000];
LLI dp[100000][2];
int doDFS(int u,int p) {
int i;
LLI m = 1e9;
parent[u] = p,size[u] = 1;
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (v != p) {
size[u] += doDFS(v,u);
dp[u][0] += dp[v][1];
dp[u][1] += min(dp[v][0]+1,dp[v][1]);
m = min(m,min(dp[v][0],dp[v][1])+1-min(dp[v][0]+1,dp[v][1]));
}
}
dp[u][1] += m;
dp[u][0] = min(dp[u][0],(LLI) 1e9),dp[u][1] = min(dp[u][1],(LLI) 1e9);
return size[u];
}
vi adjList2[100000];
int getAns(int u,int p,int t) {
int i;
if (t == 0) {
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (v != p) getAns(v,u,1);
}
}
else {
LLI m = 1e9;
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (v != p) m = min(m,min(dp[v][0],dp[v][1])+1-min(dp[v][0]+1,dp[v][1]));
}
m = max(m,0LL);
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (v != p) {
if (min(dp[v][0],dp[v][1])+1-min(dp[v][0]+1,dp[v][1]) <= m) {
adjList2[u].pb(v);
adjList2[v].pb(u);
getAns(v,u,dp[v][1] < dp[v][0]);
m = 0;
}
else getAns(v,u,dp[v][1] < dp[v][0]+1);
}
}
}
return 0;
}
vi order;
int visited[100000];
int doDFS2(int u,int p) {
int i;
order.pb(u),visited[u] = 1;
for (i = 0; i < adjList2[u].size(); i++) {
int v = adjList2[u][i];
if (v != p) doDFS2(v,u);
}
return 0;
}
int doDFS3(int u,int p) {
int i;
order.pb(u);
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (v != p) doDFS3(v,u);
}
return 0;
}
int ans[100000];
int main() {
int i;
int N,a,b;
scanf("%d",&N);
for (i = 0; i < N-1; i++) {
scanf("%d %d",&a,&b);
a--,b--;
adjList[a].pb(b);
adjList[b].pb(a);
}
int j;
LLI sum = 0;
doDFS(0,-1);
for (i = 1; i < N; i++) sum += 2*min(size[i],N-size[i]);
printf("%lld %lld\n",2*dp[0][1],sum);
getAns(0,-1,1);
for (i = 0; i < N; i++) {
if (!visited[i]) {
doDFS2(i,-1);
for (j = 0; j < order.size(); j++) ans[order[j]] = order[(j+1) % order.size()];
order.clear();
}
}
for (i = 0; i < N; i++) printf("%d ",ans[i]+1);
printf("\n");
for (i = 0; i < N; i++) {
for (j = 0; j < adjList[i].size(); j++) {
int v = adjList[i][j];
if ((v == parent[i]) && (N-size[i] > N/2)) break;
else if ((v != parent[i]) && (size[v] > N/2)) break;
}
if (j == adjList[i].size()) break;
}
int c = i;
for (i = 0; i < adjList[c].size(); i++) doDFS3(adjList[c][i],c);
if (!(N & 1)) order.pb(c);
for (i = 0; i < order.size(); i++) ans[order[i]] = order[(i+(order.size()/2)) % order.size()];
if (N & 1) ans[c] = ans[order[0]],ans[order[0]] = c;
for (i = 0; i < N; i++) printf("%d ",ans[i]+1);
printf("\n");
return 0;
}
Compilation message
Village.cpp: In function 'int doDFS(int, int)':
Village.cpp:65:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for (i = 0; i < adjList[u].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~
Village.cpp: In function 'int getAns(int, int, int)':
Village.cpp:82:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for (i = 0; i < adjList[u].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~
Village.cpp:89:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for (i = 0; i < adjList[u].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~
Village.cpp:94:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for (i = 0; i < adjList[u].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~
Village.cpp: In function 'int doDFS2(int, int)':
Village.cpp:114:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | for (i = 0; i < adjList2[u].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~~
Village.cpp: In function 'int doDFS3(int, int)':
Village.cpp:123:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
123 | for (i = 0; i < adjList[u].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~
Village.cpp: In function 'int main()':
Village.cpp:150:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
150 | for (j = 0; j < order.size(); j++) ans[order[j]] = order[(j+1) % order.size()];
| ~~^~~~~~~~~~~~~~
Village.cpp:158:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
158 | for (j = 0; j < adjList[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~~~~~
Village.cpp:163:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
163 | if (j == adjList[i].size()) break;
| ~~^~~~~~~~~~~~~~~~~~~~
Village.cpp:166:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
166 | for (i = 0; i < adjList[c].size(); i++) doDFS3(adjList[c][i],c);
| ~~^~~~~~~~~~~~~~~~~~~
Village.cpp:168:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
168 | for (i = 0; i < order.size(); i++) ans[order[i]] = order[(i+(order.size()/2)) % order.size()];
| ~~^~~~~~~~~~~~~~
Village.cpp:133:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
133 | scanf("%d",&N);
| ~~~~~^~~~~~~~~
Village.cpp:135:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
135 | scanf("%d %d",&a,&b);
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
5080 KB |
Output is correct |
5 |
Correct |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
3 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
5 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
5 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
5084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
5 ms |
5120 KB |
Output is correct |
4 |
Correct |
6 ms |
5248 KB |
Output is correct |
5 |
Correct |
5 ms |
5120 KB |
Output is correct |
6 |
Correct |
5 ms |
5120 KB |
Output is correct |
7 |
Correct |
5 ms |
5248 KB |
Output is correct |
8 |
Correct |
5 ms |
5120 KB |
Output is correct |
9 |
Correct |
5 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5248 KB |
Output is correct |
11 |
Correct |
5 ms |
5120 KB |
Output is correct |
12 |
Correct |
5 ms |
5120 KB |
Output is correct |
13 |
Correct |
5 ms |
5248 KB |
Output is correct |
14 |
Correct |
5 ms |
5224 KB |
Output is correct |
15 |
Correct |
5 ms |
5120 KB |
Output is correct |
16 |
Correct |
5 ms |
5120 KB |
Output is correct |
17 |
Correct |
5 ms |
5248 KB |
Output is correct |
18 |
Correct |
5 ms |
5120 KB |
Output is correct |
19 |
Correct |
5 ms |
5120 KB |
Output is correct |
20 |
Correct |
4 ms |
5120 KB |
Output is correct |
21 |
Correct |
5 ms |
5120 KB |
Output is correct |
22 |
Correct |
5 ms |
5120 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
4 ms |
5120 KB |
Output is correct |
25 |
Correct |
5 ms |
5120 KB |
Output is correct |
26 |
Correct |
5 ms |
5120 KB |
Output is correct |
27 |
Correct |
4 ms |
5120 KB |
Output is correct |
28 |
Correct |
5 ms |
5120 KB |
Output is correct |
29 |
Correct |
5 ms |
5232 KB |
Output is correct |
30 |
Correct |
5 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
5080 KB |
Output is correct |
5 |
Correct |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
3 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
5 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
5 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
5084 KB |
Output is correct |
18 |
Correct |
4 ms |
5120 KB |
Output is correct |
19 |
Correct |
4 ms |
5120 KB |
Output is correct |
20 |
Correct |
5 ms |
5120 KB |
Output is correct |
21 |
Correct |
6 ms |
5248 KB |
Output is correct |
22 |
Correct |
5 ms |
5120 KB |
Output is correct |
23 |
Correct |
5 ms |
5120 KB |
Output is correct |
24 |
Correct |
5 ms |
5248 KB |
Output is correct |
25 |
Correct |
5 ms |
5120 KB |
Output is correct |
26 |
Correct |
5 ms |
5120 KB |
Output is correct |
27 |
Correct |
4 ms |
5248 KB |
Output is correct |
28 |
Correct |
5 ms |
5120 KB |
Output is correct |
29 |
Correct |
5 ms |
5120 KB |
Output is correct |
30 |
Correct |
5 ms |
5248 KB |
Output is correct |
31 |
Correct |
5 ms |
5224 KB |
Output is correct |
32 |
Correct |
5 ms |
5120 KB |
Output is correct |
33 |
Correct |
5 ms |
5120 KB |
Output is correct |
34 |
Correct |
5 ms |
5248 KB |
Output is correct |
35 |
Correct |
5 ms |
5120 KB |
Output is correct |
36 |
Correct |
5 ms |
5120 KB |
Output is correct |
37 |
Correct |
4 ms |
5120 KB |
Output is correct |
38 |
Correct |
5 ms |
5120 KB |
Output is correct |
39 |
Correct |
5 ms |
5120 KB |
Output is correct |
40 |
Correct |
4 ms |
5120 KB |
Output is correct |
41 |
Correct |
4 ms |
5120 KB |
Output is correct |
42 |
Correct |
5 ms |
5120 KB |
Output is correct |
43 |
Correct |
5 ms |
5120 KB |
Output is correct |
44 |
Correct |
4 ms |
5120 KB |
Output is correct |
45 |
Correct |
5 ms |
5120 KB |
Output is correct |
46 |
Correct |
5 ms |
5232 KB |
Output is correct |
47 |
Correct |
5 ms |
5120 KB |
Output is correct |
48 |
Correct |
159 ms |
16400 KB |
Output is correct |
49 |
Correct |
162 ms |
17400 KB |
Output is correct |
50 |
Correct |
174 ms |
17508 KB |
Output is correct |
51 |
Correct |
119 ms |
14704 KB |
Output is correct |
52 |
Correct |
165 ms |
17344 KB |
Output is correct |
53 |
Correct |
155 ms |
16112 KB |
Output is correct |
54 |
Correct |
77 ms |
14712 KB |
Output is correct |
55 |
Correct |
208 ms |
24864 KB |
Output is correct |
56 |
Correct |
212 ms |
20848 KB |
Output is correct |
57 |
Correct |
220 ms |
19524 KB |
Output is correct |
58 |
Correct |
229 ms |
18540 KB |
Output is correct |
59 |
Correct |
182 ms |
17524 KB |
Output is correct |
60 |
Correct |
127 ms |
18032 KB |
Output is correct |
61 |
Correct |
147 ms |
18332 KB |
Output is correct |
62 |
Correct |
164 ms |
18288 KB |
Output is correct |
63 |
Correct |
145 ms |
17476 KB |
Output is correct |
64 |
Correct |
145 ms |
18164 KB |
Output is correct |
65 |
Correct |
149 ms |
18416 KB |
Output is correct |
66 |
Correct |
124 ms |
17604 KB |
Output is correct |
67 |
Correct |
99 ms |
14704 KB |
Output is correct |
68 |
Correct |
121 ms |
16368 KB |
Output is correct |
69 |
Correct |
161 ms |
18524 KB |
Output is correct |
70 |
Correct |
122 ms |
17652 KB |
Output is correct |
71 |
Correct |
90 ms |
14324 KB |
Output is correct |
72 |
Correct |
130 ms |
15724 KB |
Output is correct |
73 |
Correct |
151 ms |
18420 KB |
Output is correct |
74 |
Correct |
133 ms |
17332 KB |
Output is correct |
75 |
Correct |
203 ms |
17372 KB |
Output is correct |
76 |
Correct |
202 ms |
17268 KB |
Output is correct |
77 |
Correct |
155 ms |
17600 KB |
Output is correct |
78 |
Correct |
84 ms |
13596 KB |
Output is correct |
79 |
Correct |
118 ms |
14900 KB |
Output is correct |
80 |
Correct |
182 ms |
17356 KB |
Output is correct |
81 |
Correct |
148 ms |
17784 KB |
Output is correct |
82 |
Correct |
146 ms |
17972 KB |
Output is correct |