답안 #555264

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
555264 2022-04-30T10:49:55 Z ddy888 Village (BOI20_village) C++17
50 / 100
110 ms 21312 KB
#undef _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
void debug_out() {cerr<<endl;}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {cerr<<" "<<to_string(H);debug_out(T...);}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__)
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define int long long 
#define pb push_back
#define fi first
#define si second
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef tuple<int,int,int> ti;  
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MAXN = 100010;
int n, c, cnt;
vector<int> g[MAXN], nodes;
int minans, maxans;
int par[MAXN];
int sz[MAXN];
int vis[MAXN];
int pos[MAXN], dep[MAXN];
int mnc[MAXN], mxc[MAXN];
int group[MAXN];
vector<int> cent;

void getsz(int x, int fa) {
    sz[x] = 1;
    par[x] = fa;
    dep[x] = dep[fa] + 1;
    for (auto i: g[x]) {
        if (i == fa) 
            continue;
        getsz(i, x);
        sz[x] += sz[i];
    }
}

int centroid(int x, int fa) {
    for (auto i: g[x]) {
        if (i == fa)
            continue;
        if (sz[i] * 2 > n)
            return centroid(i, x);
    }
    return x;
}

void dfs(int x, int fa, int branch) {
    group[x] = branch;
    for (auto i: g[x]) {
        if (i == fa)
            continue;
        if (x == c) 
            dfs(i, x, ++cnt);
        else 
            dfs(i, x, branch);
    }
}

