답안 #392094

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
392094 2021-04-20T13:35:28 Z Jarif_Rahman 자매 도시 (APIO20_swap) C++17
100 / 100
454 ms 63908 KB
#include "swap.h"
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
const ll inf = 1.5e9;
 
struct segtree{
    
    int k;
    vector<pair<int, int>> mn;
    segtree(int n){
        k = 1;
        while(k <= n) k*=2; k*=2;
        mn.assign(k, {inf,0});
        for(int i = 0; i < k/2; i++) mn[i+k/2].sc = i;
        for(int i = k/2; i > 0; i--) mn[i].sc = mn[2*i].sc;
    }
    void upd(int in, pair<int, int> x){
        in+=k/2;
        mn[in] = x, in/=2;
        while(in > 0){
            mn[in] = min(mn[2*in], mn[2*in+1]);
            in/=2;
        }
    }
    pair<int, int> smn(int l, int r, int nd, int a, int b){
        if(b < l || a > r) return {inf, inf};
        if(a >= l && b <= r) return mn[nd];
        int c = (a+b)/2;
        return min(smn(l, r, 2*nd, a, c), smn(l, r, 2*nd+1, c+1, b));
    }
    pair<int, int> smn(int l, int r){
        return smn(l, r, 1, 0, k/2-1);
    }
}seg(10);
 
struct reachability_tree{
    int n;
    vector<int> p;
    vector<int> adj;
    vector<bool> ok;
    vector<vector<int>> v;
    vector<int> w;
    reachability_tree(int n){
        reachability_tree::n = n;
        v.resize(n);
        p.resize(n);
        ok.resize(n, 0);
        adj.resize(n, 0);
        w.resize(n);
        for(int i = 0; i < n; i++) p[i] = i;
    }
    int get(int x){
        if(p[x] != x) return p[x] = get(p[x]);
        return p[x];
    }
    void unite(int a, int b, int vl){
        int aa = a, bb = b;
        a = get(a), b = get(b);
        adj[aa]++;
        adj[bb]++;
        p.pb(n);
        ok.pb(0);
        v.pb({});
        p[a] = n;
        p[b] = n;
        v[n].pb(a);
        if(a != b) v[n].pb(b);
        ok[n] = ok[a]|ok[b];
        if(max(adj[aa], adj[bb]) >= 3 || a == b) ok[n] = 1;
        n++;
        w.pb(vl);
    }
}rt(0);
 
 
int n, m;
vector<int> depth, ett, pos, anc;
 
void dfs(int nd, int d, int ls){
    depth[nd] = d;
    ett.pb(nd);
    if(rt.ok[nd]) ls = nd;
    anc[nd] = ls;
    for(int x: rt.v[nd]){
        dfs(x, d+1, ls);
        ett.pb(nd);
    }
}
 
void init(int n, int m, vector<int> u, vector<int>v, vector<int>w){
    ::n = n, ::m = m;
    rt = reachability_tree(n);
    vector<tuple<int, int, int>> edges;
    for(int i = 0; i < m; i++){
        edges.pb({w[i], u[i], v[i]});
    }
    sort(edges.begin(), edges.end());
    for(auto [wi, ui, vi]: edges) rt.unite(ui, vi, wi);
    depth.resize(rt.n);
    pos.resize(rt.n);
    anc.resize(rt.n);
    dfs(rt.get(0), 1, -1);
    seg = segtree(ett.size());
    for(int i = 0; i < ett.size(); i++) seg.upd(i, {depth[ett[i]], ett[i]});
    for(int i = 0; i < ett.size(); i++) pos[ett[i]] = i;
}
 
int getMinimumFuelCapacity(int x, int y){
    x = pos[x], y = pos[y];
    if(x > y) swap(x, y);
    int lca = seg.smn(x, y).sc;
    int ans = anc[lca];
    if(ans != -1) ans  = rt.w[ans];
    return ans;
}

Compilation message

swap.cpp: In constructor 'segtree::segtree(int)':
swap.cpp:17:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
   17 |         while(k <= n) k*=2; k*=2;
      |         ^~~~~
