답안 #409664

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
409664 2021-05-21T10:15:28 Z kwongweng 자매 도시 (APIO20_swap) C++17
13 / 100
156 ms 17780 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ms memset
#define pb push_back
#define F first
#define S second

vector<pair<int, ii> > edges;
int n, m;
vi p(100001), sz(100001, 1), s(100001), e(100001), line(100001, -1), sub(100001);
vi d(100001), g[100001];

int get(int a){
    if (p[a] == a) return a;
    return get(p[a]);
}

void Union(int a, int b, int w){
    int c = get(a); int d = get(b);
    if (c == d){
        if (line[c] == -1) line[c] = w;
        return;
    }
    if (sz[c] < sz[d]){
        swap(c, d); swap(a, b);
    }
    p[d] = c;
    sz[c] += sz[d];
    sub[d] = w;
    if (line[c] == -1 && line[d] == -1){
        if (s[c] == a && b == s[d]){
            s[c] = e[d]; return;
        }
        if (s[c] == a && b == e[d]){
            s[c] = s[d]; return;
        }
        if (e[c] == a && b == s[d]){
            e[c] = e[d]; return;
        }
        if (e[c] == a && b == e[d]){
            e[c] = s[d]; return;
        }
        line[c] = w; return;
    }
    if (line[c] == -1) line[c] = line[d];
}

void init(int N, int M, vi U, vi V, vi W) {
    n = N; m = M;
    FOR(i, 0, m){
        edges.pb({W[i], {U[i], V[i]}});
    }
    FOR(i, 0, n){
        p[i] = i;
        s[i] = i;
        e[i] = i;
    }
    sort(edges.begin(), edges.end());
    FOR(i, 0, m){
        Union(edges[i].S.F, edges[i].S.S, edges[i].F);
    }
    int head;
    FOR(i, 0, n){
        if (p[i] != i){
            g[p[i]].pb(i); continue;
        }
        head = i;
    }
    vi bfs; bfs.pb(head);
    FOR(i, 0, bfs.size()){
        int u = bfs[i];
        for (int v : g[u]){
            d[v] = d[u] + 1;
            bfs.pb(v);
        }
    }
}


int getMinimumFuelCapacity(int x, int y) {
    int u = x; int v = y;
    int maxi = 0;
    if (d[u] < d[v]) swap(u, v);
    while (d[u] > d[v]){
        maxi = max(maxi, sub[u]);
        u = p[u];
    }
    while (u != v){
        maxi = max(maxi, max(sub[u], sub[v]));
        u = p[u]; v = p[v];
    }
    while (line[u] == -1 && p[u] != u){
        maxi = max(maxi, sub[u]); u = p[u];
    }
    if (p[u] == u && line[u] == -1) return -1;
    return max(maxi, line[u]);
    return -1;
}

Compilation message

