Submission #971445

# Submission time Handle Problem Language Result Execution time Memory
971445 2024-04-28T14:04:54 Z asdasdqwer Village (BOI20_village) C++14
100 / 100
229 ms 53968 KB
#include <bits/stdc++.h>
using namespace std;

#define int int64_t

pair<int, vector<int>> big(int n, vector<vector<int>> &g) { 
    vector<int> v;
    vector<int> d(n, 0);
    vector<vector<int>> lft(n, vector<int>(20, -1));
    function<void(int,int)> dfs=[&](int node, int p) {
        v.push_back(node);
        for (int x:g[node]) {
            if (x==p)continue;
            d[x]=d[node]+1;
            lft[x][0]=node;
            dfs(x, node);
        }
    };
 
    dfs(0, -1);
    
    for (int j=1;j<20;j++) {
        for (int i=0;i<n;i++) {
            lft[i][j] = lft[i][j-1];
            if (lft[i][j] != -1) {
                lft[i][j] = lft[lft[i][j]][j-1];
            }
        }
    }
    vector<int> idx(n, -1);
    for (int i=0;i<n;i++) {
        idx[v[i]]=i;
    }
 
    vector<int> out(n);
    for (int i=0;i<n;i++) {
        out[i] = v[(idx[i] + n/2) % n];
    }
    
    long long dist = 0;
 
    auto jmp=[&](int a, int steps) {
        int cnt=0;
        while (steps) {
            if ((steps&1)==1) {
                a = lft[a][cnt];
            }
            cnt++;
            steps>>=1;
        }
        return a;
    };
 
    auto lca=[&](int a, int b) {
        if (d[a] < d[b])swap(a,b);
        int ret = d[a]-d[b];
        a = jmp(a, d[a]-d[b]);
        if (a == b) return ret;
 
        for (int i=19;i>=0;i--) {
            if (lft[a][i] != lft[b][i]) {
                a = lft[a][i];
                b = lft[b][i];
                ret += (1<<(i+1));
            }
        }
 
        return ret+2;
    };
 
    for (int i=0;i<n;i++) {
        dist += lca(i, out[i]);
    }
 
    return make_pair(dist, out);
}



signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;cin>>n;
    vector<set<int>> g(n);
    vector<vector<int>> bck(n);
    vector<int> dg(n,0);
    for (int i=1;i<n;i++) {
        int a,b;cin>>a>>b;a--;b--;
        g[a].insert(b);
        g[b].insert(a);
        bck[a].push_back(b);
        bck[b].push_back(a);
        dg[a]++;dg[b]++;
    }

    queue<int> q;
    vector<bool> vis(n, false);
    for (int i=0;i<n;i++) {
        if (dg[i]==1){
            q.push(i);
            vis[i]=true;
        }
    }
    
    int ans=0;
    vector<int> conf(n,0);
    for (int i=0;i<n;i++) {
        conf[i]=i;
    }
    
    vector<bool> swp(n, false);
    
    while (!q.empty()) {
        int t=q.front();q.pop();
        if (g[t].size() == 0) {
            if (!swp[conf[t]]) {
                swap(conf[t], conf[bck[t][0]]);
                swp[conf[t]]=true;
                ans += 2;
            }
            continue;
        }
    
        int ne = *g[t].begin();
        vis[ne]=true;
    
        if (g[ne].find(t) != g[ne].end()) g[ne].erase(g[ne].find(t));
        for (int x:g[ne]) {
            if (g[x].find(ne) != g[x].end()) g[x].erase(g[x].find(ne));
            dg[x]--;
            if (dg[x]<=1 && !vis[x]) {
                q.push(x);
                vis[x]=true;
            }
        }
        if (!swp[ne] && !swp[t]) {
            dg[ne]--;
            ans+=2;
            swap(conf[ne], conf[t]);
            swp[ne]=swp[t]=true;
        }
    }

    auto [ans2, out] = big(n, bck);
    
    cout<<ans<<" "<<ans2<<"\n";
    for (int x:conf) {
        cout<<x+1<<" ";
    }
    cout<<"\n";

    for (int x:out) {
        cout<<x+1<<" ";
    }
    cout<<"\n";
}

