Submission #768198

# Submission time Handle Problem Language Result Execution time Memory
768198 2023-06-27T16:49:33 Z PoonYaPat Village (BOI20_village) C++14
100 / 100
63 ms 18244 KB
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;

int n,pos[100001],ans_min,st=1,sz[100001],mov[100001];
long long ans_max;
vector<int> adj[100001];

void dfs_min(int x, int par) {
    for (auto s : adj[x]) if (s!=par) dfs_min(s,x);
    if (pos[x]==x) {
        if (x==st) swap(pos[x],pos[adj[x][0]]);
        else swap(pos[x],pos[par]);
        ans_min+=2;
    }
}

int dfs_sz(int x, int par) {
    sz[x]=1;
    for (auto s : adj[x]) if (s!=par) sz[x]+=dfs_sz(s,x);
    ans_max+=2*min(sz[x],n-sz[x]);
    return sz[x];
}

int cen(int x, int par) {
    for (auto s : adj[x]) if (s!=par && sz[s]>n/2) return cen(s,x);
    return x;
}

vector<int> group[100001];

void dfs(int x, int par, int idx) {
    group[idx].push_back(x);
    for (auto s : adj[x]) if (s!=par) dfs(s,x,idx);
}

void sol_max() {
    dfs_sz(1,0);
    int centroid=cen(1,0);
    for (int i=0; i<adj[centroid].size(); ++i) dfs(adj[centroid][i],centroid,i+1);
    group[0].push_back(centroid);

    vector<pii> v;
    for (int i=0; i<=adj[centroid].size(); ++i) v.push_back(pii(group[i].size(),i));
    sort(v.begin(),v.end());

    int siz=v.size();
    queue<int> q;
    for (int i=0; i<siz-1; ++i) {
        int n1=v[i].second, n2=v[i+1].second;
        for (int j=0; j<group[n1].size(); ++j) swap(mov[group[n1][j]],mov[group[n2][j]]);
        if (i!=siz-2) for (auto s : group[n1]) q.push(s);
    }

    for (int i=group[v[siz-2].second].size(); i<group[v[siz-1].second].size(); ++i) {
        swap(mov[group[v[siz-1].second][i]],mov[q.front()]);
        q.pop();
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin>>n;
    for (int i=1; i<=n; ++i) pos[i]=i, mov[i]=i;
    for (int i=0; i<n-1; ++i) {
        int a,b; cin>>a>>b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    for (int i=1; i<=n; ++i) if (adj[i].size()>1) st=i;
    dfs_min(st,0);
    sol_max();
    cout<<ans_min<<" "<<ans_max<<"\n";
    for (int i=1; i<=n; ++i) cout<<pos[i]<<" ";
    cout<<"\n";
    for (int i=1; i<=n; ++i) cout<<mov[i]<<" ";
}

Compilation message

Village.cpp: In function 'void sol_max()':
Village.cpp:40:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |     for (int i=0; i<adj[centroid].size(); ++i) dfs(adj[centroid][i],centroid,i+1);
      |                   ~^~~~~~~~~~~~~~~~~~~~~
Village.cpp:44:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |     for (int i=0; i<=adj[centroid].size(); ++i) v.push_back(pii(group[i].size(),i));
      |                   ~^~~~~~~~~~~~~~~~~~~~~~
Village.cpp:51:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |         for (int j=0; j<group[n1].size(); ++j) swap(mov[group[n1][j]],mov[group[n2][j]]);
      |                       ~^~~~~~~~~~~~~~~~~
Village.cpp:55:48: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |     for (int i=group[v[siz-2].second].size(); i<group[v[siz-1].second].size(); ++i) {
      |                                               ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 5020 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 3 ms 4960 KB Output is correct
9 Correct 2 ms 5020 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 2 ms 4976 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 2 ms 5020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5004 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 3 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 2 ms 5076 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 4 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 2 ms 5076 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 2 ms 4948 KB Output is correct
26 Correct 2 ms 5076 KB Output is correct
27 Correct 2 ms 4948 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
29 Correct 3 ms 5076 KB Output is correct
30 Correct 3 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 5020 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 3 ms 4960 KB Output is correct
9 Correct 2 ms 5020 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 2 ms 4976 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 2 ms 5020 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 5004 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 2 ms 5076 KB Output is correct
28 Correct 2 ms 5076 KB Output is correct
29 Correct 4 ms 5076 KB Output is correct
30 Correct 3 ms 5076 KB Output is correct
31 Correct 2 ms 5076 KB Output is correct
32 Correct 3 ms 5076 KB Output is correct
33 Correct 3 ms 5076 KB Output is correct
34 Correct 3 ms 5076 KB Output is correct
35 Correct 3 ms 5076 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 3 ms 5076 KB Output is correct
38 Correct 3 ms 5076 KB Output is correct
39 Correct 3 ms 5076 KB Output is correct
40 Correct 3 ms 5076 KB Output is correct
41 Correct 3 ms 5076 KB Output is correct
42 Correct 2 ms 4948 KB Output is correct
43 Correct 2 ms 5076 KB Output is correct
44 Correct 2 ms 4948 KB Output is correct
45 Correct 3 ms 5076 KB Output is correct
46 Correct 3 ms 5076 KB Output is correct
47 Correct 3 ms 5076 KB Output is correct
48 Correct 40 ms 11624 KB Output is correct
49 Correct 50 ms 12316 KB Output is correct
50 Correct 44 ms 12284 KB Output is correct
51 Correct 34 ms 10776 KB Output is correct
52 Correct 59 ms 12332 KB Output is correct
53 Correct 40 ms 11668 KB Output is correct
54 Correct 27 ms 11408 KB Output is correct
55 Correct 56 ms 18244 KB Output is correct
56 Correct 63 ms 15008 KB Output is correct
57 Correct 54 ms 13940 KB Output is correct
58 Correct 49 ms 13088 KB Output is correct
59 Correct 43 ms 12364 KB Output is correct
60 Correct 43 ms 15408 KB Output is correct
61 Correct 39 ms 13224 KB Output is correct
62 Correct 39 ms 12604 KB Output is correct
63 Correct 36 ms 12368 KB Output is correct
64 Correct 40 ms 12736 KB Output is correct
65 Correct 37 ms 12728 KB Output is correct
66 Correct 34 ms 12372 KB Output is correct
67 Correct 31 ms 11060 KB Output is correct
68 Correct 33 ms 11576 KB Output is correct
69 Correct 38 ms 12872 KB Output is correct
70 Correct 34 ms 12424 KB Output is correct
71 Correct 26 ms 10444 KB Output is correct
72 Correct 29 ms 11192 KB Output is correct
73 Correct 36 ms 12840 KB Output is correct
74 Correct 34 ms 12096 KB Output is correct
75 Correct 44 ms 12136 KB Output is correct
76 Correct 42 ms 12124 KB Output is correct
77 Correct 37 ms 12312 KB Output is correct
78 Correct 27 ms 9944 KB Output is correct
79 Correct 31 ms 10792 KB Output is correct
80 Correct 54 ms 12232 KB Output is correct
81 Correct 38 ms 12496 KB Output is correct
82 Correct 37 ms 12548 KB Output is correct