답안 #295683

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
295683 2020-09-09T20:02:55 Z junseo 자매 도시 (APIO20_swap) C++17
100 / 100
490 ms 48740 KB
#include "swap.h"
#include <bits/stdc++.h>
#define fi first
#define se second
#define eb emplace_back
#define all(v) v.begin(), v.end()
using namespace std;
typedef pair<int, int> pii;

const int maxn = 1e5 + 10;
const int maxm = 2e5 + 10;
const int maxv = maxn * 3;

struct Node {
    int pa;
    int s, e, w;
    Node(void) : pa(-1), s(-1), e(-1), w(-1) {}
    Node(int pa, int s, int e, int w) : pa(pa), s(s), e(e), w(w) {}
};

int V = 0;
int p[20][maxv], h[maxv];
Node info[maxv];
vector<int> adj[maxv];

int fnd(int x) {
    if(info[x].pa == x) return x;
    return info[x].pa = fnd(info[x].pa);
}

void uni(int a, int b, int w) {
    int x = fnd(a), y = fnd(b);
    if(x == y) {
        if(!~info[x].s) return;
        int z = ++V;
        info[z] = Node(z, -1, -1, w);
        p[0][x] = z;
        info[x].pa = z;
        return;
    }
    int z = ++V;
    p[0][x] = p[0][y] = z;
    info[x].pa = info[y].pa = z;
    info[z] = Node(z, (info[x].s ^ info[x].e ^ a), (info[y].s ^ info[y].e ^ b), w);

    bool af = (a == info[x].s || a == info[x].e), bf = (b == info[y].s || b == info[y].e);
    if(!~info[x].s || !~info[y].s || !af || !bf)    info[z].s = info[z].e = -1;
}

void dfs(int u) {
    for(auto v : adj[u]) {
        h[v] = h[u] + 1;
        dfs(v);
    }
}

void init(int N, int M, vector<int> _u, vector<int> _v, vector<int> _w) {
    V = N;
    for(int i = 1; i <= N; ++i) info[i] = Node(i, i, i, 0);
    vector<pair<int, pii>> E;
    for(int i = 0; i < M; ++i)  E.eb(_w[i], pii(++_u[i], ++_v[i]));
    sort(all(E));
    for(auto [w, t] : E)    uni(t.fi, t.se, w);
    for(int i = 1; i <= V; ++i) if(~info[i].s)  info[i].w = -1;
    for(int i = 1; i < 20; ++i) for(int j = 1; j <= V; ++j) p[i][j] = p[i - 1][p[i - 1][j]];
    for(int i = 1; i < V; ++i)  adj[p[0][i]].eb(i);
    dfs(V);
}

int lca(int u, int v) {
    if(h[u] < h[v]) swap(u, v);
    for(int i = 19; i >= 0; --i) if(h[u] - (1 << i) >= h[v])    u = p[i][u];
    if(u == v)  return u;
    for(int i = 19; i >= 0; --i) if(p[i][u] != p[i][v]) {
        u = p[i][u];
        v = p[i][v];
    }
    return p[0][u];
}

