#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
using namespace std;
#define int long long
const int mxn=1e5,mod=1000000007,lg=25,root=1000,inf=1e18;
vector<int>adj[mxn+10];
int subsize[mxn+10],n;
int ansmn[mxn+10],ansmx[mxn+10],up[mxn+10][lg+10],h[mxn+10];
vector<int>sub[mxn+10];
int cnt=0;
int ans2=0,ans1=0;
bool cmp(int a,int b){return subsize[a]>subsize[b];}
void build(int cur,int p){
subsize[cur]=1;
for(auto i:adj[cur]){
if(i==p)continue;
build(i,cur);
ans2+=min(subsize[i],n-subsize[i]);//we claim we can achive this
subsize[cur]+=subsize[i];
}
}
int getcen(int cur,int p){
for(auto i:adj[cur]){
if(i==p)continue;
if(subsize[i]*2>n)return getcen(i,cur);
}
return cur;
}
void dfs(int cur,int p){
sub[cnt].pb(cur);
for(auto i:adj[cur]){
if(i==p)continue;
dfs(i,cur);
}
}
int solvemn(int cur,int p){
vector<int>v;
for(auto i:adj[cur]){
if(i==p)continue;
if(solvemn(i,cur))v.pb(i);
}
if(v.size()){
ans1++;
ansmn[cur]=v[0];
for(int i=0;i<v.size()-1;i++)ansmn[v[i]]=v[i+1],ans1+=2;
ansmn[v.back()]=cur;
ans1++;
}
else return 1;
return 0;
}
void solvemx(){
int node=getcen(1,-1);
priority_queue<pii>pq;
for(auto i:adj[node]){
dfs(i,node);
pq.push({sub[cnt].size(),cnt});
cnt++;
}
//for(auto i:sub)cout<<i<<"\n";
int cur=node,tmp;
ansmx[node]=sub[pq.top().s].back();
cur=pq.top().s;
pq.pop();
while(!pq.empty()){
ansmx[sub[cur].back()]=sub[pq.top().s].back();
sub[cur].pop_back();
tmp=cur;
cur=pq.top().s;
pq.pop();
if(sub[tmp].size())pq.push({sub[tmp].size(),tmp});
}
ansmx[sub[cur].back()]=node;
}
int32_t main(){
fastio
cin>>n;
for(int i=0;i<n-1;i++){
int u,v;cin>>u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
//fill(ansmn,ansmn+1+n,1);
//fill(ansmx,ansmx+n+1,1);
build(1,-1);
solvemx();
if(solvemn(1,-1)){
int node=adj[1][0];
ansmn[1]=node;
int next=ansmn[node];
while(ansmn[next]!=node)next=ansmn[next];
ansmn[next]=1;
ans1+=2;
}
cout<<ans1<<" ";
cout<<ans2*2<<'\n';
for(int i=1;i<=n;i++)cout<<ansmn[i]<<" ";
cout<<'\n';
for(int i=1;i<=n;i++)cout<<ansmx[i]<<" ";
}
Compilation message
Village.cpp: In function 'long long int solvemn(long long int, long long int)':
Village.cpp:70:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(int i=0;i<v.size()-1;i++)ansmn[v[i]]=v[i+1],ans1+=2;
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
2 ms |
10840 KB |
Output is correct |
3 |
Correct |
4 ms |
11096 KB |
Output is correct |
4 |
Correct |
2 ms |
10840 KB |
Output is correct |
5 |
Correct |
2 ms |
10840 KB |
Output is correct |
6 |
Correct |
2 ms |
10840 KB |
Output is correct |
7 |
Correct |
2 ms |
10840 KB |
Output is correct |
8 |
Correct |
2 ms |
10840 KB |
Output is correct |
9 |
Correct |
2 ms |
10840 KB |
Output is correct |
10 |
Correct |
3 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10840 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10840 KB |
Output is correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10840 KB |
Output is correct |
16 |
Correct |
2 ms |
10844 KB |
Output is correct |
17 |
Correct |
2 ms |
10840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
2 ms |
10840 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
11096 KB |
Output is correct |
5 |
Correct |
3 ms |
11096 KB |
Output is correct |
6 |
Correct |
2 ms |
11096 KB |
Output is correct |
7 |
Correct |
2 ms |
11096 KB |
Output is correct |
8 |
Correct |
2 ms |
11096 KB |
Output is correct |
9 |
Correct |
2 ms |
11100 KB |
Output is correct |
10 |
Correct |
2 ms |
11096 KB |
Output is correct |
11 |
Correct |
2 ms |
11100 KB |
Output is correct |
12 |
Correct |
2 ms |
11100 KB |
Output is correct |
13 |
Correct |
2 ms |
11100 KB |
Output is correct |
14 |
Correct |
2 ms |
11096 KB |
Output is correct |
15 |
Correct |
2 ms |
11100 KB |
Output is correct |
16 |
Correct |
2 ms |
11096 KB |
Output is correct |
17 |
Correct |
2 ms |
11096 KB |
Output is correct |
18 |
Correct |
2 ms |
11096 KB |
Output is correct |
19 |
Correct |
2 ms |
11100 KB |
Output is correct |
20 |
Correct |
2 ms |
11096 KB |
Output is correct |
21 |
Correct |
2 ms |
11096 KB |
Output is correct |
22 |
Correct |
3 ms |
11096 KB |
Output is correct |
23 |
Correct |
2 ms |
11100 KB |
Output is correct |
24 |
Correct |
2 ms |
11096 KB |
Output is correct |
25 |
Correct |
2 ms |
10840 KB |
Output is correct |
26 |
Correct |
2 ms |
11096 KB |
Output is correct |
27 |
Correct |
2 ms |
10844 KB |
Output is correct |
28 |
Correct |
2 ms |
11096 KB |
Output is correct |
29 |
Correct |
2 ms |
11100 KB |
Output is correct |
30 |
Correct |
2 ms |
11312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
2 ms |
10840 KB |
Output is correct |
3 |
Correct |
4 ms |
11096 KB |
Output is correct |
4 |
Correct |
2 ms |
10840 KB |
Output is correct |
5 |
Correct |
2 ms |
10840 KB |
Output is correct |
6 |
Correct |
2 ms |
10840 KB |
Output is correct |
7 |
Correct |
2 ms |
10840 KB |
Output is correct |
8 |
Correct |
2 ms |
10840 KB |
Output is correct |
9 |
Correct |
2 ms |
10840 KB |
Output is correct |
10 |
Correct |
3 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10840 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10840 KB |
Output is correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10840 KB |
Output is correct |
16 |
Correct |
2 ms |
10844 KB |
Output is correct |
17 |
Correct |
2 ms |
10840 KB |
Output is correct |
18 |
Correct |
2 ms |
10840 KB |
Output is correct |
19 |
Correct |
2 ms |
10840 KB |
Output is correct |
20 |
Correct |
2 ms |
10844 KB |
Output is correct |
21 |
Correct |
2 ms |
11096 KB |
Output is correct |
22 |
Correct |
3 ms |
11096 KB |
Output is correct |
23 |
Correct |
2 ms |
11096 KB |
Output is correct |
24 |
Correct |
2 ms |
11096 KB |
Output is correct |
25 |
Correct |
2 ms |
11096 KB |
Output is correct |
26 |
Correct |
2 ms |
11100 KB |
Output is correct |
27 |
Correct |
2 ms |
11096 KB |
Output is correct |
28 |
Correct |
2 ms |
11100 KB |
Output is correct |
29 |
Correct |
2 ms |
11100 KB |
Output is correct |
30 |
Correct |
2 ms |
11100 KB |
Output is correct |
31 |
Correct |
2 ms |
11096 KB |
Output is correct |
32 |
Correct |
2 ms |
11100 KB |
Output is correct |
33 |
Correct |
2 ms |
11096 KB |
Output is correct |
34 |
Correct |
2 ms |
11096 KB |
Output is correct |
35 |
Correct |
2 ms |
11096 KB |
Output is correct |
36 |
Correct |
2 ms |
11100 KB |
Output is correct |
37 |
Correct |
2 ms |
11096 KB |
Output is correct |
38 |
Correct |
2 ms |
11096 KB |
Output is correct |
39 |
Correct |
3 ms |
11096 KB |
Output is correct |
40 |
Correct |
2 ms |
11100 KB |
Output is correct |
41 |
Correct |
2 ms |
11096 KB |
Output is correct |
42 |
Correct |
2 ms |
10840 KB |
Output is correct |
43 |
Correct |
2 ms |
11096 KB |
Output is correct |
44 |
Correct |
2 ms |
10844 KB |
Output is correct |
45 |
Correct |
2 ms |
11096 KB |
Output is correct |
46 |
Correct |
2 ms |
11100 KB |
Output is correct |
47 |
Correct |
2 ms |
11312 KB |
Output is correct |
48 |
Correct |
44 ms |
16352 KB |
Output is correct |
49 |
Correct |
45 ms |
16908 KB |
Output is correct |
50 |
Correct |
46 ms |
17032 KB |
Output is correct |
51 |
Correct |
35 ms |
15500 KB |
Output is correct |
52 |
Correct |
52 ms |
16848 KB |
Output is correct |
53 |
Correct |
41 ms |
16368 KB |
Output is correct |
54 |
Correct |
28 ms |
17740 KB |
Output is correct |
55 |
Correct |
77 ms |
25164 KB |
Output is correct |
56 |
Correct |
53 ms |
21068 KB |
Output is correct |
57 |
Correct |
64 ms |
19532 KB |
Output is correct |
58 |
Correct |
51 ms |
18108 KB |
Output is correct |
59 |
Correct |
50 ms |
16896 KB |
Output is correct |
60 |
Correct |
51 ms |
21016 KB |
Output is correct |
61 |
Correct |
43 ms |
18180 KB |
Output is correct |
62 |
Correct |
55 ms |
17232 KB |
Output is correct |
63 |
Correct |
45 ms |
16732 KB |
Output is correct |
64 |
Correct |
51 ms |
17272 KB |
Output is correct |
65 |
Correct |
48 ms |
17292 KB |
Output is correct |
66 |
Correct |
48 ms |
16880 KB |
Output is correct |
67 |
Correct |
36 ms |
15920 KB |
Output is correct |
68 |
Correct |
37 ms |
16464 KB |
Output is correct |
69 |
Correct |
46 ms |
17584 KB |
Output is correct |
70 |
Correct |
35 ms |
17112 KB |
Output is correct |
71 |
Correct |
31 ms |
15320 KB |
Output is correct |
72 |
Correct |
31 ms |
16080 KB |
Output is correct |
73 |
Correct |
44 ms |
17572 KB |
Output is correct |
74 |
Correct |
45 ms |
17008 KB |
Output is correct |
75 |
Correct |
65 ms |
17212 KB |
Output is correct |
76 |
Correct |
44 ms |
16988 KB |
Output is correct |
77 |
Correct |
48 ms |
17096 KB |
Output is correct |
78 |
Correct |
28 ms |
15060 KB |
Output is correct |
79 |
Correct |
32 ms |
15844 KB |
Output is correct |
80 |
Correct |
66 ms |
17364 KB |
Output is correct |
81 |
Correct |
50 ms |
17472 KB |
Output is correct |
82 |
Correct |
42 ms |
17116 KB |
Output is correct |