swap.cpp:17:29: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
   17 |         while(k <= n) k*=2; k*=2;
      |                             ^
swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:109:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  109 |     for(int i = 0; i < ett.size(); i++) seg.upd(i, {depth[ett[i]], ett[i]});
      |                    ~~^~~~~~~~~~~~
swap.cpp:110:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |     for(int i = 0; i < ett.size(); i++) pos[ett[i]] = i;
      |                    ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 101 ms 25180 KB Output is correct
10 Correct 128 ms 28244 KB Output is correct
11 Correct 125 ms 27872 KB Output is correct
12 Correct 131 ms 28800 KB Output is correct
13 Correct 119 ms 31932 KB Output is correct
14 Correct 116 ms 25292 KB Output is correct
15 Correct 308 ms 29848 KB Output is correct
16 Correct 295 ms 29304 KB Output is correct
17 Correct 323 ms 30436 KB Output is correct
18 Correct 298 ms 33532 KB Output is correct
19 Correct 136 ms 8652 KB Output is correct
20 Correct 301 ms 29828 KB Output is correct
21 Correct 309 ms 29172 KB Output is correct
22 Correct 315 ms 30440 KB Output is correct
23 Correct 297 ms 33480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 275 ms 35356 KB Output is correct
4 Correct 281 ms 39336 KB Output is correct
5 Correct 287 ms 38652 KB Output is correct
6 Correct 279 ms 39164 KB Output is correct
7 Correct 287 ms 38976 KB Output is correct
8 Correct 295 ms 37956 KB Output is correct
9 Correct 284 ms 38712 KB Output is correct
10 Correct 286 ms 37640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 2 ms 460 KB Output is correct
11 Correct 2 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 436 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 2 ms 588 KB Output is correct
18 Correct 2 ms 460 KB Output is correct
19 Correct 2 ms 588 KB Output is correct
20 Correct 1 ms 588 KB Output is correct
21 Correct 2 ms 568 KB Output is correct
22 Correct 2 ms 652 KB Output is correct
23 Correct 1 ms 460 KB Output is correct
24 Correct 2 ms 816 KB Output is correct
25 Correct 2 ms 716 KB Output is correct
26 Correct 2 ms 844 KB Output is correct
27 Correct 2 ms 588 KB Output is correct
28 Correct 2 ms 824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 101 ms 25180 KB Output is correct
11 Correct 128 ms 28244 KB Output is correct
12 Correct 125 ms 27872 KB Output is correct
13 Correct 131 ms 28800 KB Output is correct
14 Correct 119 ms 31932 KB Output is correct
15 Correct 2 ms 460 KB Output is correct
16 Correct 2 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 460 KB Output is correct
19 Correct 1 ms 436 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 2 ms 588 KB Output is correct
23 Correct 2 ms 460 KB Output is correct
24 Correct 2 ms 588 KB Output is correct
25 Correct 1 ms 588 KB Output is correct
26 Correct 2 ms 568 KB Output is correct
27 Correct 2 ms 652 KB Output is correct
28 Correct 1 ms 460 KB Output is correct
29 Correct 2 ms 816 KB Output is correct
30 Correct 2 ms 716 KB Output is correct
31 Correct 2 ms 844 KB Output is correct
32 Correct 2 ms 588 KB Output is correct
33 Correct 2 ms 824 KB Output is correct
34 Correct 15 ms 4212 KB Output is correct
35 Correct 132 ms 29552 KB Output is correct
36 Correct 130 ms 29568 KB Output is correct
37 Correct 130 ms 29632 KB Output is correct
38 Correct 127 ms 29352 KB Output is correct
39 Correct 127 ms 29248 KB Output is correct
40 Correct 116 ms 27708 KB Output is correct
41 Correct 133 ms 29808 KB Output is correct
42 Correct 129 ms 29544 KB Output is correct
43 Correct 119 ms 33772 KB Output is correct
44 Correct 136 ms 29924 KB Output is correct
45 Correct 194 ms 41032 KB Output is correct
46 Correct 130 ms 29852 KB Output is correct
47 Correct 132 ms 30000 KB Output is correct
48 Correct 149 ms 33528 KB Output is correct
49 Correct 144 ms 41800 KB Output is correct
50 Correct 115 ms 29564 KB Output is correct
51 Correct 157 ms 37264 KB Output is correct
52 Correct 198 ms 42284 KB Output is correct
53 Correct 240 ms 54668 KB Output is correct
54 Correct 255 ms 60064 KB Output is correct
55 Correct 121 ms 33568 KB Output is correct
56 Correct 224 ms 56504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 101 ms 25180 KB Output is correct
10 Correct 128 ms 28244 KB Output is correct
11 Correct 125 ms 27872 KB Output is correct
12 Correct 131 ms 28800 KB Output is correct
13 Correct 119 ms 31932 KB Output is correct
14 Correct 116 ms 25292 KB Output is correct
15 Correct 308 ms 29848 KB Output is correct
16 Correct 295 ms 29304 KB Output is correct
17 Correct 323 ms 30436 KB Output is correct
18 Correct 298 ms 33532 KB Output is correct
19 Correct 275 ms 35356 KB Output is correct
20 Correct 281 ms 39336 KB Output is correct
21 Correct 287 ms 38652 KB Output is correct
22 Correct 279 ms 39164 KB Output is correct
23 Correct 287 ms 38976 KB Output is correct
24 Correct 295 ms 37956 KB Output is correct
25 Correct 284 ms 38712 KB Output is correct
26 Correct 286 ms 37640 KB Output is correct
27 Correct 2 ms 460 KB Output is correct
28 Correct 2 ms 460 KB Output is correct
29 Correct 1 ms 460 KB Output is correct
30 Correct 1 ms 460 KB Output is correct
31 Correct 1 ms 436 KB Output is correct
32 Correct 1 ms 460 KB Output is correct
33 Correct 1 ms 460 KB Output is correct
34 Correct 2 ms 588 KB Output is correct
35 Correct 2 ms 460 KB Output is correct
36 Correct 15 ms 4212 KB Output is correct
37 Correct 132 ms 29552 KB Output is correct
38 Correct 130 ms 29568 KB Output is correct
39 Correct 130 ms 29632 KB Output is correct
40 Correct 127 ms 29352 KB Output is correct
41 Correct 127 ms 29248 KB Output is correct
42 Correct 116 ms 27708 KB Output is correct
43 Correct 133 ms 29808 KB Output is correct
44 Correct 129 ms 29544 KB Output is correct
45 Correct 119 ms 33772 KB Output is correct
46 Correct 136 ms 29924 KB Output is correct
47 Correct 29 ms 4292 KB Output is correct
48 Correct 328 ms 33624 KB Output is correct
49 Correct 319 ms 33504 KB Output is correct
50 Correct 328 ms 33428 KB Output is correct
51 Correct 313 ms 33304 KB Output is correct
52 Correct 308 ms 32320 KB Output is correct
53 Correct 290 ms 27656 KB Output is correct
54 Correct 321 ms 33792 KB Output is correct
55 Correct 329 ms 33404 KB Output is correct
56 Correct 341 ms 36716 KB Output is correct
57 Correct 321 ms 33796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 101 ms 25180 KB Output is correct
11 Correct 128 ms 28244 KB Output is correct
12 Correct 125 ms 27872 KB Output is correct
13 Correct 131 ms 28800 KB Output is correct
14 Correct 119 ms 31932 KB Output is correct
15 Correct 116 ms 25292 KB Output is correct
16 Correct 308 ms 29848 KB Output is correct
17 Correct 295 ms 29304 KB Output is correct
18 Correct 323 ms 30436 KB Output is correct
19 Correct 298 ms 33532 KB Output is correct
20 Correct 275 ms 35356 KB Output is correct
21 Correct 281 ms 39336 KB Output is correct
22 Correct 287 ms 38652 KB Output is correct
23 Correct 279 ms 39164 KB Output is correct
24 Correct 287 ms 38976 KB Output is correct
25 Correct 295 ms 37956 KB Output is correct
26 Correct 284 ms 38712 KB Output is correct
27 Correct 286 ms 37640 KB Output is correct
28 Correct 2 ms 460 KB Output is correct
29 Correct 2 ms 460 KB Output is correct
30 Correct 1 ms 460 KB Output is correct
31 Correct 1 ms 460 KB Output is correct
32 Correct 1 ms 436 KB Output is correct
33 Correct 1 ms 460 KB Output is correct
34 Correct 1 ms 460 KB Output is correct
35 Correct 2 ms 588 KB Output is correct
36 Correct 2 ms 460 KB Output is correct
37 Correct 15 ms 4212 KB Output is correct
38 Correct 132 ms 29552 KB Output is correct
39 Correct 130 ms 29568 KB Output is correct
40 Correct 130 ms 29632 KB Output is correct
41 Correct 127 ms 29352 KB Output is correct
42 Correct 127 ms 29248 KB Output is correct
43 Correct 116 ms 27708 KB Output is correct
44 Correct 133 ms 29808 KB Output is correct
45 Correct 129 ms 29544 KB Output is correct
46 Correct 119 ms 33772 KB Output is correct
47 Correct 136 ms 29924 KB Output is correct
48 Correct 29 ms 4292 KB Output is correct
49 Correct 328 ms 33624 KB Output is correct
50 Correct 319 ms 33504 KB Output is correct
51 Correct 328 ms 33428 KB Output is correct
52 Correct 313 ms 33304 KB Output is correct
53 Correct 308 ms 32320 KB Output is correct
54 Correct 290 ms 27656 KB Output is correct
55 Correct 321 ms 33792 KB Output is correct
56 Correct 329 ms 33404 KB Output is correct
57 Correct 341 ms 36716 KB Output is correct
58 Correct 321 ms 33796 KB Output is correct
59 Correct 136 ms 8652 KB Output is correct
60 Correct 301 ms 29828 KB Output is correct
61 Correct 309 ms 29172 KB Output is correct
62 Correct 315 ms 30440 KB Output is correct
63 Correct 297 ms 33480 KB Output is correct
64 Correct 2 ms 588 KB Output is correct
65 Correct 1 ms 588 KB Output is correct
66 Correct 2 ms 568 KB Output is correct
67 Correct 2 ms 652 KB Output is correct
68 Correct 1 ms 460 KB Output is correct
69 Correct 2 ms 816 KB Output is correct
70 Correct 2 ms 716 KB Output is correct
71 Correct 2 ms 844 KB Output is correct
72 Correct 2 ms 588 KB Output is correct
73 Correct 2 ms 824 KB Output is correct
74 Correct 194 ms 41032 KB Output is correct
75 Correct 130 ms 29852 KB Output is correct
76 Correct 132 ms 30000 KB Output is correct
77 Correct 149 ms 33528 KB Output is correct
78 Correct 144 ms 41800 KB Output is correct
79 Correct 115 ms 29564 KB Output is correct
80 Correct 157 ms 37264 KB Output is correct
81 Correct 198 ms 42284 KB Output is correct
82 Correct 240 ms 54668 KB Output is correct
83 Correct 255 ms 60064 KB Output is correct
84 Correct 121 ms 33568 KB Output is correct
85 Correct 224 ms 56504 KB Output is correct
86 Correct 99 ms 16724 KB Output is correct
87 Correct 378 ms 33796 KB Output is correct
88 Correct 338 ms 33800 KB Output is correct
89 Correct 368 ms 36480 KB Output is correct
90 Correct 299 ms 46892 KB Output is correct
91 Correct 310 ms 44136 KB Output is correct
92 Correct 331 ms 39712 KB Output is correct
93 Correct 384 ms 45620 KB Output is correct
94 Correct 454 ms 58628 KB Output is correct
95 Correct 436 ms 63908 KB Output is correct
96 Correct 356 ms 37236 KB Output is correct
97 Correct 383 ms 42804 KB Output is correct