Compilation message

Village.cpp: In function 'int main()':
Village.cpp:144:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  144 |     auto [ans2, out] = big(n, bck);
      |          ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 940 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 2 ms 860 KB Output is correct
11 Correct 1 ms 856 KB Output is correct
12 Correct 1 ms 856 KB Output is correct
13 Correct 1 ms 912 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 1 ms 860 KB Output is correct
20 Correct 1 ms 860 KB Output is correct
21 Correct 1 ms 860 KB Output is correct
22 Correct 1 ms 856 KB Output is correct
23 Correct 1 ms 860 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 860 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 1 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 940 KB Output is correct
22 Correct 1 ms 860 KB Output is correct
23 Correct 1 ms 860 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 2 ms 860 KB Output is correct
28 Correct 1 ms 856 KB Output is correct
29 Correct 1 ms 856 KB Output is correct
30 Correct 1 ms 912 KB Output is correct
31 Correct 1 ms 860 KB Output is correct
32 Correct 1 ms 860 KB Output is correct
33 Correct 1 ms 860 KB Output is correct
34 Correct 1 ms 860 KB Output is correct
35 Correct 1 ms 860 KB Output is correct
36 Correct 1 ms 860 KB Output is correct
37 Correct 1 ms 860 KB Output is correct
38 Correct 1 ms 860 KB Output is correct
39 Correct 1 ms 856 KB Output is correct
40 Correct 1 ms 860 KB Output is correct
41 Correct 1 ms 860 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 860 KB Output is correct
44 Correct 1 ms 604 KB Output is correct
45 Correct 1 ms 860 KB Output is correct
46 Correct 1 ms 860 KB Output is correct
47 Correct 1 ms 860 KB Output is correct
48 Correct 147 ms 42732 KB Output is correct
49 Correct 205 ms 47048 KB Output is correct
50 Correct 183 ms 47060 KB Output is correct
51 Correct 115 ms 36604 KB Output is correct
52 Correct 195 ms 46280 KB Output is correct
53 Correct 179 ms 41668 KB Output is correct
54 Correct 68 ms 26828 KB Output is correct
55 Correct 229 ms 53968 KB Output is correct
56 Correct 207 ms 50376 KB Output is correct
57 Correct 215 ms 48756 KB Output is correct
58 Correct 213 ms 48088 KB Output is correct
59 Correct 187 ms 47156 KB Output is correct
60 Correct 158 ms 46980 KB Output is correct
61 Correct 187 ms 47236 KB Output is correct
62 Correct 171 ms 47300 KB Output is correct
63 Correct 168 ms 44224 KB Output is correct
64 Correct 172 ms 47308 KB Output is correct
65 Correct 176 ms 47312 KB Output is correct
66 Correct 188 ms 44488 KB Output is correct
67 Correct 97 ms 34892 KB Output is correct
68 Correct 130 ms 40168 KB Output is correct
69 Correct 167 ms 47560 KB Output is correct
70 Correct 175 ms 44580 KB Output is correct
71 Correct 113 ms 33104 KB Output is correct
72 Correct 109 ms 37560 KB Output is correct
73 Correct 190 ms 47328 KB Output is correct
74 Correct 142 ms 43468 KB Output is correct
75 Correct 206 ms 47564 KB Output is correct
76 Correct 195 ms 46812 KB Output is correct
77 Correct 193 ms 44760 KB Output is correct
78 Correct 90 ms 31532 KB Output is correct
79 Correct 117 ms 36560 KB Output is correct
80 Correct 173 ms 47480 KB Output is correct
81 Correct 154 ms 44828 KB Output is correct
82 Correct 172 ms 46788 KB Output is correct