답안 #1050679

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1050679 2024-08-09T12:26:35 Z shiocan 자매 도시 (APIO20_swap) C++17
100 / 100
330 ms 219836 KB
#include <bits/stdc++.h>
#include <cstdlib>
#include <stdlib.h>
using namespace std;
/*
    #define cin fin
    #define cout fout
    string __fname = ""; ifstream fin(__fname + ".in"); ofstream fout(__fname + ".out");
*/
#define ull unsigned long long 
//#define ll long long
//#define int long long
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
//ll mod = 1e9 + 7; 
//const int inf = 1e18;
//const int N = 3e5 + 100;

vector<vector<int>> a;
int n, m;

struct dsu{
    int sz = 0;
    vector<int> p, cnt;
    vector<bool> sw;
    dsu(int n, int m){
        sz = n;
        p.resize(n + m + 5);
        cnt.resize(n, 0);
        sw.resize(n + m + 5, 0);

        for(int i = 0; i < n + m + 5; i++)  
            p[i] = i;
    }

    int find(int u){
        return p[u] == u ? u : p[u] = find(p[u]);
    }

    void merge(int u, int v){
        if(u < n && v < n){
            cnt[u]++, cnt[v]++;
            if(cnt[u] > 2)
                sw[sz] = 1;
            if(cnt[v] > 2)
                sw[sz] = 1;
        }
        
        u = find(u);
        v = find(v);

        if(u == v){
            p[sz] = p[u] = sz;

            a[sz].push_back(u);
            
            sw[sz] = 1;

            sz++;
            return;
        }

        p[sz] = p[u] = p[v] = sz;
        a[sz].push_back(u);
        a[sz].push_back(v);

        if(sw[u] || sw[v])
            sw[sz] = 1;

        sz++;
    }
};

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

vector<int> first_reachable, is_reachable;

const int K = 30;
vector<vector<int>> st;
vector<int> tin, tout;
int timer = 0;

void dfs(int u, int p){
    tin[u] = ++timer;
    
    st[u][0] = p;

    for(int i = 1; i < K; i++)
        st[u][i] = st[st[u][i - 1]][i - 1];

    if(p != u && !is_reachable[u])
        first_reachable[u] = first_reachable[p];

    for(auto i : a[u])
        if(i != p){
            dfs(i, u);
        }

    tout[u] = ++timer;
}

bool is_ancestor(int u, int v){
    return tin[u] <= tin[v] && tout[u] >= tout[v];
}

int lca(int u, int v){
    if(is_ancestor(u, v))
        return u;
    if(is_ancestor(v, u))
        return v;

    for(int i = K - 1; i >= 0; i--)
        if(!is_ancestor(st[u][i], v))
            u = st[u][i]; 

    return st[u][0];
}

vector<edge> edges;

void init(int N, int M, vector<int> u, vector<int> v, vector<int> w){
    n = N;
    m = M;

    a = vector<vector<int>> (n + m + 5);

    for(int i = 0; i<m; i++)
        edges.push_back({w[i], u[i], v[i]});

    sort(all(edges), [&] (edge a, edge b){
        return a.w < b.w;
    });

    dsu ds(n, m);

    for(auto i : edges)
        ds.merge(i.u, i.v);

    first_reachable = is_reachable = vector<int> (ds.sw.size(), 0ll);

    for(int i = 0; i < (int)first_reachable.size(); i++)
        if(ds.sw[i])
            is_reachable[i] = 1, first_reachable[i] = i;

    tin = tout = vector<int> (2 * (n + m + 5));
    st = vector<vector<int>> (4 * (n + m + 5), vector<int> (K));

    dfs(ds.sz - 1, ds.sz - 1);

    // cout << "tin : ";
    // for(auto i : tin)
    //     cout << i << ' ';
    // cout << '\n';

    // cout << "tout : ";
    // for(auto i : tout)
    //     cout << i << ' ';
    // cout << '\n';
}

