Submission #1050638

# Submission time Handle Problem Language Result Execution time Memory
1050638 2024-08-09T12:02:00 Z shiocan Swapping Cities (APIO20_swap) C++17
100 / 100
180 ms 179388 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;
};
 
const int K = 26;
vector<vector<int>> st;
vector<int> euler, h, first, d;
 
void build(vector<int> & height){
    int n = height.size();
    st = vector<vector<int>> (K, vector<int> (n+2, 0));
 
    for(int i = 0; i<n; i++)
        st[0][i] = i;
        
    for(int i = 1; i<K; i++)
        for(int j = 0; j + (1 << i) <= n; j++)
            if(height[st[i-1][j]] < height[st[i-1][j + (1 << (i - 1))]])
                st[i][j] = st[i-1][j];
            else
                st[i][j] = st[i-1][j + (1 << (i - 1))];
}
 
int get(vector<int> & height, int l, int r){
    int i = 64 - __builtin_clzll(r - l + 1) - 1;
 
    if(height[st[i][l]] < height[st[i][r - (1 << i) + 1]])
        return st[i][l];
    return st[i][r - (1 << i) + 1];    
}
 
vector<int> first_reachable, is_reachable;
 
void dfs(int u, int p = -1, int k = 0){
    euler.push_back(u);
    h[euler.size() - 1] = k;
    first[u] = euler.size() - 1;   
 
    if(p > -1 && !is_reachable[u])
        first_reachable[u] = first_reachable[p];
 
    for(auto i : a[u])
        if(i != p){
            dfs(i, u, k + 1);
 
            euler.push_back(u);
            h[euler.size() - 1] = k;
        }
}
 
int lca(int u, int v){
    if(first[u] > first[v])
        swap(u, v);
 
    return euler[get(h, first[u], first[v])];
}
 
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 = vector<int> (2 * (n + m + 5));
    h = vector<int> (4 * (n + m + 5));
 
    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;
 
    dfs(ds.sz - 1);
 
    build(h);
}
 