int getMinimumFuelCapacity(int X, int Y) {
    int Z = lca(++X, ++Y);
    if(!~info[Z].s) return info[Z].w;
    for(int i = 19; i >= 0; --i) if(~info[p[i][Z]].s)   Z = p[i][Z];
    return info[p[0][Z]].w;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12160 KB Output is correct
2 Correct 8 ms 12160 KB Output is correct
3 Correct 8 ms 12160 KB Output is correct
4 Correct 8 ms 12288 KB Output is correct
5 Correct 9 ms 12416 KB Output is correct
6 Correct 9 ms 12416 KB Output is correct
7 Correct 9 ms 12416 KB Output is correct
8 Correct 9 ms 12416 KB Output is correct
9 Correct 93 ms 30312 KB Output is correct
10 Correct 110 ms 34412 KB Output is correct
11 Correct 117 ms 34028 KB Output is correct
12 Correct 120 ms 35308 KB Output is correct
13 Correct 112 ms 36844 KB Output is correct
14 Correct 112 ms 30448 KB Output is correct
15 Correct 367 ms 36076 KB Output is correct
16 Correct 363 ms 35624 KB Output is correct
17 Correct 383 ms 36844 KB Output is correct
18 Correct 478 ms 38384 KB Output is correct
19 Correct 149 ms 19196 KB Output is correct
20 Correct 349 ms 36460 KB Output is correct
21 Correct 368 ms 35932 KB Output is correct
22 Correct 382 ms 37292 KB Output is correct
23 Correct 490 ms 38968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12160 KB Output is correct
2 Correct 8 ms 12160 KB Output is correct
3 Correct 409 ms 39088 KB Output is correct
4 Correct 410 ms 40120 KB Output is correct
5 Correct 419 ms 39608 KB Output is correct
6 Correct 400 ms 39948 KB Output is correct
7 Correct 415 ms 39784 KB Output is correct
8 Correct 400 ms 38856 KB Output is correct
9 Correct 413 ms 39660 KB Output is correct
10 Correct 405 ms 38756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12160 KB Output is correct
2 Correct 8 ms 12160 KB Output is correct
3 Correct 8 ms 12160 KB Output is correct
4 Correct 8 ms 12160 KB Output is correct
5 Correct 8 ms 12288 KB Output is correct
6 Correct 9 ms 12416 KB Output is correct
7 Correct 9 ms 12416 KB Output is correct
8 Correct 9 ms 12416 KB Output is correct
9 Correct 9 ms 12416 KB Output is correct
10 Correct 9 ms 12416 KB Output is correct
11 Correct 9 ms 12416 KB Output is correct
12 Correct 11 ms 12416 KB Output is correct
13 Correct 9 ms 12416 KB Output is correct
14 Correct 9 ms 12416 KB Output is correct
15 Correct 9 ms 12416 KB Output is correct
16 Correct 9 ms 12416 KB Output is correct
17 Correct 9 ms 12416 KB Output is correct
18 Correct 9 ms 12416 KB Output is correct
19 Correct 9 ms 12416 KB Output is correct
20 Correct 9 ms 12416 KB Output is correct
21 Correct 9 ms 12416 KB Output is correct
22 Correct 9 ms 12416 KB Output is correct
23 Correct 9 ms 12416 KB Output is correct
24 Correct 9 ms 12416 KB Output is correct
25 Correct 10 ms 12520 KB Output is correct
26 Correct 10 ms 12672 KB Output is correct
27 Correct 10 ms 12416 KB Output is correct
28 Correct 10 ms 12544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12160 KB Output is correct
2 Correct 8 ms 12160 KB Output is correct
3 Correct 8 ms 12160 KB Output is correct
4 Correct 8 ms 12160 KB Output is correct
5 Correct 8 ms 12288 KB Output is correct
6 Correct 9 ms 12416 KB Output is correct
7 Correct 9 ms 12416 KB Output is correct
8 Correct 9 ms 12416 KB Output is correct
9 Correct 9 ms 12416 KB Output is correct
10 Correct 93 ms 30312 KB Output is correct
11 Correct 110 ms 34412 KB Output is correct
12 Correct 117 ms 34028 KB Output is correct
13 Correct 120 ms 35308 KB Output is correct
14 Correct 112 ms 36844 KB Output is correct
15 Correct 9 ms 12416 KB Output is correct
16 Correct 9 ms 12416 KB Output is correct
17 Correct 11 ms 12416 KB Output is correct
18 Correct 9 ms 12416 KB Output is correct
19 Correct 9 ms 12416 KB Output is correct
20 Correct 9 ms 12416 KB Output is correct
21 Correct 9 ms 12416 KB Output is correct
22 Correct 9 ms 12416 KB Output is correct
23 Correct 9 ms 12416 KB Output is correct
24 Correct 23 ms 15808 KB Output is correct
25 Correct 121 ms 37228 KB Output is correct
26 Correct 118 ms 37100 KB Output is correct
27 Correct 116 ms 37100 KB Output is correct
28 Correct 116 ms 36844 KB Output is correct
29 Correct 115 ms 36700 KB Output is correct
30 Correct 106 ms 34668 KB Output is correct
31 Correct 121 ms 37356 KB Output is correct
32 Correct 121 ms 37100 KB Output is correct
33 Correct 105 ms 39148 KB Output is correct
34 Correct 124 ms 37484 KB Output is correct
35 Correct 9 ms 12416 KB Output is correct
36 Correct 9 ms 12416 KB Output is correct
37 Correct 9 ms 12416 KB Output is correct
38 Correct 9 ms 12416 KB Output is correct
39 Correct 9 ms 12416 KB Output is correct
40 Correct 9 ms 12416 KB Output is correct
41 Correct 10 ms 12520 KB Output is correct
42 Correct 10 ms 12672 KB Output is correct
43 Correct 10 ms 12416 KB Output is correct
44 Correct 10 ms 12544 KB Output is correct
45 Correct 145 ms 36580 KB Output is correct
46 Correct 119 ms 37100 KB Output is correct
47 Correct 119 ms 36976 KB Output is correct
48 Correct 126 ms 37868 KB Output is correct
49 Correct 105 ms 21740 KB Output is correct
50 Correct 78 ms 20332 KB Output is correct
51 Correct 121 ms 32492 KB Output is correct
52 Correct 163 ms 40936 KB Output is correct
53 Correct 191 ms 46056 KB Output is correct
54 Correct 185 ms 42728 KB Output is correct
55 Correct 104 ms 39148 KB Output is correct
56 Correct 177 ms 42468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12160 KB Output is correct
2 Correct 8 ms 12160 KB Output is correct
3 Correct 8 ms 12160 KB Output is correct
4 Correct 8 ms 12288 KB Output is correct
5 Correct 9 ms 12416 KB Output is correct
6 Correct 9 ms 12416 KB Output is correct
7 Correct 9 ms 12416 KB Output is correct
8 Correct 9 ms 12416 KB Output is correct
9 Correct 93 ms 30312 KB Output is correct
10 Correct 110 ms 34412 KB Output is correct
11 Correct 117 ms 34028 KB Output is correct
12 Correct 120 ms 35308 KB Output is correct
13 Correct 112 ms 36844 KB Output is correct
14 Correct 112 ms 30448 KB Output is correct
15 Correct 367 ms 36076 KB Output is correct
16 Correct 363 ms 35624 KB Output is correct
17 Correct 383 ms 36844 KB Output is correct
18 Correct 478 ms 38384 KB Output is correct
19 Correct 409 ms 39088 KB Output is correct
20 Correct 410 ms 40120 KB Output is correct
21 Correct 419 ms 39608 KB Output is correct
22 Correct 400 ms 39948 KB Output is correct
23 Correct 415 ms 39784 KB Output is correct
24 Correct 400 ms 38856 KB Output is correct
25 Correct 413 ms 39660 KB Output is correct
26 Correct 405 ms 38756 KB Output is correct
27 Correct 9 ms 12416 KB Output is correct
28 Correct 9 ms 12416 KB Output is correct
29 Correct 11 ms 12416 KB Output is correct
30 Correct 9 ms 12416 KB Output is correct
31 Correct 9 ms 12416 KB Output is correct
32 Correct 9 ms 12416 KB Output is correct
33 Correct 9 ms 12416 KB Output is correct
34 Correct 9 ms 12416 KB Output is correct
35 Correct 9 ms 12416 KB Output is correct
36 Correct 23 ms 15808 KB Output is correct
37 Correct 121 ms 37228 KB Output is correct
38 Correct 118 ms 37100 KB Output is correct
39 Correct 116 ms 37100 KB Output is correct
40 Correct 116 ms 36844 KB Output is correct
41 Correct 115 ms 36700 KB Output is correct
42 Correct 106 ms 34668 KB Output is correct
43 Correct 121 ms 37356 KB Output is correct
44 Correct 121 ms 37100 KB Output is correct
45 Correct 105 ms 39148 KB Output is correct
46 Correct 124 ms 37484 KB Output is correct
47 Correct 32 ms 15680 KB Output is correct
48 Correct 345 ms 41068 KB Output is correct
49 Correct 316 ms 41068 KB Output is correct
50 Correct 304 ms 40940 KB Output is correct
51 Correct 294 ms 40816 KB Output is correct
52 Correct 284 ms 39372 KB Output is correct
53 Correct 236 ms 34132 KB Output is correct
54 Correct 337 ms 41784 KB Output is correct
55 Correct 354 ms 41072 KB Output is correct
56 Correct 419 ms 42604 KB Output is correct
57 Correct 303 ms 41784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12160 KB Output is correct
2 Correct 8 ms 12160 KB Output is correct
3 Correct 8 ms 12160 KB Output is correct
4 Correct 8 ms 12160 KB Output is correct
5 Correct 8 ms 12288 KB Output is correct
6 Correct 9 ms 12416 KB Output is correct
7 Correct 9 ms 12416 KB Output is correct
8 Correct 9 ms 12416 KB Output is correct
9 Correct 9 ms 12416 KB Output is correct
10 Correct 93 ms 30312 KB Output is correct
11 Correct 110 ms 34412 KB Output is correct
12 Correct 117 ms 34028 KB Output is correct
13 Correct 120 ms 35308 KB Output is correct
14 Correct 112 ms 36844 KB Output is correct
15 Correct 112 ms 30448 KB Output is correct
16 Correct 367 ms 36076 KB Output is correct
17 Correct 363 ms 35624 KB Output is correct
18 Correct 383 ms 36844 KB Output is correct
19 Correct 478 ms 38384 KB Output is correct
20 Correct 409 ms 39088 KB Output is correct
21 Correct 410 ms 40120 KB Output is correct
22 Correct 419 ms 39608 KB Output is correct
23 Correct 400 ms 39948 KB Output is correct
24 Correct 415 ms 39784 KB Output is correct
25 Correct 400 ms 38856 KB Output is correct
26 Correct 413 ms 39660 KB Output is correct
27 Correct 405 ms 38756 KB Output is correct
28 Correct 9 ms 12416 KB Output is correct
29 Correct 9 ms 12416 KB Output is correct
30 Correct 11 ms 12416 KB Output is correct
31 Correct 9 ms 12416 KB Output is correct
32 Correct 9 ms 12416 KB Output is correct
33 Correct 9 ms 12416 KB Output is correct
34 Correct 9 ms 12416 KB Output is correct
35 Correct 9 ms 12416 KB Output is correct
36 Correct 9 ms 12416 KB Output is correct
37 Correct 23 ms 15808 KB Output is correct
38 Correct 121 ms 37228 KB Output is correct
39 Correct 118 ms 37100 KB Output is correct
40 Correct 116 ms 37100 KB Output is correct
41 Correct 116 ms 36844 KB Output is correct
42 Correct 115 ms 36700 KB Output is correct
43 Correct 106 ms 34668 KB Output is correct
44 Correct 121 ms 37356 KB Output is correct
45 Correct 121 ms 37100 KB Output is correct
46 Correct 105 ms 39148 KB Output is correct
47 Correct 124 ms 37484 KB Output is correct
48 Correct 32 ms 15680 KB Output is correct
49 Correct 345 ms 41068 KB Output is correct
50 Correct 316 ms 41068 KB Output is correct
51 Correct 304 ms 40940 KB Output is correct
52 Correct 294 ms 40816 KB Output is correct
53 Correct 284 ms 39372 KB Output is correct
54 Correct 236 ms 34132 KB Output is correct
55 Correct 337 ms 41784 KB Output is correct
56 Correct 354 ms 41072 KB Output is correct
57 Correct 419 ms 42604 KB Output is correct
58 Correct 303 ms 41784 KB Output is correct
59 Correct 149 ms 19196 KB Output is correct
60 Correct 349 ms 36460 KB Output is correct
61 Correct 368 ms 35932 KB Output is correct
62 Correct 382 ms 37292 KB Output is correct
63 Correct 490 ms 38968 KB Output is correct
64 Correct 9 ms 12416 KB Output is correct
65 Correct 9 ms 12416 KB Output is correct
66 Correct 9 ms 12416 KB Output is correct
67 Correct 9 ms 12416 KB Output is correct
68 Correct 9 ms 12416 KB Output is correct
69 Correct 9 ms 12416 KB Output is correct
70 Correct 10 ms 12520 KB Output is correct
71 Correct 10 ms 12672 KB Output is correct
72 Correct 10 ms 12416 KB Output is correct
73 Correct 10 ms 12544 KB Output is correct
74 Correct 145 ms 36580 KB Output is correct
75 Correct 119 ms 37100 KB Output is correct
76 Correct 119 ms 36976 KB Output is correct
77 Correct 126 ms 37868 KB Output is correct
78 Correct 105 ms 21740 KB Output is correct
79 Correct 78 ms 20332 KB Output is correct
80 Correct 121 ms 32492 KB Output is correct
81 Correct 163 ms 40936 KB Output is correct
82 Correct 191 ms 46056 KB Output is correct
83 Correct 185 ms 42728 KB Output is correct
84 Correct 104 ms 39148 KB Output is correct
85 Correct 177 ms 42468 KB Output is correct
86 Correct 94 ms 22384 KB Output is correct
87 Correct 298 ms 41068 KB Output is correct
88 Correct 306 ms 40940 KB Output is correct
89 Correct 366 ms 40284 KB Output is correct
90 Correct 208 ms 25456 KB Output is correct
91 Correct 234 ms 26216 KB Output is correct
92 Correct 342 ms 36076 KB Output is correct
93 Correct 360 ms 44644 KB Output is correct
94 Correct 478 ms 48740 KB Output is correct
95 Correct 424 ms 46668 KB Output is correct
96 Correct 416 ms 42732 KB Output is correct
97 Correct 403 ms 44260 KB Output is correct