답안 #965725

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
965725 2024-04-19T06:12:58 Z phoenix0423 자매 도시 (APIO20_swap) C++17
100 / 100
269 ms 49596 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#include "swap.h"

namespace{
    const int maxn = 3e5 + 5;
    const int INF = 1e9 + 7;
    struct DSU{
        vector<int> par, siz;
        int n;
        DSU(){}
        DSU(int _n) : n(_n){
            par.resize(n);
            siz.resize(n, 1);
            iota(par.begin(), par.end(), 0);
        }
        int root(int x){ return x == par[x] ? x : par[x] = root(par[x]);}
        void unite(int x, int y){
            x = root(x), y = root(y);
            if(x == y) return;
            if(siz[x] > siz[y]) swap(x, y);
            par[x] = y, siz[y] += siz[x];
        }
    } dsu;
    struct edge{
        int u, v, w;
        edge(){}
        edge(int _u, int _v, int _w) : u(_u), v(_v), w(_w){}
        bool operator < (const edge& other) const{
            return w < other.w;
        }
    };
    int par[maxn], w[maxn], k[maxn], dep[maxn], succ[maxn][19], mn[maxn], deg[maxn];
    vector<int> adj[maxn];
}

void dfs(int pos, int prev, int nd){
    if(k[pos]) nd = w[pos];
    mn[pos] = nd;
    succ[pos][0] = prev;
    for(int i = 1; i < 19; i++) succ[pos][i] = succ[succ[pos][i - 1]][i - 1];
    for(auto x : adj[pos]){
        dep[x] = dep[pos] + 1;
        dfs(x, pos, nd);
    }
}
void init(int n, int m, vector<int> U, vector<int> V, vector<int> W){
    dsu = DSU(n);
    vector<edge> e;
    for(int i = 0; i < m; i++) e.pb(edge(U[i], V[i], W[i]));
    sort(e.begin(), e.end());
    vector<int> b(n);
    iota(b.begin(), b.end(), 0);
    int id = n;
    for(auto [u, v, wg] : e){
        deg[u] ++, deg[v] ++;
        int ok = (deg[u] > 2 || deg[v] > 2);
        u = dsu.root(u), v = dsu.root(v);
        if(u == v){
            int x = b[u];
            if(k[x]) continue;
            int nw = id++;
            adj[nw].pb(x);
            par[x] = nw;
            w[nw] = wg; 
            k[nw] = 1;
            b[u] = nw;
        }
        else{
            dsu.unite(u, v);
            int nw = id++;
            w[nw] = wg, k[nw] = k[b[u]] | k[b[v]] | ok;
            par[b[u]] = nw, par[b[v]] = nw;
            adj[nw].pb(b[u]), adj[nw].pb(b[v]);
            b[u] = nw, b[v] = nw;
        }
    }
    dfs(id - 1, id - 1, INF);
}

int lift(int x, int steps){
    for(int i = 18; i >= 0; i--){
        if(steps & (1 << i)) x = succ[x][i];
    }
    return x;
}

int find_lca(int x, int y){
    if(dep[x] > dep[y]) swap(x, y);
    y = lift(y, dep[y] - dep[x]);
    if(x == y) return x;
    for(int i = 18; i >= 0; i--){
        if(succ[x][i] != succ[y][i]) x = succ[x][i], y = succ[y][i];
    }
    return succ[x][0];
}
int getMinimumFuelCapacity(int x, int y){
    int lca = find_lca(x, y);
    if(mn[lca] == INF) mn[lca] = -1;
    return mn[lca];
}

# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12892 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
4 Correct 2 ms 12892 KB Output is correct
5 Correct 3 ms 12924 KB Output is correct
6 Correct 3 ms 12892 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 4 ms 12892 KB Output is correct
9 Correct 73 ms 31952 KB Output is correct
10 Correct 101 ms 35652 KB Output is correct
11 Correct 92 ms 35272 KB Output is correct
12 Correct 102 ms 35896 KB Output is correct
13 Correct 79 ms 38856 KB Output is correct
14 Correct 80 ms 31940 KB Output is correct
15 Correct 200 ms 36960 KB Output is correct
16 Correct 176 ms 36824 KB Output is correct
17 Correct 179 ms 37252 KB Output is correct
18 Correct 185 ms 40356 KB Output is correct
19 Correct 67 ms 19480 KB Output is correct
20 Correct 172 ms 37472 KB Output is correct
21 Correct 188 ms 37260 KB Output is correct
22 Correct 174 ms 37684 KB Output is correct
23 Correct 186 ms 40800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12892 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 161 ms 43160 KB Output is correct
4 Correct 157 ms 43552 KB Output is correct
5 Correct 178 ms 43508 KB Output is correct
6 Correct 165 ms 47508 KB Output is correct
7 Correct 164 ms 47556 KB Output is correct
8 Correct 154 ms 46884 KB Output is correct
9 Correct 189 ms 47300 KB Output is correct
10 Correct 154 ms 46840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12892 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
4 Correct 2 ms 12892 KB Output is correct
5 Correct 3 ms 12924 KB Output is correct
6 Correct 3 ms 12892 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 4 ms 12892 KB Output is correct
9 Correct 2 ms 12892 KB Output is correct
10 Correct 3 ms 12892 KB Output is correct
11 Correct 3 ms 12892 KB Output is correct
12 Correct 3 ms 12892 KB Output is correct
13 Correct 3 ms 12956 KB Output is correct
14 Correct 3 ms 12892 KB Output is correct
15 Correct 3 ms 12892 KB Output is correct
16 Correct 3 ms 12892 KB Output is correct
17 Correct 3 ms 12892 KB Output is correct
18 Correct 3 ms 12892 KB Output is correct
19 Correct 4 ms 12892 KB Output is correct
20 Correct 3 ms 12892 KB Output is correct
21 Correct 3 ms 12888 KB Output is correct
22 Correct 3 ms 12892 KB Output is correct
23 Correct 3 ms 12892 KB Output is correct
24 Correct 3 ms 12888 KB Output is correct
25 Correct 5 ms 12892 KB Output is correct
26 Correct 4 ms 12892 KB Output is correct
27 Correct 4 ms 12892 KB Output is correct
28 Correct 3 ms 12892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12892 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 3 ms 12892 KB Output is correct
4 Correct 2 ms 12892 KB Output is correct
5 Correct 2 ms 12892 KB Output is correct
6 Correct 3 ms 12924 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 3 ms 12892 KB Output is correct
9 Correct 4 ms 12892 KB Output is correct
10 Correct 73 ms 31952 KB Output is correct
11 Correct 101 ms 35652 KB Output is correct
12 Correct 92 ms 35272 KB Output is correct
13 Correct 102 ms 35896 KB Output is correct
14 Correct 79 ms 38856 KB Output is correct
15 Correct 3 ms 12892 KB Output is correct
16 Correct 3 ms 12892 KB Output is correct
17 Correct 3 ms 12892 KB Output is correct
18 Correct 3 ms 12956 KB Output is correct
19 Correct 3 ms 12892 KB Output is correct
20 Correct 3 ms 12892 KB Output is correct
21 Correct 3 ms 12892 KB Output is correct
22 Correct 3 ms 12892 KB Output is correct
23 Correct 3 ms 12892 KB Output is correct
24 Correct 4 ms 12892 KB Output is correct
25 Correct 3 ms 12892 KB Output is correct
26 Correct 3 ms 12888 KB Output is correct
27 Correct 3 ms 12892 KB Output is correct
28 Correct 3 ms 12892 KB Output is correct
29 Correct 3 ms 12888 KB Output is correct
30 Correct 5 ms 12892 KB Output is correct
31 Correct 4 ms 12892 KB Output is correct
32 Correct 4 ms 12892 KB Output is correct
33 Correct 3 ms 12892 KB Output is correct
34 Correct 11 ms 16416 KB Output is correct
35 Correct 101 ms 37600 KB Output is correct
36 Correct 97 ms 37572 KB Output is correct
37 Correct 98 ms 37576 KB Output is correct
38 Correct 102 ms 37592 KB Output is correct
39 Correct 98 ms 37316 KB Output is correct
40 Correct 84 ms 36568 KB Output is correct
41 Correct 97 ms 37868 KB Output is correct
42 Correct 99 ms 37572 KB Output is correct
43 Correct 83 ms 41964 KB Output is correct
44 Correct 99 ms 38036 KB Output is correct
45 Correct 93 ms 39060 KB Output is correct
46 Correct 96 ms 37572 KB Output is correct
47 Correct 103 ms 37720 KB Output is correct
48 Correct 95 ms 39540 KB Output is correct
49 Correct 50 ms 21448 KB Output is correct
50 Correct 47 ms 22116 KB Output is correct
51 Correct 74 ms 34908 KB Output is correct
52 Correct 136 ms 41772 KB Output is correct
53 Correct 144 ms 46532 KB Output is correct
54 Correct 129 ms 43200 KB Output is correct
55 Correct 78 ms 41456 KB Output is correct
56 Correct 122 ms 43984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12892 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
4 Correct 2 ms 12892 KB Output is correct
5 Correct 3 ms 12924 KB Output is correct
6 Correct 3 ms 12892 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 4 ms 12892 KB Output is correct
9 Correct 73 ms 31952 KB Output is correct
10 Correct 101 ms 35652 KB Output is correct
11 Correct 92 ms 35272 KB Output is correct
12 Correct 102 ms 35896 KB Output is correct
13 Correct 79 ms 38856 KB Output is correct
14 Correct 80 ms 31940 KB Output is correct
15 Correct 200 ms 36960 KB Output is correct
16 Correct 176 ms 36824 KB Output is correct
17 Correct 179 ms 37252 KB Output is correct
18 Correct 185 ms 40356 KB Output is correct
19 Correct 161 ms 43160 KB Output is correct
20 Correct 157 ms 43552 KB Output is correct
21 Correct 178 ms 43508 KB Output is correct
22 Correct 165 ms 47508 KB Output is correct
23 Correct 164 ms 47556 KB Output is correct
24 Correct 154 ms 46884 KB Output is correct
25 Correct 189 ms 47300 KB Output is correct
26 Correct 154 ms 46840 KB Output is correct
27 Correct 3 ms 12892 KB Output is correct
28 Correct 3 ms 12892 KB Output is correct
29 Correct 3 ms 12892 KB Output is correct
30 Correct 3 ms 12956 KB Output is correct
31 Correct 3 ms 12892 KB Output is correct
32 Correct 3 ms 12892 KB Output is correct
33 Correct 3 ms 12892 KB Output is correct
34 Correct 3 ms 12892 KB Output is correct
35 Correct 3 ms 12892 KB Output is correct
36 Correct 11 ms 16416 KB Output is correct
37 Correct 101 ms 37600 KB Output is correct
38 Correct 97 ms 37572 KB Output is correct
39 Correct 98 ms 37576 KB Output is correct
40 Correct 102 ms 37592 KB Output is correct
41 Correct 98 ms 37316 KB Output is correct
42 Correct 84 ms 36568 KB Output is correct
43 Correct 97 ms 37868 KB Output is correct
44 Correct 99 ms 37572 KB Output is correct
45 Correct 83 ms 41964 KB Output is correct
46 Correct 99 ms 38036 KB Output is correct
47 Correct 17 ms 16664 KB Output is correct
48 Correct 178 ms 41416 KB Output is correct
49 Correct 196 ms 41348 KB Output is correct
50 Correct 171 ms 41416 KB Output is correct
51 Correct 172 ms 41420 KB Output is correct
52 Correct 168 ms 40780 KB Output is correct
53 Correct 136 ms 35320 KB Output is correct
54 Correct 171 ms 42128 KB Output is correct
55 Correct 171 ms 41412 KB Output is correct
56 Correct 180 ms 44744 KB Output is correct
57 Correct 179 ms 42176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12892 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 3 ms 12892 KB Output is correct
4 Correct 2 ms 12892 KB Output is correct
5 Correct 2 ms 12892 KB Output is correct
6 Correct 3 ms 12924 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 3 ms 12892 KB Output is correct
9 Correct 4 ms 12892 KB Output is correct
10 Correct 73 ms 31952 KB Output is correct
11 Correct 101 ms 35652 KB Output is correct
12 Correct 92 ms 35272 KB Output is correct
13 Correct 102 ms 35896 KB Output is correct
14 Correct 79 ms 38856 KB Output is correct
15 Correct 80 ms 31940 KB Output is correct
16 Correct 200 ms 36960 KB Output is correct
17 Correct 176 ms 36824 KB Output is correct
18 Correct 179 ms 37252 KB Output is correct
19 Correct 185 ms 40356 KB Output is correct
20 Correct 161 ms 43160 KB Output is correct
21 Correct 157 ms 43552 KB Output is correct
22 Correct 178 ms 43508 KB Output is correct
23 Correct 165 ms 47508 KB Output is correct
24 Correct 164 ms 47556 KB Output is correct
25 Correct 154 ms 46884 KB Output is correct
26 Correct 189 ms 47300 KB Output is correct
27 Correct 154 ms 46840 KB Output is correct
28 Correct 3 ms 12892 KB Output is correct
29 Correct 3 ms 12892 KB Output is correct
30 Correct 3 ms 12892 KB Output is correct
31 Correct 3 ms 12956 KB Output is correct
32 Correct 3 ms 12892 KB Output is correct
33 Correct 3 ms 12892 KB Output is correct
34 Correct 3 ms 12892 KB Output is correct
35 Correct 3 ms 12892 KB Output is correct
36 Correct 3 ms 12892 KB Output is correct
37 Correct 11 ms 16416 KB Output is correct
38 Correct 101 ms 37600 KB Output is correct
39 Correct 97 ms 37572 KB Output is correct
40 Correct 98 ms 37576 KB Output is correct
41 Correct 102 ms 37592 KB Output is correct
42 Correct 98 ms 37316 KB Output is correct
43 Correct 84 ms 36568 KB Output is correct
44 Correct 97 ms 37868 KB Output is correct
45 Correct 99 ms 37572 KB Output is correct
46 Correct 83 ms 41964 KB Output is correct
47 Correct 99 ms 38036 KB Output is correct
48 Correct 17 ms 16664 KB Output is correct
49 Correct 178 ms 41416 KB Output is correct
50 Correct 196 ms 41348 KB Output is correct
51 Correct 171 ms 41416 KB Output is correct
52 Correct 172 ms 41420 KB Output is correct
53 Correct 168 ms 40780 KB Output is correct
54 Correct 136 ms 35320 KB Output is correct
55 Correct 171 ms 42128 KB Output is correct
56 Correct 171 ms 41412 KB Output is correct
57 Correct 180 ms 44744 KB Output is correct
58 Correct 179 ms 42176 KB Output is correct
59 Correct 67 ms 19480 KB Output is correct
60 Correct 172 ms 37472 KB Output is correct
61 Correct 188 ms 37260 KB Output is correct
62 Correct 174 ms 37684 KB Output is correct
63 Correct 186 ms 40800 KB Output is correct
64 Correct 4 ms 12892 KB Output is correct
65 Correct 3 ms 12892 KB Output is correct
66 Correct 3 ms 12888 KB Output is correct
67 Correct 3 ms 12892 KB Output is correct
68 Correct 3 ms 12892 KB Output is correct
69 Correct 3 ms 12888 KB Output is correct
70 Correct 5 ms 12892 KB Output is correct
71 Correct 4 ms 12892 KB Output is correct
72 Correct 4 ms 12892 KB Output is correct
73 Correct 3 ms 12892 KB Output is correct
74 Correct 93 ms 39060 KB Output is correct
75 Correct 96 ms 37572 KB Output is correct
76 Correct 103 ms 37720 KB Output is correct
77 Correct 95 ms 39540 KB Output is correct
78 Correct 50 ms 21448 KB Output is correct
79 Correct 47 ms 22116 KB Output is correct
80 Correct 74 ms 34908 KB Output is correct
81 Correct 136 ms 41772 KB Output is correct
82 Correct 144 ms 46532 KB Output is correct
83 Correct 129 ms 43200 KB Output is correct
84 Correct 78 ms 41456 KB Output is correct
85 Correct 122 ms 43984 KB Output is correct
86 Correct 48 ms 22772 KB Output is correct
87 Correct 201 ms 41592 KB Output is correct
88 Correct 167 ms 41476 KB Output is correct
89 Correct 174 ms 42948 KB Output is correct
90 Correct 121 ms 25468 KB Output is correct
91 Correct 126 ms 27328 KB Output is correct
92 Correct 162 ms 38428 KB Output is correct
93 Correct 191 ms 45468 KB Output is correct
94 Correct 269 ms 49596 KB Output is correct
95 Correct 217 ms 47044 KB Output is correct
96 Correct 187 ms 45000 KB Output is correct
97 Correct 219 ms 45848 KB Output is correct