Submission #1019409

# Submission time Handle Problem Language Result Execution time Memory
1019409 2024-07-10T19:27:29 Z Fatonim Village (BOI20_village) C++17
100 / 100
127 ms 63812 KB
// "We create our own demons"
#include <bits/stdc++.h>

using namespace std;

#ifdef ONPC
    #include "debug.h"
#else
    #define dbg(...)
#endif

#define int long long
#define ll long long
#define ld long double
#define pi pair<int, int>
// vector
#define sz(a) (int)((a).size())
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define maxel(x) (*max_element(all(x)))
#define minel(x) (*min_element(all(x)))
#define maxpos(x) (max_element(all(x)) - x.begin())
#define minpos(x) (min_element(all(x)) - x.begin())
#define rev(x) (reverse(all(x)))
// bits
#define popcount(n) __builtin_popcountll(n)
#define clz(n) __builtin_clzll(n)
// math
#define sqr(n) ((n) * (n))
#define divup(a, b) (((a) + (b)-1) / (b))
// ostream
#define Fixed(a) cout << fixed << setprecision(12) << a;

template <class T> bool chmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template <class T> bool chmax(T& a, const T& b) { return b > a ? a = b, 1 : 0; }

template <class T> using min_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> using max_queue = priority_queue<T, vector<T>, less<T>>;

template <class T> using V = vector<T>;
using vi = V<int>;
using vd = V<ld>;
using vb = V<bool>;
using vpi = V<pi>;
using vvi = V<vi>;
using vvb = V<vb>;

const int mod = 1e9 + 7; // 998244353 1e9 + 7
const ll inf = (int)(1e18) + 7;
const int inf_s = 1e9 + 7;
const ld eps = 1e-9;

const int B = 32;
const int N = 1000 + 3;
const int logn = 20;
const int maxn = 2e5 + 7;
/////////////////////////solve/////////////////////////

vi g[maxn];
int ans[maxn], h[maxn], s[maxn];
int ans2[maxn];
int tour[maxn];
int dp[maxn][2];
vpi dp_par[maxn][2];
vi dp_swap[maxn][2];

int solve2(int n) {
    for (int i = 0; i < n; ++i) {
        ans[i] = i;
    }

    function<void(int, int)> dfs = [&](int v, int p) {
        set <pi> s;
        for (auto u : g[v]) {
            if (u != p) {
                dfs(u, v);
                dp[v][0] += dp[u][1];
                dp_par[v][0].push_back({u, 1});
                s.insert({dp[u][0] - dp[u][1] + 2, u});
            }
        }
        if (s.empty()) dp[v][1] = inf_s;
        else {
            int u = s.begin()->second;
            dp_swap[v][1].push_back(u);
            if (dp[u][0] <= dp[u][1]) {
                dp[v][1] += dp[u][0] + 2;
                dp_par[v][1].push_back({u, 0});
            }
            else {
                dp[v][1] += dp[u][1] + 2;
                dp_par[v][1].push_back({u, 1});
            }
            s.erase(s.begin());

            for (auto& [x, u] : s) {
                if (dp[u][0] + 2 < dp[u][1]) {
                    dp[v][1] += dp[u][0] + 2;
                    dp_par[v][1].push_back({u, 0});
                    dp_swap[v][1].push_back(u);
                }
                else {
                    dp[v][1] += dp[u][1];
                    dp_par[v][1].push_back({u, 1});
                }
            }
        }
    };

    dfs(0, -1);

    function<void(int, int, int)> dfs2 = [&](int v, int t, int p) {
        for (auto& [u, t2] : dp_par[v][t]) {
            dfs2(u, t2, v);
        }
        for (auto& u : dp_swap[v][t]) {
            swap(ans[v], ans[u]);
        }
    };

    dfs2(0, 1, -1);

    return dp[0][1];
}

