답안 #743180

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
743180 2023-05-17T08:38:45 Z fanwen 자매 도시 (APIO20_swap) C++17
100 / 100
614 ms 46264 KB
#include <bits/stdc++.h>

using namespace std;

#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()

#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define FORE(it, s) for (__typeof(s.begin()) it = (s).begin(); it != (s).end(); ++it)

template <class U, class V> bool maximize(U &A, const V &B) { return (A < B) ? (A = B, true) : false; }
template <class U, class V> bool minimize(U &A, const V &B) { return (A > B) ? (A = B, true) : false; }
const int MAXN = 1e5 + 5;
int N, M, d[MAXN * 3], lab[MAXN * 3], par[22][MAXN * 3], deg[MAXN], weight[MAXN * 3], jump[MAXN * 3];
vector <int> ke[MAXN * 3];
bool is[MAXN * 3];
int find(int u) { return lab[u] == u ? u : lab[u] = find(lab[u]); }

void merge(int u, int v, int w) {
    deg[u]++, deg[v]++;
    bool More2 = (deg[u] > 2 || deg[v] > 2);
    u = find(u), v = find(v);
    if(u == v) {
        if(is[u] == 0) is[u] = 1, weight[u] = w;
        return;
    } 
    N++;
    lab[u] = lab[v] = N;
    ke[N].push_back(u); ke[N].push_back(v);
    is[N] = (More2 || is[u] || is[v]);
    weight[N] = w;
}

void dfs(int u, int pre) {
    if(is[u]) pre = u;
    jump[u] = pre;
    for (auto v : ke[u]) {
        par[0][v] = u;
        FOR(i, 1, 19) par[i][v] = par[i - 1][par[i - 1][v]];
        d[v] = d[u] + 1;
        dfs(v, pre);
    }
}

int lca(int u, int v) {
    
    if(d[u] < d[v]) swap(u, v);

    FORD(i, 19, 0) if(d[par[i][u]] >= d[v]) {
        u = par[i][u];
    }
    
    if(u == v) return u;

    FORD(i, 19, 0) if(par[i][u] != par[i][v]) {
        u = par[i][u];
        v = par[i][v];
    }
    return par[0][u];
}

void init(int n, int m, vector <int> U, vector <int> V, vector <int> W) {
    N = n, M = m;
    vector <int> idx(m);
    iota(lab, lab + N + M + 1, 0);
    iota(ALL(idx), 0);
    sort(ALL(idx), [&](const int &a, const int &b) { return W[a] < W[b]; });
    for (auto i : idx) {
        merge(U[i] + 1, V[i] + 1, W[i]);
    }
    weight[0] = -1; dfs(N, 0);
}