signed main() {
    fast;
    cin >> n;
    for (int i = 1; i < n; ++i) {
        int u, v; cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    getsz(1, 1);
    c = centroid(1, 1);
    // Minimum
    for (int i = 1; i <= n; ++i) { 
        nodes.pb(i);
        pos[i] = i;
    }
    sort(nodes.begin(), nodes.end(), [](int a, int b) {
        return sz[a] < sz[b];
    });
    for (auto i: nodes) {
        if (vis[i])
            continue;
        bool ok = false;
        for (auto j: g[i]) {
            if (vis[j])
                continue;
            minans += 2, vis[j] = 1, ok = true;
            swap(pos[i], pos[j]);
            break;
        }
        if (!ok) {
            minans += 2;
            swap(pos[i], pos[par[i]]);
            if (i == par[i])
                swap(pos[i], pos[g[i].back()]);
        }
        vis[i] = 1;
    }
    for (int i = 1; i <= n; ++i)
        mnc[pos[i]] = i;

    // Maximum
    getsz(c, c);
    for (int i = 1; i <= n; ++i) {
        for (auto j: g[i]) {
            int hi = i, lo = j;
            if (dep[i] > dep[j])
                swap(lo, hi);
            maxans += min(n - sz[lo] + 1, sz[lo]);
        }
    }
    dfs(c, c, cnt );
    sort(nodes.begin(), nodes.end(), [](int a, int b) {
        return group[a] < group[b];
    });
    memset(vis, 0, sizeof vis);
    for (int i = 1; i <= n; ++i)
        pos[i] = i;
    int j = 1;
    for (int i = 1; i < n; ++i) {
        if (vis[nodes[i]] || vis[nodes[j]])
            continue;
        j = max(j, i);
        while (group[nodes[i]] == group[nodes[j]])
            ++j;
        if (j >= n)
            break;
        swap(pos[nodes[i]], pos[nodes[j]]);
        vis[nodes[i]] = 1, vis[nodes[j]] = 1;
        ++j;
    }
    if (n % 2) 
        swap(pos[nodes.back()], pos[c]);
    else {
        for (int i = 1; i < n; ++i) {
            if (!vis[nodes[i]]) {
                swap(pos[c], pos[nodes[i]]);
            }
        }
    }
    for (int i = 1; i <= n; ++i)
        mxc[pos[i]] = i;

    cout << minans << ' ' << maxans << '\n';
    for (int i = 1; i <= n; ++i)
        cout << mnc[i] << ' ';
    cout << '\n';
    for (int i = 1; i <= n; ++i)
        cout << mxc[i] << ' ';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
3 Correct 2 ms 3412 KB Output is correct
4 Partially correct 2 ms 3412 KB Partially correct
5 Correct 2 ms 3420 KB Output is correct
6 Partially correct 2 ms 3412 KB Partially correct
7 Correct 2 ms 3412 KB Output is correct
8 Partially correct 2 ms 3452 KB Partially correct
9 Correct 2 ms 3412 KB Output is correct
10 Correct 2 ms 3412 KB Output is correct
11 Correct 2 ms 3412 KB Output is correct
12 Correct 2 ms 3412 KB Output is correct
13 Partially correct 2 ms 3412 KB Partially correct
14 Correct 2 ms 3412 KB Output is correct
15 Correct 2 ms 3412 KB Output is correct
16 Correct 2 ms 3412 KB Output is correct
17 Correct 2 ms 3448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Partially correct 3 ms 3524 KB Partially correct
2 Partially correct 2 ms 3540 KB Partially correct
3 Partially correct 2 ms 3540 KB Partially correct
4 Partially correct 3 ms 3540 KB Partially correct
5 Partially correct 2 ms 3540 KB Partially correct
6 Partially correct 2 ms 3584 KB Partially correct
7 Correct 2 ms 3584 KB Output is correct
8 Partially correct 2 ms 3540 KB Partially correct
9 Partially correct 2 ms 3540 KB Partially correct
10 Correct 2 ms 3540 KB Output is correct
11 Partially correct 2 ms 3540 KB Partially correct
12 Correct 3 ms 3540 KB Output is correct
13 Partially correct 2 ms 3540 KB Partially correct
14 Partially correct 2 ms 3540 KB Partially correct
15 Partially correct 2 ms 3540 KB Partially correct
16 Partially correct 2 ms 3540 KB Partially correct
17 Partially correct 2 ms 3540 KB Partially correct
18 Correct 2 ms 3580 KB Output is correct
19 Partially correct 2 ms 3540 KB Partially correct
20 Partially correct 2 ms 3540 KB Partially correct
21 Partially correct 2 ms 3540 KB Partially correct
22 Partially correct 2 ms 3540 KB Partially correct
23 Partially correct 2 ms 3540 KB Partially correct
24 Correct 3 ms 3584 KB Output is correct
25 Correct 2 ms 3540 KB Output is correct
26 Partially correct 3 ms 3540 KB Partially correct
27 Partially correct 2 ms 3540 KB Partially correct
28 Partially correct 2 ms 3540 KB Partially correct
29 Partially correct 3 ms 3540 KB Partially correct
30 Partially correct 2 ms 3540 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
3 Correct 2 ms 3412 KB Output is correct
4 Partially correct 2 ms 3412 KB Partially correct
5 Correct 2 ms 3420 KB Output is correct
6 Partially correct 2 ms 3412 KB Partially correct
7 Correct 2 ms 3412 KB Output is correct
8 Partially correct 2 ms 3452 KB Partially correct
9 Correct 2 ms 3412 KB Output is correct
10 Correct 2 ms 3412 KB Output is correct
11 Correct 2 ms 3412 KB Output is correct
12 Correct 2 ms 3412 KB Output is correct
13 Partially correct 2 ms 3412 KB Partially correct
14 Correct 2 ms 3412 KB Output is correct
15 Correct 2 ms 3412 KB Output is correct
16 Correct 2 ms 3412 KB Output is correct
17 Correct 2 ms 3448 KB Output is correct
18 Partially correct 3 ms 3524 KB Partially correct
19 Partially correct 2 ms 3540 KB Partially correct
20 Partially correct 2 ms 3540 KB Partially correct
21 Partially correct 3 ms 3540 KB Partially correct
22 Partially correct 2 ms 3540 KB Partially correct
23 Partially correct 2 ms 3584 KB Partially correct
24 Correct 2 ms 3584 KB Output is correct
25 Partially correct 2 ms 3540 KB Partially correct
26 Partially correct 2 ms 3540 KB Partially correct
27 Correct 2 ms 3540 KB Output is correct
28 Partially correct 2 ms 3540 KB Partially correct
29 Correct 3 ms 3540 KB Output is correct
30 Partially correct 2 ms 3540 KB Partially correct
31 Partially correct 2 ms 3540 KB Partially correct
32 Partially correct 2 ms 3540 KB Partially correct
33 Partially correct 2 ms 3540 KB Partially correct
34 Partially correct 2 ms 3540 KB Partially correct
35 Correct 2 ms 3580 KB Output is correct
36 Partially correct 2 ms 3540 KB Partially correct
37 Partially correct 2 ms 3540 KB Partially correct
38 Partially correct 2 ms 3540 KB Partially correct
39 Partially correct 2 ms 3540 KB Partially correct
40 Partially correct 2 ms 3540 KB Partially correct
41 Correct 3 ms 3584 KB Output is correct
42 Correct 2 ms 3540 KB Output is correct
43 Partially correct 3 ms 3540 KB Partially correct
44 Partially correct 2 ms 3540 KB Partially correct
45 Partially correct 2 ms 3540 KB Partially correct
46 Partially correct 3 ms 3540 KB Partially correct
47 Partially correct 2 ms 3540 KB Partially correct
48 Partially correct 84 ms 14864 KB Partially correct
49 Partially correct 84 ms 15932 KB Partially correct
50 Partially correct 80 ms 15960 KB Partially correct
51 Partially correct 58 ms 13172 KB Partially correct
52 Partially correct 90 ms 15740 KB Partially correct
53 Partially correct 69 ms 14524 KB Partially correct
54 Correct 40 ms 12232 KB Output is correct
55 Correct 110 ms 21312 KB Output is correct
56 Correct 106 ms 18760 KB Output is correct
57 Partially correct 99 ms 17604 KB Partially correct
58 Correct 99 ms 16828 KB Output is correct
59 Partially correct 81 ms 16064 KB Partially correct
60 Correct 59 ms 15972 KB Output is correct
61 Partially correct 65 ms 16056 KB Partially correct
62 Partially correct 62 ms 16236 KB Partially correct
63 Partially correct 58 ms 15424 KB Partially correct
64 Partially correct 79 ms 16280 KB Partially correct
65 Partially correct 65 ms 16392 KB Partially correct
66 Partially correct 58 ms 15464 KB Partially correct
67 Partially correct 42 ms 12996 KB Partially correct
68 Partially correct 52 ms 14452 KB Partially correct
69 Partially correct 72 ms 16448 KB Partially correct
70 Partially correct 56 ms 15628 KB Partially correct
71 Partially correct 41 ms 12552 KB Partially correct
72 Partially correct 46 ms 13648 KB Partially correct
73 Partially correct 76 ms 16488 KB Partially correct
74 Partially correct 66 ms 15340 KB Partially correct
75 Partially correct 86 ms 16428 KB Partially correct
76 Partially correct 83 ms 15800 KB Partially correct
77 Partially correct 72 ms 15880 KB Partially correct
78 Partially correct 46 ms 11984 KB Partially correct
79 Partially correct 51 ms 13252 KB Partially correct
80 Partially correct 99 ms 16420 KB Partially correct
81 Partially correct 70 ms 16024 KB Partially correct
82 Partially correct 68 ms 16024 KB Partially correct