#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define nd second
#define st first
typedef pair<ll, ll> pii;
const int maxn = (1e+5)+9;
vector <ll> t[maxn];
ll siz[maxn];
ll dep[maxn];
ll sumdep[maxn];
ll cen = -1;
ll dp[maxn][2];
vector<pii> resmin[maxn][2];
ll tabmin[maxn];
ll tabmax[maxn];
vector <int> podd[maxn];
ll N;
void DFS(int v , int o)
{
vector <int> V[2];
ll ind = -1;
ll p , res = 0; bool B = 0; ll mini = (1e+18);
for(int i = 0 ;i < t[v].size();i++)
{
p = t[v][i];
if(p == o)continue;
DFS(p,v);
if(dp[p][1]+2 <= dp[p][0])
{
V[1].push_back(p);
res += dp[p][1]+2;
B = 1;
}
else
{
V[0].push_back(p);
res += dp[p][0];
if(dp[p][1]+2-dp[p][0] < mini)ind = p;
mini = min(dp[p][1]+2-dp[p][0] , mini);
}
}
dp[v][1] = res;
if(B == 0)
{
res += mini;
}
dp[v][0] = res;
}
void DFS2(int v, int o)
{
siz[v] = 1;
int p , pmax = 0;
for(int i = 0; i < t[v].size() ;i++)
{
p = t[v][i];
if(p == o)continue;
DFS2(p,v);
pmax = max((ll)pmax,siz[p]);
siz[v] += siz[p];
}
pmax = max((ll)pmax,N-siz[v]);
if(pmax <= N/2)cen = v;
}
void DFS3(int v ,int o , int c)
{
podd[c].push_back(v);
sumdep[v] = dep[v];
int p;
for(int i = 0 ;i < t[v].size() ;i++)
{
p = t[v][i];
if(p == o)continue;
dep[p] = dep[v]+1;
DFS3(p,v,c);
sumdep[v] += sumdep[p];
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
ll A , B;
cin >> N;
for(int i = 0 ;i < N-1 ; i++)
{
cin >> A >> B;
t[A].push_back(B);
t[B].push_back(A);
}
if(N == 2)
{
cout << 2 << ' ' << 2 << '\n';
cout << 2 << ' ' << 1 << '\n';
cout << 2 << ' ' << 1 << '\n';
return 0;
}
priority_queue <pii> q;
ll ansmax = 0;
DFS2(1,-1);
podd[cen].push_back(cen);
for(int i = 0 ; i < t[cen].size() ;i++)
{
dep[t[cen][i]] = 1;
DFS3(t[cen][i],cen,t[cen][i]);
ansmax += sumdep[t[cen][i]];
q.push({podd[t[cen][i]].size(),t[cen][i]});
/*cout << t[cen][i] << ": " ;
for(int j = 0; j < podd[t[cen][i]].size() ; j++)
{
cout << podd[t[cen][i]][j] << ' ';
}
cout << '\n';*/
}
if(N%2)
{
A = q.top().nd;
q.pop();
B = q.top().nd;
q.pop();
tabmax[podd[A][podd[A].size()-1]] = cen;
tabmax[cen] = podd[B][podd[B].size()-1];
tabmax[podd[B][podd[B].size()-1]] = podd[A][podd[A].size()-1];
podd[A].pop_back();
podd[B].pop_back();
if(podd[A].size() > 0)q.push({podd[A].size(),A});
if(podd[B].size() > 0)q.push({podd[B].size(),B});
}
else
{
q.push({1,cen});
}
while(!q.empty())
{
A = q.top().nd;
q.pop();
B = q.top().nd;
q.pop();
tabmax[podd[A][podd[A].size()-1]] = podd[B][podd[B].size()-1];
tabmax[podd[B][podd[B].size()-1]] = podd[A][podd[A].size()-1];
podd[A].pop_back();
podd[B].pop_back();
if(podd[A].size() > 0)q.push({podd[A].size(),A});
if(podd[B].size() > 0)q.push({podd[B].size(),B});
}
cout << 1 << ' ' << ansmax*2 << '\n';
for(int i = 1 ; i <= N ; i++)cout << tabmin[i]+2 << ' '; cout << '\n';
for(int i = 1 ; i <= N ; i++)cout << tabmax[i] << ' '; cout << '\n';
return 0;
}
Compilation message
Village.cpp: In function 'void DFS(int, int)':
Village.cpp:28:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for(int i = 0 ;i < t[v].size();i++)
| ~~^~~~~~~~~~~~~
Village.cpp:26:8: warning: variable 'ind' set but not used [-Wunused-but-set-variable]
26 | ll ind = -1;
| ^~~
Village.cpp: In function 'void DFS2(int, int)':
Village.cpp:64:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for(int i = 0; i < t[v].size() ;i++)
| ~~^~~~~~~~~~~~~
Village.cpp: In function 'void DFS3(int, int, int)':
Village.cpp:83:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for(int i = 0 ;i < t[v].size() ;i++)
| ~~^~~~~~~~~~~~~
Village.cpp: In function 'int main()':
Village.cpp:118:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
118 | for(int i = 0 ; i < t[cen].size() ;i++)
| ~~^~~~~~~~~~~~~~~
Village.cpp:173:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
173 | for(int i = 1 ; i <= N ; i++)cout << tabmin[i]+2 << ' '; cout << '\n';
| ^~~
Village.cpp:173:62: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
173 | for(int i = 1 ; i <= N ; i++)cout << tabmin[i]+2 << ' '; cout << '\n';
| ^~~~
Village.cpp:174:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
174 | for(int i = 1 ; i <= N ; i++)cout << tabmax[i] << ' '; cout << '\n';
| ^~~
Village.cpp:174:60: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
174 | for(int i = 1 ; i <= N ; i++)cout << tabmax[i] << ' '; cout << '\n';
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
6 ms |
9816 KB |
Partially correct |
2 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
5 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
6 |
Partially correct |
5 ms |
9816 KB |
Partially correct |
7 |
Partially correct |
5 ms |
9820 KB |
Partially correct |
8 |
Partially correct |
5 ms |
9820 KB |
Partially correct |
9 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
10 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
11 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
12 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
13 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
14 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
15 |
Partially correct |
4 ms |
9816 KB |
Partially correct |
16 |
Partially correct |
5 ms |
9816 KB |
Partially correct |
17 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
2 |
Partially correct |
4 ms |
9816 KB |
Partially correct |
3 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
4 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
5 |
Partially correct |
4 ms |
9816 KB |
Partially correct |
6 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
7 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
8 |
Partially correct |
4 ms |
9808 KB |
Partially correct |
9 |
Partially correct |
6 ms |
10328 KB |
Partially correct |
10 |
Partially correct |
5 ms |
9820 KB |
Partially correct |
11 |
Partially correct |
4 ms |
9808 KB |
Partially correct |
12 |
Partially correct |
7 ms |
9816 KB |
Partially correct |
13 |
Partially correct |
4 ms |
9872 KB |
Partially correct |
14 |
Partially correct |
5 ms |
9820 KB |
Partially correct |
15 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
16 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
17 |
Partially correct |
5 ms |
9820 KB |
Partially correct |
18 |
Partially correct |
5 ms |
9768 KB |
Partially correct |
19 |
Partially correct |
5 ms |
9820 KB |
Partially correct |
20 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
21 |
Partially correct |
5 ms |
9820 KB |
Partially correct |
22 |
Partially correct |
5 ms |
9820 KB |
Partially correct |
23 |
Partially correct |
5 ms |
9820 KB |
Partially correct |
24 |
Partially correct |
5 ms |
9820 KB |
Partially correct |
25 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
26 |
Partially correct |
4 ms |
9816 KB |
Partially correct |
27 |
Partially correct |
5 ms |
9820 KB |
Partially correct |
28 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
29 |
Partially correct |
5 ms |
9816 KB |
Partially correct |
30 |
Partially correct |
5 ms |
9820 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
6 ms |
9816 KB |
Partially correct |
2 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
5 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
6 |
Partially correct |
5 ms |
9816 KB |
Partially correct |
7 |
Partially correct |
5 ms |
9820 KB |
Partially correct |
8 |
Partially correct |
5 ms |
9820 KB |
Partially correct |
9 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
10 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
11 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
12 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
13 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
14 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
15 |
Partially correct |
4 ms |
9816 KB |
Partially correct |
16 |
Partially correct |
5 ms |
9816 KB |
Partially correct |
17 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
18 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
19 |
Partially correct |
4 ms |
9816 KB |
Partially correct |
20 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
21 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
22 |
Partially correct |
4 ms |
9816 KB |
Partially correct |
23 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
24 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
25 |
Partially correct |
4 ms |
9808 KB |
Partially correct |
26 |
Partially correct |
6 ms |
10328 KB |
Partially correct |
27 |
Partially correct |
5 ms |
9820 KB |
Partially correct |
28 |
Partially correct |
4 ms |
9808 KB |
Partially correct |
29 |
Partially correct |
7 ms |
9816 KB |
Partially correct |
30 |
Partially correct |
4 ms |
9872 KB |
Partially correct |
31 |
Partially correct |
5 ms |
9820 KB |
Partially correct |
32 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
33 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
34 |
Partially correct |
5 ms |
9820 KB |
Partially correct |
35 |
Partially correct |
5 ms |
9768 KB |
Partially correct |
36 |
Partially correct |
5 ms |
9820 KB |
Partially correct |
37 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
38 |
Partially correct |
5 ms |
9820 KB |
Partially correct |
39 |
Partially correct |
5 ms |
9820 KB |
Partially correct |
40 |
Partially correct |
5 ms |
9820 KB |
Partially correct |
41 |
Partially correct |
5 ms |
9820 KB |
Partially correct |
42 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
43 |
Partially correct |
4 ms |
9816 KB |
Partially correct |
44 |
Partially correct |
5 ms |
9820 KB |
Partially correct |
45 |
Partially correct |
4 ms |
9820 KB |
Partially correct |
46 |
Partially correct |
5 ms |
9816 KB |
Partially correct |
47 |
Partially correct |
5 ms |
9820 KB |
Partially correct |
48 |
Partially correct |
44 ms |
18452 KB |
Partially correct |
49 |
Partially correct |
51 ms |
19148 KB |
Partially correct |
50 |
Partially correct |
46 ms |
19140 KB |
Partially correct |
51 |
Partially correct |
38 ms |
17128 KB |
Partially correct |
52 |
Partially correct |
50 ms |
19084 KB |
Partially correct |
53 |
Partially correct |
41 ms |
18148 KB |
Partially correct |
54 |
Partially correct |
31 ms |
17740 KB |
Partially correct |
55 |
Partially correct |
57 ms |
25936 KB |
Partially correct |
56 |
Partially correct |
58 ms |
22540 KB |
Partially correct |
57 |
Partially correct |
49 ms |
21436 KB |
Partially correct |
58 |
Partially correct |
50 ms |
20308 KB |
Partially correct |
59 |
Partially correct |
61 ms |
19256 KB |
Partially correct |
60 |
Partially correct |
60 ms |
23232 KB |
Partially correct |
61 |
Partially correct |
58 ms |
20032 KB |
Partially correct |
62 |
Partially correct |
43 ms |
19548 KB |
Partially correct |
63 |
Partially correct |
41 ms |
18856 KB |
Partially correct |
64 |
Partially correct |
44 ms |
19536 KB |
Partially correct |
65 |
Partially correct |
42 ms |
19572 KB |
Partially correct |
66 |
Partially correct |
51 ms |
18836 KB |
Partially correct |
67 |
Partially correct |
37 ms |
17364 KB |
Partially correct |
68 |
Partially correct |
38 ms |
18000 KB |
Partially correct |
69 |
Partially correct |
47 ms |
19672 KB |
Partially correct |
70 |
Partially correct |
40 ms |
19028 KB |
Partially correct |
71 |
Partially correct |
30 ms |
16724 KB |
Partially correct |
72 |
Partially correct |
32 ms |
17492 KB |
Partially correct |
73 |
Partially correct |
46 ms |
19660 KB |
Partially correct |
74 |
Partially correct |
44 ms |
18768 KB |
Partially correct |
75 |
Partially correct |
49 ms |
19468 KB |
Partially correct |
76 |
Partially correct |
58 ms |
18960 KB |
Partially correct |
77 |
Partially correct |
41 ms |
19148 KB |
Partially correct |
78 |
Partially correct |
31 ms |
16216 KB |
Partially correct |
79 |
Partially correct |
37 ms |
17248 KB |
Partially correct |
80 |
Partially correct |
46 ms |
19580 KB |
Partially correct |
81 |
Partially correct |
51 ms |
19540 KB |
Partially correct |
82 |
Partially correct |
39 ms |
19280 KB |
Partially correct |