답안 #875316

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
875316 2023-11-19T05:36:42 Z sleepntsheep 자매 도시 (APIO20_swap) C++17
100 / 100
218 ms 43224 KB
#include "swap.h"
#include <iostream>
#include <tuple>
#include <algorithm> 
#include <vector>

using namespace std;
#define N 400000
#define M 400000

vector<int> g[N];
int p[N+M], P[N+M], J[N+M], D[N+M], d[N+M], f[N+M], l, V[N+M], W[N+M];

int fnd(int x) { return p[x] == x ? x : p[x] = fnd(p[x]); }

void link(int w, int u, int v)
{
    int ru = fnd(u), rv = fnd(v), sw = f[ru] | f[rv];
    if (ru == rv) { if (f[ru]) return; f[ru] = 1; W[ru] = w; return; }
    if (++d[u] >= 3 || ++d[v] >= 3) sw = 1;
    u = ru, v = rv;
    p[u] = p[v] = l; W[l] = w;
    g[l].push_back(u), g[l].push_back(v);
    f[l++] = sw;
}

void dfs(int u)
{
    V[u] = 1;
    for (auto v : g[u]) if (!V[v])
    {
        P[v] = u; D[v] = D[u] + 1;
        J[v] = (D[u] - D[J[u]] == D[J[u]] - D[J[J[u]]] ? J[J[u]] : u);
        dfs(v);
    }
}

int lca(int u, int v)
{
    if (D[u] < D[v]) return lca(v, u);
    if (fnd(u) != fnd(v)) return -1;
    while (D[u] > D[v])
    {
        if (D[J[u]] >= D[v]) u = J[u];
        else u = P[u];
    }
    while (u != v)
    {
        if (J[u] == J[v]) u = P[u], v = P[v];
        else u = J[u], v = J[v];
    }
    return u;
}

int lowest_switchable(int u)
{
    while (!f[u] && u != P[u])
        if (f[J[u]]) u = P[u];
        else u = J[u];
    return u;
}

void init(int n, int m, std::vector<int> u, std::vector<int> v, std::vector<int> w)
{
    vector<tuple<int, int, int>> e;
    l = n;
    for (int i = 0; i < u.size(); ++i) e.emplace_back(w[i], u[i], v[i]); 
    sort(e.begin(), e.end());
    for (int i = 0; i < n + m; ++i) p[i] = i;
    for (auto &[w, u, v] : e) link(w, u, v);
    for (int i = l; --i; ) if (!V[i]) dfs(P[i] = J[i] = i);
}

int getMinimumFuelCapacity(int x, int y)
{
    int a = lca(x, y);
    if (a == -1) return -1;
    int b = lowest_switchable(a);
    if (!f[b]) return -1;
    return W[b];
}

Compilation message

swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:67:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |     for (int i = 0; i < u.size(); ++i) e.emplace_back(w[i], u[i], v[i]);
      |                     ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18268 KB Output is correct