void solve() {
    int n;
    cin >> n;

    for (int i = 0; i < n - 1; ++i) {
        int v, u;
        cin >> v >> u;
        --v; --u;

        g[v].push_back(u);
        g[u].push_back(v);
    }

    function<int(int, int)> dfs1 = [&](int v, int p) -> int {
        s[v] = 1;
        for (auto u : g[v]) {
            if (u != p) {
                s[v] += dfs1(u, v);
            }
        }
        return s[v];
    };

    function<int(int, int)> centroid = [&](int v, int p) -> int {
        for (auto u : g[v]) {
            if (u != p && s[u] > n / 2) {
                return centroid(u, v);
            }
        }
        return v;
    };

    int sum = 0, tim = -1;
    function<void(int, int)> dfs2 = [&](int v, int p) {
        if (p != -1) h[v] = h[p] + 1;
        tour[++tim] = v;
        for (auto u : g[v]) {
            if (u != p) {
                dfs2(u, v);
            }
        }
        sum += h[v];
    };

    dfs1(0, -1);
    int c = centroid(0, -1);
    dbg(c);
    dfs2(c, -1);

    int sum1 = solve2(n);

    cout << sum1 << " " << 2 * sum << "\n";

    // for (int i = 0; i < n; ++i) {
    //     cout << tour[i] + 1 << " ";
    // } cout << "\n";

    for (int i = 0; i < n; ++i) {
        ans2[tour[i]] = tour[(i + n / 2) % n];
        cout << ans[i] + 1 << " ";
    } cout << "\n";
    for (int i = 0; i < n; ++i) {
        cout << ans2[i] + 1 << " ";
    }
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

#ifdef ONPC
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
#endif

    int t = 1;
    //cin >> t;
    for (int i = 1; i <= t; ++i) {
#ifdef ONPC
        cerr << "===========" << i << "===========" << '\n';
#endif
        solve();
    }

#ifdef ONPC
    cerr << endl << "Time " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
#endif
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23900 KB Output is correct
2 Correct 10 ms 23988 KB Output is correct
3 Correct 10 ms 23984 KB Output is correct
4 Correct 10 ms 23812 KB Output is correct
5 Correct 10 ms 23944 KB Output is correct
6 Correct 11 ms 23900 KB Output is correct
7 Correct 13 ms 23988 KB Output is correct
8 Correct 12 ms 23900 KB Output is correct
9 Correct 13 ms 23896 KB Output is correct
10 Correct 10 ms 23896 KB Output is correct
11 Correct 10 ms 23956 KB Output is correct
12 Correct 13 ms 24156 KB Output is correct
13 Correct 10 ms 23896 KB Output is correct
14 Correct 10 ms 23932 KB Output is correct
15 Correct 10 ms 24156 KB Output is correct
16 Correct 11 ms 23948 KB Output is correct
17 Correct 10 ms 23980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 23896 KB Output is correct
2 Correct 11 ms 23900 KB Output is correct
3 Correct 11 ms 23900 KB Output is correct
4 Correct 11 ms 24124 KB Output is correct
5 Correct 11 ms 23952 KB Output is correct
6 Correct 11 ms 24156 KB Output is correct
7 Correct 10 ms 24156 KB Output is correct
8 Correct 10 ms 24168 KB Output is correct
9 Correct 11 ms 24156 KB Output is correct
10 Correct 11 ms 24160 KB Output is correct
11 Correct 11 ms 23968 KB Output is correct
12 Correct 11 ms 24200 KB Output is correct
13 Correct 11 ms 23968 KB Output is correct
14 Correct 11 ms 24160 KB Output is correct
15 Correct 12 ms 23956 KB Output is correct
16 Correct 11 ms 24152 KB Output is correct
17 Correct 11 ms 24156 KB Output is correct
18 Correct 11 ms 24156 KB Output is correct
19 Correct 13 ms 24156 KB Output is correct
20 Correct 14 ms 24152 KB Output is correct
21 Correct 13 ms 24152 KB Output is correct
22 Correct 11 ms 24152 KB Output is correct
23 Correct 11 ms 23900 KB Output is correct
24 Correct 10 ms 24068 KB Output is correct
25 Correct 10 ms 23844 KB Output is correct
26 Correct 10 ms 24156 KB Output is correct
27 Correct 10 ms 23900 KB Output is correct
28 Correct 11 ms 24156 KB Output is correct
29 Correct 11 ms 24156 KB Output is correct
30 Correct 10 ms 23956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23900 KB Output is correct
2 Correct 10 ms 23988 KB Output is correct
3 Correct 10 ms 23984 KB Output is correct
4 Correct 10 ms 23812 KB Output is correct
5 Correct 10 ms 23944 KB Output is correct
6 Correct 11 ms 23900 KB Output is correct
7 Correct 13 ms 23988 KB Output is correct
8 Correct 12 ms 23900 KB Output is correct
9 Correct 13 ms 23896 KB Output is correct
10 Correct 10 ms 23896 KB Output is correct
11 Correct 10 ms 23956 KB Output is correct
12 Correct 13 ms 24156 KB Output is correct
13 Correct 10 ms 23896 KB Output is correct
14 Correct 10 ms 23932 KB Output is correct
15 Correct 10 ms 24156 KB Output is correct
16 Correct 11 ms 23948 KB Output is correct
17 Correct 10 ms 23980 KB Output is correct
18 Correct 10 ms 23896 KB Output is correct
19 Correct 11 ms 23900 KB Output is correct
20 Correct 11 ms 23900 KB Output is correct
21 Correct 11 ms 24124 KB Output is correct
22 Correct 11 ms 23952 KB Output is correct
23 Correct 11 ms 24156 KB Output is correct
24 Correct 10 ms 24156 KB Output is correct
25 Correct 10 ms 24168 KB Output is correct
26 Correct 11 ms 24156 KB Output is correct
27 Correct 11 ms 24160 KB Output is correct
28 Correct 11 ms 23968 KB Output is correct
29 Correct 11 ms 24200 KB Output is correct
30 Correct 11 ms 23968 KB Output is correct
31 Correct 11 ms 24160 KB Output is correct
32 Correct 12 ms 23956 KB Output is correct
33 Correct 11 ms 24152 KB Output is correct
34 Correct 11 ms 24156 KB Output is correct
35 Correct 11 ms 24156 KB Output is correct
36 Correct 13 ms 24156 KB Output is correct
37 Correct 14 ms 24152 KB Output is correct
38 Correct 13 ms 24152 KB Output is correct
39 Correct 11 ms 24152 KB Output is correct
40 Correct 11 ms 23900 KB Output is correct
41 Correct 10 ms 24068 KB Output is correct
42 Correct 10 ms 23844 KB Output is correct
43 Correct 10 ms 24156 KB Output is correct
44 Correct 10 ms 23900 KB Output is correct
45 Correct 11 ms 24156 KB Output is correct
46 Correct 11 ms 24156 KB Output is correct
47 Correct 10 ms 23956 KB Output is correct
48 Correct 95 ms 40276 KB Output is correct
49 Correct 86 ms 42164 KB Output is correct
50 Correct 85 ms 42064 KB Output is correct
51 Correct 72 ms 37960 KB Output is correct
52 Correct 83 ms 41756 KB Output is correct
53 Correct 79 ms 40012 KB Output is correct
54 Correct 58 ms 43152 KB Output is correct
55 Correct 127 ms 63812 KB Output is correct
56 Correct 108 ms 52244 KB Output is correct
57 Correct 115 ms 48832 KB Output is correct
58 Correct 112 ms 45912 KB Output is correct
59 Correct 99 ms 42320 KB Output is correct
60 Correct 104 ms 46916 KB Output is correct
61 Correct 76 ms 42796 KB Output is correct
62 Correct 68 ms 40336 KB Output is correct
63 Correct 72 ms 39504 KB Output is correct
64 Correct 71 ms 41556 KB Output is correct
65 Correct 66 ms 41552 KB Output is correct
66 Correct 72 ms 40016 KB Output is correct
67 Correct 72 ms 37884 KB Output is correct
68 Correct 78 ms 38996 KB Output is correct
69 Correct 71 ms 41556 KB Output is correct
70 Correct 69 ms 40580 KB Output is correct
71 Correct 53 ms 35892 KB Output is correct
72 Correct 54 ms 37712 KB Output is correct
73 Correct 70 ms 41680 KB Output is correct
74 Correct 64 ms 40004 KB Output is correct
75 Correct 89 ms 41808 KB Output is correct
76 Correct 88 ms 41816 KB Output is correct
77 Correct 79 ms 42324 KB Output is correct
78 Correct 55 ms 35672 KB Output is correct
79 Correct 59 ms 36944 KB Output is correct
80 Correct 87 ms 41808 KB Output is correct
81 Correct 78 ms 42780 KB Output is correct
82 Correct 64 ms 39508 KB Output is correct