답안 #699475

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
699475 2023-02-17T05:00:10 Z He_Huanglu 자매 도시 (APIO20_swap) C++17
100 / 100
261 ms 11612 KB
#include "swap.h"
#include <bits/stdc++.h>
#define ii pair<int, int>
#define fi first
#define se second
using namespace std;

const int N = 2e5 + 5;
int n, m, par[N], p1[N], p2[N], past[N], cur[N];
vector <pair<int, ii> > edg;

int root(int u)
{
    return (par[u] < 0) ? u : root(par[u]);
}

bool check(int u, int v, int mid)
{
    while (par[u] > 0 && past[u] <= mid) u = par[u];
    while (par[v] > 0 && past[v] <= mid) v = par[v];
    if(u == v && cur[u] && cur[u] <= mid) return 1;
    return 0;
}

void join(int u, int v, int w)
{
    int r1 = u, r2 = v;
    u = root(u), v = root(v);
    if(u == v)
    {
        if(!cur[u]) cur[u] = w;
        return ;
    }
    if(par[u] > par[v]) swap(u, v), swap(r1, r2);
    par[u] += par[v];
    past[v] = w;

    if(par[v] == -1 && !cur[u])
    {
        if(p1[u] == r1) p1[u] = v;
        else if(p2[u] == r1) p2[u] = v;
        else cur[u] = w;
    }
    else if(!cur[u] && !cur[v])
    {
             if(p1[u] == r1 && p1[v] == r2) p1[u] = p2[v];
        else if(p1[u] == r1 && p2[v] == r2) p1[u] = p1[v];
        else if(p2[u] == r1 && p1[v] == r2) p2[u] = p2[v];
        else if(p2[u] == r1 && p2[v] == r2) p2[u] = p1[v];
        else cur[u] = w;
    }
    else if(!cur[u]) cur[u] = w;
    par[v] = u;
}

void init(int n, int m, vector <int> U, vector <int> V, vector <int> W)
{
    for(int i = 1; i <= n; i++)
    {
        p1[i] = p2[i] = i;
        par[i] = -1;
    }
    for(int i = 0; i < m; i++) edg.push_back({W[i], {++U[i], ++V[i]}});
    sort(edg.begin(), edg.end());
    for(auto [w, e] : edg) join(e.fi, e.se, w);
}

