# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
418828 |
2021-06-06T01:26:45 Z |
ly20 |
Village (BOI20_village) |
C++17 |
|
132 ms |
20492 KB |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 112345;
vector <int> grafo[MAXN];
long long mn, mx;
int rmn[MAXN], rmx[MAXN];
int cur[MAXN];
int grau[MAXN];
int marc[MAXN];
int dist[MAXN];
int sub[MAXN];
int n;
vector <int> cmp[MAXN];
int cm;
void dfs(int v, int p) {
sub[v] = 1;
for(int i = 0; i < grafo[v].size(); i++) {
int viz = grafo[v][i];
if(viz == p) continue;
dfs(viz, v);
sub[v] += sub[viz];
}
mx += min(sub[v], n - sub[v]);
//printf("%d\n", sub[v]);
}
int dfs2(int v, int p) {
for(int i = 0; i < grafo[v].size(); i++) {
int viz = grafo[v][i];
if(viz == p) continue;
if(sub[viz] > n / 2) return dfs2(viz, v);
}
return v;
}
void dfs3(int v, int p) {
cmp[cm].push_back(v);
for(int i = 0; i < grafo[v].size(); i++) {
int viz = grafo[v][i];
if(viz == p) continue;
dfs3(viz, v);
}
}
int main() {
scanf("%d", &n);
for(int i = 0; i < n - 1; i++) {
int a, b;
scanf("%d %d", &a, &b);
grafo[a].push_back(b);
grafo[b].push_back(a);
grau[a]++; grau[b]++;
}
queue <int> fila;
for(int i = 1; i <= n; i++) {
rmn[i] = i; rmx[i] = i;
if(grau[i] == 1) fila.push(i);
}
while(!fila.empty()) {
int cur = fila.front(); fila.pop();
marc[cur] = 1;
int p = grafo[cur][0];
for(int i = 0 ;i < grafo[cur].size(); i++) {
int viz = grafo[cur][i];
grau[viz]--;
if(grau[viz] == 1) fila.push(viz);
if(marc[viz] == 0) p = viz;
}
if(rmn[cur] == cur) {
swap(rmn[cur], rmn[p]);
mn += 2;
}
}
dfs(1, 1);
int cn = dfs2(1, 1);
for(int i = 0; i < grafo[cn].size(); i++) {
dfs3(grafo[cn][i], cn);
cm++;
}
int mt = 1123456789;
int id = 0;
for(int i = 0; i < cm; i++) {
if(cmp[i].size() < mt) {
mt = cmp[i].size();
id = i;
}
}
if(n % 2 == 0) cmp[id].push_back(cn);
vector <int> temp;
for(int i = 0; i < cm; i++) {
for(int j = 0; j < cmp[i].size(); j++) temp.push_back(cmp[i][j]);
}
for(int i = 0; i < temp.size() / 2; i++) {
int pr = i + temp.size() / 2;
swap(rmx[temp[i]], rmx[temp[pr]]);
}
if(n % 2 == 1) swap(rmx[temp[0]], rmx[cn]);
printf("%lld %lld\n", mn, mx * 2);
for(int i = 1; i <= n; i++) {
printf("%d ", rmn[i]);
}
printf("\n");
for(int i = 1; i <= n; i++) {
printf("%d ", rmx[i]);
}
printf("\n");
return 0;
}
Compilation message
Village.cpp: In function 'void dfs(int, int)':
Village.cpp:17:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for(int i = 0; i < grafo[v].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~
Village.cpp: In function 'int dfs2(int, int)':
Village.cpp:27:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(int i = 0; i < grafo[v].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~
Village.cpp: In function 'void dfs3(int, int)':
Village.cpp:36:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for(int i = 0; i < grafo[v].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~
Village.cpp: In function 'int main()':
Village.cpp:60:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for(int i = 0 ;i < grafo[cur].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~
Village.cpp:73:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int i = 0; i < grafo[cn].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~
Village.cpp:80:26: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
80 | if(cmp[i].size() < mt) {
| ~~~~~~~~~~~~~~^~~~
Village.cpp:88:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for(int j = 0; j < cmp[i].size(); j++) temp.push_back(cmp[i][j]);
| ~~^~~~~~~~~~~~~~~
Village.cpp:90:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | for(int i = 0; i < temp.size() / 2; i++) {
| ~~^~~~~~~~~~~~~~~~~
Village.cpp:43:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
Village.cpp:46:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | scanf("%d %d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5580 KB |
Output is correct |
2 |
Correct |
3 ms |
5580 KB |
Output is correct |
3 |
Correct |
3 ms |
5580 KB |
Output is correct |
4 |
Correct |
3 ms |
5580 KB |
Output is correct |
5 |
Correct |
3 ms |
5580 KB |
Output is correct |
6 |
Correct |
5 ms |
5580 KB |
Output is correct |
7 |
Correct |
4 ms |
5580 KB |
Output is correct |
8 |
Correct |
4 ms |
5580 KB |
Output is correct |
9 |
Correct |
4 ms |
5580 KB |
Output is correct |
10 |
Correct |
4 ms |
5580 KB |
Output is correct |
11 |
Correct |
3 ms |
5576 KB |
Output is correct |
12 |
Correct |
3 ms |
5580 KB |
Output is correct |
13 |
Correct |
3 ms |
5580 KB |
Output is correct |
14 |
Correct |
3 ms |
5580 KB |
Output is correct |
15 |
Correct |
3 ms |
5576 KB |
Output is correct |
16 |
Correct |
4 ms |
5580 KB |
Output is correct |
17 |
Correct |
4 ms |
5576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5580 KB |
Output is correct |
2 |
Correct |
4 ms |
5580 KB |
Output is correct |
3 |
Correct |
4 ms |
5580 KB |
Output is correct |
4 |
Correct |
4 ms |
5584 KB |
Output is correct |
5 |
Correct |
4 ms |
5584 KB |
Output is correct |
6 |
Correct |
5 ms |
5588 KB |
Output is correct |
7 |
Correct |
4 ms |
5708 KB |
Output is correct |
8 |
Correct |
4 ms |
5580 KB |
Output is correct |
9 |
Correct |
4 ms |
5572 KB |
Output is correct |
10 |
Correct |
5 ms |
5580 KB |
Output is correct |
11 |
Correct |
4 ms |
5580 KB |
Output is correct |
12 |
Correct |
4 ms |
5716 KB |
Output is correct |
13 |
Correct |
4 ms |
5588 KB |
Output is correct |
14 |
Correct |
4 ms |
5584 KB |
Output is correct |
15 |
Correct |
4 ms |
5580 KB |
Output is correct |
16 |
Correct |
4 ms |
5588 KB |
Output is correct |
17 |
Correct |
4 ms |
5580 KB |
Output is correct |
18 |
Correct |
4 ms |
5580 KB |
Output is correct |
19 |
Correct |
4 ms |
5580 KB |
Output is correct |
20 |
Correct |
4 ms |
5580 KB |
Output is correct |
21 |
Correct |
4 ms |
5580 KB |
Output is correct |
22 |
Correct |
4 ms |
5584 KB |
Output is correct |
23 |
Correct |
4 ms |
5580 KB |
Output is correct |
24 |
Correct |
4 ms |
5580 KB |
Output is correct |
25 |
Correct |
4 ms |
5576 KB |
Output is correct |
26 |
Correct |
5 ms |
5580 KB |
Output is correct |
27 |
Correct |
4 ms |
5580 KB |
Output is correct |
28 |
Correct |
4 ms |
5576 KB |
Output is correct |
29 |
Correct |
6 ms |
5580 KB |
Output is correct |
30 |
Correct |
5 ms |
5580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5580 KB |
Output is correct |
2 |
Correct |
3 ms |
5580 KB |
Output is correct |
3 |
Correct |
3 ms |
5580 KB |
Output is correct |
4 |
Correct |
3 ms |
5580 KB |
Output is correct |
5 |
Correct |
3 ms |
5580 KB |
Output is correct |
6 |
Correct |
5 ms |
5580 KB |
Output is correct |
7 |
Correct |
4 ms |
5580 KB |
Output is correct |
8 |
Correct |
4 ms |
5580 KB |
Output is correct |
9 |
Correct |
4 ms |
5580 KB |
Output is correct |
10 |
Correct |
4 ms |
5580 KB |
Output is correct |
11 |
Correct |
3 ms |
5576 KB |
Output is correct |
12 |
Correct |
3 ms |
5580 KB |
Output is correct |
13 |
Correct |
3 ms |
5580 KB |
Output is correct |
14 |
Correct |
3 ms |
5580 KB |
Output is correct |
15 |
Correct |
3 ms |
5576 KB |
Output is correct |
16 |
Correct |
4 ms |
5580 KB |
Output is correct |
17 |
Correct |
4 ms |
5576 KB |
Output is correct |
18 |
Correct |
4 ms |
5580 KB |
Output is correct |
19 |
Correct |
4 ms |
5580 KB |
Output is correct |
20 |
Correct |
4 ms |
5580 KB |
Output is correct |
21 |
Correct |
4 ms |
5584 KB |
Output is correct |
22 |
Correct |
4 ms |
5584 KB |
Output is correct |
23 |
Correct |
5 ms |
5588 KB |
Output is correct |
24 |
Correct |
4 ms |
5708 KB |
Output is correct |
25 |
Correct |
4 ms |
5580 KB |
Output is correct |
26 |
Correct |
4 ms |
5572 KB |
Output is correct |
27 |
Correct |
5 ms |
5580 KB |
Output is correct |
28 |
Correct |
4 ms |
5580 KB |
Output is correct |
29 |
Correct |
4 ms |
5716 KB |
Output is correct |
30 |
Correct |
4 ms |
5588 KB |
Output is correct |
31 |
Correct |
4 ms |
5584 KB |
Output is correct |
32 |
Correct |
4 ms |
5580 KB |
Output is correct |
33 |
Correct |
4 ms |
5588 KB |
Output is correct |
34 |
Correct |
4 ms |
5580 KB |
Output is correct |
35 |
Correct |
4 ms |
5580 KB |
Output is correct |
36 |
Correct |
4 ms |
5580 KB |
Output is correct |
37 |
Correct |
4 ms |
5580 KB |
Output is correct |
38 |
Correct |
4 ms |
5580 KB |
Output is correct |
39 |
Correct |
4 ms |
5584 KB |
Output is correct |
40 |
Correct |
4 ms |
5580 KB |
Output is correct |
41 |
Correct |
4 ms |
5580 KB |
Output is correct |
42 |
Correct |
4 ms |
5576 KB |
Output is correct |
43 |
Correct |
5 ms |
5580 KB |
Output is correct |
44 |
Correct |
4 ms |
5580 KB |
Output is correct |
45 |
Correct |
4 ms |
5576 KB |
Output is correct |
46 |
Correct |
6 ms |
5580 KB |
Output is correct |
47 |
Correct |
5 ms |
5580 KB |
Output is correct |
48 |
Correct |
88 ms |
12448 KB |
Output is correct |
49 |
Correct |
114 ms |
13232 KB |
Output is correct |
50 |
Correct |
100 ms |
13264 KB |
Output is correct |
51 |
Correct |
71 ms |
11632 KB |
Output is correct |
52 |
Correct |
125 ms |
13116 KB |
Output is correct |
53 |
Correct |
86 ms |
12428 KB |
Output is correct |
54 |
Correct |
52 ms |
12872 KB |
Output is correct |
55 |
Correct |
115 ms |
20492 KB |
Output is correct |
56 |
Correct |
112 ms |
16600 KB |
Output is correct |
57 |
Correct |
108 ms |
15420 KB |
Output is correct |
58 |
Correct |
107 ms |
14396 KB |
Output is correct |
59 |
Correct |
99 ms |
13372 KB |
Output is correct |
60 |
Correct |
93 ms |
16172 KB |
Output is correct |
61 |
Correct |
79 ms |
14040 KB |
Output is correct |
62 |
Correct |
90 ms |
13720 KB |
Output is correct |
63 |
Correct |
85 ms |
13308 KB |
Output is correct |
64 |
Correct |
88 ms |
13472 KB |
Output is correct |
65 |
Correct |
118 ms |
13692 KB |
Output is correct |
66 |
Correct |
77 ms |
13212 KB |
Output is correct |
67 |
Correct |
67 ms |
11812 KB |
Output is correct |
68 |
Correct |
75 ms |
12412 KB |
Output is correct |
69 |
Correct |
88 ms |
13664 KB |
Output is correct |
70 |
Correct |
95 ms |
13172 KB |
Output is correct |
71 |
Correct |
66 ms |
11276 KB |
Output is correct |
72 |
Correct |
65 ms |
11984 KB |
Output is correct |
73 |
Correct |
97 ms |
13880 KB |
Output is correct |
74 |
Correct |
78 ms |
12988 KB |
Output is correct |
75 |
Correct |
132 ms |
13152 KB |
Output is correct |
76 |
Correct |
93 ms |
13132 KB |
Output is correct |
77 |
Correct |
89 ms |
13248 KB |
Output is correct |
78 |
Correct |
61 ms |
10764 KB |
Output is correct |
79 |
Correct |
67 ms |
11580 KB |
Output is correct |
80 |
Correct |
97 ms |
13116 KB |
Output is correct |
81 |
Correct |
107 ms |
13456 KB |
Output is correct |
82 |
Correct |
95 ms |
13268 KB |
Output is correct |