답안 #1059964

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1059964 2024-08-15T09:42:53 Z mispertion 자매 도시 (APIO20_swap) C++17
30 / 100
863 ms 524288 KB
#include "swap.h"
#include <bits/stdc++.h>
#include <vector>

#pragma gcc optimize("unroll-loops")
#pragma gcc optimize("o3")
#pragma gcc optimize("Ofast")

using namespace std;

const int N = 1e5 + 5;
const int infi = INT_MAX;

typedef pair<int, int> pii;

#define mp make_pair
#define pb push_back
#define F first
#define S second

struct dsu{
    int pw[N];
    vector<pii> p[N], cnt1[N], cnt2[N], sz[N];
    vector<int> st[N];

    dsu(int n){
        for(int i = 1; i <= n; i++){
            st[i] = {i};
            p[i] = {{-1, i}};
            sz[i] = {{-1, 1}};
            cnt1[i] = {{-1, 0}};
            cnt2[i] = {{-1, 0}};
            pw[i] = 0;
        }
    }
    dsu(){

    }

    int getp(int v, int t){
        int pos = (--upper_bound(p[v].begin(), p[v].end(),mp(t, infi))) - p[v].begin();
        return p[v][pos].S;
    }

    void upd(int v, int nw, int t){
        if(nw == 1)
            cnt1[v].pb({t, cnt1[v].back().S - 1});
        if(nw == 2)
            cnt2[v].pb({t, cnt2[v].back().S - 1});
        if(nw == 0)
            cnt1[v].pb({t, cnt1[v].back().S + 1});
        if(nw == 1)
            cnt2[v].pb({t, cnt2[v].back().S + 1});
    }

    void uni(int a, int b, int t){
        int n1 = pw[a];
        int n2 = pw[b];
        //cout << n1 << ' ' << n2 << '\n';
        pw[a]++;
        pw[b]++;
        a = getp(a, t);
        b = getp(b, t);
        if(a == b){
            upd(a, n1, t);
            upd(a, n2, t);
            return;
        }
        if(sz[a] > sz[b])
            swap(a, b);
        sz[b].pb({t, sz[b].back().S + sz[a].back().S});
        for(auto e : st[a]){
            st[b].pb(e);
            p[e].pb({t, b});
        }
        cnt1[b].pb({t, cnt1[a].back().S + cnt1[b].back().S});
        cnt2[b].pb({t, cnt2[a].back().S + cnt2[b].back().S});
        upd(b, n1, t);
        upd(b, n2, t);
    }
};

int n, m;
vector<pair<int, pair<int, int>>> edges;
dsu ds;

void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
    n = N;
    m = M;
    for(int i = 0; i < m; i++){
        U[i]++;
        V[i]++;
        edges.pb({W[i], {V[i], U[i]}});
    }
    sort(edges.begin(), edges.end());
    ds = dsu(n);
    for(int i = 0; i < m; i++){
        ds.uni(edges[i].S.F, edges[i].S.S, i);
        /*cout << "comp: ";
        for(int j = 1; j <= n; j++){
            cout << ds.getp(j, infi) << ' ';
        }
        cout << '\n';
        cout << "cnt1: ";
        for(int i = 1; i <= n; i++){
            cout << ds.cnt1[ds.getp(i, infi)].back().S << ' ';
        }
        cout << '\n';
        cout << "cnt2: ";
        for(int i = 1; i <= n; i++){
            cout << ds.cnt2[ds.getp(i, infi)].back().S << ' ';
        }
        cout << '\n';*/
    }
}

