Submission #726039

# Submission time Handle Problem Language Result Execution time Memory
726039 2023-04-18T10:52:51 Z Johann Village (BOI20_village) C++14
75 / 100
130 ms 22464 KB
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
int N;
vvi adj;

// Code for the mini length
vi dpP, dpN;
vi ans_mini;
int miniCheck = 0;

void dfs_mini(int v, int p)
{
    if (sz(adj[v]) == 1 && v != p)
    {
        // leaf
        dpN[v] = 0;
        dpP[v] = INT_MAX;
        return;
    }
    for (int u : adj[v])
        if (u != p)
            dfs_mini(u, v);

    dpN[v] = 0;
    for (int u : adj[v])
        if (u != p)
            dpN[v] += min(dpP[u], dpN[u] + 2);
    for (int u : adj[v])
        if (u != p)
        {
            int tmp = dpN[v] - min(dpP[u], dpN[u] + 2) + min(dpP[u], dpN[u]) + 2;
            dpP[v] = min(dpP[v], tmp);
        }
}
void reconstruct_dpP(int v, int p);
void reconstruct_dpN(int v, int p, int except);

void reconstruct_dpN(int v, int p, int except = -1)
{
    if (sz(adj[v]) == 1 && v != p)
        return; // leaf

    for (int u : adj[v])
    {
        if (u == p)
            continue;
        if (u == except)
        {
            swap(ans_mini[v], ans_mini[u]);
            miniCheck += 2;
            if (dpP[u] < dpN[u])
                reconstruct_dpP(u, v);
            else
                reconstruct_dpN(u, v);
        }
        else if (dpP[u] < dpN[u] + 2)
            reconstruct_dpP(u, v);
        else
        {
            swap(ans_mini[v], ans_mini[u]);
            miniCheck += 2;
            reconstruct_dpN(u, v);
        }
    }
}

void reconstruct_dpP(int v, int p)
{
    if (sz(adj[v]) == 1 && v != p)
        return; // leaf

    int w = -1;
    for (int u : adj[v])
        if (u != p)
        {
            int tmp = dpN[v] - min(dpP[u], dpN[u] + 2) + min(dpP[u], dpN[u]) + 2;
            if (dpP[v] == tmp)
            {
                w = u;
                break;
            }
        }
    reconstruct_dpN(v, p, w);
}

// code for the maxi part

vi subsize, swapsRequired;
vvi contains;
vi ans_maxi;
void dfs_maxi(int v, int p)
{
    subsize[v] = 1;
    for (int u : adj[v])
        if (u != p)
        {
            dfs_maxi(u, v);
            subsize[v] += subsize[u];
        }
}
int find_split(int v)
{ // v must be root of the tree with respect to subsize
    pii maxSub = {-1, -1};
    for (int u : adj[v])
        maxSub = max(maxSub, {subsize[u], u});
    if (maxSub.first <= N / 2)
        return v;
    subsize[v] = N - maxSub.first;
    subsize[maxSub.second] = N;
    return find_split(maxSub.second);
}
void collect(int v, int p)
{
    contains[v].push_back(v);
    for (int u : adj[v])
        if (u != p)
        {
            collect(u, v);
            swapsRequired[v] += swapsRequired[u] + subsize[u];
            if (v != p)
            {
                if (sz(contains[u]) > sz(contains[v]))
                    swap(contains[u], contains[v]);
                while (!contains[u].empty())
                    contains[v].push_back(contains[u].back()), contains[u].pop_back();
            }
        }
}
void distribute(int v)
{
    vi places;
    places.push_back(v);
    for (int u : adj[v])
        places.push_back(u);
    sort(all(places), [&](int a, int b)
         { return sz(contains[a]) > sz(contains[b]); });
    queue<int> todo;
    for (int u : places)
        for (int w : contains[u])
            todo.push(w);
    for (int i = 0; i < sz(places); ++i)
        for (int u : contains[places[(i + 1) % sz(places)]])
            ans_maxi[u] = todo.front(), todo.pop();
}

// main
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> N;
    adj.resize(N);
    for (int i = 0, a, b; i < N - 1; ++i)
    {
        cin >> a >> b, --a, --b;
        adj[a].push_back(b), adj[b].push_back(a);
    }

    // mini
    dpP.assign(N, INT_MAX), dpN.assign(N, INT_MAX);
    dfs_mini(0, 0);

    ans_mini.resize(N);
    iota(all(ans_mini), 1);
    reconstruct_dpP(0, 0);

    // maxi
    subsize.resize(N);
    dfs_maxi(0, 0);
    int center = find_split(0);
    contains.resize(N);
    swapsRequired.assign(N, 0);
    collect(center, center);
    ans_maxi.resize(N);
    distribute(center);

    // ans
    cout << dpP[0] << " ";
    cout << 2 * swapsRequired[center] << endl;
    assert(miniCheck == dpP[0]);
    for (int v : ans_mini)
        cout << v << " ";
    cout << endl;

    for (int v : ans_maxi)
        cout << v + 1 << " ";
    cout << endl;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 0 ms 320 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 452 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 2 ms 456 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 452 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 2 ms 452 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 0 ms 320 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 320 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 452 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 2 ms 456 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 452 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 1 ms 468 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 1 ms 468 KB Output is correct
41 Correct 1 ms 468 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 2 ms 452 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 468 KB Output is correct
46 Correct 1 ms 468 KB Output is correct
47 Correct 1 ms 456 KB Output is correct
48 Correct 105 ms 16500 KB Output is correct
49 Correct 114 ms 18120 KB Output is correct
50 Correct 95 ms 18104 KB Output is correct
51 Correct 72 ms 14240 KB Output is correct
52 Correct 112 ms 17808 KB Output is correct
53 Correct 99 ms 16212 KB Output is correct
54 Correct 56 ms 11156 KB Output is correct
55 Partially correct 117 ms 22464 KB Partially correct
56 Partially correct 121 ms 19416 KB Partially correct
57 Correct 127 ms 18336 KB Output is correct
58 Correct 130 ms 17896 KB Output is correct
59 Correct 113 ms 17636 KB Output is correct
60 Correct 65 ms 16604 KB Output is correct
61 Correct 75 ms 16856 KB Output is correct
62 Correct 76 ms 17512 KB Output is correct
63 Correct 84 ms 16776 KB Output is correct
64 Correct 83 ms 18208 KB Output is correct
65 Correct 75 ms 17976 KB Output is correct
66 Correct 74 ms 16324 KB Output is correct
67 Correct 50 ms 12556 KB Output is correct
68 Correct 70 ms 15292 KB Output is correct
69 Correct 80 ms 17592 KB Output is correct
70 Correct 78 ms 16492 KB Output is correct
71 Correct 49 ms 12344 KB Output is correct
72 Correct 75 ms 14044 KB Output is correct
73 Correct 75 ms 17672 KB Output is correct
74 Correct 68 ms 16104 KB Output is correct
75 Correct 112 ms 18744 KB Output is correct
76 Correct 96 ms 18856 KB Output is correct
77 Correct 84 ms 17412 KB Output is correct
78 Correct 69 ms 12436 KB Output is correct
79 Correct 68 ms 14040 KB Output is correct
80 Correct 115 ms 18844 KB Output is correct
81 Correct 82 ms 17364 KB Output is correct
82 Correct 77 ms 17088 KB Output is correct