int getMinimumFuelCapacity(int u, int v)
{
    u++, v++;
    int l = 1, r = edg[edg.size() - 1].fi, mid, ans = -1;
    while (l <= r)
    {
        mid = (l + r) / 2;
        if(check(u, v, mid)) ans = mid, r = mid - 1;
        else l = mid + 1;
    }
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 324 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 40 ms 5428 KB Output is correct
10 Correct 47 ms 6124 KB Output is correct
11 Correct 41 ms 5980 KB Output is correct
12 Correct 43 ms 6224 KB Output is correct
13 Correct 43 ms 6276 KB Output is correct
14 Correct 54 ms 5656 KB Output is correct
15 Correct 208 ms 7868 KB Output is correct
16 Correct 210 ms 7756 KB Output is correct
17 Correct 210 ms 8244 KB Output is correct
18 Correct 129 ms 7996 KB Output is correct
19 Correct 118 ms 5512 KB Output is correct
20 Correct 211 ms 8928 KB Output is correct
21 Correct 216 ms 9160 KB Output is correct
22 Correct 216 ms 9412 KB Output is correct
23 Correct 155 ms 9420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 136 ms 8548 KB Output is correct
4 Correct 147 ms 8612 KB Output is correct
5 Correct 142 ms 8900 KB Output is correct
6 Correct 134 ms 8480 KB Output is correct
7 Correct 153 ms 8792 KB Output is correct
8 Correct 135 ms 8500 KB Output is correct
9 Correct 140 ms 8628 KB Output is correct
10 Correct 141 ms 8592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 324 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 316 KB Output is correct
10 Correct 1 ms 324 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 316 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 324 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 320 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 448 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 1 ms 328 KB Output is correct
10 Correct 40 ms 5428 KB Output is correct
11 Correct 47 ms 6124 KB Output is correct
12 Correct 41 ms 5980 KB Output is correct
13 Correct 43 ms 6224 KB Output is correct
14 Correct 43 ms 6276 KB Output is correct
15 Correct 1 ms 324 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 324 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 320 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 448 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 6 ms 1428 KB Output is correct
35 Correct 54 ms 6208 KB Output is correct
36 Correct 42 ms 6304 KB Output is correct
37 Correct 41 ms 6472 KB Output is correct
38 Correct 42 ms 6672 KB Output is correct
39 Correct 40 ms 6580 KB Output is correct
40 Correct 39 ms 6088 KB Output is correct
41 Correct 52 ms 6244 KB Output is correct
42 Correct 41 ms 6300 KB Output is correct
43 Correct 38 ms 6224 KB Output is correct
44 Correct 46 ms 6716 KB Output is correct
45 Correct 67 ms 8676 KB Output is correct
46 Correct 39 ms 6344 KB Output is correct
47 Correct 41 ms 6392 KB Output is correct
48 Correct 42 ms 6916 KB Output is correct
49 Correct 69 ms 7948 KB Output is correct
50 Correct 48 ms 5572 KB Output is correct
51 Correct 55 ms 6976 KB Output is correct
52 Correct 70 ms 9196 KB Output is correct
53 Correct 79 ms 9624 KB Output is correct
54 Correct 93 ms 10188 KB Output is correct
55 Correct 40 ms 6300 KB Output is correct
56 Correct 73 ms 9492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 324 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 40 ms 5428 KB Output is correct
10 Correct 47 ms 6124 KB Output is correct
11 Correct 41 ms 5980 KB Output is correct
12 Correct 43 ms 6224 KB Output is correct
13 Correct 43 ms 6276 KB Output is correct
14 Correct 54 ms 5656 KB Output is correct
15 Correct 208 ms 7868 KB Output is correct
16 Correct 210 ms 7756 KB Output is correct
17 Correct 210 ms 8244 KB Output is correct
18 Correct 129 ms 7996 KB Output is correct
19 Correct 136 ms 8548 KB Output is correct
20 Correct 147 ms 8612 KB Output is correct
21 Correct 142 ms 8900 KB Output is correct
22 Correct 134 ms 8480 KB Output is correct
23 Correct 153 ms 8792 KB Output is correct
24 Correct 135 ms 8500 KB Output is correct
25 Correct 140 ms 8628 KB Output is correct
26 Correct 141 ms 8592 KB Output is correct
27 Correct 1 ms 324 KB Output is correct
28 Correct 1 ms 320 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 316 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 324 KB Output is correct
36 Correct 6 ms 1428 KB Output is correct
37 Correct 54 ms 6208 KB Output is correct
38 Correct 42 ms 6304 KB Output is correct
39 Correct 41 ms 6472 KB Output is correct
40 Correct 42 ms 6672 KB Output is correct
41 Correct 40 ms 6580 KB Output is correct
42 Correct 39 ms 6088 KB Output is correct
43 Correct 52 ms 6244 KB Output is correct
44 Correct 41 ms 6300 KB Output is correct
45 Correct 38 ms 6224 KB Output is correct
46 Correct 46 ms 6716 KB Output is correct
47 Correct 22 ms 1860 KB Output is correct
48 Correct 193 ms 8776 KB Output is correct
49 Correct 208 ms 9060 KB Output is correct
50 Correct 187 ms 9128 KB Output is correct
51 Correct 217 ms 9148 KB Output is correct
52 Correct 174 ms 8880 KB Output is correct
53 Correct 204 ms 8380 KB Output is correct
54 Correct 219 ms 9408 KB Output is correct
55 Correct 189 ms 8784 KB Output is correct
56 Correct 142 ms 8780 KB Output is correct
57 Correct 210 ms 9896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 1 ms 328 KB Output is correct
10 Correct 40 ms 5428 KB Output is correct
11 Correct 47 ms 6124 KB Output is correct
12 Correct 41 ms 5980 KB Output is correct
13 Correct 43 ms 6224 KB Output is correct
14 Correct 43 ms 6276 KB Output is correct
15 Correct 54 ms 5656 KB Output is correct
16 Correct 208 ms 7868 KB Output is correct
17 Correct 210 ms 7756 KB Output is correct
18 Correct 210 ms 8244 KB Output is correct
19 Correct 129 ms 7996 KB Output is correct
20 Correct 136 ms 8548 KB Output is correct
21 Correct 147 ms 8612 KB Output is correct
22 Correct 142 ms 8900 KB Output is correct
23 Correct 134 ms 8480 KB Output is correct
24 Correct 153 ms 8792 KB Output is correct
25 Correct 135 ms 8500 KB Output is correct
26 Correct 140 ms 8628 KB Output is correct
27 Correct 141 ms 8592 KB Output is correct
28 Correct 1 ms 324 KB Output is correct
29 Correct 1 ms 320 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 316 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 324 KB Output is correct
37 Correct 6 ms 1428 KB Output is correct
38 Correct 54 ms 6208 KB Output is correct
39 Correct 42 ms 6304 KB Output is correct
40 Correct 41 ms 6472 KB Output is correct
41 Correct 42 ms 6672 KB Output is correct
42 Correct 40 ms 6580 KB Output is correct
43 Correct 39 ms 6088 KB Output is correct
44 Correct 52 ms 6244 KB Output is correct
45 Correct 41 ms 6300 KB Output is correct
46 Correct 38 ms 6224 KB Output is correct
47 Correct 46 ms 6716 KB Output is correct
48 Correct 22 ms 1860 KB Output is correct
49 Correct 193 ms 8776 KB Output is correct
50 Correct 208 ms 9060 KB Output is correct
51 Correct 187 ms 9128 KB Output is correct
52 Correct 217 ms 9148 KB Output is correct
53 Correct 174 ms 8880 KB Output is correct
54 Correct 204 ms 8380 KB Output is correct
55 Correct 219 ms 9408 KB Output is correct
56 Correct 189 ms 8784 KB Output is correct
57 Correct 142 ms 8780 KB Output is correct
58 Correct 210 ms 9896 KB Output is correct
59 Correct 118 ms 5512 KB Output is correct
60 Correct 211 ms 8928 KB Output is correct
61 Correct 216 ms 9160 KB Output is correct
62 Correct 216 ms 9412 KB Output is correct
63 Correct 155 ms 9420 KB Output is correct
64 Correct 1 ms 340 KB Output is correct
65 Correct 1 ms 320 KB Output is correct
66 Correct 1 ms 340 KB Output is correct
67 Correct 1 ms 340 KB Output is correct
68 Correct 1 ms 340 KB Output is correct
69 Correct 1 ms 468 KB Output is correct
70 Correct 1 ms 468 KB Output is correct
71 Correct 1 ms 448 KB Output is correct
72 Correct 1 ms 340 KB Output is correct
73 Correct 1 ms 468 KB Output is correct
74 Correct 67 ms 8676 KB Output is correct
75 Correct 39 ms 6344 KB Output is correct
76 Correct 41 ms 6392 KB Output is correct
77 Correct 42 ms 6916 KB Output is correct
78 Correct 69 ms 7948 KB Output is correct
79 Correct 48 ms 5572 KB Output is correct
80 Correct 55 ms 6976 KB Output is correct
81 Correct 70 ms 9196 KB Output is correct
82 Correct 79 ms 9624 KB Output is correct
83 Correct 93 ms 10188 KB Output is correct
84 Correct 40 ms 6300 KB Output is correct
85 Correct 73 ms 9492 KB Output is correct
86 Correct 58 ms 3940 KB Output is correct
87 Correct 194 ms 8824 KB Output is correct
88 Correct 182 ms 9076 KB Output is correct
89 Correct 162 ms 9164 KB Output is correct
90 Correct 187 ms 9812 KB Output is correct
91 Correct 198 ms 9440 KB Output is correct
92 Correct 181 ms 9660 KB Output is correct
93 Correct 230 ms 10992 KB Output is correct
94 Correct 222 ms 11544 KB Output is correct
95 Correct 261 ms 11612 KB Output is correct
96 Correct 134 ms 8872 KB Output is correct
97 Correct 191 ms 10364 KB Output is correct