int getMinimumFuelCapacity(int u, int v){
    int l = lca(u, v);
 
    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;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 860 KB Output is correct
5 Correct 1 ms 1372 KB Output is correct
6 Correct 1 ms 1372 KB Output is correct
7 Correct 1 ms 1372 KB Output is correct
8 Correct 1 ms 1372 KB Output is correct
9 Correct 65 ms 85540 KB Output is correct
10 Correct 71 ms 104132 KB Output is correct
11 Correct 72 ms 102436 KB Output is correct
12 Correct 73 ms 108476 KB Output is correct
13 Correct 69 ms 112324 KB Output is correct
14 Correct 69 ms 85440 KB Output is correct
15 Correct 100 ms 108700 KB Output is correct
16 Correct 101 ms 105920 KB Output is correct
17 Correct 104 ms 112408 KB Output is correct
18 Correct 110 ms 116124 KB Output is correct
19 Correct 37 ms 21916 KB Output is correct
20 Correct 100 ms 109140 KB Output is correct
21 Correct 105 ms 104900 KB Output is correct
22 Correct 108 ms 112332 KB Output is correct
23 Correct 103 ms 116264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 96 ms 113524 KB Output is correct
4 Correct 99 ms 119236 KB Output is correct
5 Correct 101 ms 115136 KB Output is correct
6 Correct 101 ms 118856 KB Output is correct
7 Correct 101 ms 117128 KB Output is correct
8 Correct 98 ms 112220 KB Output is correct
9 Correct 115 ms 116420 KB Output is correct
10 Correct 96 ms 111300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 860 KB Output is correct
5 Correct 1 ms 1372 KB Output is correct
6 Correct 1 ms 1372 KB Output is correct
7 Correct 1 ms 1372 KB Output is correct
8 Correct 1 ms 1372 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 1372 KB Output is correct
11 Correct 1 ms 1472 KB Output is correct
12 Correct 1 ms 1372 KB Output is correct
13 Correct 1 ms 1116 KB Output is correct
14 Correct 1 ms 1372 KB Output is correct
15 Correct 1 ms 1372 KB Output is correct
16 Correct 1 ms 1372 KB Output is correct
17 Correct 1 ms 1372 KB Output is correct
18 Correct 1 ms 1628 KB Output is correct
19 Correct 1 ms 1372 KB Output is correct
20 Correct 1 ms 1372 KB Output is correct
21 Correct 1 ms 1472 KB Output is correct
22 Correct 2 ms 1628 KB Output is correct
23 Correct 1 ms 1116 KB Output is correct
24 Correct 1 ms 1884 KB Output is correct
25 Correct 2 ms 1884 KB Output is correct
26 Correct 1 ms 2140 KB Output is correct
27 Correct 1 ms 1372 KB Output is correct
28 Correct 1 ms 2032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 860 KB Output is correct
6 Correct 1 ms 1372 KB Output is correct
7 Correct 1 ms 1372 KB Output is correct
8 Correct 1 ms 1372 KB Output is correct
9 Correct 1 ms 1372 KB Output is correct
10 Correct 65 ms 85540 KB Output is correct
11 Correct 71 ms 104132 KB Output is correct
12 Correct 72 ms 102436 KB Output is correct
13 Correct 73 ms 108476 KB Output is correct
14 Correct 69 ms 112324 KB Output is correct
15 Correct 1 ms 1372 KB Output is correct
16 Correct 1 ms 1472 KB Output is correct
17 Correct 1 ms 1372 KB Output is correct
18 Correct 1 ms 1116 KB Output is correct
19 Correct 1 ms 1372 KB Output is correct
20 Correct 1 ms 1372 KB Output is correct
21 Correct 1 ms 1372 KB Output is correct
22 Correct 1 ms 1372 KB Output is correct
23 Correct 1 ms 1628 KB Output is correct
24 Correct 1 ms 1372 KB Output is correct
25 Correct 1 ms 1372 KB Output is correct
26 Correct 1 ms 1472 KB Output is correct
27 Correct 2 ms 1628 KB Output is correct
28 Correct 1 ms 1116 KB Output is correct
29 Correct 1 ms 1884 KB Output is correct
30 Correct 2 ms 1884 KB Output is correct
31 Correct 1 ms 2140 KB Output is correct
32 Correct 1 ms 1372 KB Output is correct
33 Correct 1 ms 2032 KB Output is correct
34 Correct 12 ms 15000 KB Output is correct
35 Correct 74 ms 108204 KB Output is correct
36 Correct 75 ms 108060 KB Output is correct
37 Correct 73 ms 108152 KB Output is correct
38 Correct 71 ms 106432 KB Output is correct
39 Correct 71 ms 105920 KB Output is correct
40 Correct 65 ms 97408 KB Output is correct
41 Correct 71 ms 108484 KB Output is correct
42 Correct 71 ms 108072 KB Output is correct
43 Correct 79 ms 113344 KB Output is correct
44 Correct 75 ms 108552 KB Output is correct
45 Correct 103 ms 133824 KB Output is correct
46 Correct 73 ms 108224 KB Output is correct
47 Correct 72 ms 108228 KB Output is correct
48 Correct 79 ms 116204 KB Output is correct
49 Correct 86 ms 106180 KB Output is correct
50 Correct 72 ms 83496 KB Output is correct
51 Correct 101 ms 113604 KB Output is correct
52 Correct 109 ms 149696 KB Output is correct
53 Correct 122 ms 161224 KB Output is correct
54 Correct 140 ms 174776 KB Output is correct
55 Correct 67 ms 112804 KB Output is correct
56 Correct 119 ms 159164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 860 KB Output is correct
5 Correct 1 ms 1372 KB Output is correct
6 Correct 1 ms 1372 KB Output is correct
7 Correct 1 ms 1372 KB Output is correct
8 Correct 1 ms 1372 KB Output is correct
9 Correct 65 ms 85540 KB Output is correct
10 Correct 71 ms 104132 KB Output is correct
11 Correct 72 ms 102436 KB Output is correct
12 Correct 73 ms 108476 KB Output is correct
13 Correct 69 ms 112324 KB Output is correct
14 Correct 69 ms 85440 KB Output is correct
15 Correct 100 ms 108700 KB Output is correct
16 Correct 101 ms 105920 KB Output is correct
17 Correct 104 ms 112408 KB Output is correct
18 Correct 110 ms 116124 KB Output is correct
19 Correct 96 ms 113524 KB Output is correct
20 Correct 99 ms 119236 KB Output is correct
21 Correct 101 ms 115136 KB Output is correct
22 Correct 101 ms 118856 KB Output is correct
23 Correct 101 ms 117128 KB Output is correct
24 Correct 98 ms 112220 KB Output is correct
25 Correct 115 ms 116420 KB Output is correct
26 Correct 96 ms 111300 KB Output is correct
27 Correct 1 ms 1372 KB Output is correct
28 Correct 1 ms 1472 KB Output is correct
29 Correct 1 ms 1372 KB Output is correct
30 Correct 1 ms 1116 KB Output is correct
31 Correct 1 ms 1372 KB Output is correct
32 Correct 1 ms 1372 KB Output is correct
33 Correct 1 ms 1372 KB Output is correct
34 Correct 1 ms 1372 KB Output is correct
35 Correct 1 ms 1628 KB Output is correct
36 Correct 12 ms 15000 KB Output is correct
37 Correct 74 ms 108204 KB Output is correct
38 Correct 75 ms 108060 KB Output is correct
39 Correct 73 ms 108152 KB Output is correct
40 Correct 71 ms 106432 KB Output is correct
41 Correct 71 ms 105920 KB Output is correct
42 Correct 65 ms 97408 KB Output is correct
43 Correct 71 ms 108484 KB Output is correct
44 Correct 71 ms 108072 KB Output is correct
45 Correct 79 ms 113344 KB Output is correct
46 Correct 75 ms 108552 KB Output is correct
47 Correct 14 ms 13972 KB Output is correct
48 Correct 105 ms 111940 KB Output is correct
49 Correct 104 ms 111812 KB Output is correct
50 Correct 109 ms 111808 KB Output is correct
51 Correct 102 ms 111168 KB Output is correct
52 Correct 101 ms 104788 KB Output is correct
53 Correct 94 ms 76152 KB Output is correct
54 Correct 105 ms 112320 KB Output is correct
55 Correct 104 ms 112068 KB Output is correct
56 Correct 105 ms 116160 KB Output is correct
57 Correct 107 ms 112360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 860 KB Output is correct
6 Correct 1 ms 1372 KB Output is correct
7 Correct 1 ms 1372 KB Output is correct
8 Correct 1 ms 1372 KB Output is correct
9 Correct 1 ms 1372 KB Output is correct
10 Correct 65 ms 85540 KB Output is correct
11 Correct 71 ms 104132 KB Output is correct
12 Correct 72 ms 102436 KB Output is correct
13 Correct 73 ms 108476 KB Output is correct
14 Correct 69 ms 112324 KB Output is correct
15 Correct 69 ms 85440 KB Output is correct
16 Correct 100 ms 108700 KB Output is correct
17 Correct 101 ms 105920 KB Output is correct
18 Correct 104 ms 112408 KB Output is correct
19 Correct 110 ms 116124 KB Output is correct
20 Correct 96 ms 113524 KB Output is correct
21 Correct 99 ms 119236 KB Output is correct
22 Correct 101 ms 115136 KB Output is correct
23 Correct 101 ms 118856 KB Output is correct
24 Correct 101 ms 117128 KB Output is correct
25 Correct 98 ms 112220 KB Output is correct
26 Correct 115 ms 116420 KB Output is correct
27 Correct 96 ms 111300 KB Output is correct
28 Correct 1 ms 1372 KB Output is correct
29 Correct 1 ms 1472 KB Output is correct
30 Correct 1 ms 1372 KB Output is correct
31 Correct 1 ms 1116 KB Output is correct
32 Correct 1 ms 1372 KB Output is correct
33 Correct 1 ms 1372 KB Output is correct
34 Correct 1 ms 1372 KB Output is correct
35 Correct 1 ms 1372 KB Output is correct
36 Correct 1 ms 1628 KB Output is correct
37 Correct 12 ms 15000 KB Output is correct
38 Correct 74 ms 108204 KB Output is correct
39 Correct 75 ms 108060 KB Output is correct
40 Correct 73 ms 108152 KB Output is correct
41 Correct 71 ms 106432 KB Output is correct
42 Correct 71 ms 105920 KB Output is correct
43 Correct 65 ms 97408 KB Output is correct
44 Correct 71 ms 108484 KB Output is correct
45 Correct 71 ms 108072 KB Output is correct
46 Correct 79 ms 113344 KB Output is correct
47 Correct 75 ms 108552 KB Output is correct
48 Correct 14 ms 13972 KB Output is correct
49 Correct 105 ms 111940 KB Output is correct
50 Correct 104 ms 111812 KB Output is correct
51 Correct 109 ms 111808 KB Output is correct
52 Correct 102 ms 111168 KB Output is correct
53 Correct 101 ms 104788 KB Output is correct
54 Correct 94 ms 76152 KB Output is correct
55 Correct 105 ms 112320 KB Output is correct
56 Correct 104 ms 112068 KB Output is correct
57 Correct 105 ms 116160 KB Output is correct
58 Correct 107 ms 112360 KB Output is correct
59 Correct 37 ms 21916 KB Output is correct
60 Correct 100 ms 109140 KB Output is correct
61 Correct 105 ms 104900 KB Output is correct
62 Correct 108 ms 112332 KB Output is correct
63 Correct 103 ms 116264 KB Output is correct
64 Correct 1 ms 1372 KB Output is correct
65 Correct 1 ms 1372 KB Output is correct
66 Correct 1 ms 1472 KB Output is correct
67 Correct 2 ms 1628 KB Output is correct
68 Correct 1 ms 1116 KB Output is correct
69 Correct 1 ms 1884 KB Output is correct
70 Correct 2 ms 1884 KB Output is correct
71 Correct 1 ms 2140 KB Output is correct
72 Correct 1 ms 1372 KB Output is correct
73 Correct 1 ms 2032 KB Output is correct
74 Correct 103 ms 133824 KB Output is correct
75 Correct 73 ms 108224 KB Output is correct
76 Correct 72 ms 108228 KB Output is correct
77 Correct 79 ms 116204 KB Output is correct
78 Correct 86 ms 106180 KB Output is correct
79 Correct 72 ms 83496 KB Output is correct
80 Correct 101 ms 113604 KB Output is correct
81 Correct 109 ms 149696 KB Output is correct
82 Correct 122 ms 161224 KB Output is correct
83 Correct 140 ms 174776 KB Output is correct
84 Correct 67 ms 112804 KB Output is correct
85 Correct 119 ms 159164 KB Output is correct
86 Correct 45 ms 48068 KB Output is correct
87 Correct 102 ms 112068 KB Output is correct
88 Correct 107 ms 111920 KB Output is correct
89 Correct 110 ms 114204 KB Output is correct
90 Correct 118 ms 115908 KB Output is correct
91 Correct 180 ms 108120 KB Output is correct
92 Correct 123 ms 113344 KB Output is correct
93 Correct 163 ms 152300 KB Output is correct
94 Correct 170 ms 163016 KB Output is correct
95 Correct 179 ms 179388 KB Output is correct
96 Correct 112 ms 116404 KB Output is correct
97 Correct 149 ms 138560 KB Output is correct