답안 #375386

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
375386 2021-03-09T10:23:27 Z rocks03 자매 도시 (APIO20_swap) C++14
100 / 100
671 ms 71596 KB
//#pragma GCC target("avx2")
//#pragma GCC optimization("O3")
//#pragma GCC optimization("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define pb push_back
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << ": " << x << " "
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MAXN = 5e5+100;
int dsu[MAXN], deg[MAXN], we[MAXN];
vector<int> g[MAXN];
bool dsu_c[MAXN], c[MAXN];

void init(int n){
    rep(i, 0, n){
        dsu[i] = i, dsu_c[i] = false, c[i] = false;
    }
}

int find(int x){
    if(x == dsu[x]) return x;
    dsu_c[dsu[x]] |= dsu_c[x];
    dsu[x] = find(dsu[x]);
    return dsu[x];
}

int root;
void unite(int a, int b, int w){
    a = find(a); b = find(b);
    dsu_c[root] = c[root] = (dsu_c[a] || dsu_c[b] || (a == b));
    we[root] = w;
    dsu[a] = root;
    g[root].pb(a);
    if(a != b){
        dsu[b] = root;
        g[root].pb(b);
    }
    root++;
}

const int MAXK = 20+5;
int dep[MAXN], p[MAXK][MAXN];

void dfs(int v, int pa){
    p[0][v] = pa;
    for(int u : g[v]){
        dep[u] = dep[v] + 1;
        dfs(u, v);
    }
}

struct Edge{
    int u, v, w;
    bool operator<(const Edge& oth) const{
        return w < oth.w;
    }
};

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W){
    vector<Edge> e;
    rep(i, 0, M){
        e.pb({U[i], V[i], W[i]});
    }
    sort(all(e));
    root = N;
    init(N + M);
    rep(i, 0, M){
        auto [u, v, w] = e[i];
        deg[u]++, deg[v]++;
        if(deg[u] >= 3) dsu_c[u] = true;
        if(deg[v] >= 3) dsu_c[v] = true;
        unite(u, v, w);
    }
    rep(i, 0, N + M){
        if(find(i) == i)
            dfs(i, -1);
    }
    rep(k, 0, MAXK - 1){
        rep(i, 0, N + M){
            if(p[k][i] != -1){
                p[k + 1][i] = p[k][p[k][i]];
            } else{
                p[k + 1][i] = -1;
            }
        }
    }
}

int query(int u, int v){
    if(dep[u] < dep[v]) swap(u, v);
    int diff = dep[u] - dep[v];
    per(k, MAXK - 1, 0){
        if(diff >> k & 1){
            u = p[k][u];
        }
    }
    if(u == v && c[u]) return we[u];
    per(k, MAXK - 1, 0){
        if(p[k][u] != -1 && (p[k][u] != p[k][v] || !c[p[k][u]])){
            u = p[k][u], v = p[k][v];
        }
    }
    if(p[0][u] == -1) return -1;
    return we[p[0][u]];
}

int getMinimumFuelCapacity(int X, int Y){
    return query(X, Y);
}

Compilation message

swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:78:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   78 |         auto [u, v, w] = e[i];
      |              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12396 KB Output is correct
