답안 #967871

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
967871 2024-04-23T03:23:54 Z Pring 자매 도시 (APIO20_swap) C++17
100 / 100
307 ms 60680 KB
#include <bits/stdc++.h>
#include "swap.h"
using namespace std;

#ifdef MIKU
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 fs first
#define sc second
#define mp make_pair
#define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++)
using ll = long long;
typedef pair<int, int> pii;
typedef array<int, 3> p3i;

namespace {
    const int MXN = 300005, LG = 19;
    int n, m;
    vector<p3i> edges;
    int deg[MXN];

    struct DSU {
        int p[MXN];
        void init(int n) {
            fill(p, p + n, -1);
        }
        int find(int x) {
            return (p[x] < 0 ? x : p[x] = find(p[x]));
        }
        void onion(int rt, int x) {
            p[x] = rt;
        }
    } dsu;

    namespace TREE {
        vector<int> edge[MXN];
        int val[MXN];
        bitset<MXN> tag;
        int p[LG][MXN], d[MXN];

        void ADD_EDGE(int x, int y) {
            edge[x].push_back(y);
            edge[y].push_back(x);
        }

        void CONSTRUCT() {
            sort(edges.begin(), edges.end());
            dsu.init(n + m);
            FOR(i, 0, m) {
                auto [w, u, v] = edges[i];
                deg[u]++;
                deg[v]++;
                int uu = dsu.find(u), vv = dsu.find(v);
                int now = i + n;
                debug(now, u, v, uu, vv);
                val[now] = w;
                if (uu == vv) {
                    tag[now] = true;
                    dsu.onion(now, uu);
                    ADD_EDGE(now, uu);
                } else {
                    tag[now] = ((tag[uu]) | (tag[vv]) | (deg[u] >= 3) | (deg[v] >= 3));
                    dsu.onion(now, uu);
                    dsu.onion(now, vv);
                    ADD_EDGE(now, uu);
                    ADD_EDGE(now, vv);
                }
            }
        }

        void DFS(int id, int par, int dep) {
            p[0][id] = par;
            d[id] = dep;
            for (auto &i : edge[id]) {
                if (i == par) continue;
                DFS(i, id, dep + 1);
            }
        }

        void BUILD() {
            CONSTRUCT();
            DFS(n + m - 1, n + m - 1, 0);
            // FOR(i, 0, n + m) {
            //     for (auto &j : edge[i]) cout << j << ' ';
            //     cout << '\n';
            // }
            // FOR(i, 0, n + m) cout << p[0][i] << ' ';
            // cout << '\n';
            // FOR(i, 0, n + m) cout << tag[i] << ' ';
            // cout << '\n';
            // cout << "---\n";
            FOR(w, 1, LG) {
                FOR(i, 0, n + m) {
                    p[w][i] = p[w - 1][p[w - 1][i]];
                }
            }
        }
        
        int LEAP(int x, int k) {
            FOR(w, 0, LG) if (k & (1 << w)) x = p[w][x];
            return x;
        }

        int LCA(int x, int y) {
            if (d[x] < d[y]) swap(x, y);
            x = LEAP(x, d[x] - d[y]);
            if (x == y) return x;
            for (int w = LG - 1; w >= 0; w--) {
                if (p[w][x] == p[w][y]) continue;
                x = p[w][x];
                y = p[w][y];
            }
            return p[0][x];
        }

        int BSH(int x) {
            if (tag[x]) return x;
            for (int w = LG - 1; w >= 0; w--) {
                if (tag[p[w][x]]) continue;
                x = p[w][x];
            }
            return p[0][x];
        }

        int SOLVE(int x, int y) {
            int lca = LCA(x, y);
            debug(x, y, lca);
            int ans = BSH(lca);
            if (!tag[ans]) return -1;
            return val[ans];
        }
    }
}

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
    n = N;
    m = M;
    FOR(i, 0, m) edges.push_back(p3i{W[i], U[i], V[i]});
    TREE::BUILD();
}

