#include <bits/stdc++.h>
#define DIMN 100010
using namespace std;
int n , mini , fth , centroid;
long long maxi;
int which[DIMN] , wm[DIMN] , sub[DIMN] , f[DIMN];
vector <int> v[DIMN] , w[DIMN];
priority_queue <pair <int , int> > h;
void dfs (int nod , int tt){
int i , vecin , ok = 1;
for (i = 0 ; i < v[nod].size() ; i++){
vecin = v[nod][i];
if (vecin != tt){
dfs (vecin , nod);
sub[nod] += sub[vecin] + 1;
if (sub[vecin] + 1 > n / 2)
ok = 0;
}
}
if (n - sub[nod] - 1 > n / 2)
ok = 0;
if (ok && !centroid){
centroid = nod;
fth = tt;
}
maxi = maxi + 2 * min(sub[nod] + 1 , n - 1 - sub[nod]);
if (which[nod] == nod){ /// nu l ai schimbat inca
if (tt){
swap(which[nod] , which[tt]);
mini += 2;
}
else {
for (i = 0 ; i < v[nod].size() ; i++){
vecin = v[nod][i];
if (vecin != tt){
swap(which[vecin] , which[nod]);
mini += 2;
break;
}
}
}
}
}
void dfs2 (int nod , int poz){
int i , vecin;
f[nod] = 1;
if (nod != centroid)
w[poz].push_back(nod);
for (i = 0 ; i < v[nod].size() ; i++){
vecin = v[nod][i];
if (!f[vecin]){
if (nod == centroid)
poz++;
dfs2 (vecin , poz);
}
}
}
int main()
{
FILE *fin = stdin;
FILE *fout = stdout;
int x , y , i , a , b , elem;
fscanf (fin,"%d",&n);
for (i = 1 ; i < n ; i++){
fscanf (fin,"%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
/// solve pentru minim
mini = 0;
for (i = 1 ; i <= n ; i++){
which[i] = i; /// ce nod e acum in locul in care se afla nodul i?
wm[i] = i;
}
dfs (1 , 0);
fprintf (fout,"%d %lld\n" , mini , maxi);
for (i = 1 ; i <= n ; i++)
fprintf (fout,"%d ",which[i]);
fprintf (fout,"\n");
dfs2 (centroid , 0);
a = 1;
b = 2;
elem = v[centroid].size();
for (i = 1 ; i <= elem ; i++)
h.push(make_pair(w[i].size() , i));
while (h.size() >= 2){
a = h.top().second;
h.pop();
b = h.top().second;
h.pop();
swap(wm[w[a].back()] , wm[w[b].back()]);
w[a].pop_back();
w[b].pop_back();
if (w[a].size())
h.push(make_pair(w[a].size() , a));
if (w[b].size())
h.push(make_pair(w[b].size() , b));
}
if (h.empty()){
if (centroid == 1){
swap(wm[centroid] , wm[2]);
}
else swap(wm[centroid] , wm[centroid - 1]);
}
else {
a = h.top().second;
swap(wm[w[a].back()] , wm[centroid]);
}
for (i = 1 ; i <= n ; i++){
fprintf (fout,"%d ",wm[i]);
}
return 0;
}
Compilation message
Village.cpp: In function 'void dfs(int, int)':
Village.cpp:15:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | for (i = 0 ; i < v[nod].size() ; i++){
| ~~^~~~~~~~~~~~~~~
Village.cpp:44:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for (i = 0 ; i < v[nod].size() ; i++){
| ~~^~~~~~~~~~~~~~~
Village.cpp: In function 'void dfs2(int, int)':
Village.cpp:69:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for (i = 0 ; i < v[nod].size() ; i++){
| ~~^~~~~~~~~~~~~~~
Village.cpp: In function 'int main()':
Village.cpp:87:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | fscanf (fin,"%d",&n);
| ~~~~~~~^~~~~~~~~~~~~
Village.cpp:89:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | fscanf (fin,"%d%d",&x,&y);
| ~~~~~~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
5036 KB |
Output is correct |
4 |
Correct |
4 ms |
5068 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
6 |
Correct |
5 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 ms |
5012 KB |
Output is correct |
8 |
Correct |
4 ms |
5068 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 ms |
5068 KB |
Output is correct |
11 |
Correct |
4 ms |
5072 KB |
Output is correct |
12 |
Correct |
4 ms |
5068 KB |
Output is correct |
13 |
Correct |
4 ms |
5068 KB |
Output is correct |
14 |
Correct |
5 ms |
5068 KB |
Output is correct |
15 |
Correct |
4 ms |
5068 KB |
Output is correct |
16 |
Correct |
4 ms |
5068 KB |
Output is correct |
17 |
Correct |
4 ms |
5068 KB |
Output is correct |
18 |
Correct |
4 ms |
5068 KB |
Output is correct |
19 |
Correct |
4 ms |
5068 KB |
Output is correct |
20 |
Correct |
4 ms |
5068 KB |
Output is correct |
21 |
Correct |
4 ms |
5068 KB |
Output is correct |
22 |
Correct |
4 ms |
4940 KB |
Output is correct |
23 |
Correct |
4 ms |
4940 KB |
Output is correct |
24 |
Correct |
4 ms |
4940 KB |
Output is correct |
25 |
Correct |
4 ms |
4940 KB |
Output is correct |
26 |
Correct |
4 ms |
5068 KB |
Output is correct |
27 |
Correct |
4 ms |
4940 KB |
Output is correct |
28 |
Correct |
4 ms |
5068 KB |
Output is correct |
29 |
Correct |
4 ms |
5068 KB |
Output is correct |
30 |
Correct |
4 ms |
5068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
4 ms |
5036 KB |
Output is correct |
21 |
Correct |
4 ms |
5068 KB |
Output is correct |
22 |
Correct |
4 ms |
5068 KB |
Output is correct |
23 |
Correct |
5 ms |
4940 KB |
Output is correct |
24 |
Correct |
4 ms |
5012 KB |
Output is correct |
25 |
Correct |
4 ms |
5068 KB |
Output is correct |
26 |
Correct |
4 ms |
4940 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
4 ms |
5072 KB |
Output is correct |
29 |
Correct |
4 ms |
5068 KB |
Output is correct |
30 |
Correct |
4 ms |
5068 KB |
Output is correct |
31 |
Correct |
5 ms |
5068 KB |
Output is correct |
32 |
Correct |
4 ms |
5068 KB |
Output is correct |
33 |
Correct |
4 ms |
5068 KB |
Output is correct |
34 |
Correct |
4 ms |
5068 KB |
Output is correct |
35 |
Correct |
4 ms |
5068 KB |
Output is correct |
36 |
Correct |
4 ms |
5068 KB |
Output is correct |
37 |
Correct |
4 ms |
5068 KB |
Output is correct |
38 |
Correct |
4 ms |
5068 KB |
Output is correct |
39 |
Correct |
4 ms |
4940 KB |
Output is correct |
40 |
Correct |
4 ms |
4940 KB |
Output is correct |
41 |
Correct |
4 ms |
4940 KB |
Output is correct |
42 |
Correct |
4 ms |
4940 KB |
Output is correct |
43 |
Correct |
4 ms |
5068 KB |
Output is correct |
44 |
Correct |
4 ms |
4940 KB |
Output is correct |
45 |
Correct |
4 ms |
5068 KB |
Output is correct |
46 |
Correct |
4 ms |
5068 KB |
Output is correct |
47 |
Correct |
4 ms |
5068 KB |
Output is correct |
48 |
Correct |
83 ms |
10928 KB |
Output is correct |
49 |
Correct |
88 ms |
11504 KB |
Output is correct |
50 |
Correct |
100 ms |
11580 KB |
Output is correct |
51 |
Correct |
68 ms |
10136 KB |
Output is correct |
52 |
Correct |
95 ms |
11492 KB |
Output is correct |
53 |
Correct |
96 ms |
10840 KB |
Output is correct |
54 |
Correct |
45 ms |
11664 KB |
Output is correct |
55 |
Correct |
107 ms |
18948 KB |
Output is correct |
56 |
Correct |
96 ms |
14864 KB |
Output is correct |
57 |
Correct |
106 ms |
13668 KB |
Output is correct |
58 |
Correct |
95 ms |
12612 KB |
Output is correct |
59 |
Correct |
89 ms |
11560 KB |
Output is correct |
60 |
Correct |
85 ms |
14816 KB |
Output is correct |
61 |
Correct |
88 ms |
12096 KB |
Output is correct |
62 |
Correct |
84 ms |
11708 KB |
Output is correct |
63 |
Correct |
75 ms |
11400 KB |
Output is correct |
64 |
Correct |
113 ms |
11764 KB |
Output is correct |
65 |
Correct |
84 ms |
11948 KB |
Output is correct |
66 |
Correct |
79 ms |
11428 KB |
Output is correct |
67 |
Correct |
61 ms |
10296 KB |
Output is correct |
68 |
Correct |
69 ms |
10920 KB |
Output is correct |
69 |
Correct |
88 ms |
11872 KB |
Output is correct |
70 |
Correct |
86 ms |
11464 KB |
Output is correct |
71 |
Correct |
55 ms |
9796 KB |
Output is correct |
72 |
Correct |
60 ms |
10452 KB |
Output is correct |
73 |
Correct |
82 ms |
11964 KB |
Output is correct |
74 |
Correct |
75 ms |
11348 KB |
Output is correct |
75 |
Correct |
121 ms |
11328 KB |
Output is correct |
76 |
Correct |
97 ms |
11336 KB |
Output is correct |
77 |
Correct |
99 ms |
11532 KB |
Output is correct |
78 |
Correct |
74 ms |
9428 KB |
Output is correct |
79 |
Correct |
77 ms |
10180 KB |
Output is correct |
80 |
Correct |
97 ms |
11284 KB |
Output is correct |
81 |
Correct |
93 ms |
11732 KB |
Output is correct |
82 |
Correct |
84 ms |
11708 KB |
Output is correct |