Submission #475470

# Submission time Handle Problem Language Result Execution time Memory
475470 2021-09-22T15:02:59 Z Shin Village (BOI20_village) C++17
100 / 100
124 ms 20152 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define TASK "task"
#define all(x) x.begin(), x.end()

using namespace std;
const int N = 2e5 + 7;
const int MOD = 1e9 + 7; // 998244353;
const int INF = 1e9 + 7;
const long long INFLL = 1e18 + 7;

typedef long long ll;
typedef unsigned long long ull;
template <class X, class Y> bool minimize(X &a, Y b) {
    if (a > b) return a = b, true;
    return false;
}
template <class X, class Y> bool maximize(X &a, Y b) {
    if (a < b) return a = b, true;
    return false;
}

int n;
vector<int> adj[N];

namespace task1 {
    static int best = 0;
    static int newHouse[N];
    void dfs(int u, int p) {
        for (int v: adj[u]) if (v != p) {
            dfs(v, u);
            if (v == newHouse[v]) {
                best += 2;
                swap(newHouse[u], newHouse[v]);
            }
        }
    }
     
    void solve(void) {
        for (int i = 1; i <= n; i ++) newHouse[i] = i;
        dfs(1, 1);
        if (newHouse[1] == 1) {
            best += 2;
            swap(newHouse[1], newHouse[*adj[1].begin()]);
        }
    }
}

namespace task2 {
    static ll best;
    static int sub[N];
    static int newHouse[N];
    vector<int> vertex;

    void dfs1(int u, int p) {
        sub[u] = 1;
        vertex.push_back(u);
        for (int v: adj[u]) if (v != p) {
            dfs1(v, u);
            sub[u] += sub[v];
            best += min(sub[v], n - sub[v]);
        }
    }

    int centroid(int u, int p) {
        for (int v: adj[u]) if (v != p) {
            if (sub[v] > n / 2) return centroid(v, u);
        }
        return u;
    }

    void solve(void) {
        for (int i = 1; i <= n; i ++) newHouse[i] = i;
        dfs1(1, 1);
        int centerNode = centroid(1, 1);
        // cout << centerNode << '\n';
        dfs1(centerNode, centerNode);

        int mx = 0;
        for (int v: adj[centerNode]) maximize(mx, sub[v]);
        for (int i = 0; i < n; i ++) 
            newHouse[vertex[i]] = vertex[(i + mx) % n];
    }   
}

