Submission #941074

# Submission time Handle Problem Language Result Execution time Memory
941074 2024-03-08T06:28:24 Z Pacybwoah Village (BOI20_village) C++17
100 / 100
114 ms 22400 KB
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<vector<int> > graph;
vector<int> match,prem;
vector<vector<int> > dp;
vector<int> sz,ord;
int n;
typedef long long ll;
ll max_cnt=0;
void dfs_min(int node,int parent){
    sz[node]++;
    for(auto x:graph[node]){
        if(x==parent) continue;
        dfs_min(x,node);
        sz[node]+=sz[x];
        dp[node][0]+=max(dp[x][0],dp[x][1]);
    }
    for(auto x:graph[node]){
        if(x==parent) continue;
        if(dp[node][1]<dp[node][0]-max(dp[x][0],dp[x][1])+dp[x][0]+1){
            dp[node][1]=dp[node][0]-max(dp[x][0],dp[x][1])+dp[x][0]+1;
            prem[node]=x;
        }
    }
}
void dfs_match(int node,int parent,bool flag){
    if(flag||dp[node][0]>=dp[node][1]){
        for(auto x:graph[node]){
            if(x==parent) continue;
            dfs_match(x,node,0);
        }
    }
    else{
        match[node]=prem[node];
        match[prem[node]]=node;
        for(auto x:graph[node]){
            if(x==parent) continue;
            if(x==prem[node]) dfs_match(x,node,1);
            else dfs_match(x,node,0);
        }
    }
}
int dfs_cent(int node,int parent){
    for(auto x:graph[node]){
        if(x==parent) continue;
        if(sz[x]>n/2) return dfs_cent(x,node);
    }
    return node;
}
void dfs_sz(int node,int parent){
    sz[node]=1;
    for(auto x:graph[node]){
        if(x==parent) continue;
        dfs_sz(x,node);
        sz[node]+=sz[x];
    }
    if(parent!=0) max_cnt+=2*min(1ll*sz[node],1ll*(n-sz[node]));
}
void dfs_ord(int node,int parent){
    if(parent!=0) ord.push_back(node);
    for(auto x:graph[node]){
        if(x==parent) continue;
        dfs_ord(x,node);
    }
}
int main(){
    cin>>n;
    graph.resize(n+1);
    prem.resize(n+1);
    sz.resize(n+1);
    int a,b;
    for(int i=1;i<n;i++){
        cin>>a>>b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    dp.resize(n+1,vector<int>(2));
    match.resize(n+1,-1);
    dfs_min(1,0);
    dfs_match(1,0,0);
    vector<int> ans_min(n+1);
    for(int i=1;i<=n;i++){
        if(match[i]!=-1){
            ans_min[i]=match[i];
        }
        else ans_min[i]=i;
    }
    int min_cnt=max(dp[1][1],dp[1][0])*2;
    for(int i=1;i<=n;i++){
        if(match[i]==-1){
            swap(ans_min[i],ans_min[graph[i][0]]),min_cnt+=2;
        }
    }
    int cent=dfs_cent(1,0);
    dfs_sz(cent,0);
    sort(graph[cent].begin(),graph[cent].end(),[&](int a,int b){return sz[a]>sz[b];});
    dfs_ord(cent,0);
    vector<int> ans_max(n+1);
    for(int i=1;i<=n;i++) ans_max[i]=i;
    if(n&1){
        for(int i=0;i<n/2;i++) swap(ans_max[ord[i]],ans_max[ord[i+n/2]]);
        swap(ans_max[cent],ans_max[(cent==1)?2:1]);
    }
    else{
        ord.push_back(cent);
        for(int i=0;i<n/2;i++) swap(ans_max[ord[i]],ans_max[ord[i+n/2]]);
    }
    cout<<min_cnt<<" "<<max_cnt<<"\n";
    for(int i=1;i<=n;i++) cout<<ans_min[i]<<" \n"[i==n];
    for(int i=1;i<=n;i++) cout<<ans_max[i]<<" \n"[i==n];
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 344 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 85 ms 13740 KB Output is correct
49 Correct 114 ms 15056 KB Output is correct
50 Correct 89 ms 15016 KB Output is correct
51 Correct 75 ms 11732 KB Output is correct
52 Correct 96 ms 14804 KB Output is correct
53 Correct 82 ms 13512 KB Output is correct
54 Correct 54 ms 11220 KB Output is correct
55 Correct 111 ms 22400 KB Output is correct
56 Correct 98 ms 18512 KB Output is correct
57 Correct 95 ms 17152 KB Output is correct
58 Correct 111 ms 16336 KB Output is correct
59 Correct 89 ms 15020 KB Output is correct
60 Correct 77 ms 15372 KB Output is correct
61 Correct 85 ms 15440 KB Output is correct
62 Correct 81 ms 15564 KB Output is correct
63 Correct 75 ms 14404 KB Output is correct
64 Correct 87 ms 15440 KB Output is correct
65 Correct 80 ms 15544 KB Output is correct
66 Correct 77 ms 14544 KB Output is correct
67 Correct 58 ms 11456 KB Output is correct
68 Correct 68 ms 13040 KB Output is correct
69 Correct 80 ms 15568 KB Output is correct
70 Correct 74 ms 14544 KB Output is correct
71 Correct 53 ms 10952 KB Output is correct
72 Correct 60 ms 12236 KB Output is correct
73 Correct 79 ms 15464 KB Output is correct
74 Correct 76 ms 14280 KB Output is correct
75 Correct 92 ms 15048 KB Output is correct
76 Correct 91 ms 14796 KB Output is correct
77 Correct 78 ms 14540 KB Output is correct
78 Correct 61 ms 10340 KB Output is correct
79 Correct 62 ms 11980 KB Output is correct
80 Correct 86 ms 14980 KB Output is correct
81 Correct 82 ms 14676 KB Output is correct
82 Correct 85 ms 15260 KB Output is correct