int getMinimumFuelCapacity(int X, int Y) {
    return weight[jump[lca(X + 1, Y + 1)]];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 7508 KB Output is correct
2 Correct 4 ms 7492 KB Output is correct
3 Correct 4 ms 7492 KB Output is correct
4 Correct 4 ms 7500 KB Output is correct
5 Correct 5 ms 7644 KB Output is correct
6 Correct 6 ms 7732 KB Output is correct
7 Correct 5 ms 7764 KB Output is correct
8 Correct 5 ms 7764 KB Output is correct
9 Correct 99 ms 28496 KB Output is correct
10 Correct 118 ms 33292 KB Output is correct
11 Correct 124 ms 32876 KB Output is correct
12 Correct 127 ms 34372 KB Output is correct
13 Correct 113 ms 38296 KB Output is correct
14 Correct 133 ms 28784 KB Output is correct
15 Correct 479 ms 37324 KB Output is correct
16 Correct 586 ms 36596 KB Output is correct
17 Correct 603 ms 38132 KB Output is correct
18 Correct 606 ms 42156 KB Output is correct
19 Correct 155 ms 16796 KB Output is correct
20 Correct 555 ms 38232 KB Output is correct
21 Correct 550 ms 37588 KB Output is correct
22 Correct 534 ms 39384 KB Output is correct
23 Correct 614 ms 43288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 7508 KB Output is correct
2 Correct 4 ms 7492 KB Output is correct
3 Correct 489 ms 44664 KB Output is correct
4 Correct 501 ms 46264 KB Output is correct
5 Correct 461 ms 45508 KB Output is correct
6 Correct 449 ms 46092 KB Output is correct
7 Correct 528 ms 46052 KB Output is correct
8 Correct 482 ms 44600 KB Output is correct
9 Correct 464 ms 45548 KB Output is correct
10 Correct 460 ms 44260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 7508 KB Output is correct
2 Correct 4 ms 7492 KB Output is correct
3 Correct 4 ms 7492 KB Output is correct
4 Correct 4 ms 7500 KB Output is correct
5 Correct 5 ms 7644 KB Output is correct
6 Correct 6 ms 7732 KB Output is correct
7 Correct 5 ms 7764 KB Output is correct
8 Correct 5 ms 7764 KB Output is correct
9 Correct 4 ms 7508 KB Output is correct
10 Correct 6 ms 7760 KB Output is correct
11 Correct 5 ms 7764 KB Output is correct
12 Correct 5 ms 7636 KB Output is correct
13 Correct 6 ms 7632 KB Output is correct
14 Correct 5 ms 7636 KB Output is correct
15 Correct 5 ms 7760 KB Output is correct
16 Correct 5 ms 7764 KB Output is correct
17 Correct 5 ms 7764 KB Output is correct
18 Correct 5 ms 7764 KB Output is correct
19 Correct 5 ms 7636 KB Output is correct
20 Correct 5 ms 7764 KB Output is correct
21 Correct 5 ms 7636 KB Output is correct
22 Correct 5 ms 7628 KB Output is correct
23 Correct 5 ms 7628 KB Output is correct
24 Correct 5 ms 7760 KB Output is correct
25 Correct 5 ms 7764 KB Output is correct
26 Correct 5 ms 7764 KB Output is correct
27 Correct 5 ms 7760 KB Output is correct
28 Correct 5 ms 7808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7508 KB Output is correct
2 Correct 7 ms 7508 KB Output is correct
3 Correct 4 ms 7492 KB Output is correct
4 Correct 4 ms 7492 KB Output is correct
5 Correct 4 ms 7500 KB Output is correct
6 Correct 5 ms 7644 KB Output is correct
7 Correct 6 ms 7732 KB Output is correct
8 Correct 5 ms 7764 KB Output is correct
9 Correct 5 ms 7764 KB Output is correct
10 Correct 99 ms 28496 KB Output is correct
11 Correct 118 ms 33292 KB Output is correct
12 Correct 124 ms 32876 KB Output is correct
13 Correct 127 ms 34372 KB Output is correct
14 Correct 113 ms 38296 KB Output is correct
15 Correct 6 ms 7760 KB Output is correct
16 Correct 5 ms 7764 KB Output is correct
17 Correct 5 ms 7636 KB Output is correct
18 Correct 6 ms 7632 KB Output is correct
19 Correct 5 ms 7636 KB Output is correct
20 Correct 5 ms 7760 KB Output is correct
21 Correct 5 ms 7764 KB Output is correct
22 Correct 5 ms 7764 KB Output is correct
23 Correct 5 ms 7764 KB Output is correct
24 Correct 5 ms 7636 KB Output is correct
25 Correct 5 ms 7764 KB Output is correct
26 Correct 5 ms 7636 KB Output is correct
27 Correct 5 ms 7628 KB Output is correct
28 Correct 5 ms 7628 KB Output is correct
29 Correct 5 ms 7760 KB Output is correct
30 Correct 5 ms 7764 KB Output is correct
31 Correct 5 ms 7764 KB Output is correct
32 Correct 5 ms 7760 KB Output is correct
33 Correct 5 ms 7808 KB Output is correct
34 Correct 15 ms 11088 KB Output is correct
35 Correct 129 ms 33996 KB Output is correct
36 Correct 131 ms 34076 KB Output is correct
37 Correct 127 ms 34012 KB Output is correct
38 Correct 147 ms 33700 KB Output is correct
39 Correct 128 ms 33460 KB Output is correct
40 Correct 113 ms 31300 KB Output is correct
41 Correct 129 ms 34368 KB Output is correct
42 Correct 144 ms 33988 KB Output is correct
43 Correct 111 ms 39112 KB Output is correct
44 Correct 152 ms 34352 KB Output is correct
45 Correct 143 ms 34032 KB Output is correct
46 Correct 137 ms 34080 KB Output is correct
47 Correct 148 ms 34080 KB Output is correct
48 Correct 146 ms 35988 KB Output is correct
49 Correct 73 ms 15472 KB Output is correct
50 Correct 62 ms 15308 KB Output is correct
51 Correct 134 ms 29552 KB Output is correct
52 Correct 202 ms 37492 KB Output is correct
53 Correct 163 ms 39372 KB Output is correct
54 Correct 168 ms 39332 KB Output is correct
55 Correct 99 ms 38848 KB Output is correct
56 Correct 154 ms 40476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 7508 KB Output is correct
2 Correct 4 ms 7492 KB Output is correct
3 Correct 4 ms 7492 KB Output is correct
4 Correct 4 ms 7500 KB Output is correct
5 Correct 5 ms 7644 KB Output is correct
6 Correct 6 ms 7732 KB Output is correct
7 Correct 5 ms 7764 KB Output is correct
8 Correct 5 ms 7764 KB Output is correct
9 Correct 99 ms 28496 KB Output is correct
10 Correct 118 ms 33292 KB Output is correct
11 Correct 124 ms 32876 KB Output is correct
12 Correct 127 ms 34372 KB Output is correct
13 Correct 113 ms 38296 KB Output is correct
14 Correct 133 ms 28784 KB Output is correct
15 Correct 479 ms 37324 KB Output is correct
16 Correct 586 ms 36596 KB Output is correct
17 Correct 603 ms 38132 KB Output is correct
18 Correct 606 ms 42156 KB Output is correct
19 Correct 489 ms 44664 KB Output is correct
20 Correct 501 ms 46264 KB Output is correct
21 Correct 461 ms 45508 KB Output is correct
22 Correct 449 ms 46092 KB Output is correct
23 Correct 528 ms 46052 KB Output is correct
24 Correct 482 ms 44600 KB Output is correct
25 Correct 464 ms 45548 KB Output is correct
26 Correct 460 ms 44260 KB Output is correct
27 Correct 6 ms 7760 KB Output is correct
28 Correct 5 ms 7764 KB Output is correct
29 Correct 5 ms 7636 KB Output is correct
30 Correct 6 ms 7632 KB Output is correct
31 Correct 5 ms 7636 KB Output is correct
32 Correct 5 ms 7760 KB Output is correct
33 Correct 5 ms 7764 KB Output is correct
34 Correct 5 ms 7764 KB Output is correct
35 Correct 5 ms 7764 KB Output is correct
36 Correct 15 ms 11088 KB Output is correct
37 Correct 129 ms 33996 KB Output is correct
38 Correct 131 ms 34076 KB Output is correct
39 Correct 127 ms 34012 KB Output is correct
40 Correct 147 ms 33700 KB Output is correct
41 Correct 128 ms 33460 KB Output is correct
42 Correct 113 ms 31300 KB Output is correct
43 Correct 129 ms 34368 KB Output is correct
44 Correct 144 ms 33988 KB Output is correct
45 Correct 111 ms 39112 KB Output is correct
46 Correct 152 ms 34352 KB Output is correct
47 Correct 29 ms 11292 KB Output is correct
48 Correct 490 ms 38568 KB Output is correct
49 Correct 505 ms 38584 KB Output is correct
50 Correct 504 ms 38460 KB Output is correct
51 Correct 369 ms 38272 KB Output is correct
52 Correct 353 ms 36792 KB Output is correct
53 Correct 249 ms 30828 KB Output is correct
54 Correct 520 ms 39476 KB Output is correct
55 Correct 508 ms 38568 KB Output is correct
56 Correct 481 ms 42576 KB Output is correct
57 Correct 321 ms 39564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7508 KB Output is correct
2 Correct 7 ms 7508 KB Output is correct
3 Correct 4 ms 7492 KB Output is correct
4 Correct 4 ms 7492 KB Output is correct
5 Correct 4 ms 7500 KB Output is correct
6 Correct 5 ms 7644 KB Output is correct
7 Correct 6 ms 7732 KB Output is correct
8 Correct 5 ms 7764 KB Output is correct
9 Correct 5 ms 7764 KB Output is correct
10 Correct 99 ms 28496 KB Output is correct
11 Correct 118 ms 33292 KB Output is correct
12 Correct 124 ms 32876 KB Output is correct
13 Correct 127 ms 34372 KB Output is correct
14 Correct 113 ms 38296 KB Output is correct
15 Correct 133 ms 28784 KB Output is correct
16 Correct 479 ms 37324 KB Output is correct
17 Correct 586 ms 36596 KB Output is correct
18 Correct 603 ms 38132 KB Output is correct
19 Correct 606 ms 42156 KB Output is correct
20 Correct 489 ms 44664 KB Output is correct
21 Correct 501 ms 46264 KB Output is correct
22 Correct 461 ms 45508 KB Output is correct
23 Correct 449 ms 46092 KB Output is correct
24 Correct 528 ms 46052 KB Output is correct
25 Correct 482 ms 44600 KB Output is correct
26 Correct 464 ms 45548 KB Output is correct
27 Correct 460 ms 44260 KB Output is correct
28 Correct 6 ms 7760 KB Output is correct
29 Correct 5 ms 7764 KB Output is correct
30 Correct 5 ms 7636 KB Output is correct
31 Correct 6 ms 7632 KB Output is correct
32 Correct 5 ms 7636 KB Output is correct
33 Correct 5 ms 7760 KB Output is correct
34 Correct 5 ms 7764 KB Output is correct
35 Correct 5 ms 7764 KB Output is correct
36 Correct 5 ms 7764 KB Output is correct
37 Correct 15 ms 11088 KB Output is correct
38 Correct 129 ms 33996 KB Output is correct
39 Correct 131 ms 34076 KB Output is correct
40 Correct 127 ms 34012 KB Output is correct
41 Correct 147 ms 33700 KB Output is correct
42 Correct 128 ms 33460 KB Output is correct
43 Correct 113 ms 31300 KB Output is correct
44 Correct 129 ms 34368 KB Output is correct
45 Correct 144 ms 33988 KB Output is correct
46 Correct 111 ms 39112 KB Output is correct
47 Correct 152 ms 34352 KB Output is correct
48 Correct 29 ms 11292 KB Output is correct
49 Correct 490 ms 38568 KB Output is correct
50 Correct 505 ms 38584 KB Output is correct
51 Correct 504 ms 38460 KB Output is correct
52 Correct 369 ms 38272 KB Output is correct
53 Correct 353 ms 36792 KB Output is correct
54 Correct 249 ms 30828 KB Output is correct
55 Correct 520 ms 39476 KB Output is correct
56 Correct 508 ms 38568 KB Output is correct
57 Correct 481 ms 42576 KB Output is correct
58 Correct 321 ms 39564 KB Output is correct
59 Correct 155 ms 16796 KB Output is correct
60 Correct 555 ms 38232 KB Output is correct
61 Correct 550 ms 37588 KB Output is correct
62 Correct 534 ms 39384 KB Output is correct
63 Correct 614 ms 43288 KB Output is correct
64 Correct 5 ms 7636 KB Output is correct
65 Correct 5 ms 7764 KB Output is correct
66 Correct 5 ms 7636 KB Output is correct
67 Correct 5 ms 7628 KB Output is correct
68 Correct 5 ms 7628 KB Output is correct
69 Correct 5 ms 7760 KB Output is correct
70 Correct 5 ms 7764 KB Output is correct
71 Correct 5 ms 7764 KB Output is correct
72 Correct 5 ms 7760 KB Output is correct
73 Correct 5 ms 7808 KB Output is correct
74 Correct 143 ms 34032 KB Output is correct
75 Correct 137 ms 34080 KB Output is correct
76 Correct 148 ms 34080 KB Output is correct
77 Correct 146 ms 35988 KB Output is correct
78 Correct 73 ms 15472 KB Output is correct
79 Correct 62 ms 15308 KB Output is correct
80 Correct 134 ms 29552 KB Output is correct
81 Correct 202 ms 37492 KB Output is correct
82 Correct 163 ms 39372 KB Output is correct
83 Correct 168 ms 39332 KB Output is correct
84 Correct 99 ms 38848 KB Output is correct
85 Correct 154 ms 40476 KB Output is correct
86 Correct 107 ms 18508 KB Output is correct
87 Correct 389 ms 38452 KB Output is correct
88 Correct 380 ms 38576 KB Output is correct
89 Correct 382 ms 38740 KB Output is correct
90 Correct 173 ms 19452 KB Output is correct
91 Correct 220 ms 21548 KB Output is correct
92 Correct 303 ms 34032 KB Output is correct
93 Correct 406 ms 41720 KB Output is correct
94 Correct 447 ms 42980 KB Output is correct
95 Correct 501 ms 43084 KB Output is correct
96 Correct 519 ms 42948 KB Output is correct
97 Correct 457 ms 42816 KB Output is correct