int main(void) {
    cin.tie(0)->sync_with_stdio(0); 
    int test = 1;
    // cin >> test;
    while (test --) {
        cin >> n;
        for (int i = 1; i < n; i ++) {
            int u, v; cin >> u >> v;
            adj[u].push_back(v);
            adj[v].push_back(u);
        }
        task1::solve();
        task2::solve();
        cout << task1::best << " " << task2::best << '\n';
        for (int i = 1; i <= n; i ++) cout << task1::newHouse[i] << " \n"[i == n];
        for (int i = 1; i <= n; i ++) cout << task2::newHouse[i] << " \n"[i == n];
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 5000 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 4 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 4 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 5 ms 4940 KB Output is correct
16 Correct 4 ms 5020 KB Output is correct
17 Correct 3 ms 5020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 4 ms 5068 KB Output is correct
3 Correct 3 ms 5068 KB Output is correct
4 Correct 4 ms 5068 KB Output is correct
5 Correct 4 ms 5024 KB Output is correct
6 Correct 4 ms 5068 KB Output is correct
7 Correct 4 ms 5152 KB Output is correct
8 Correct 5 ms 5084 KB Output is correct
9 Correct 4 ms 5068 KB Output is correct
10 Correct 4 ms 5068 KB Output is correct
11 Correct 4 ms 5020 KB Output is correct
12 Correct 4 ms 5068 KB Output is correct
13 Correct 4 ms 5028 KB Output is correct
14 Correct 4 ms 5068 KB Output is correct
15 Correct 4 ms 5024 KB Output is correct
16 Correct 4 ms 5060 KB Output is correct
17 Correct 4 ms 5068 KB Output is correct
18 Correct 4 ms 5068 KB Output is correct
19 Correct 4 ms 5068 KB Output is correct
20 Correct 4 ms 5068 KB Output is correct
21 Correct 4 ms 5024 KB Output is correct
22 Correct 4 ms 5068 KB Output is correct
23 Correct 4 ms 5028 KB Output is correct
24 Correct 4 ms 5068 KB Output is correct
25 Correct 4 ms 5068 KB Output is correct
26 Correct 4 ms 5068 KB Output is correct
27 Correct 4 ms 5068 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
29 Correct 4 ms 5068 KB Output is correct
30 Correct 4 ms 5024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 5000 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 4 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 4 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 5 ms 4940 KB Output is correct
16 Correct 4 ms 5020 KB Output is correct
17 Correct 3 ms 5020 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 4 ms 5068 KB Output is correct
20 Correct 3 ms 5068 KB Output is correct
21 Correct 4 ms 5068 KB Output is correct
22 Correct 4 ms 5024 KB Output is correct
23 Correct 4 ms 5068 KB Output is correct
24 Correct 4 ms 5152 KB Output is correct
25 Correct 5 ms 5084 KB Output is correct
26 Correct 4 ms 5068 KB Output is correct
27 Correct 4 ms 5068 KB Output is correct
28 Correct 4 ms 5020 KB Output is correct
29 Correct 4 ms 5068 KB Output is correct
30 Correct 4 ms 5028 KB Output is correct
31 Correct 4 ms 5068 KB Output is correct
32 Correct 4 ms 5024 KB Output is correct
33 Correct 4 ms 5060 KB Output is correct
34 Correct 4 ms 5068 KB Output is correct
35 Correct 4 ms 5068 KB Output is correct
36 Correct 4 ms 5068 KB Output is correct
37 Correct 4 ms 5068 KB Output is correct
38 Correct 4 ms 5024 KB Output is correct
39 Correct 4 ms 5068 KB Output is correct
40 Correct 4 ms 5028 KB Output is correct
41 Correct 4 ms 5068 KB Output is correct
42 Correct 4 ms 5068 KB Output is correct
43 Correct 4 ms 5068 KB Output is correct
44 Correct 4 ms 5068 KB Output is correct
45 Correct 4 ms 5068 KB Output is correct
46 Correct 4 ms 5068 KB Output is correct
47 Correct 4 ms 5024 KB Output is correct
48 Correct 90 ms 11972 KB Output is correct
49 Correct 83 ms 12668 KB Output is correct
50 Correct 88 ms 12696 KB Output is correct
51 Correct 83 ms 10980 KB Output is correct
52 Correct 94 ms 12552 KB Output is correct
53 Correct 88 ms 11944 KB Output is correct
54 Correct 50 ms 12352 KB Output is correct
55 Correct 108 ms 20152 KB Output is correct
56 Correct 109 ms 16220 KB Output is correct
57 Correct 97 ms 14904 KB Output is correct
58 Correct 117 ms 13844 KB Output is correct
59 Correct 108 ms 12780 KB Output is correct
60 Correct 64 ms 12784 KB Output is correct
61 Correct 69 ms 12968 KB Output is correct
62 Correct 74 ms 13040 KB Output is correct
63 Correct 69 ms 12612 KB Output is correct
64 Correct 75 ms 12940 KB Output is correct
65 Correct 72 ms 13120 KB Output is correct
66 Correct 64 ms 12580 KB Output is correct
67 Correct 48 ms 10932 KB Output is correct
68 Correct 64 ms 11848 KB Output is correct
69 Correct 75 ms 13048 KB Output is correct
70 Correct 65 ms 12596 KB Output is correct
71 Correct 49 ms 10680 KB Output is correct
72 Correct 60 ms 11388 KB Output is correct
73 Correct 84 ms 13112 KB Output is correct
74 Correct 64 ms 12352 KB Output is correct
75 Correct 124 ms 12468 KB Output is correct
76 Correct 80 ms 12576 KB Output is correct
77 Correct 90 ms 12688 KB Output is correct
78 Correct 50 ms 10144 KB Output is correct
79 Correct 60 ms 11008 KB Output is correct
80 Correct 81 ms 12472 KB Output is correct
81 Correct 74 ms 12792 KB Output is correct
82 Correct 74 ms 12880 KB Output is correct