답안 #555010

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
555010 2022-04-29T23:16:01 Z Zhora_004 자매 도시 (APIO20_swap) C++17
100 / 100
283 ms 19764 KB
#include "swap.h"
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_set>
#include <queue>
#include <deque>
#include <string>
#include <sstream>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <stack>
#include <cstdio>
#include <climits>
#include <tuple>
#include <ctime>
#include <cstring>
#include <numeric>
#include <functional>
#include <chrono>
#include <cassert>
#include <bitset>
#include <fstream>
#define sz(a) ((int)((a).size()))
// printf("%.10f\n", ans);
using ll = long long;
using namespace std;
const int inf = 1e9;

struct Edge {
	int w, u, v;
};

bool comp(Edge& e1, Edge& e2)
{
	return e1.w < e2.w;
}

int n, m, q, x, y;
vector<int> a, b, c, par, cap, start, finish, neww, noline;
vector<Edge> edges;

int find_par(int u)
{
	if (u == par[u]) return u;
	return find_par(par[u]);
}

void union_sets(int p1, int p2, int u, int v, int w)
{
    if (cap[p1] < cap[p2]) swap(p1, p2);
    par[p2] = p1;
    cap[p1] += cap[p2];
    neww[p2] = w;
    if (noline[p1] == noline[p2] && noline[p1] == 2 * inf)
    {
        if (finish[p1] == u && start[p2] == v) finish[p1] = finish[p2];
        else if (finish[p1] == u && finish[p2] == v) finish[p1] = start[p2];
        else if (start[p1] == u && start[p2] == v) start[p1] = finish[p2];
        else if (start[p1] == u && finish[p2] == v) start[p1] = start[p2];

        else if (finish[p1] == v && start[p2] == u) finish[p1] = finish[p2];
        else if (finish[p1] == v && finish[p2] == u) finish[p1] = start[p2];
        else if (start[p1] == v && start[p2] == u) start[p1] = finish[p2];
        else if (start[p1] == v && finish[p2] == u) start[p1] = start[p2];
        else
        {
            start[p1] = finish[p1] = -1;
            noline[p1] = w;
        }
    }
    else
    {
        start[p1] = finish[p1] = -1;
        noline[p1] = min(noline[p1], w);
    }
}

void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
	n = N;
	m = M;
	a = U;
	b = V;
	c = W;
    for (int i = 0; i < m; i++) edges.push_back({ c[i], a[i], b[i] });
    sort(edges.begin(), edges.end(), comp);
    par = start = finish = neww = vector<int>(n);
    cap = vector<int>(n, 1);
    noline = vector<int>(n, 2 * inf);
    iota(par.begin(), par.end(), 0);
    iota(start.begin(), start.end(), 0);
    iota(finish.begin(), finish.end(), 0);
    for (int i = 0; i < m; i++)
    {
        int w = edges[i].w, u = edges[i].u, v = edges[i].v;
        int p1 = find_par(u), p2 = find_par(v);
        if (p1 == p2)
        {
            start[p1] = finish[p1] = -1;
            noline[p1] = min(noline[p1], w);
        }
        else union_sets(p1, p2, u, v, w);
    }
}

int find_par1(int u, int& mid)
{
    if (u == par[u] || neww[u] > mid) return u;
    return find_par1(par[u], mid);
}

bool ok(int mid, int u, int v)
{
    int p1 = find_par1(u, mid), p2 = find_par1(v, mid);
    if (p1 != p2 || noline[p1] > mid) return 0;
    return 1;
}