int getMinimumFuelCapacity(int X, int Y) {
    X++;
    Y++;
    int lo = -1, hi = m;
    while(lo + 1 < hi){
        int m = (lo + hi) / 2;
        int x = ds.getp(X, m), y = ds.getp(Y, m);
        if(x != y){
            lo = m;
            continue;
        }
        int c1 = (--upper_bound(ds.cnt1[x].begin(), ds.cnt1[x].end(),mp(m, infi)))->S;
        int c2 = (--upper_bound(ds.cnt2[x].begin(), ds.cnt2[x].end(),mp(m, infi)))->S;
        int sz = (--upper_bound(ds.sz[x].begin(), ds.sz[x].end(),mp(m, infi)))->S;
        if(c1 == 2 && c2 == sz - 2){
            lo = m;
            continue;
        }
        hi = m;
    }
    if(hi == m)
        return -1;
    return edges[hi].F;
}

Compilation message

swap.cpp:5: warning: ignoring '#pragma gcc optimize' [-Wunknown-pragmas]
    5 | #pragma gcc optimize("unroll-loops")
      | 
swap.cpp:6: warning: ignoring '#pragma gcc optimize' [-Wunknown-pragmas]
    6 | #pragma gcc optimize("o3")
      | 
swap.cpp:7: warning: ignoring '#pragma gcc optimize' [-Wunknown-pragmas]
    7 | #pragma gcc optimize("Ofast")
      |
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 24668 KB Output is correct
2 Correct 10 ms 24668 KB Output is correct
3 Correct 10 ms 24668 KB Output is correct
4 Correct 10 ms 24792 KB Output is correct
5 Correct 11 ms 24920 KB Output is correct
6 Correct 12 ms 24892 KB Output is correct
7 Correct 13 ms 24924 KB Output is correct
8 Correct 10 ms 24924 KB Output is correct
9 Correct 128 ms 58472 KB Output is correct
10 Correct 156 ms 65476 KB Output is correct
11 Correct 171 ms 65136 KB Output is correct
12 Correct 172 ms 68888 KB Output is correct
13 Correct 108 ms 56516 KB Output is correct
14 Correct 134 ms 59080 KB Output is correct
15 Correct 286 ms 69872 KB Output is correct
16 Correct 316 ms 68216 KB Output is correct
17 Correct 287 ms 71464 KB Output is correct
18 Correct 259 ms 59944 KB Output is correct
19 Correct 112 ms 35860 KB Output is correct
20 Correct 304 ms 71388 KB Output is correct
21 Correct 318 ms 70016 KB Output is correct
22 Correct 313 ms 72752 KB Output is correct
23 Correct 282 ms 58912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 24668 KB Output is correct
2 Correct 10 ms 24668 KB Output is correct
3 Correct 415 ms 52580 KB Output is correct
4 Correct 415 ms 53800 KB Output is correct
5 Correct 439 ms 53336 KB Output is correct
6 Correct 459 ms 53800 KB Output is correct
7 Correct 417 ms 53572 KB Output is correct
8 Correct 433 ms 52472 KB Output is correct
9 Correct 449 ms 53432 KB Output is correct
10 Correct 465 ms 52400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 24668 KB Output is correct
2 Correct 10 ms 24668 KB Output is correct
3 Correct 10 ms 24668 KB Output is correct
4 Correct 10 ms 24792 KB Output is correct
5 Correct 11 ms 24920 KB Output is correct
6 Correct 12 ms 24892 KB Output is correct
7 Correct 13 ms 24924 KB Output is correct
8 Correct 10 ms 24924 KB Output is correct
9 Correct 10 ms 24668 KB Output is correct
10 Correct 18 ms 24912 KB Output is correct
11 Correct 16 ms 24924 KB Output is correct
12 Correct 11 ms 24920 KB Output is correct
13 Correct 12 ms 24924 KB Output is correct
14 Correct 10 ms 25024 KB Output is correct
15 Correct 11 ms 24976 KB Output is correct
16 Correct 11 ms 24960 KB Output is correct
17 Correct 17 ms 24912 KB Output is correct
18 Correct 11 ms 24924 KB Output is correct
19 Correct 12 ms 24860 KB Output is correct
20 Correct 11 ms 24900 KB Output is correct
21 Correct 11 ms 24924 KB Output is correct
22 Correct 13 ms 24668 KB Output is correct
23 Correct 11 ms 24920 KB Output is correct
24 Correct 11 ms 24924 KB Output is correct
25 Correct 11 ms 24956 KB Output is correct
26 Correct 12 ms 25128 KB Output is correct
27 Correct 11 ms 24924 KB Output is correct
28 Correct 13 ms 25156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 24668 KB Output is correct
2 Correct 10 ms 24668 KB Output is correct
3 Correct 10 ms 24668 KB Output is correct
4 Correct 10 ms 24668 KB Output is correct
5 Correct 10 ms 24792 KB Output is correct
6 Correct 11 ms 24920 KB Output is correct
7 Correct 12 ms 24892 KB Output is correct
8 Correct 13 ms 24924 KB Output is correct
9 Correct 10 ms 24924 KB Output is correct
10 Correct 128 ms 58472 KB Output is correct
11 Correct 156 ms 65476 KB Output is correct
12 Correct 171 ms 65136 KB Output is correct
13 Correct 172 ms 68888 KB Output is correct
14 Correct 108 ms 56516 KB Output is correct
15 Correct 18 ms 24912 KB Output is correct
16 Correct 16 ms 24924 KB Output is correct
17 Correct 11 ms 24920 KB Output is correct
18 Correct 12 ms 24924 KB Output is correct
19 Correct 10 ms 25024 KB Output is correct
20 Correct 11 ms 24976 KB Output is correct
21 Correct 11 ms 24960 KB Output is correct
22 Correct 17 ms 24912 KB Output is correct
23 Correct 11 ms 24924 KB Output is correct
24 Correct 12 ms 24860 KB Output is correct
25 Correct 11 ms 24900 KB Output is correct
26 Correct 11 ms 24924 KB Output is correct
27 Correct 13 ms 24668 KB Output is correct
28 Correct 11 ms 24920 KB Output is correct
29 Correct 11 ms 24924 KB Output is correct
30 Correct 11 ms 24956 KB Output is correct
31 Correct 12 ms 25128 KB Output is correct
32 Correct 11 ms 24924 KB Output is correct
33 Correct 13 ms 25156 KB Output is correct
34 Correct 25 ms 30028 KB Output is correct
35 Correct 196 ms 68572 KB Output is correct
36 Correct 208 ms 67260 KB Output is correct
37 Correct 206 ms 67092 KB Output is correct
38 Correct 184 ms 64204 KB Output is correct
39 Correct 192 ms 65148 KB Output is correct
40 Correct 177 ms 63936 KB Output is correct
41 Correct 203 ms 67700 KB Output is correct
42 Correct 184 ms 67528 KB Output is correct
43 Runtime error 863 ms 524288 KB Execution killed with signal 9
44 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 24668 KB Output is correct
2 Correct 10 ms 24668 KB Output is correct
3 Correct 10 ms 24668 KB Output is correct
4 Correct 10 ms 24792 KB Output is correct
5 Correct 11 ms 24920 KB Output is correct
6 Correct 12 ms 24892 KB Output is correct
7 Correct 13 ms 24924 KB Output is correct
8 Correct 10 ms 24924 KB Output is correct
9 Correct 128 ms 58472 KB Output is correct
10 Correct 156 ms 65476 KB Output is correct
11 Correct 171 ms 65136 KB Output is correct
12 Correct 172 ms 68888 KB Output is correct
13 Correct 108 ms 56516 KB Output is correct
14 Correct 134 ms 59080 KB Output is correct
15 Correct 286 ms 69872 KB Output is correct
16 Correct 316 ms 68216 KB Output is correct
17 Correct 287 ms 71464 KB Output is correct
18 Correct 259 ms 59944 KB Output is correct
19 Correct 415 ms 52580 KB Output is correct
20 Correct 415 ms 53800 KB Output is correct
21 Correct 439 ms 53336 KB Output is correct
22 Correct 459 ms 53800 KB Output is correct
23 Correct 417 ms 53572 KB Output is correct
24 Correct 433 ms 52472 KB Output is correct
25 Correct 449 ms 53432 KB Output is correct
26 Correct 465 ms 52400 KB Output is correct
27 Correct 18 ms 24912 KB Output is correct
28 Correct 16 ms 24924 KB Output is correct
29 Correct 11 ms 24920 KB Output is correct
30 Correct 12 ms 24924 KB Output is correct
31 Correct 10 ms 25024 KB Output is correct
32 Correct 11 ms 24976 KB Output is correct
33 Correct 11 ms 24960 KB Output is correct
34 Correct 17 ms 24912 KB Output is correct
35 Correct 11 ms 24924 KB Output is correct
36 Correct 25 ms 30028 KB Output is correct
37 Correct 196 ms 68572 KB Output is correct
38 Correct 208 ms 67260 KB Output is correct
39 Correct 206 ms 67092 KB Output is correct
40 Correct 184 ms 64204 KB Output is correct
41 Correct 192 ms 65148 KB Output is correct
42 Correct 177 ms 63936 KB Output is correct
43 Correct 203 ms 67700 KB Output is correct
44 Correct 184 ms 67528 KB Output is correct
45 Runtime error 863 ms 524288 KB Execution killed with signal 9
46 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 24668 KB Output is correct
2 Correct 10 ms 24668 KB Output is correct
3 Correct 10 ms 24668 KB Output is correct
4 Correct 10 ms 24668 KB Output is correct
5 Correct 10 ms 24792 KB Output is correct
6 Correct 11 ms 24920 KB Output is correct
7 Correct 12 ms 24892 KB Output is correct
8 Correct 13 ms 24924 KB Output is correct
9 Correct 10 ms 24924 KB Output is correct
10 Correct 128 ms 58472 KB Output is correct
11 Correct 156 ms 65476 KB Output is correct
12 Correct 171 ms 65136 KB Output is correct
13 Correct 172 ms 68888 KB Output is correct
14 Correct 108 ms 56516 KB Output is correct
15 Correct 134 ms 59080 KB Output is correct
16 Correct 286 ms 69872 KB Output is correct
17 Correct 316 ms 68216 KB Output is correct
18 Correct 287 ms 71464 KB Output is correct
19 Correct 259 ms 59944 KB Output is correct
20 Correct 415 ms 52580 KB Output is correct
21 Correct 415 ms 53800 KB Output is correct
22 Correct 439 ms 53336 KB Output is correct
23 Correct 459 ms 53800 KB Output is correct
24 Correct 417 ms 53572 KB Output is correct
25 Correct 433 ms 52472 KB Output is correct
26 Correct 449 ms 53432 KB Output is correct
27 Correct 465 ms 52400 KB Output is correct
28 Correct 18 ms 24912 KB Output is correct
29 Correct 16 ms 24924 KB Output is correct
30 Correct 11 ms 24920 KB Output is correct
31 Correct 12 ms 24924 KB Output is correct
32 Correct 10 ms 25024 KB Output is correct
33 Correct 11 ms 24976 KB Output is correct
34 Correct 11 ms 24960 KB Output is correct
35 Correct 17 ms 24912 KB Output is correct
36 Correct 11 ms 24924 KB Output is correct
37 Correct 25 ms 30028 KB Output is correct
38 Correct 196 ms 68572 KB Output is correct
39 Correct 208 ms 67260 KB Output is correct
40 Correct 206 ms 67092 KB Output is correct
41 Correct 184 ms 64204 KB Output is correct
42 Correct 192 ms 65148 KB Output is correct
43 Correct 177 ms 63936 KB Output is correct
44 Correct 203 ms 67700 KB Output is correct
45 Correct 184 ms 67528 KB Output is correct
46 Runtime error 863 ms 524288 KB Execution killed with signal 9
47 Halted 0 ms 0 KB -