2 Correct 3 ms 18268 KB Output is correct
3 Correct 4 ms 18268 KB Output is correct
4 Correct 4 ms 18268 KB Output is correct
5 Correct 5 ms 18268 KB Output is correct
6 Correct 4 ms 18372 KB Output is correct
7 Correct 4 ms 18268 KB Output is correct
8 Correct 4 ms 18268 KB Output is correct
9 Correct 41 ms 27244 KB Output is correct
10 Correct 50 ms 30984 KB Output is correct
11 Correct 50 ms 30756 KB Output is correct
12 Correct 53 ms 31428 KB Output is correct
13 Correct 48 ms 34504 KB Output is correct
14 Correct 48 ms 29384 KB Output is correct
15 Correct 113 ms 34756 KB Output is correct
16 Correct 112 ms 34596 KB Output is correct
17 Correct 116 ms 35152 KB Output is correct
18 Correct 163 ms 38332 KB Output is correct
19 Correct 58 ms 26992 KB Output is correct
20 Correct 149 ms 35012 KB Output is correct
21 Correct 145 ms 35052 KB Output is correct
22 Correct 120 ms 35656 KB Output is correct
23 Correct 198 ms 38800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18268 KB Output is correct
2 Correct 3 ms 18268 KB Output is correct
3 Correct 172 ms 37896 KB Output is correct
4 Correct 139 ms 40692 KB Output is correct
5 Correct 167 ms 42228 KB Output is correct
6 Correct 137 ms 40600 KB Output is correct
7 Correct 165 ms 40644 KB Output is correct
8 Correct 146 ms 41608 KB Output is correct
9 Correct 218 ms 40644 KB Output is correct
10 Correct 145 ms 41532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18268 KB Output is correct
2 Correct 3 ms 18268 KB Output is correct
3 Correct 4 ms 18268 KB Output is correct
4 Correct 4 ms 18268 KB Output is correct
5 Correct 5 ms 18268 KB Output is correct
6 Correct 4 ms 18372 KB Output is correct
7 Correct 4 ms 18268 KB Output is correct
8 Correct 4 ms 18268 KB Output is correct
9 Correct 3 ms 18264 KB Output is correct
10 Correct 7 ms 18356 KB Output is correct
11 Correct 4 ms 18268 KB Output is correct
12 Correct 4 ms 18268 KB Output is correct
13 Correct 4 ms 18268 KB Output is correct
14 Correct 4 ms 18268 KB Output is correct
15 Correct 5 ms 18268 KB Output is correct
16 Correct 4 ms 18352 KB Output is correct
17 Correct 4 ms 18264 KB Output is correct
18 Correct 4 ms 18264 KB Output is correct
19 Correct 5 ms 18268 KB Output is correct
20 Correct 4 ms 18268 KB Output is correct
21 Correct 5 ms 18268 KB Output is correct
22 Correct 5 ms 18356 KB Output is correct
23 Correct 4 ms 18268 KB Output is correct
24 Correct 4 ms 18268 KB Output is correct
25 Correct 4 ms 18268 KB Output is correct
26 Correct 5 ms 18296 KB Output is correct
27 Correct 4 ms 18268 KB Output is correct
28 Correct 4 ms 18264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 18264 KB Output is correct
2 Correct 4 ms 18268 KB Output is correct
3 Correct 3 ms 18268 KB Output is correct
4 Correct 4 ms 18268 KB Output is correct
5 Correct 4 ms 18268 KB Output is correct
6 Correct 5 ms 18268 KB Output is correct
7 Correct 4 ms 18372 KB Output is correct
8 Correct 4 ms 18268 KB Output is correct
9 Correct 4 ms 18268 KB Output is correct
10 Correct 41 ms 27244 KB Output is correct
11 Correct 50 ms 30984 KB Output is correct
12 Correct 50 ms 30756 KB Output is correct
13 Correct 53 ms 31428 KB Output is correct
14 Correct 48 ms 34504 KB Output is correct
15 Correct 7 ms 18356 KB Output is correct
16 Correct 4 ms 18268 KB Output is correct
17 Correct 4 ms 18268 KB Output is correct
18 Correct 4 ms 18268 KB Output is correct
19 Correct 4 ms 18268 KB Output is correct
20 Correct 5 ms 18268 KB Output is correct
21 Correct 4 ms 18352 KB Output is correct
22 Correct 4 ms 18264 KB Output is correct
23 Correct 4 ms 18264 KB Output is correct
24 Correct 5 ms 18268 KB Output is correct
25 Correct 4 ms 18268 KB Output is correct
26 Correct 5 ms 18268 KB Output is correct
27 Correct 5 ms 18356 KB Output is correct
28 Correct 4 ms 18268 KB Output is correct
29 Correct 4 ms 18268 KB Output is correct
30 Correct 4 ms 18268 KB Output is correct
31 Correct 5 ms 18296 KB Output is correct
32 Correct 4 ms 18268 KB Output is correct
33 Correct 4 ms 18264 KB Output is correct
34 Correct 10 ms 21788 KB Output is correct
35 Correct 57 ms 31312 KB Output is correct
36 Correct 55 ms 31176 KB Output is correct
37 Correct 55 ms 30944 KB Output is correct
38 Correct 54 ms 30920 KB Output is correct
39 Correct 62 ms 31180 KB Output is correct
40 Correct 51 ms 29936 KB Output is correct
41 Correct 57 ms 31432 KB Output is correct
42 Correct 60 ms 31180 KB Output is correct
43 Correct 73 ms 35484 KB Output is correct
44 Correct 61 ms 31432 KB Output is correct
45 Correct 94 ms 37880 KB Output is correct
46 Correct 57 ms 31168 KB Output is correct
47 Correct 54 ms 31076 KB Output is correct
48 Correct 73 ms 32984 KB Output is correct
49 Correct 51 ms 26820 KB Output is correct
50 Correct 41 ms 25288 KB Output is correct
51 Correct 90 ms 32296 KB Output is correct
52 Correct 94 ms 37716 KB Output is correct
53 Correct 121 ms 38852 KB Output is correct
54 Correct 87 ms 39108 KB Output is correct
55 Correct 75 ms 34772 KB Output is correct
56 Correct 85 ms 39876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18268 KB Output is correct
2 Correct 3 ms 18268 KB Output is correct
3 Correct 4 ms 18268 KB Output is correct
4 Correct 4 ms 18268 KB Output is correct
5 Correct 5 ms 18268 KB Output is correct
6 Correct 4 ms 18372 KB Output is correct
7 Correct 4 ms 18268 KB Output is correct
8 Correct 4 ms 18268 KB Output is correct
9 Correct 41 ms 27244 KB Output is correct
10 Correct 50 ms 30984 KB Output is correct
11 Correct 50 ms 30756 KB Output is correct
12 Correct 53 ms 31428 KB Output is correct
13 Correct 48 ms 34504 KB Output is correct
14 Correct 48 ms 29384 KB Output is correct
15 Correct 113 ms 34756 KB Output is correct
16 Correct 112 ms 34596 KB Output is correct
17 Correct 116 ms 35152 KB Output is correct
18 Correct 163 ms 38332 KB Output is correct
19 Correct 172 ms 37896 KB Output is correct
20 Correct 139 ms 40692 KB Output is correct
21 Correct 167 ms 42228 KB Output is correct
22 Correct 137 ms 40600 KB Output is correct
23 Correct 165 ms 40644 KB Output is correct
24 Correct 146 ms 41608 KB Output is correct
25 Correct 218 ms 40644 KB Output is correct
26 Correct 145 ms 41532 KB Output is correct
27 Correct 7 ms 18356 KB Output is correct
28 Correct 4 ms 18268 KB Output is correct
29 Correct 4 ms 18268 KB Output is correct
30 Correct 4 ms 18268 KB Output is correct
31 Correct 4 ms 18268 KB Output is correct
32 Correct 5 ms 18268 KB Output is correct
33 Correct 4 ms 18352 KB Output is correct
34 Correct 4 ms 18264 KB Output is correct
35 Correct 4 ms 18264 KB Output is correct
36 Correct 10 ms 21788 KB Output is correct
37 Correct 57 ms 31312 KB Output is correct
38 Correct 55 ms 31176 KB Output is correct
39 Correct 55 ms 30944 KB Output is correct
40 Correct 54 ms 30920 KB Output is correct
41 Correct 62 ms 31180 KB Output is correct
42 Correct 51 ms 29936 KB Output is correct
43 Correct 57 ms 31432 KB Output is correct
44 Correct 60 ms 31180 KB Output is correct
45 Correct 73 ms 35484 KB Output is correct
46 Correct 61 ms 31432 KB Output is correct
47 Correct 17 ms 22044 KB Output is correct
48 Correct 158 ms 34900 KB Output is correct
49 Correct 122 ms 34916 KB Output is correct
50 Correct 145 ms 34812 KB Output is correct
51 Correct 136 ms 35020 KB Output is correct
52 Correct 174 ms 34344 KB Output is correct
53 Correct 115 ms 32688 KB Output is correct
54 Correct 146 ms 35848 KB Output is correct
55 Correct 125 ms 35016 KB Output is correct
56 Correct 173 ms 38088 KB Output is correct
57 Correct 140 ms 35724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 18264 KB Output is correct
2 Correct 4 ms 18268 KB Output is correct
3 Correct 3 ms 18268 KB Output is correct
4 Correct 4 ms 18268 KB Output is correct
5 Correct 4 ms 18268 KB Output is correct
6 Correct 5 ms 18268 KB Output is correct
7 Correct 4 ms 18372 KB Output is correct
8 Correct 4 ms 18268 KB Output is correct
9 Correct 4 ms 18268 KB Output is correct
10 Correct 41 ms 27244 KB Output is correct
11 Correct 50 ms 30984 KB Output is correct
12 Correct 50 ms 30756 KB Output is correct
13 Correct 53 ms 31428 KB Output is correct
14 Correct 48 ms 34504 KB Output is correct
15 Correct 48 ms 29384 KB Output is correct
16 Correct 113 ms 34756 KB Output is correct
17 Correct 112 ms 34596 KB Output is correct
18 Correct 116 ms 35152 KB Output is correct
19 Correct 163 ms 38332 KB Output is correct
20 Correct 172 ms 37896 KB Output is correct
21 Correct 139 ms 40692 KB Output is correct
22 Correct 167 ms 42228 KB Output is correct
23 Correct 137 ms 40600 KB Output is correct
24 Correct 165 ms 40644 KB Output is correct
25 Correct 146 ms 41608 KB Output is correct
26 Correct 218 ms 40644 KB Output is correct
27 Correct 145 ms 41532 KB Output is correct
28 Correct 7 ms 18356 KB Output is correct
29 Correct 4 ms 18268 KB Output is correct
30 Correct 4 ms 18268 KB Output is correct
31 Correct 4 ms 18268 KB Output is correct
32 Correct 4 ms 18268 KB Output is correct
33 Correct 5 ms 18268 KB Output is correct
34 Correct 4 ms 18352 KB Output is correct
35 Correct 4 ms 18264 KB Output is correct
36 Correct 4 ms 18264 KB Output is correct
37 Correct 10 ms 21788 KB Output is correct
38 Correct 57 ms 31312 KB Output is correct
39 Correct 55 ms 31176 KB Output is correct
40 Correct 55 ms 30944 KB Output is correct
41 Correct 54 ms 30920 KB Output is correct
42 Correct 62 ms 31180 KB Output is correct
43 Correct 51 ms 29936 KB Output is correct
44 Correct 57 ms 31432 KB Output is correct
45 Correct 60 ms 31180 KB Output is correct
46 Correct 73 ms 35484 KB Output is correct
47 Correct 61 ms 31432 KB Output is correct
48 Correct 17 ms 22044 KB Output is correct
49 Correct 158 ms 34900 KB Output is correct
50 Correct 122 ms 34916 KB Output is correct
51 Correct 145 ms 34812 KB Output is correct
52 Correct 136 ms 35020 KB Output is correct
53 Correct 174 ms 34344 KB Output is correct
54 Correct 115 ms 32688 KB Output is correct
55 Correct 146 ms 35848 KB Output is correct
56 Correct 125 ms 35016 KB Output is correct
57 Correct 173 ms 38088 KB Output is correct
58 Correct 140 ms 35724 KB Output is correct
59 Correct 58 ms 26992 KB Output is correct
60 Correct 149 ms 35012 KB Output is correct
61 Correct 145 ms 35052 KB Output is correct
62 Correct 120 ms 35656 KB Output is correct
63 Correct 198 ms 38800 KB Output is correct
64 Correct 5 ms 18268 KB Output is correct
65 Correct 4 ms 18268 KB Output is correct
66 Correct 5 ms 18268 KB Output is correct
67 Correct 5 ms 18356 KB Output is correct
68 Correct 4 ms 18268 KB Output is correct
69 Correct 4 ms 18268 KB Output is correct
70 Correct 4 ms 18268 KB Output is correct
71 Correct 5 ms 18296 KB Output is correct
72 Correct 4 ms 18268 KB Output is correct
73 Correct 4 ms 18264 KB Output is correct
74 Correct 94 ms 37880 KB Output is correct
75 Correct 57 ms 31168 KB Output is correct
76 Correct 54 ms 31076 KB Output is correct
77 Correct 73 ms 32984 KB Output is correct
78 Correct 51 ms 26820 KB Output is correct
79 Correct 41 ms 25288 KB Output is correct
80 Correct 90 ms 32296 KB Output is correct
81 Correct 94 ms 37716 KB Output is correct
82 Correct 121 ms 38852 KB Output is correct
83 Correct 87 ms 39108 KB Output is correct
84 Correct 75 ms 34772 KB Output is correct
85 Correct 85 ms 39876 KB Output is correct
86 Correct 55 ms 26316 KB Output is correct
87 Correct 157 ms 35016 KB Output is correct
88 Correct 123 ms 35048 KB Output is correct
89 Correct 151 ms 38088 KB Output is correct
90 Correct 117 ms 30660 KB Output is correct
91 Correct 120 ms 32828 KB Output is correct
92 Correct 211 ms 37640 KB Output is correct
93 Correct 150 ms 41892 KB Output is correct
94 Correct 174 ms 43224 KB Output is correct
95 Correct 148 ms 42692 KB Output is correct
96 Correct 177 ms 38408 KB Output is correct
97 Correct 179 ms 42140 KB Output is correct