int getMinimumFuelCapacity(int X, int Y) {
    return TREE::SOLVE(X, Y);
}

Compilation message

swap.cpp: In function 'void {anonymous}::TREE::CONSTRUCT()':
swap.cpp:12:20: warning: statement has no effect [-Wunused-value]
   12 | #define debug(...) 39
      |                    ^~
swap.cpp:62:17: note: in expansion of macro 'debug'
   62 |                 debug(now, u, v, uu, vv);
      |                 ^~~~~
swap.cpp: In function 'int {anonymous}::TREE::SOLVE(int, int)':
swap.cpp:12:20: warning: statement has no effect [-Wunused-value]
   12 | #define debug(...) 39
      |                    ^~
swap.cpp:134:13: note: in expansion of macro 'debug'
  134 |             debug(x, y, lca);
      |             ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 33232 KB Output is correct
2 Correct 5 ms 33116 KB Output is correct
3 Correct 5 ms 33116 KB Output is correct
4 Correct 5 ms 33116 KB Output is correct
5 Correct 5 ms 33216 KB Output is correct
6 Correct 6 ms 33372 KB Output is correct
7 Correct 6 ms 33216 KB Output is correct
8 Correct 5 ms 33384 KB Output is correct
9 Correct 61 ms 42952 KB Output is correct
10 Correct 75 ms 44916 KB Output is correct
11 Correct 77 ms 44736 KB Output is correct
12 Correct 74 ms 45508 KB Output is correct
13 Correct 63 ms 46808 KB Output is correct
14 Correct 65 ms 43204 KB Output is correct
15 Correct 184 ms 49252 KB Output is correct
16 Correct 171 ms 48820 KB Output is correct
17 Correct 201 ms 49708 KB Output is correct
18 Correct 209 ms 51036 KB Output is correct
19 Correct 77 ms 40208 KB Output is correct
20 Correct 175 ms 50304 KB Output is correct
21 Correct 177 ms 50308 KB Output is correct
22 Correct 175 ms 51080 KB Output is correct
23 Correct 215 ms 52536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 33232 KB Output is correct
2 Correct 5 ms 33116 KB Output is correct
3 Correct 177 ms 52636 KB Output is correct
4 Correct 216 ms 52788 KB Output is correct
5 Correct 167 ms 52780 KB Output is correct
6 Correct 181 ms 52776 KB Output is correct
7 Correct 211 ms 53032 KB Output is correct
8 Correct 192 ms 52296 KB Output is correct
9 Correct 163 ms 52712 KB Output is correct
10 Correct 159 ms 52124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 33232 KB Output is correct
2 Correct 5 ms 33116 KB Output is correct
3 Correct 5 ms 33116 KB Output is correct
4 Correct 5 ms 33116 KB Output is correct
5 Correct 5 ms 33216 KB Output is correct
6 Correct 6 ms 33372 KB Output is correct
7 Correct 6 ms 33216 KB Output is correct
8 Correct 5 ms 33384 KB Output is correct
9 Correct 5 ms 33116 KB Output is correct
10 Correct 5 ms 33372 KB Output is correct
11 Correct 6 ms 33372 KB Output is correct
12 Correct 5 ms 33372 KB Output is correct
13 Correct 5 ms 33352 KB Output is correct
14 Correct 5 ms 33372 KB Output is correct
15 Correct 6 ms 33372 KB Output is correct
16 Correct 5 ms 33368 KB Output is correct
17 Correct 5 ms 33372 KB Output is correct
18 Correct 5 ms 33372 KB Output is correct
19 Correct 5 ms 33252 KB Output is correct
20 Correct 5 ms 33372 KB Output is correct
21 Correct 5 ms 33624 KB Output is correct
22 Correct 5 ms 33280 KB Output is correct
23 Correct 5 ms 33344 KB Output is correct
24 Correct 5 ms 33372 KB Output is correct
25 Correct 7 ms 33372 KB Output is correct
26 Correct 6 ms 33372 KB Output is correct
27 Correct 5 ms 33244 KB Output is correct
28 Correct 6 ms 33372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 33116 KB Output is correct
2 Correct 5 ms 33232 KB Output is correct
3 Correct 5 ms 33116 KB Output is correct
4 Correct 5 ms 33116 KB Output is correct
5 Correct 5 ms 33116 KB Output is correct
6 Correct 5 ms 33216 KB Output is correct
7 Correct 6 ms 33372 KB Output is correct
8 Correct 6 ms 33216 KB Output is correct
9 Correct 5 ms 33384 KB Output is correct
10 Correct 61 ms 42952 KB Output is correct
11 Correct 75 ms 44916 KB Output is correct
12 Correct 77 ms 44736 KB Output is correct
13 Correct 74 ms 45508 KB Output is correct
14 Correct 63 ms 46808 KB Output is correct
15 Correct 5 ms 33372 KB Output is correct
16 Correct 6 ms 33372 KB Output is correct
17 Correct 5 ms 33372 KB Output is correct
18 Correct 5 ms 33352 KB Output is correct
19 Correct 5 ms 33372 KB Output is correct
20 Correct 6 ms 33372 KB Output is correct
21 Correct 5 ms 33368 KB Output is correct
22 Correct 5 ms 33372 KB Output is correct
23 Correct 5 ms 33372 KB Output is correct
24 Correct 5 ms 33252 KB Output is correct
25 Correct 5 ms 33372 KB Output is correct
26 Correct 5 ms 33624 KB Output is correct
27 Correct 5 ms 33280 KB Output is correct
28 Correct 5 ms 33344 KB Output is correct
29 Correct 5 ms 33372 KB Output is correct
30 Correct 7 ms 33372 KB Output is correct
31 Correct 6 ms 33372 KB Output is correct
32 Correct 5 ms 33244 KB Output is correct
33 Correct 6 ms 33372 KB Output is correct
34 Correct 14 ms 34844 KB Output is correct
35 Correct 93 ms 45256 KB Output is correct
36 Correct 72 ms 45260 KB Output is correct
37 Correct 75 ms 45116 KB Output is correct
38 Correct 76 ms 44976 KB Output is correct
39 Correct 79 ms 44996 KB Output is correct
40 Correct 73 ms 44124 KB Output is correct
41 Correct 77 ms 45524 KB Output is correct
42 Correct 86 ms 45292 KB Output is correct
43 Correct 61 ms 47064 KB Output is correct
44 Correct 107 ms 45652 KB Output is correct
45 Correct 104 ms 51388 KB Output is correct
46 Correct 83 ms 45304 KB Output is correct
47 Correct 76 ms 45392 KB Output is correct
48 Correct 79 ms 47048 KB Output is correct
49 Correct 79 ms 52204 KB Output is correct
50 Correct 65 ms 47208 KB Output is correct
51 Correct 98 ms 49224 KB Output is correct
52 Correct 136 ms 53696 KB Output is correct
53 Correct 123 ms 54364 KB Output is correct
54 Correct 138 ms 57008 KB Output is correct
55 Correct 63 ms 46928 KB Output is correct
56 Correct 125 ms 54540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 33232 KB Output is correct
2 Correct 5 ms 33116 KB Output is correct
3 Correct 5 ms 33116 KB Output is correct
4 Correct 5 ms 33116 KB Output is correct
5 Correct 5 ms 33216 KB Output is correct
6 Correct 6 ms 33372 KB Output is correct
7 Correct 6 ms 33216 KB Output is correct
8 Correct 5 ms 33384 KB Output is correct
9 Correct 61 ms 42952 KB Output is correct
10 Correct 75 ms 44916 KB Output is correct
11 Correct 77 ms 44736 KB Output is correct
12 Correct 74 ms 45508 KB Output is correct
13 Correct 63 ms 46808 KB Output is correct
14 Correct 65 ms 43204 KB Output is correct
15 Correct 184 ms 49252 KB Output is correct
16 Correct 171 ms 48820 KB Output is correct
17 Correct 201 ms 49708 KB Output is correct
18 Correct 209 ms 51036 KB Output is correct
19 Correct 177 ms 52636 KB Output is correct
20 Correct 216 ms 52788 KB Output is correct
21 Correct 167 ms 52780 KB Output is correct
22 Correct 181 ms 52776 KB Output is correct
23 Correct 211 ms 53032 KB Output is correct
24 Correct 192 ms 52296 KB Output is correct
25 Correct 163 ms 52712 KB Output is correct
26 Correct 159 ms 52124 KB Output is correct
27 Correct 5 ms 33372 KB Output is correct
28 Correct 6 ms 33372 KB Output is correct
29 Correct 5 ms 33372 KB Output is correct
30 Correct 5 ms 33352 KB Output is correct
31 Correct 5 ms 33372 KB Output is correct
32 Correct 6 ms 33372 KB Output is correct
33 Correct 5 ms 33368 KB Output is correct
34 Correct 5 ms 33372 KB Output is correct
35 Correct 5 ms 33372 KB Output is correct
36 Correct 14 ms 34844 KB Output is correct
37 Correct 93 ms 45256 KB Output is correct
38 Correct 72 ms 45260 KB Output is correct
39 Correct 75 ms 45116 KB Output is correct
40 Correct 76 ms 44976 KB Output is correct
41 Correct 79 ms 44996 KB Output is correct
42 Correct 73 ms 44124 KB Output is correct
43 Correct 77 ms 45524 KB Output is correct
44 Correct 86 ms 45292 KB Output is correct
45 Correct 61 ms 47064 KB Output is correct
46 Correct 107 ms 45652 KB Output is correct
47 Correct 20 ms 35196 KB Output is correct
48 Correct 193 ms 50236 KB Output is correct
49 Correct 169 ms 50224 KB Output is correct
50 Correct 181 ms 50220 KB Output is correct
51 Correct 196 ms 49996 KB Output is correct
52 Correct 172 ms 49376 KB Output is correct
53 Correct 144 ms 47156 KB Output is correct
54 Correct 176 ms 51004 KB Output is correct
55 Correct 206 ms 50140 KB Output is correct
56 Correct 214 ms 51676 KB Output is correct
57 Correct 189 ms 51200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 33116 KB Output is correct
2 Correct 5 ms 33232 KB Output is correct
3 Correct 5 ms 33116 KB Output is correct
4 Correct 5 ms 33116 KB Output is correct
5 Correct 5 ms 33116 KB Output is correct
6 Correct 5 ms 33216 KB Output is correct
7 Correct 6 ms 33372 KB Output is correct
8 Correct 6 ms 33216 KB Output is correct
9 Correct 5 ms 33384 KB Output is correct
10 Correct 61 ms 42952 KB Output is correct
11 Correct 75 ms 44916 KB Output is correct
12 Correct 77 ms 44736 KB Output is correct
13 Correct 74 ms 45508 KB Output is correct
14 Correct 63 ms 46808 KB Output is correct
15 Correct 65 ms 43204 KB Output is correct
16 Correct 184 ms 49252 KB Output is correct
17 Correct 171 ms 48820 KB Output is correct
18 Correct 201 ms 49708 KB Output is correct
19 Correct 209 ms 51036 KB Output is correct
20 Correct 177 ms 52636 KB Output is correct
21 Correct 216 ms 52788 KB Output is correct
22 Correct 167 ms 52780 KB Output is correct
23 Correct 181 ms 52776 KB Output is correct
24 Correct 211 ms 53032 KB Output is correct
25 Correct 192 ms 52296 KB Output is correct
26 Correct 163 ms 52712 KB Output is correct
27 Correct 159 ms 52124 KB Output is correct
28 Correct 5 ms 33372 KB Output is correct
29 Correct 6 ms 33372 KB Output is correct
30 Correct 5 ms 33372 KB Output is correct
31 Correct 5 ms 33352 KB Output is correct
32 Correct 5 ms 33372 KB Output is correct
33 Correct 6 ms 33372 KB Output is correct
34 Correct 5 ms 33368 KB Output is correct
35 Correct 5 ms 33372 KB Output is correct
36 Correct 5 ms 33372 KB Output is correct
37 Correct 14 ms 34844 KB Output is correct
38 Correct 93 ms 45256 KB Output is correct
39 Correct 72 ms 45260 KB Output is correct
40 Correct 75 ms 45116 KB Output is correct
41 Correct 76 ms 44976 KB Output is correct
42 Correct 79 ms 44996 KB Output is correct
43 Correct 73 ms 44124 KB Output is correct
44 Correct 77 ms 45524 KB Output is correct
45 Correct 86 ms 45292 KB Output is correct
46 Correct 61 ms 47064 KB Output is correct
47 Correct 107 ms 45652 KB Output is correct
48 Correct 20 ms 35196 KB Output is correct
49 Correct 193 ms 50236 KB Output is correct
50 Correct 169 ms 50224 KB Output is correct
51 Correct 181 ms 50220 KB Output is correct
52 Correct 196 ms 49996 KB Output is correct
53 Correct 172 ms 49376 KB Output is correct
54 Correct 144 ms 47156 KB Output is correct
55 Correct 176 ms 51004 KB Output is correct
56 Correct 206 ms 50140 KB Output is correct
57 Correct 214 ms 51676 KB Output is correct
58 Correct 189 ms 51200 KB Output is correct
59 Correct 77 ms 40208 KB Output is correct
60 Correct 175 ms 50304 KB Output is correct
61 Correct 177 ms 50308 KB Output is correct
62 Correct 175 ms 51080 KB Output is correct
63 Correct 215 ms 52536 KB Output is correct
64 Correct 5 ms 33252 KB Output is correct
65 Correct 5 ms 33372 KB Output is correct
66 Correct 5 ms 33624 KB Output is correct
67 Correct 5 ms 33280 KB Output is correct
68 Correct 5 ms 33344 KB Output is correct
69 Correct 5 ms 33372 KB Output is correct
70 Correct 7 ms 33372 KB Output is correct
71 Correct 6 ms 33372 KB Output is correct
72 Correct 5 ms 33244 KB Output is correct
73 Correct 6 ms 33372 KB Output is correct
74 Correct 104 ms 51388 KB Output is correct
75 Correct 83 ms 45304 KB Output is correct
76 Correct 76 ms 45392 KB Output is correct
77 Correct 79 ms 47048 KB Output is correct
78 Correct 79 ms 52204 KB Output is correct
79 Correct 65 ms 47208 KB Output is correct
80 Correct 98 ms 49224 KB Output is correct
81 Correct 136 ms 53696 KB Output is correct
82 Correct 123 ms 54364 KB Output is correct
83 Correct 138 ms 57008 KB Output is correct
84 Correct 63 ms 46928 KB Output is correct
85 Correct 125 ms 54540 KB Output is correct
86 Correct 61 ms 40528 KB Output is correct
87 Correct 200 ms 50120 KB Output is correct
88 Correct 156 ms 50116 KB Output is correct
89 Correct 181 ms 51420 KB Output is correct
90 Correct 158 ms 55828 KB Output is correct
91 Correct 162 ms 54760 KB Output is correct
92 Correct 202 ms 53700 KB Output is correct
93 Correct 214 ms 57132 KB Output is correct
94 Correct 284 ms 59432 KB Output is correct
95 Correct 307 ms 60680 KB Output is correct
96 Correct 227 ms 51676 KB Output is correct
97 Correct 248 ms 56008 KB Output is correct