swap.cpp: In function 'void init(int, int, vi, vi, vi)':
swap.cpp:8:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 | #define FOR(i, a, b) for(int i = a; i < b; i++)
......
   77 |     FOR(i, 0, bfs.size()){
      |         ~~~~~~~~~~~~~~~~               
swap.cpp:77:5: note: in expansion of macro 'FOR'
   77 |     FOR(i, 0, bfs.size()){
      |     ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5324 KB Output is correct
2 Correct 3 ms 5324 KB Output is correct
3 Correct 3 ms 5324 KB Output is correct
4 Correct 4 ms 5324 KB Output is correct
5 Correct 4 ms 5452 KB Output is correct
6 Correct 4 ms 5452 KB Output is correct
7 Correct 5 ms 5416 KB Output is correct
8 Correct 5 ms 5424 KB Output is correct
9 Correct 53 ms 11548 KB Output is correct
10 Correct 65 ms 12900 KB Output is correct
11 Correct 63 ms 12760 KB Output is correct
12 Correct 69 ms 13080 KB Output is correct
13 Correct 56 ms 12432 KB Output is correct
14 Correct 63 ms 11864 KB Output is correct
15 Correct 145 ms 16700 KB Output is correct
16 Correct 141 ms 16572 KB Output is correct
17 Correct 156 ms 16992 KB Output is correct
18 Correct 123 ms 16404 KB Output is correct
19 Correct 74 ms 11624 KB Output is correct
20 Correct 147 ms 17176 KB Output is correct
21 Correct 147 ms 17396 KB Output is correct
22 Correct 155 ms 17780 KB Output is correct
23 Correct 136 ms 17352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5324 KB Output is correct
2 Correct 3 ms 5324 KB Output is correct
3 Correct 119 ms 12680 KB Output is correct
4 Correct 116 ms 12548 KB Output is correct
5 Correct 130 ms 13180 KB Output is correct
6 Correct 117 ms 12580 KB Output is correct
7 Correct 120 ms 12860 KB Output is correct
8 Correct 114 ms 12648 KB Output is correct
9 Correct 119 ms 12668 KB Output is correct
10 Correct 117 ms 12660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5324 KB Output is correct
2 Correct 3 ms 5324 KB Output is correct
3 Correct 3 ms 5324 KB Output is correct
4 Correct 4 ms 5324 KB Output is correct
5 Correct 4 ms 5452 KB Output is correct
6 Correct 4 ms 5452 KB Output is correct
7 Correct 5 ms 5416 KB Output is correct
8 Correct 5 ms 5424 KB Output is correct
9 Correct 4 ms 5364 KB Output is correct
10 Correct 4 ms 5452 KB Output is correct
11 Correct 4 ms 5452 KB Output is correct
12 Correct 4 ms 5452 KB Output is correct
13 Correct 4 ms 5412 KB Output is correct
14 Correct 4 ms 5416 KB Output is correct
15 Correct 4 ms 5412 KB Output is correct
16 Correct 4 ms 5416 KB Output is correct
17 Correct 4 ms 5452 KB Output is correct
18 Correct 4 ms 5452 KB Output is correct
19 Correct 4 ms 5404 KB Output is correct
20 Correct 4 ms 5452 KB Output is correct
21 Correct 4 ms 5368 KB Output is correct
22 Correct 4 ms 5452 KB Output is correct
23 Correct 4 ms 5452 KB Output is correct
24 Incorrect 4 ms 5452 KB Output isn't correct
25 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5364 KB Output is correct
2 Correct 3 ms 5324 KB Output is correct
3 Correct 3 ms 5324 KB Output is correct
4 Correct 3 ms 5324 KB Output is correct
5 Correct 4 ms 5324 KB Output is correct
6 Correct 4 ms 5452 KB Output is correct
7 Correct 4 ms 5452 KB Output is correct
8 Correct 5 ms 5416 KB Output is correct
9 Correct 5 ms 5424 KB Output is correct
10 Correct 53 ms 11548 KB Output is correct
11 Correct 65 ms 12900 KB Output is correct
12 Correct 63 ms 12760 KB Output is correct
13 Correct 69 ms 13080 KB Output is correct
14 Correct 56 ms 12432 KB Output is correct
15 Correct 4 ms 5452 KB Output is correct
16 Correct 4 ms 5452 KB Output is correct
17 Correct 4 ms 5452 KB Output is correct
18 Correct 4 ms 5412 KB Output is correct
19 Correct 4 ms 5416 KB Output is correct
20 Correct 4 ms 5412 KB Output is correct
21 Correct 4 ms 5416 KB Output is correct
22 Correct 4 ms 5452 KB Output is correct
23 Correct 4 ms 5452 KB Output is correct
24 Correct 4 ms 5404 KB Output is correct
25 Correct 4 ms 5452 KB Output is correct
26 Correct 4 ms 5368 KB Output is correct
27 Correct 4 ms 5452 KB Output is correct
28 Correct 4 ms 5452 KB Output is correct
29 Incorrect 4 ms 5452 KB Output isn't correct
30 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5324 KB Output is correct
2 Correct 3 ms 5324 KB Output is correct
3 Correct 3 ms 5324 KB Output is correct
4 Correct 4 ms 5324 KB Output is correct
5 Correct 4 ms 5452 KB Output is correct
6 Correct 4 ms 5452 KB Output is correct
7 Correct 5 ms 5416 KB Output is correct
8 Correct 5 ms 5424 KB Output is correct
9 Correct 53 ms 11548 KB Output is correct
10 Correct 65 ms 12900 KB Output is correct
11 Correct 63 ms 12760 KB Output is correct
12 Correct 69 ms 13080 KB Output is correct
13 Correct 56 ms 12432 KB Output is correct
14 Correct 63 ms 11864 KB Output is correct
15 Correct 145 ms 16700 KB Output is correct
16 Correct 141 ms 16572 KB Output is correct
17 Correct 156 ms 16992 KB Output is correct
18 Correct 123 ms 16404 KB Output is correct
19 Correct 119 ms 12680 KB Output is correct
20 Correct 116 ms 12548 KB Output is correct
21 Correct 130 ms 13180 KB Output is correct
22 Correct 117 ms 12580 KB Output is correct
23 Correct 120 ms 12860 KB Output is correct
24 Correct 114 ms 12648 KB Output is correct
25 Correct 119 ms 12668 KB Output is correct
26 Correct 117 ms 12660 KB Output is correct
27 Correct 4 ms 5452 KB Output is correct
28 Correct 4 ms 5452 KB Output is correct
29 Correct 4 ms 5452 KB Output is correct
30 Correct 4 ms 5412 KB Output is correct
31 Correct 4 ms 5416 KB Output is correct
32 Correct 4 ms 5412 KB Output is correct
33 Correct 4 ms 5416 KB Output is correct
34 Correct 4 ms 5452 KB Output is correct
35 Correct 4 ms 5452 KB Output is correct
36 Correct 11 ms 6380 KB Output is correct
37 Correct 63 ms 12812 KB Output is correct
38 Correct 67 ms 12804 KB Output is correct
39 Correct 64 ms 12804 KB Output is correct
40 Correct 63 ms 12660 KB Output is correct
41 Correct 68 ms 12600 KB Output is correct
42 Correct 58 ms 12124 KB Output is correct
43 Correct 71 ms 13060 KB Output is correct
44 Correct 63 ms 12816 KB Output is correct
45 Correct 54 ms 12164 KB Output is correct
46 Correct 67 ms 13060 KB Output is correct
47 Correct 22 ms 6872 KB Output is correct
48 Correct 147 ms 16832 KB Output is correct
49 Incorrect 145 ms 16988 KB Output isn't correct
50 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5364 KB Output is correct
2 Correct 3 ms 5324 KB Output is correct
3 Correct 3 ms 5324 KB Output is correct
4 Correct 3 ms 5324 KB Output is correct
5 Correct 4 ms 5324 KB Output is correct
6 Correct 4 ms 5452 KB Output is correct
7 Correct 4 ms 5452 KB Output is correct
8 Correct 5 ms 5416 KB Output is correct
9 Correct 5 ms 5424 KB Output is correct
10 Correct 53 ms 11548 KB Output is correct
11 Correct 65 ms 12900 KB Output is correct
12 Correct 63 ms 12760 KB Output is correct
13 Correct 69 ms 13080 KB Output is correct
14 Correct 56 ms 12432 KB Output is correct
15 Correct 63 ms 11864 KB Output is correct
16 Correct 145 ms 16700 KB Output is correct
17 Correct 141 ms 16572 KB Output is correct
18 Correct 156 ms 16992 KB Output is correct
19 Correct 123 ms 16404 KB Output is correct
20 Correct 119 ms 12680 KB Output is correct
21 Correct 116 ms 12548 KB Output is correct
22 Correct 130 ms 13180 KB Output is correct
23 Correct 117 ms 12580 KB Output is correct
24 Correct 120 ms 12860 KB Output is correct
25 Correct 114 ms 12648 KB Output is correct
26 Correct 119 ms 12668 KB Output is correct
27 Correct 117 ms 12660 KB Output is correct
28 Correct 4 ms 5452 KB Output is correct
29 Correct 4 ms 5452 KB Output is correct
30 Correct 4 ms 5452 KB Output is correct
31 Correct 4 ms 5412 KB Output is correct
32 Correct 4 ms 5416 KB Output is correct
33 Correct 4 ms 5412 KB Output is correct
34 Correct 4 ms 5416 KB Output is correct
35 Correct 4 ms 5452 KB Output is correct
36 Correct 4 ms 5452 KB Output is correct
37 Correct 11 ms 6380 KB Output is correct
38 Correct 63 ms 12812 KB Output is correct
39 Correct 67 ms 12804 KB Output is correct
40 Correct 64 ms 12804 KB Output is correct
41 Correct 63 ms 12660 KB Output is correct
42 Correct 68 ms 12600 KB Output is correct
43 Correct 58 ms 12124 KB Output is correct
44 Correct 71 ms 13060 KB Output is correct
45 Correct 63 ms 12816 KB Output is correct
46 Correct 54 ms 12164 KB Output is correct
47 Correct 67 ms 13060 KB Output is correct
48 Correct 22 ms 6872 KB Output is correct
49 Correct 147 ms 16832 KB Output is correct
50 Incorrect 145 ms 16988 KB Output isn't correct
51 Halted 0 ms 0 KB -