int getMinimumFuelCapacity(int X, int Y) {
	x = X;
	y = Y;
    int l = 0, r = inf + 1;
    while (l + 1 < r)
    {
        int mid = (l + r) >> 1;
        if (ok(mid, x, y)) r = mid;
        else l = mid;
    }
    if (r == inf + 1) r = -1;
    return r;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 312 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 308 KB Output is correct
9 Correct 39 ms 7464 KB Output is correct
10 Correct 47 ms 9024 KB Output is correct
11 Correct 48 ms 8928 KB Output is correct
12 Correct 50 ms 9424 KB Output is correct
13 Correct 48 ms 9408 KB Output is correct
14 Correct 53 ms 7748 KB Output is correct
15 Correct 223 ms 11600 KB Output is correct
16 Correct 216 ms 11412 KB Output is correct
17 Correct 225 ms 11748 KB Output is correct
18 Correct 148 ms 11816 KB Output is correct
19 Correct 132 ms 6772 KB Output is correct
20 Correct 222 ms 12992 KB Output is correct
21 Correct 232 ms 13096 KB Output is correct
22 Correct 246 ms 13512 KB Output is correct
23 Correct 150 ms 13504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 149 ms 12764 KB Output is correct
4 Correct 148 ms 13980 KB Output is correct
5 Correct 149 ms 14168 KB Output is correct
6 Correct 144 ms 13912 KB Output is correct
7 Correct 161 ms 14216 KB Output is correct
8 Correct 143 ms 13820 KB Output is correct
9 Correct 146 ms 14040 KB Output is correct
10 Correct 147 ms 13844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 312 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 308 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 332 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 340 KB Output is correct
19 Correct 1 ms 312 KB Output is correct
20 Correct 1 ms 340 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 340 KB Output is correct
25 Correct 1 ms 340 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
# 결과 실행 시간 메모리 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 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 308 KB Output is correct
10 Correct 39 ms 7464 KB Output is correct
11 Correct 47 ms 9024 KB Output is correct
12 Correct 48 ms 8928 KB Output is correct
13 Correct 50 ms 9424 KB Output is correct
14 Correct 48 ms 9408 KB Output is correct
15 Correct 1 ms 340 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 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 332 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 312 KB Output is correct
25 Correct 1 ms 340 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 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 6 ms 1428 KB Output is correct
35 Correct 47 ms 9156 KB Output is correct
36 Correct 48 ms 9144 KB Output is correct
37 Correct 47 ms 9124 KB Output is correct
38 Correct 48 ms 8992 KB Output is correct
39 Correct 46 ms 8980 KB Output is correct
40 Correct 43 ms 8252 KB Output is correct
41 Correct 49 ms 9392 KB Output is correct
42 Correct 47 ms 9124 KB Output is correct
43 Correct 45 ms 9124 KB Output is correct
44 Correct 49 ms 9376 KB Output is correct
45 Correct 71 ms 12100 KB Output is correct
46 Correct 47 ms 9140 KB Output is correct
47 Correct 49 ms 9120 KB Output is correct
48 Correct 51 ms 9784 KB Output is correct
49 Correct 68 ms 11736 KB Output is correct
50 Correct 52 ms 8480 KB Output is correct
51 Correct 62 ms 10460 KB Output is correct
52 Correct 83 ms 13560 KB Output is correct
53 Correct 87 ms 14780 KB Output is correct
54 Correct 98 ms 15924 KB Output is correct
55 Correct 46 ms 9140 KB Output is correct
56 Correct 86 ms 14436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 312 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 308 KB Output is correct
9 Correct 39 ms 7464 KB Output is correct
10 Correct 47 ms 9024 KB Output is correct
11 Correct 48 ms 8928 KB Output is correct
12 Correct 50 ms 9424 KB Output is correct
13 Correct 48 ms 9408 KB Output is correct
14 Correct 53 ms 7748 KB Output is correct
15 Correct 223 ms 11600 KB Output is correct
16 Correct 216 ms 11412 KB Output is correct
17 Correct 225 ms 11748 KB Output is correct
18 Correct 148 ms 11816 KB Output is correct
19 Correct 149 ms 12764 KB Output is correct
20 Correct 148 ms 13980 KB Output is correct
21 Correct 149 ms 14168 KB Output is correct
22 Correct 144 ms 13912 KB Output is correct
23 Correct 161 ms 14216 KB Output is correct
24 Correct 143 ms 13820 KB Output is correct
25 Correct 146 ms 14040 KB Output is correct
26 Correct 147 ms 13844 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 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 332 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 340 KB Output is correct
36 Correct 6 ms 1428 KB Output is correct
37 Correct 47 ms 9156 KB Output is correct
38 Correct 48 ms 9144 KB Output is correct
39 Correct 47 ms 9124 KB Output is correct
40 Correct 48 ms 8992 KB Output is correct
41 Correct 46 ms 8980 KB Output is correct
42 Correct 43 ms 8252 KB Output is correct
43 Correct 49 ms 9392 KB Output is correct
44 Correct 47 ms 9124 KB Output is correct
45 Correct 45 ms 9124 KB Output is correct
46 Correct 49 ms 9376 KB Output is correct
47 Correct 22 ms 1900 KB Output is correct
48 Correct 221 ms 13976 KB Output is correct
49 Correct 215 ms 13956 KB Output is correct
50 Correct 219 ms 13964 KB Output is correct
51 Correct 220 ms 14004 KB Output is correct
52 Correct 211 ms 13564 KB Output is correct
53 Correct 221 ms 12180 KB Output is correct
54 Correct 236 ms 14868 KB Output is correct
55 Correct 235 ms 13972 KB Output is correct
56 Correct 152 ms 14028 KB Output is correct
57 Correct 224 ms 14872 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 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 308 KB Output is correct
10 Correct 39 ms 7464 KB Output is correct
11 Correct 47 ms 9024 KB Output is correct
12 Correct 48 ms 8928 KB Output is correct
13 Correct 50 ms 9424 KB Output is correct
14 Correct 48 ms 9408 KB Output is correct
15 Correct 53 ms 7748 KB Output is correct
16 Correct 223 ms 11600 KB Output is correct
17 Correct 216 ms 11412 KB Output is correct
18 Correct 225 ms 11748 KB Output is correct
19 Correct 148 ms 11816 KB Output is correct
20 Correct 149 ms 12764 KB Output is correct
21 Correct 148 ms 13980 KB Output is correct
22 Correct 149 ms 14168 KB Output is correct
23 Correct 144 ms 13912 KB Output is correct
24 Correct 161 ms 14216 KB Output is correct
25 Correct 143 ms 13820 KB Output is correct
26 Correct 146 ms 14040 KB Output is correct
27 Correct 147 ms 13844 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 332 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 340 KB Output is correct
37 Correct 6 ms 1428 KB Output is correct
38 Correct 47 ms 9156 KB Output is correct
39 Correct 48 ms 9144 KB Output is correct
40 Correct 47 ms 9124 KB Output is correct
41 Correct 48 ms 8992 KB Output is correct
42 Correct 46 ms 8980 KB Output is correct
43 Correct 43 ms 8252 KB Output is correct
44 Correct 49 ms 9392 KB Output is correct
45 Correct 47 ms 9124 KB Output is correct
46 Correct 45 ms 9124 KB Output is correct
47 Correct 49 ms 9376 KB Output is correct
48 Correct 22 ms 1900 KB Output is correct
49 Correct 221 ms 13976 KB Output is correct
50 Correct 215 ms 13956 KB Output is correct
51 Correct 219 ms 13964 KB Output is correct
52 Correct 220 ms 14004 KB Output is correct
53 Correct 211 ms 13564 KB Output is correct
54 Correct 221 ms 12180 KB Output is correct
55 Correct 236 ms 14868 KB Output is correct
56 Correct 235 ms 13972 KB Output is correct
57 Correct 152 ms 14028 KB Output is correct
58 Correct 224 ms 14872 KB Output is correct
59 Correct 132 ms 6772 KB Output is correct
60 Correct 222 ms 12992 KB Output is correct
61 Correct 232 ms 13096 KB Output is correct
62 Correct 246 ms 13512 KB Output is correct
63 Correct 150 ms 13504 KB Output is correct
64 Correct 1 ms 312 KB Output is correct
65 Correct 1 ms 340 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 340 KB Output is correct
70 Correct 1 ms 340 KB Output is correct
71 Correct 1 ms 340 KB Output is correct
72 Correct 1 ms 340 KB Output is correct
73 Correct 1 ms 340 KB Output is correct
74 Correct 71 ms 12100 KB Output is correct
75 Correct 47 ms 9140 KB Output is correct
76 Correct 49 ms 9120 KB Output is correct
77 Correct 51 ms 9784 KB Output is correct
78 Correct 68 ms 11736 KB Output is correct
79 Correct 52 ms 8480 KB Output is correct
80 Correct 62 ms 10460 KB Output is correct
81 Correct 83 ms 13560 KB Output is correct
82 Correct 87 ms 14780 KB Output is correct
83 Correct 98 ms 15924 KB Output is correct
84 Correct 46 ms 9140 KB Output is correct
85 Correct 86 ms 14436 KB Output is correct
86 Correct 57 ms 5400 KB Output is correct
87 Correct 213 ms 13972 KB Output is correct
88 Correct 225 ms 14072 KB Output is correct
89 Correct 177 ms 14176 KB Output is correct
90 Correct 193 ms 15676 KB Output is correct
91 Correct 197 ms 15120 KB Output is correct
92 Correct 192 ms 15240 KB Output is correct
93 Correct 246 ms 18312 KB Output is correct
94 Correct 232 ms 19336 KB Output is correct
95 Correct 283 ms 19764 KB Output is correct
96 Correct 151 ms 14216 KB Output is correct
97 Correct 203 ms 16936 KB Output is correct