int getMinimumFuelCapacity(int u, int v){
    int l = lca(u, v);

    // cout << u << ' ' << v << " : " << l << '\n';
    if(l >= n && is_reachable[l])
        return edges[l - n].w;
    if(first_reachable[l] >= n && is_reachable[first_reachable[l]])
        return edges[first_reachable[l] - n].w;
    return -1;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 856 KB Output is correct
5 Correct 1 ms 1628 KB Output is correct
6 Correct 1 ms 1628 KB Output is correct
7 Correct 2 ms 1628 KB Output is correct
8 Correct 1 ms 1628 KB Output is correct
9 Correct 121 ms 109020 KB Output is correct
10 Correct 131 ms 133112 KB Output is correct
11 Correct 127 ms 131016 KB Output is correct
12 Correct 136 ms 138696 KB Output is correct
13 Correct 140 ms 140996 KB Output is correct
14 Correct 114 ms 108996 KB Output is correct
15 Correct 195 ms 138184 KB Output is correct
16 Correct 175 ms 134344 KB Output is correct
17 Correct 179 ms 142628 KB Output is correct
18 Correct 237 ms 144836 KB Output is correct
19 Correct 54 ms 27172 KB Output is correct
20 Correct 193 ms 139336 KB Output is correct
21 Correct 176 ms 133844 KB Output is correct
22 Correct 178 ms 143484 KB Output is correct
23 Correct 237 ms 145772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 218 ms 139856 KB Output is correct
4 Correct 205 ms 146972 KB Output is correct
5 Correct 212 ms 142172 KB Output is correct
6 Correct 200 ms 146224 KB Output is correct
7 Correct 239 ms 144452 KB Output is correct
8 Correct 228 ms 138432 KB Output is correct
9 Correct 205 ms 143688 KB Output is correct
10 Correct 196 ms 137088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 856 KB Output is correct
5 Correct 1 ms 1628 KB Output is correct
6 Correct 1 ms 1628 KB Output is correct
7 Correct 2 ms 1628 KB Output is correct
8 Correct 1 ms 1628 KB Output is correct
9 Correct 0 ms 600 KB Output is correct
10 Correct 1 ms 1820 KB Output is correct
11 Correct 1 ms 1880 KB Output is correct
12 Correct 1 ms 1628 KB Output is correct
13 Correct 1 ms 1372 KB Output is correct
14 Correct 1 ms 1628 KB Output is correct
15 Correct 2 ms 1624 KB Output is correct
16 Correct 1 ms 1628 KB Output is correct
17 Correct 1 ms 1628 KB Output is correct
18 Correct 1 ms 1628 KB Output is correct
19 Correct 1 ms 1628 KB Output is correct
20 Correct 1 ms 1628 KB Output is correct
21 Correct 1 ms 1628 KB Output is correct
22 Correct 1 ms 1960 KB Output is correct
23 Correct 1 ms 1372 KB Output is correct
24 Correct 2 ms 2396 KB Output is correct
25 Correct 2 ms 2396 KB Output is correct
26 Correct 2 ms 2496 KB Output is correct
27 Correct 1 ms 1628 KB Output is correct
28 Correct 2 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 856 KB Output is correct
6 Correct 1 ms 1628 KB Output is correct
7 Correct 1 ms 1628 KB Output is correct
8 Correct 2 ms 1628 KB Output is correct
9 Correct 1 ms 1628 KB Output is correct
10 Correct 121 ms 109020 KB Output is correct
11 Correct 131 ms 133112 KB Output is correct
12 Correct 127 ms 131016 KB Output is correct
13 Correct 136 ms 138696 KB Output is correct
14 Correct 140 ms 140996 KB Output is correct
15 Correct 1 ms 1820 KB Output is correct
16 Correct 1 ms 1880 KB Output is correct
17 Correct 1 ms 1628 KB Output is correct
18 Correct 1 ms 1372 KB Output is correct
19 Correct 1 ms 1628 KB Output is correct
20 Correct 2 ms 1624 KB Output is correct
21 Correct 1 ms 1628 KB Output is correct
22 Correct 1 ms 1628 KB Output is correct
23 Correct 1 ms 1628 KB Output is correct
24 Correct 1 ms 1628 KB Output is correct
25 Correct 1 ms 1628 KB Output is correct
26 Correct 1 ms 1628 KB Output is correct
27 Correct 1 ms 1960 KB Output is correct
28 Correct 1 ms 1372 KB Output is correct
29 Correct 2 ms 2396 KB Output is correct
30 Correct 2 ms 2396 KB Output is correct
31 Correct 2 ms 2496 KB Output is correct
32 Correct 1 ms 1628 KB Output is correct
33 Correct 2 ms 2396 KB Output is correct
34 Correct 24 ms 19736 KB Output is correct
35 Correct 130 ms 138440 KB Output is correct
36 Correct 128 ms 138436 KB Output is correct
37 Correct 143 ms 138100 KB Output is correct
38 Correct 149 ms 136472 KB Output is correct
39 Correct 144 ms 135608 KB Output is correct
40 Correct 123 ms 124288 KB Output is correct
41 Correct 126 ms 138696 KB Output is correct
42 Correct 145 ms 138500 KB Output is correct
43 Correct 144 ms 141488 KB Output is correct
44 Correct 170 ms 138692 KB Output is correct
45 Correct 180 ms 164804 KB Output is correct
46 Correct 136 ms 138440 KB Output is correct
47 Correct 127 ms 138180 KB Output is correct
48 Correct 149 ms 146376 KB Output is correct
49 Correct 114 ms 124408 KB Output is correct
50 Correct 93 ms 98856 KB Output is correct
51 Correct 150 ms 139792 KB Output is correct
52 Correct 189 ms 187584 KB Output is correct
53 Correct 233 ms 200124 KB Output is correct
54 Correct 224 ms 216508 KB Output is correct
55 Correct 138 ms 141096 KB Output is correct
56 Correct 215 ms 196548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 856 KB Output is correct
5 Correct 1 ms 1628 KB Output is correct
6 Correct 1 ms 1628 KB Output is correct
7 Correct 2 ms 1628 KB Output is correct
8 Correct 1 ms 1628 KB Output is correct
9 Correct 121 ms 109020 KB Output is correct
10 Correct 131 ms 133112 KB Output is correct
11 Correct 127 ms 131016 KB Output is correct
12 Correct 136 ms 138696 KB Output is correct
13 Correct 140 ms 140996 KB Output is correct
14 Correct 114 ms 108996 KB Output is correct
15 Correct 195 ms 138184 KB Output is correct
16 Correct 175 ms 134344 KB Output is correct
17 Correct 179 ms 142628 KB Output is correct
18 Correct 237 ms 144836 KB Output is correct
19 Correct 218 ms 139856 KB Output is correct
20 Correct 205 ms 146972 KB Output is correct
21 Correct 212 ms 142172 KB Output is correct
22 Correct 200 ms 146224 KB Output is correct
23 Correct 239 ms 144452 KB Output is correct
24 Correct 228 ms 138432 KB Output is correct
25 Correct 205 ms 143688 KB Output is correct
26 Correct 196 ms 137088 KB Output is correct
27 Correct 1 ms 1820 KB Output is correct
28 Correct 1 ms 1880 KB Output is correct
29 Correct 1 ms 1628 KB Output is correct
30 Correct 1 ms 1372 KB Output is correct
31 Correct 1 ms 1628 KB Output is correct
32 Correct 2 ms 1624 KB Output is correct
33 Correct 1 ms 1628 KB Output is correct
34 Correct 1 ms 1628 KB Output is correct
35 Correct 1 ms 1628 KB Output is correct
36 Correct 24 ms 19736 KB Output is correct
37 Correct 130 ms 138440 KB Output is correct
38 Correct 128 ms 138436 KB Output is correct
39 Correct 143 ms 138100 KB Output is correct
40 Correct 149 ms 136472 KB Output is correct
41 Correct 144 ms 135608 KB Output is correct
42 Correct 123 ms 124288 KB Output is correct
43 Correct 126 ms 138696 KB Output is correct
44 Correct 145 ms 138500 KB Output is correct
45 Correct 144 ms 141488 KB Output is correct
46 Correct 170 ms 138692 KB Output is correct
47 Correct 19 ms 17948 KB Output is correct
48 Correct 193 ms 142580 KB Output is correct
49 Correct 182 ms 142376 KB Output is correct
50 Correct 183 ms 142116 KB Output is correct
51 Correct 198 ms 141620 KB Output is correct
52 Correct 183 ms 133436 KB Output is correct
53 Correct 137 ms 96956 KB Output is correct
54 Correct 180 ms 143388 KB Output is correct
55 Correct 183 ms 142368 KB Output is correct
56 Correct 234 ms 144924 KB Output is correct
57 Correct 195 ms 143300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 856 KB Output is correct
6 Correct 1 ms 1628 KB Output is correct
7 Correct 1 ms 1628 KB Output is correct
8 Correct 2 ms 1628 KB Output is correct
9 Correct 1 ms 1628 KB Output is correct
10 Correct 121 ms 109020 KB Output is correct
11 Correct 131 ms 133112 KB Output is correct
12 Correct 127 ms 131016 KB Output is correct
13 Correct 136 ms 138696 KB Output is correct
14 Correct 140 ms 140996 KB Output is correct
15 Correct 114 ms 108996 KB Output is correct
16 Correct 195 ms 138184 KB Output is correct
17 Correct 175 ms 134344 KB Output is correct
18 Correct 179 ms 142628 KB Output is correct
19 Correct 237 ms 144836 KB Output is correct
20 Correct 218 ms 139856 KB Output is correct
21 Correct 205 ms 146972 KB Output is correct
22 Correct 212 ms 142172 KB Output is correct
23 Correct 200 ms 146224 KB Output is correct
24 Correct 239 ms 144452 KB Output is correct
25 Correct 228 ms 138432 KB Output is correct
26 Correct 205 ms 143688 KB Output is correct
27 Correct 196 ms 137088 KB Output is correct
28 Correct 1 ms 1820 KB Output is correct
29 Correct 1 ms 1880 KB Output is correct
30 Correct 1 ms 1628 KB Output is correct
31 Correct 1 ms 1372 KB Output is correct
32 Correct 1 ms 1628 KB Output is correct
33 Correct 2 ms 1624 KB Output is correct
34 Correct 1 ms 1628 KB Output is correct
35 Correct 1 ms 1628 KB Output is correct
36 Correct 1 ms 1628 KB Output is correct
37 Correct 24 ms 19736 KB Output is correct
38 Correct 130 ms 138440 KB Output is correct
39 Correct 128 ms 138436 KB Output is correct
40 Correct 143 ms 138100 KB Output is correct
41 Correct 149 ms 136472 KB Output is correct
42 Correct 144 ms 135608 KB Output is correct
43 Correct 123 ms 124288 KB Output is correct
44 Correct 126 ms 138696 KB Output is correct
45 Correct 145 ms 138500 KB Output is correct
46 Correct 144 ms 141488 KB Output is correct
47 Correct 170 ms 138692 KB Output is correct
48 Correct 19 ms 17948 KB Output is correct
49 Correct 193 ms 142580 KB Output is correct
50 Correct 182 ms 142376 KB Output is correct
51 Correct 183 ms 142116 KB Output is correct
52 Correct 198 ms 141620 KB Output is correct
53 Correct 183 ms 133436 KB Output is correct
54 Correct 137 ms 96956 KB Output is correct
55 Correct 180 ms 143388 KB Output is correct
56 Correct 183 ms 142368 KB Output is correct
57 Correct 234 ms 144924 KB Output is correct
58 Correct 195 ms 143300 KB Output is correct
59 Correct 54 ms 27172 KB Output is correct
60 Correct 193 ms 139336 KB Output is correct
61 Correct 176 ms 133844 KB Output is correct
62 Correct 178 ms 143484 KB Output is correct
63 Correct 237 ms 145772 KB Output is correct
64 Correct 1 ms 1628 KB Output is correct
65 Correct 1 ms 1628 KB Output is correct
66 Correct 1 ms 1628 KB Output is correct
67 Correct 1 ms 1960 KB Output is correct
68 Correct 1 ms 1372 KB Output is correct
69 Correct 2 ms 2396 KB Output is correct
70 Correct 2 ms 2396 KB Output is correct
71 Correct 2 ms 2496 KB Output is correct
72 Correct 1 ms 1628 KB Output is correct
73 Correct 2 ms 2396 KB Output is correct
74 Correct 180 ms 164804 KB Output is correct
75 Correct 136 ms 138440 KB Output is correct
76 Correct 127 ms 138180 KB Output is correct
77 Correct 149 ms 146376 KB Output is correct
78 Correct 114 ms 124408 KB Output is correct
79 Correct 93 ms 98856 KB Output is correct
80 Correct 150 ms 139792 KB Output is correct
81 Correct 189 ms 187584 KB Output is correct
82 Correct 233 ms 200124 KB Output is correct
83 Correct 224 ms 216508 KB Output is correct
84 Correct 138 ms 141096 KB Output is correct
85 Correct 215 ms 196548 KB Output is correct
86 Correct 82 ms 59576 KB Output is correct
87 Correct 192 ms 142600 KB Output is correct
88 Correct 188 ms 142368 KB Output is correct
89 Correct 231 ms 142820 KB Output is correct
90 Correct 167 ms 135076 KB Output is correct
91 Correct 178 ms 127692 KB Output is correct
92 Correct 242 ms 139152 KB Output is correct
93 Correct 255 ms 188360 KB Output is correct
94 Correct 330 ms 201660 KB Output is correct
95 Correct 272 ms 219836 KB Output is correct
96 Correct 265 ms 145100 KB Output is correct
97 Correct 257 ms 171904 KB Output is correct