답안 #940711

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
940711 2024-03-07T13:54:46 Z Pring Village (BOI20_village) C++14
50 / 100
136 ms 24740 KB
#include <bits/stdc++.h>
using namespace std;

#ifdef MIKU
const string dbmc = "\033[1;38;2;57;197;187m", dbrs = "\033[0m";
#define debug(x...) cout << dbmc << '[' << #x << "] : ", dout(x)
void dout() { cout << dbrs << endl; }
template <typename T, typename ...U>
void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); }
#else
#define debug(...) 39
#endif

#define int long long
#define fs first
#define sc second
#define mp make_pair
#define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++)
typedef pair<int, int> pii;

const int MXN = 100005;
int n;
vector<int> edge[MXN];

namespace NSA {
    int ans, va[MXN], sz[MXN];
    int bind[MXN];

    void DFS(int id, int par) {
        sz[id] = 1;
        vector<int> odd;
        for (auto &i : edge[id]) {
            if (i == par) continue;
            DFS(i, id);
            sz[id] += sz[i];
            if (sz[i] & 1) odd.push_back(i);
        }
        odd.push_back(id);
        for (int i = 1; i < odd.size(); i += 2) {
            bind[odd[i - 1]] = odd[i];
            bind[odd[i]] = odd[i - 1];
            ans += (odd[i] == id ? 2 : 4);
        }
    }

    void DO() {
        int rt = [&]() -> int {
            FOR(i, 1, n + 1) if (edge[i].size() == 1) return i;
            return 0;
        }();
        DFS(rt, 0);
        if (sz[rt] & 1) {
            bind[rt] = rt;
            swap(bind[rt], bind[edge[rt][0]]);
            ans += 2;
        }
        copy(bind + 1, bind + n + 1, va + 1);
    }
}

namespace NSB {
    int ans, va[MXN];
    int sz[MXN];
    bitset<MXN> ban;

    int GET_ANS(int id, int par) {
        sz[id] = 1;
        int ans = 0;
        for (auto &i : edge[id]) {
            if (i == par) continue;
            ans += GET_ANS(i, id);
            sz[id] += sz[i];
        }
        return (ans + min(n - sz[id], sz[id]) * 2);
    }

    void GET_SZ(int id, int par) {
        sz[id] = 1;
        for (auto &i : edge[id]) {
            if (i == par) continue;
            if (ban[i]) continue;
            GET_SZ(i, id);
            sz[id] += sz[i];
        }
    }

    int GET_CD(int id, int par, int N) {
        for (auto &i : edge[id]) {
            if (i == par) continue;
            if (ban[i]) continue;
            if (sz[i] > N / 2) return GET_CD(i, id, N);
        }
        return id;
    }

    vector<int> CD(int id) {
        GET_SZ(id, 0);
        if (sz[id] == 1) return vector<int>(1, id);
        vector<int> w(sz[id]), wid;
        for (int i = 0; i < sz[id]; i += 2) wid.push_back(i);
        for (int i = 1; i < sz[id]; i += 2) wid.push_back(i);
        int cd = GET_CD(id, 0, sz[id]);
        vector<vector<int>> v;
        ban[cd] = true;
        for (auto &i : edge[cd]) {
            if (ban[i]) continue;
            v.push_back(CD(i));
        }
        sort(v.begin(), v.end(), [&](vector<int> &a, vector<int> &b) -> bool {
            return a.size() > b.size();
        });
        v.back().push_back(cd);
        int C = 0;
        for (auto &i : v) for (auto &j : i) w[wid[C++]] = j;
        // debug(id);
        // for (auto &i : w) cout << i << ' ';
        // cout << endl;
        return w;
    }

    void DO() {
        ans = GET_ANS(1, 0);
        vector<int> v = CD(1);
        // cout << dbmc;
        // for (auto &i : v) cout << i << ' ';
        // cout << dbrs << endl;
        v.push_back(v[0]);
        FOR(i, 1, n + 1) va[v[i - 1]] = v[i];
    }
}

