답안 #940745

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
940745 2024-03-07T14:50:50 Z Pring Village (BOI20_village) C++17
100 / 100
149 ms 23680 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];

    bool DFS(int id, int par) {
        debug(id, par);
        vector<int> odd;
        for (auto &i : edge[id]) {
            if (i == par) continue;
            if (DFS(i, id)) odd.push_back(i);
        }
        // for (auto &i : odd) cout << i << ' ';
        // cout << endl;
        if (odd.empty()) return 1;
        if (odd.size() >= 2) {
            ans += 2 * odd.size();
            odd.push_back(id);
            odd.push_back(odd[0]);
            FOR(i, 1, odd.size()) bind[odd[i - 1]] = odd[i];
        } else {
            ans += 2;
            bind[id] = odd[0];
            bind[odd[0]] = id;
        }
        return 0;
    }

    void DO() {
        if (DFS(1, 0)) {
            ans += 2;
            bind[1] = 1;
            int a = edge[1][0];
            swap(bind[1], bind[a]);
        }
        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;
        return w;
    }

    void DO() {
        ans = GET_ANS(1, 0);
        vector<int> v = CD(1);
        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);
    }
    NSB::DO();
    NSA::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 'bool NSA::DFS(long long int, long long int)':
Village.cpp:11:20: warning: statement has no effect [-Wunused-value]
   11 | #define debug(...) 39
      |                    ^~
Village.cpp:30:9: note: in expansion of macro 'debug'
   30 |         debug(id, par);
      |         ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6488 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 1 ms 6556 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 2 ms 6552 KB Output is correct
12 Correct 2 ms 6488 KB Output is correct
13 Correct 2 ms 6488 KB Output is correct
14 Correct 2 ms 6492 KB Output is correct
15 Correct 2 ms 6492 KB Output is correct
16 Correct 2 ms 6552 KB Output is correct
17 Correct 2 ms 6488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6744 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 3 ms 6488 KB Output is correct
7 Correct 3 ms 6488 KB Output is correct
8 Correct 2 ms 6492 KB Output is correct
9 Correct 3 ms 6488 KB Output is correct
10 Correct 2 ms 6488 KB Output is correct
11 Correct 2 ms 6488 KB Output is correct
12 Correct 2 ms 6744 KB Output is correct
13 Correct 3 ms 6492 KB Output is correct
14 Correct 2 ms 6492 KB Output is correct
15 Correct 2 ms 6492 KB Output is correct
16 Correct 2 ms 6488 KB Output is correct
17 Correct 2 ms 6492 KB Output is correct
18 Correct 2 ms 6492 KB Output is correct
19 Correct 2 ms 6492 KB Output is correct
20 Correct 2 ms 6492 KB Output is correct
21 Correct 2 ms 6492 KB Output is correct
22 Correct 2 ms 6492 KB Output is correct
23 Correct 2 ms 6492 KB Output is correct
24 Correct 2 ms 6492 KB Output is correct
25 Correct 2 ms 6492 KB Output is correct
26 Correct 3 ms 6492 KB Output is correct
27 Correct 3 ms 6492 KB Output is correct
28 Correct 2 ms 6492 KB Output is correct
29 Correct 3 ms 6628 KB Output is correct
30 Correct 2 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6488 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 1 ms 6556 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 2 ms 6552 KB Output is correct
12 Correct 2 ms 6488 KB Output is correct
13 Correct 2 ms 6488 KB Output is correct
14 Correct 2 ms 6492 KB Output is correct
15 Correct 2 ms 6492 KB Output is correct
16 Correct 2 ms 6552 KB Output is correct
17 Correct 2 ms 6488 KB Output is correct
18 Correct 2 ms 6744 KB Output is correct
19 Correct 2 ms 6492 KB Output is correct
20 Correct 2 ms 6492 KB Output is correct
21 Correct 2 ms 6492 KB Output is correct
22 Correct 2 ms 6492 KB Output is correct
23 Correct 3 ms 6488 KB Output is correct
24 Correct 3 ms 6488 KB Output is correct
25 Correct 2 ms 6492 KB Output is correct
26 Correct 3 ms 6488 KB Output is correct
27 Correct 2 ms 6488 KB Output is correct
28 Correct 2 ms 6488 KB Output is correct
29 Correct 2 ms 6744 KB Output is correct
30 Correct 3 ms 6492 KB Output is correct
31 Correct 2 ms 6492 KB Output is correct
32 Correct 2 ms 6492 KB Output is correct
33 Correct 2 ms 6488 KB Output is correct
34 Correct 2 ms 6492 KB Output is correct
35 Correct 2 ms 6492 KB Output is correct
36 Correct 2 ms 6492 KB Output is correct
37 Correct 2 ms 6492 KB Output is correct
38 Correct 2 ms 6492 KB Output is correct
39 Correct 2 ms 6492 KB Output is correct
40 Correct 2 ms 6492 KB Output is correct
41 Correct 2 ms 6492 KB Output is correct
42 Correct 2 ms 6492 KB Output is correct
43 Correct 3 ms 6492 KB Output is correct
44 Correct 3 ms 6492 KB Output is correct
45 Correct 2 ms 6492 KB Output is correct
46 Correct 3 ms 6628 KB Output is correct
47 Correct 2 ms 6492 KB Output is correct
48 Correct 69 ms 14172 KB Output is correct
49 Correct 78 ms 14776 KB Output is correct
50 Correct 81 ms 14812 KB Output is correct
51 Correct 60 ms 13160 KB Output is correct
52 Correct 79 ms 14452 KB Output is correct
53 Correct 81 ms 14236 KB Output is correct
54 Correct 58 ms 14580 KB Output is correct
55 Correct 139 ms 23680 KB Output is correct
56 Correct 149 ms 19440 KB Output is correct
57 Correct 120 ms 17988 KB Output is correct
58 Correct 113 ms 16640 KB Output is correct
59 Correct 85 ms 15820 KB Output is correct
60 Correct 45 ms 19520 KB Output is correct
61 Correct 47 ms 16588 KB Output is correct
62 Correct 58 ms 15220 KB Output is correct
63 Correct 61 ms 15080 KB Output is correct
64 Correct 58 ms 14668 KB Output is correct
65 Correct 57 ms 15560 KB Output is correct
66 Correct 47 ms 14576 KB Output is correct
67 Correct 35 ms 14544 KB Output is correct
68 Correct 43 ms 13624 KB Output is correct
69 Correct 52 ms 15048 KB Output is correct
70 Correct 60 ms 14824 KB Output is correct
71 Correct 34 ms 12488 KB Output is correct
72 Correct 43 ms 13144 KB Output is correct
73 Correct 51 ms 15392 KB Output is correct
74 Correct 59 ms 14332 KB Output is correct
75 Correct 84 ms 15400 KB Output is correct
76 Correct 76 ms 14532 KB Output is correct
77 Correct 55 ms 14832 KB Output is correct
78 Correct 39 ms 11984 KB Output is correct
79 Correct 52 ms 12744 KB Output is correct
80 Correct 87 ms 15500 KB Output is correct
81 Correct 51 ms 14532 KB Output is correct
82 Correct 49 ms 14280 KB Output is correct