Submission #685560

# Submission time Handle Problem Language Result Execution time Memory
685560 2023-01-24T14:26:50 Z stanislavpolyn Village (BOI20_village) C++17
0 / 100
700 ms 5324 KB
#include <bits/stdc++.h>

#define fr(i, a, b) for (int i = (a); i <= (b); ++i)
#define rf(i, a, b) for (int i = (a); i >= (b); --i)
#define fe(x, y) for (auto& x : y)

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple

#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define pw(x) (1LL << (x))

using namespace std;

mt19937_64 rng(228);

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define fbo find_by_order
#define ook order_of_key

template <typename T>
bool umn(T& a, T b) {
    return a > b ? a = b, 1 : 0;
}
template <typename T>
bool umx(T& a, T b) {
    return a < b ? a = b, 1 : 0;
}

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T>
using ve = vector<T>;

const int N = 1e5 + 5;

int n;
ve<int> g[N];

int dist[1001][1001];
int start;

void dfs(int v, int p, int dep) {
    dist[start][v] = dep;
    fe (to, g[v]) {
        if (to == p) continue;
        dfs(to, v, dep + 1);
    }
}

int p[N];
ve<int> order;

void dfs1(int v = 1, int p = 0) {
    ::p[v] = p;
    fe (to, g[v]) {
        if (to == p) continue;
        dfs1(to, v);
    }
    order.pb(v);
}

void assertTL(bool b) {
    if (!b) {
        while (1);
    }
}

int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    ios::sync_with_stdio(0);
    cin.tie(0);
#endif
    cin >> n;

    fr (i, 1, n - 1) {
        int a, b;
        cin >> a >> b;

        g[a].pb(b);
        g[b].pb(a);
    }

    if (n <= 1000) {
        for (start = 1; start <= n; start++) {
            dfs(start, 0, 0);
        }
    }

    dfs1();

    ll mySum = -1;
    {
        ve<bool> used(n + 1);
        ve<int> to(n + 1);

        fe (v, order) {
            ve<int> sons;
            fe (to, g[v]) {
                if (to == p[v]) continue;

                if (!used[to]) {
                    sons.pb(to);
                }
            }

            while (sz(sons) > 1) {
                used[sons.back()] = 1;
                used[sons[sz(sons) - 2]] = 1;
                to[sons.back()] = sons[sz(sons) - 2];
                to[sons[sz(sons) - 2]] = sons.back();
                sons.pop_back();
                sons.pop_back();
            }

            if (sz(sons)) {
                used[sons.back()] = 1;
                used[v] = 1;
                to[sons.back()] = v;
                to[v] = sons.back();
                sons.pop_back();
            }
        }
        if (n % 2) {
            assertTL(!to[1]);
            bool found = 0;

            fe (u, g[1]) {
                if (p[to[u]] == 1) {
                    int v = to[u];
                    to[v] = 1;
                    to[1] = u;
                    to[u] = v;

                    found = 1;
                    break;
                }
            }

            if (!found) {
                fe (u, g[1]) {
                    int v = to[u];
                    to[1] = u;
                    to[u] = v;
                    to[v] = 1;
                    break;
                }
            }
        }

        ll sum = 0;
        fr (i, 1, n) {
            sum += dist[i][to[i]];
            assertTL(dist[i][to[i]] <= 2);
        }


        set<int> s;
        fr (i, 1, n) {
            s.insert(to[i]);
            assertTL(to[i] != i);
        }
        assertTL(sz(s) == n);

        mySum = sum;
//        cout << sum << "\n";
//        fr (i, 1, n) {
//            cout << to[i] << " ";
//        }
//        cout << "\n";
    }

    {
        ll mx = -1e18;
        ve<int> mxP;
        ll mn = 1e18;
        ve<int> mnP;

        ve<int> p;
        fr (i, 1, n) p.pb(i);
        do {
            bool bad = 0;
            fr (i, 0, sz(p) - 1) {
                bad |= p[i] == i + 1;
            }
            if (!bad) {
                ll sum = 0;
                fr (i, 0, sz(p) - 1) {
                    sum += dist[i + 1][p[i]];
                }
                if (umx(mx, sum)) {
                    mxP = p;
                }

                bad = 0;

                if (!bad) {
                    if (umn(mn, sum)) {
                        mnP = p;
                    }
                }
            }
        } while (next_permutation(all(p)));

        assert(mySum == mn);

        cout << mn << " " << mx << "\n";
        fe (x, mnP) cout << x << " ";
        cout << "\n";

        fr (i, 0, sz(mnP) - 1) {
            assert(dist[i + 1][mnP[i]] <= 2);
        }
        fe (x, mxP) cout << x << " ";
        cout << "\n";
        cout << "\n";
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 9 ms 2716 KB Output is correct
10 Correct 72 ms 2696 KB Output is correct
11 Correct 72 ms 2700 KB Output is correct
12 Correct 72 ms 2700 KB Output is correct
13 Runtime error 75 ms 5324 KB Execution killed with signal 6
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1097 ms 3668 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 9 ms 2716 KB Output is correct
10 Correct 72 ms 2696 KB Output is correct
11 Correct 72 ms 2700 KB Output is correct
12 Correct 72 ms 2700 KB Output is correct
13 Runtime error 75 ms 5324 KB Execution killed with signal 6
14 Halted 0 ms 0 KB -