void miku() {
    int x, y;
    cin >> n;
    FOR(i, 1, n) {
        cin >> x >> y;
        edge[x].push_back(y);
        edge[y].push_back(x);
    }
    NSA::DO();
    NSB::DO();
    cout << NSA::ans << ' ' << NSB::ans << '\n';
    FOR(i, 1, n + 1) cout << NSA::va[i] << ' ';
    cout << '\n';
    FOR(i, 1, n + 1) cout << NSB::va[i] << ' ';
    cout << '\n';
}

int32_t main() {
    cin.tie(0) -> sync_with_stdio(false);
    cin.exceptions(iostream::failbit);
    miku();
    return 0;
}

Compilation message

Village.cpp: In function 'void NSA::DFS(long long int, long long int)':
Village.cpp:39:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |         for (int i = 1; i < odd.size(); i += 2) {
      |                         ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6492 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Partially correct 2 ms 6492 KB Partially correct
5 Correct 2 ms 6492 KB Output is correct
6 Partially correct 2 ms 6492 KB Partially correct
7 Correct 2 ms 6488 KB Output is correct
8 Correct 2 ms 6492 KB Output is correct
9 Correct 2 ms 6492 KB Output is correct
10 Correct 2 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 2 ms 6492 KB Output is correct
13 Partially correct 2 ms 6492 KB Partially correct
14 Correct 2 ms 6492 KB Output is correct
15 Partially correct 2 ms 6564 KB Partially correct
16 Partially correct 2 ms 6488 KB Partially correct
17 Partially correct 2 ms 6492 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Partially correct 2 ms 6492 KB Partially correct
2 Partially correct 2 ms 6492 KB Partially correct
3 Partially correct 2 ms 6492 KB Partially correct
4 Partially correct 3 ms 6488 KB Partially correct
5 Partially correct 3 ms 6488 KB Partially correct
6 Partially correct 2 ms 6492 KB Partially correct
7 Correct 3 ms 6748 KB Output is correct
8 Partially correct 3 ms 6492 KB Partially correct
9 Partially correct 2 ms 6492 KB Partially correct
10 Partially correct 2 ms 6492 KB Partially correct
11 Partially correct 2 ms 6492 KB Partially correct
12 Correct 2 ms 6492 KB Output is correct
13 Correct 2 ms 6492 KB Output is correct
14 Partially correct 2 ms 6492 KB Partially correct
15 Partially correct 3 ms 6492 KB Partially correct
16 Partially correct 2 ms 6492 KB Partially correct
17 Partially correct 2 ms 6668 KB Partially correct
18 Partially correct 2 ms 6492 KB Partially correct
19 Partially correct 3 ms 6496 KB Partially correct
20 Partially correct 2 ms 6492 KB Partially correct
21 Partially correct 2 ms 6492 KB Partially correct
22 Partially correct 2 ms 6492 KB Partially correct
23 Partially correct 2 ms 6492 KB Partially correct
24 Partially correct 3 ms 6492 KB Partially correct
25 Partially correct 2 ms 6488 KB Partially correct
26 Partially correct 3 ms 6492 KB Partially correct
27 Partially correct 2 ms 6492 KB Partially correct
28 Partially correct 2 ms 6488 KB Partially correct
29 Partially correct 3 ms 6488 KB Partially correct
30 Partially correct 3 ms 6488 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6492 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Partially correct 2 ms 6492 KB Partially correct
5 Correct 2 ms 6492 KB Output is correct
6 Partially correct 2 ms 6492 KB Partially correct
7 Correct 2 ms 6488 KB Output is correct
8 Correct 2 ms 6492 KB Output is correct
9 Correct 2 ms 6492 KB Output is correct
10 Correct 2 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 2 ms 6492 KB Output is correct
13 Partially correct 2 ms 6492 KB Partially correct
14 Correct 2 ms 6492 KB Output is correct
15 Partially correct 2 ms 6564 KB Partially correct
16 Partially correct 2 ms 6488 KB Partially correct
17 Partially correct 2 ms 6492 KB Partially correct
18 Partially correct 2 ms 6492 KB Partially correct
19 Partially correct 2 ms 6492 KB Partially correct
20 Partially correct 2 ms 6492 KB Partially correct
21 Partially correct 3 ms 6488 KB Partially correct
22 Partially correct 3 ms 6488 KB Partially correct
23 Partially correct 2 ms 6492 KB Partially correct
24 Correct 3 ms 6748 KB Output is correct
25 Partially correct 3 ms 6492 KB Partially correct
26 Partially correct 2 ms 6492 KB Partially correct
27 Partially correct 2 ms 6492 KB Partially correct
28 Partially correct 2 ms 6492 KB Partially correct
29 Correct 2 ms 6492 KB Output is correct
30 Correct 2 ms 6492 KB Output is correct
31 Partially correct 2 ms 6492 KB Partially correct
32 Partially correct 3 ms 6492 KB Partially correct
33 Partially correct 2 ms 6492 KB Partially correct
34 Partially correct 2 ms 6668 KB Partially correct
35 Partially correct 2 ms 6492 KB Partially correct
36 Partially correct 3 ms 6496 KB Partially correct
37 Partially correct 2 ms 6492 KB Partially correct
38 Partially correct 2 ms 6492 KB Partially correct
39 Partially correct 2 ms 6492 KB Partially correct
40 Partially correct 2 ms 6492 KB Partially correct
41 Partially correct 3 ms 6492 KB Partially correct
42 Partially correct 2 ms 6488 KB Partially correct
43 Partially correct 3 ms 6492 KB Partially correct
44 Partially correct 2 ms 6492 KB Partially correct
45 Partially correct 2 ms 6488 KB Partially correct
46 Partially correct 3 ms 6488 KB Partially correct
47 Partially correct 3 ms 6488 KB Partially correct
48 Partially correct 70 ms 13188 KB Partially correct
49 Partially correct 78 ms 13508 KB Partially correct
50 Partially correct 78 ms 13768 KB Partially correct
51 Partially correct 60 ms 12484 KB Partially correct
52 Partially correct 88 ms 13248 KB Partially correct
53 Partially correct 92 ms 13072 KB Partially correct
54 Correct 61 ms 15844 KB Output is correct
55 Correct 136 ms 24740 KB Output is correct
56 Partially correct 129 ms 19392 KB Partially correct
57 Partially correct 122 ms 16580 KB Partially correct
58 Partially correct 114 ms 16072 KB Partially correct
59 Partially correct 89 ms 14580 KB Partially correct
60 Correct 49 ms 17460 KB Output is correct
61 Partially correct 59 ms 15464 KB Partially correct
62 Partially correct 76 ms 14060 KB Partially correct
63 Partially correct 71 ms 13804 KB Partially correct
64 Partially correct 61 ms 13516 KB Partially correct
65 Partially correct 60 ms 14680 KB Partially correct
66 Partially correct 51 ms 13544 KB Partially correct
67 Partially correct 37 ms 13728 KB Partially correct
68 Partially correct 49 ms 12704 KB Partially correct
69 Partially correct 54 ms 13764 KB Partially correct
70 Partially correct 52 ms 13540 KB Partially correct
71 Partially correct 36 ms 11728 KB Partially correct
72 Partially correct 45 ms 12384 KB Partially correct
73 Partially correct 56 ms 14036 KB Partially correct
74 Partially correct 55 ms 13360 KB Partially correct
75 Partially correct 92 ms 14396 KB Partially correct
76 Correct 77 ms 13252 KB Output is correct
77 Partially correct 66 ms 13800 KB Partially correct
78 Partially correct 41 ms 11320 KB Partially correct
79 Partially correct 45 ms 11980 KB Partially correct
80 Partially correct 103 ms 14356 KB Partially correct
81 Partially correct 57 ms 13516 KB Partially correct
82 Partially correct 53 ms 13188 KB Partially correct