2 Correct 9 ms 12268 KB Output is correct
3 Correct 9 ms 12268 KB Output is correct
4 Correct 10 ms 12396 KB Output is correct
5 Correct 9 ms 12652 KB Output is correct
6 Correct 10 ms 12524 KB Output is correct
7 Correct 10 ms 12652 KB Output is correct
8 Correct 10 ms 12652 KB Output is correct
9 Correct 87 ms 36704 KB Output is correct
10 Correct 117 ms 42244 KB Output is correct
11 Correct 129 ms 41636 KB Output is correct
12 Correct 122 ms 43360 KB Output is correct
13 Correct 101 ms 45664 KB Output is correct
14 Correct 105 ms 36960 KB Output is correct
15 Correct 298 ms 45536 KB Output is correct
16 Correct 275 ms 44768 KB Output is correct
17 Correct 293 ms 45536 KB Output is correct
18 Correct 375 ms 47712 KB Output is correct
19 Correct 109 ms 22136 KB Output is correct
20 Correct 273 ms 45920 KB Output is correct
21 Correct 309 ms 45136 KB Output is correct
22 Correct 292 ms 45868 KB Output is correct
23 Correct 419 ms 48172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12396 KB Output is correct
2 Correct 9 ms 12268 KB Output is correct
3 Correct 564 ms 46628 KB Output is correct
4 Correct 583 ms 51244 KB Output is correct
5 Correct 623 ms 50388 KB Output is correct
6 Correct 573 ms 51096 KB Output is correct
7 Correct 614 ms 50912 KB Output is correct
8 Correct 579 ms 49624 KB Output is correct
9 Correct 572 ms 50580 KB Output is correct
10 Correct 589 ms 49220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12396 KB Output is correct
2 Correct 9 ms 12268 KB Output is correct
3 Correct 9 ms 12268 KB Output is correct
4 Correct 10 ms 12396 KB Output is correct
5 Correct 9 ms 12652 KB Output is correct
6 Correct 10 ms 12524 KB Output is correct
7 Correct 10 ms 12652 KB Output is correct
8 Correct 10 ms 12652 KB Output is correct
9 Correct 9 ms 12268 KB Output is correct
10 Correct 10 ms 12652 KB Output is correct
11 Correct 11 ms 12652 KB Output is correct
12 Correct 10 ms 12652 KB Output is correct
13 Correct 10 ms 12524 KB Output is correct
14 Correct 10 ms 12524 KB Output is correct
15 Correct 9 ms 12652 KB Output is correct
16 Correct 10 ms 12652 KB Output is correct
17 Correct 11 ms 12652 KB Output is correct
18 Correct 10 ms 12652 KB Output is correct
19 Correct 10 ms 12652 KB Output is correct
20 Correct 10 ms 12652 KB Output is correct
21 Correct 10 ms 12652 KB Output is correct
22 Correct 10 ms 12780 KB Output is correct
23 Correct 10 ms 12524 KB Output is correct
24 Correct 10 ms 12780 KB Output is correct
25 Correct 12 ms 12812 KB Output is correct
26 Correct 11 ms 12908 KB Output is correct
27 Correct 10 ms 12652 KB Output is correct
28 Correct 10 ms 12780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12268 KB Output is correct
2 Correct 9 ms 12396 KB Output is correct
3 Correct 9 ms 12268 KB Output is correct
4 Correct 9 ms 12268 KB Output is correct
5 Correct 10 ms 12396 KB Output is correct
6 Correct 9 ms 12652 KB Output is correct
7 Correct 10 ms 12524 KB Output is correct
8 Correct 10 ms 12652 KB Output is correct
9 Correct 10 ms 12652 KB Output is correct
10 Correct 87 ms 36704 KB Output is correct
11 Correct 117 ms 42244 KB Output is correct
12 Correct 129 ms 41636 KB Output is correct
13 Correct 122 ms 43360 KB Output is correct
14 Correct 101 ms 45664 KB Output is correct
15 Correct 10 ms 12652 KB Output is correct
16 Correct 11 ms 12652 KB Output is correct
17 Correct 10 ms 12652 KB Output is correct
18 Correct 10 ms 12524 KB Output is correct
19 Correct 10 ms 12524 KB Output is correct
20 Correct 9 ms 12652 KB Output is correct
21 Correct 10 ms 12652 KB Output is correct
22 Correct 11 ms 12652 KB Output is correct
23 Correct 10 ms 12652 KB Output is correct
24 Correct 10 ms 12652 KB Output is correct
25 Correct 10 ms 12652 KB Output is correct
26 Correct 10 ms 12652 KB Output is correct
27 Correct 10 ms 12780 KB Output is correct
28 Correct 10 ms 12524 KB Output is correct
29 Correct 10 ms 12780 KB Output is correct
30 Correct 12 ms 12812 KB Output is correct
31 Correct 11 ms 12908 KB Output is correct
32 Correct 10 ms 12652 KB Output is correct
33 Correct 10 ms 12780 KB Output is correct
34 Correct 21 ms 16556 KB Output is correct
35 Correct 110 ms 43124 KB Output is correct
36 Correct 118 ms 43096 KB Output is correct
37 Correct 111 ms 42976 KB Output is correct
38 Correct 110 ms 42604 KB Output is correct
39 Correct 116 ms 42456 KB Output is correct
40 Correct 106 ms 39904 KB Output is correct
41 Correct 122 ms 43360 KB Output is correct
42 Correct 118 ms 43136 KB Output is correct
43 Correct 99 ms 46176 KB Output is correct
44 Correct 126 ms 43360 KB Output is correct
45 Correct 167 ms 54680 KB Output is correct
46 Correct 121 ms 43104 KB Output is correct
47 Correct 117 ms 43120 KB Output is correct
48 Correct 128 ms 46812 KB Output is correct
49 Correct 149 ms 50652 KB Output is correct
50 Correct 110 ms 42340 KB Output is correct
51 Correct 140 ms 48992 KB Output is correct
52 Correct 187 ms 58460 KB Output is correct
53 Correct 214 ms 61788 KB Output is correct
54 Correct 218 ms 67672 KB Output is correct
55 Correct 99 ms 45920 KB Output is correct
56 Correct 201 ms 62484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12396 KB Output is correct
2 Correct 9 ms 12268 KB Output is correct
3 Correct 9 ms 12268 KB Output is correct
4 Correct 10 ms 12396 KB Output is correct
5 Correct 9 ms 12652 KB Output is correct
6 Correct 10 ms 12524 KB Output is correct
7 Correct 10 ms 12652 KB Output is correct
8 Correct 10 ms 12652 KB Output is correct
9 Correct 87 ms 36704 KB Output is correct
10 Correct 117 ms 42244 KB Output is correct
11 Correct 129 ms 41636 KB Output is correct
12 Correct 122 ms 43360 KB Output is correct
13 Correct 101 ms 45664 KB Output is correct
14 Correct 105 ms 36960 KB Output is correct
15 Correct 298 ms 45536 KB Output is correct
16 Correct 275 ms 44768 KB Output is correct
17 Correct 293 ms 45536 KB Output is correct
18 Correct 375 ms 47712 KB Output is correct
19 Correct 564 ms 46628 KB Output is correct
20 Correct 583 ms 51244 KB Output is correct
21 Correct 623 ms 50388 KB Output is correct
22 Correct 573 ms 51096 KB Output is correct
23 Correct 614 ms 50912 KB Output is correct
24 Correct 579 ms 49624 KB Output is correct
25 Correct 572 ms 50580 KB Output is correct
26 Correct 589 ms 49220 KB Output is correct
27 Correct 10 ms 12652 KB Output is correct
28 Correct 11 ms 12652 KB Output is correct
29 Correct 10 ms 12652 KB Output is correct
30 Correct 10 ms 12524 KB Output is correct
31 Correct 10 ms 12524 KB Output is correct
32 Correct 9 ms 12652 KB Output is correct
33 Correct 10 ms 12652 KB Output is correct
34 Correct 11 ms 12652 KB Output is correct
35 Correct 10 ms 12652 KB Output is correct
36 Correct 21 ms 16556 KB Output is correct
37 Correct 110 ms 43124 KB Output is correct
38 Correct 118 ms 43096 KB Output is correct
39 Correct 111 ms 42976 KB Output is correct
40 Correct 110 ms 42604 KB Output is correct
41 Correct 116 ms 42456 KB Output is correct
42 Correct 106 ms 39904 KB Output is correct
43 Correct 122 ms 43360 KB Output is correct
44 Correct 118 ms 43136 KB Output is correct
45 Correct 99 ms 46176 KB Output is correct
46 Correct 126 ms 43360 KB Output is correct
47 Correct 33 ms 16556 KB Output is correct
48 Correct 298 ms 46944 KB Output is correct
49 Correct 284 ms 46996 KB Output is correct
50 Correct 277 ms 46816 KB Output is correct
51 Correct 249 ms 46688 KB Output is correct
52 Correct 240 ms 44896 KB Output is correct
53 Correct 226 ms 37952 KB Output is correct
54 Correct 293 ms 47672 KB Output is correct
55 Correct 312 ms 46944 KB Output is correct
56 Correct 526 ms 49408 KB Output is correct
57 Correct 328 ms 47788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12268 KB Output is correct
2 Correct 9 ms 12396 KB Output is correct
3 Correct 9 ms 12268 KB Output is correct
4 Correct 9 ms 12268 KB Output is correct
5 Correct 10 ms 12396 KB Output is correct
6 Correct 9 ms 12652 KB Output is correct
7 Correct 10 ms 12524 KB Output is correct
8 Correct 10 ms 12652 KB Output is correct
9 Correct 10 ms 12652 KB Output is correct
10 Correct 87 ms 36704 KB Output is correct
11 Correct 117 ms 42244 KB Output is correct
12 Correct 129 ms 41636 KB Output is correct
13 Correct 122 ms 43360 KB Output is correct
14 Correct 101 ms 45664 KB Output is correct
15 Correct 105 ms 36960 KB Output is correct
16 Correct 298 ms 45536 KB Output is correct
17 Correct 275 ms 44768 KB Output is correct
18 Correct 293 ms 45536 KB Output is correct
19 Correct 375 ms 47712 KB Output is correct
20 Correct 564 ms 46628 KB Output is correct
21 Correct 583 ms 51244 KB Output is correct
22 Correct 623 ms 50388 KB Output is correct
23 Correct 573 ms 51096 KB Output is correct
24 Correct 614 ms 50912 KB Output is correct
25 Correct 579 ms 49624 KB Output is correct
26 Correct 572 ms 50580 KB Output is correct
27 Correct 589 ms 49220 KB Output is correct
28 Correct 10 ms 12652 KB Output is correct
29 Correct 11 ms 12652 KB Output is correct
30 Correct 10 ms 12652 KB Output is correct
31 Correct 10 ms 12524 KB Output is correct
32 Correct 10 ms 12524 KB Output is correct
33 Correct 9 ms 12652 KB Output is correct
34 Correct 10 ms 12652 KB Output is correct
35 Correct 11 ms 12652 KB Output is correct
36 Correct 10 ms 12652 KB Output is correct
37 Correct 21 ms 16556 KB Output is correct
38 Correct 110 ms 43124 KB Output is correct
39 Correct 118 ms 43096 KB Output is correct
40 Correct 111 ms 42976 KB Output is correct
41 Correct 110 ms 42604 KB Output is correct
42 Correct 116 ms 42456 KB Output is correct
43 Correct 106 ms 39904 KB Output is correct
44 Correct 122 ms 43360 KB Output is correct
45 Correct 118 ms 43136 KB Output is correct
46 Correct 99 ms 46176 KB Output is correct
47 Correct 126 ms 43360 KB Output is correct
48 Correct 33 ms 16556 KB Output is correct
49 Correct 298 ms 46944 KB Output is correct
50 Correct 284 ms 46996 KB Output is correct
51 Correct 277 ms 46816 KB Output is correct
52 Correct 249 ms 46688 KB Output is correct
53 Correct 240 ms 44896 KB Output is correct
54 Correct 226 ms 37952 KB Output is correct
55 Correct 293 ms 47672 KB Output is correct
56 Correct 312 ms 46944 KB Output is correct
57 Correct 526 ms 49408 KB Output is correct
58 Correct 328 ms 47788 KB Output is correct
59 Correct 109 ms 22136 KB Output is correct
60 Correct 273 ms 45920 KB Output is correct
61 Correct 309 ms 45136 KB Output is correct
62 Correct 292 ms 45868 KB Output is correct
63 Correct 419 ms 48172 KB Output is correct
64 Correct 10 ms 12652 KB Output is correct
65 Correct 10 ms 12652 KB Output is correct
66 Correct 10 ms 12652 KB Output is correct
67 Correct 10 ms 12780 KB Output is correct
68 Correct 10 ms 12524 KB Output is correct
69 Correct 10 ms 12780 KB Output is correct
70 Correct 12 ms 12812 KB Output is correct
71 Correct 11 ms 12908 KB Output is correct
72 Correct 10 ms 12652 KB Output is correct
73 Correct 10 ms 12780 KB Output is correct
74 Correct 167 ms 54680 KB Output is correct
75 Correct 121 ms 43104 KB Output is correct
76 Correct 117 ms 43120 KB Output is correct
77 Correct 128 ms 46812 KB Output is correct
78 Correct 149 ms 50652 KB Output is correct
79 Correct 110 ms 42340 KB Output is correct
80 Correct 140 ms 48992 KB Output is correct
81 Correct 187 ms 58460 KB Output is correct
82 Correct 214 ms 61788 KB Output is correct
83 Correct 218 ms 67672 KB Output is correct
84 Correct 99 ms 45920 KB Output is correct
85 Correct 201 ms 62484 KB Output is correct
86 Correct 124 ms 28004 KB Output is correct
87 Correct 274 ms 46892 KB Output is correct
88 Correct 273 ms 46944 KB Output is correct
89 Correct 580 ms 48992 KB Output is correct
90 Correct 320 ms 56284 KB Output is correct
91 Correct 397 ms 53228 KB Output is correct
92 Correct 671 ms 51324 KB Output is correct
93 Correct 414 ms 61576 KB Output is correct
94 Correct 642 ms 65508 KB Output is correct
95 Correct 612 ms 71596 KB Output is correct
96 Correct 506 ms 49668 KB Output is correct
97 Correct 627 ms 57176 KB Output is correct