답안 #295730

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
295730 2020-09-09T20:47:42 Z jhnah917 자매 도시 (APIO20_swap) C++14
100 / 100
614 ms 54624 KB
#include "swap.h"
#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define compress(v) sort(all(v)), v.erase(unique(all(v)), v.end())
using namespace std;

typedef long long ll;
typedef pair<ll, ll> p;

struct Edge{
    int s, e, x;
    bool operator < (const Edge &t) const { return tie(x, s, e) < tie(t.x, t.s, t.e); }
};

int par[101010];
int find(int v){ return v == par[v] ? v : par[v] = find(par[v]); }
int merge(int u, int v){ u = find(u); v = find(v); return u == v ? 0 : (par[u] = v, 1); }

const int sz = 1 << 17;
struct Seg{
    int tree[1 << 18];
    void update(int x, int v){
        x |= sz; tree[x] = v;
        while(x >>= 1) tree[x] = max(tree[x << 1], tree[x << 1 | 1]);
    }
    int query(int l, int r){
        l |= sz; r |= sz; int ret = 0;
        while(l <= r){
            if(l & 1) ret = max(ret, tree[l++]);
            if(~r & 1) ret = max(ret, tree[r--]);
            l >>= 1; r >>= 1;
        }
        return ret;
    }
};

struct HLD{
    vector<p> inp[101010]; vector<int> g[101010]; Seg seg;
    int in[101010], top[101010], dep[101010], sz[101010], par[101010], w[101010], pv;
    void dfs(int v, int b){
        for(auto i : inp[v]) if(i.x != b) g[v].push_back(i.x), w[i.x] = i.y, dep[i.x] = dep[v] + 1, par[i.x] = v, dfs(i.x, v);
    }
    void dfs1(int v){
        sz[v] = 1;
        for(auto &i : g[v]){
            dfs1(i); sz[v] += sz[i];
            if(sz[i] > sz[g[v][0]]) swap(i, g[v][0]);
        }
    }
    void dfs2(int v){
        in[v] = ++pv;
        for(auto i : g[v]) top[i] = i == g[v][0] ? top[v] : i, dfs2(i);
    }
    void init(int n){
        dfs(0, -1); dfs1(0); dfs2(0);
        for(int i=0; i<n; i++) seg.update(in[i], w[i]);
    }
    int query(int u, int v){
        int ret = 0;
        for(; top[u] != top[v]; u=par[top[u]]){
            if(dep[top[u]] < dep[top[v]]) swap(u, v);
            ret = max(ret, seg.query(in[top[u]], in[u]));
        }
        if(dep[u] > dep[v]) swap(u, v);
        return max(ret, seg.query(in[u]+1, in[v]));
    }
    void addEdge(int s, int e, int x){ inp[s].emplace_back(e, x); inp[e].emplace_back(s, x); }
} t1, t2;

int n, m, deg[101010], flag;
vector<Edge> edge;
vector<int> g[101010];

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
    n = N; m = M; edge.resize(m);
    for(int i=0; i<m; i++){
        edge[i].s = U[i]; edge[i].e = V[i]; edge[i].x = W[i];
        g[edge[i].s].push_back(edge[i].e); g[edge[i].e].push_back(edge[i].s);
    }
    sort(all(edge)); iota(par, par+101010, 0);
    for(auto i : edge) if(merge(i.s, i.e)) t1.addEdge(i.s, i.e, i.x);
    iota(par, par+101010, 0);
    for(auto i : edge){
        if(merge(i.s, i.e)) t2.addEdge(i.s, i.e, i.x);
        else{
            if(merge(i.s, n)) t2.addEdge(i.s, n, i.x);
            if(merge(i.e, n)) t2.addEdge(i.e, n, i.x);
        }
        if(++deg[i.s] == 3 && merge(i.s, n)) t2.addEdge(i.s, n, i.x), flag = 1;
        if(++deg[i.e] == 3 && merge(i.e, n)) t2.addEdge(i.e, n, i.x), flag = 1;
    }
    if(n <= m) flag = 1;
    t1.init(n); t2.init(n+1);
}

int getMinimumFuelCapacity(int a, int b){
    if(!flag) return -1;
    return max({t1.query(a, b), t2.query(a, b), t2.query(a, n), t2.query(b, n)});
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12672 KB Output is correct
2 Correct 9 ms 12672 KB Output is correct
3 Correct 8 ms 12672 KB Output is correct
4 Correct 9 ms 12800 KB Output is correct
5 Correct 10 ms 13056 KB Output is correct
6 Correct 10 ms 12996 KB Output is correct
7 Correct 10 ms 13056 KB Output is correct
8 Correct 10 ms 13056 KB Output is correct
9 Correct 194 ms 41028 KB Output is correct
10 Correct 245 ms 49144 KB Output is correct
11 Correct 251 ms 47992 KB Output is correct
12 Correct 271 ms 50552 KB Output is correct
13 Correct 210 ms 52472 KB Output is correct
14 Correct 199 ms 40312 KB Output is correct
15 Correct 318 ms 50820 KB Output is correct
16 Correct 310 ms 47656 KB Output is correct
17 Correct 352 ms 54624 KB Output is correct
18 Correct 292 ms 52352 KB Output is correct
19 Correct 173 ms 22520 KB Output is correct
20 Correct 457 ms 51064 KB Output is correct
21 Correct 455 ms 48820 KB Output is correct
22 Correct 501 ms 52824 KB Output is correct
23 Correct 430 ms 53008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12672 KB Output is correct
2 Correct 9 ms 12672 KB Output is correct
3 Correct 220 ms 38960 KB Output is correct
4 Correct 227 ms 39888 KB Output is correct
5 Correct 230 ms 39424 KB Output is correct
6 Correct 224 ms 39780 KB Output is correct
7 Correct 235 ms 39784 KB Output is correct
8 Correct 219 ms 38680 KB Output is correct
9 Correct 237 ms 39400 KB Output is correct
10 Correct 219 ms 38692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 12672 KB Output is correct
2 Correct 9 ms 12672 KB Output is correct
3 Correct 9 ms 12672 KB Output is correct
4 Correct 8 ms 12672 KB Output is correct
5 Correct 9 ms 12800 KB Output is correct
6 Correct 10 ms 13056 KB Output is correct
7 Correct 10 ms 12996 KB Output is correct
8 Correct 10 ms 13056 KB Output is correct
9 Correct 10 ms 13056 KB Output is correct
10 Correct 10 ms 12928 KB Output is correct
11 Correct 10 ms 13056 KB Output is correct
12 Correct 10 ms 13056 KB Output is correct
13 Correct 10 ms 12928 KB Output is correct
14 Correct 10 ms 12928 KB Output is correct
15 Correct 10 ms 12980 KB Output is correct
16 Correct 10 ms 13056 KB Output is correct
17 Correct 10 ms 13056 KB Output is correct
18 Correct 10 ms 12928 KB Output is correct
19 Correct 10 ms 12928 KB Output is correct
20 Correct 10 ms 13056 KB Output is correct
21 Correct 9 ms 12928 KB Output is correct
22 Correct 10 ms 12896 KB Output is correct
23 Correct 9 ms 12928 KB Output is correct
24 Correct 10 ms 13056 KB Output is correct
25 Correct 10 ms 13056 KB Output is correct
26 Correct 11 ms 13056 KB Output is correct
27 Correct 10 ms 13056 KB Output is correct
28 Correct 10 ms 13056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 12672 KB Output is correct
2 Correct 9 ms 12672 KB Output is correct
3 Correct 9 ms 12672 KB Output is correct
4 Correct 8 ms 12672 KB Output is correct
5 Correct 9 ms 12800 KB Output is correct
6 Correct 10 ms 13056 KB Output is correct
7 Correct 10 ms 12996 KB Output is correct
8 Correct 10 ms 13056 KB Output is correct
9 Correct 10 ms 13056 KB Output is correct
10 Correct 194 ms 41028 KB Output is correct
11 Correct 245 ms 49144 KB Output is correct
12 Correct 251 ms 47992 KB Output is correct
13 Correct 271 ms 50552 KB Output is correct
14 Correct 210 ms 52472 KB Output is correct
15 Correct 10 ms 12928 KB Output is correct
16 Correct 10 ms 13056 KB Output is correct
17 Correct 10 ms 13056 KB Output is correct
18 Correct 10 ms 12928 KB Output is correct
19 Correct 10 ms 12928 KB Output is correct
20 Correct 10 ms 12980 KB Output is correct
21 Correct 10 ms 13056 KB Output is correct
22 Correct 10 ms 13056 KB Output is correct
23 Correct 10 ms 12928 KB Output is correct
24 Correct 30 ms 16376 KB Output is correct
25 Correct 268 ms 49656 KB Output is correct
26 Correct 247 ms 46072 KB Output is correct
27 Correct 253 ms 43384 KB Output is correct
28 Correct 248 ms 42488 KB Output is correct
29 Correct 252 ms 41468 KB Output is correct
30 Correct 217 ms 38136 KB Output is correct
31 Correct 262 ms 49020 KB Output is correct
32 Correct 267 ms 48760 KB Output is correct
33 Correct 209 ms 50168 KB Output is correct
34 Correct 242 ms 40184 KB Output is correct
35 Correct 10 ms 12928 KB Output is correct
36 Correct 10 ms 13056 KB Output is correct
37 Correct 9 ms 12928 KB Output is correct
38 Correct 10 ms 12896 KB Output is correct
39 Correct 9 ms 12928 KB Output is correct
40 Correct 10 ms 13056 KB Output is correct
41 Correct 10 ms 13056 KB Output is correct
42 Correct 11 ms 13056 KB Output is correct
43 Correct 10 ms 13056 KB Output is correct
44 Correct 10 ms 13056 KB Output is correct
45 Correct 240 ms 36840 KB Output is correct
46 Correct 252 ms 44792 KB Output is correct
47 Correct 243 ms 42188 KB Output is correct
48 Correct 242 ms 40696 KB Output is correct
49 Correct 96 ms 20772 KB Output is correct
50 Correct 90 ms 19960 KB Output is correct
51 Correct 203 ms 32716 KB Output is correct
52 Correct 324 ms 47044 KB Output is correct
53 Correct 321 ms 43500 KB Output is correct
54 Correct 396 ms 51136 KB Output is correct
55 Correct 220 ms 51832 KB Output is correct
56 Correct 320 ms 42872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12672 KB Output is correct
2 Correct 9 ms 12672 KB Output is correct
3 Correct 8 ms 12672 KB Output is correct
4 Correct 9 ms 12800 KB Output is correct
5 Correct 10 ms 13056 KB Output is correct
6 Correct 10 ms 12996 KB Output is correct
7 Correct 10 ms 13056 KB Output is correct
8 Correct 10 ms 13056 KB Output is correct
9 Correct 194 ms 41028 KB Output is correct
10 Correct 245 ms 49144 KB Output is correct
11 Correct 251 ms 47992 KB Output is correct
12 Correct 271 ms 50552 KB Output is correct
13 Correct 210 ms 52472 KB Output is correct
14 Correct 199 ms 40312 KB Output is correct
15 Correct 318 ms 50820 KB Output is correct
16 Correct 310 ms 47656 KB Output is correct
17 Correct 352 ms 54624 KB Output is correct
18 Correct 292 ms 52352 KB Output is correct
19 Correct 220 ms 38960 KB Output is correct
20 Correct 227 ms 39888 KB Output is correct
21 Correct 230 ms 39424 KB Output is correct
22 Correct 224 ms 39780 KB Output is correct
23 Correct 235 ms 39784 KB Output is correct
24 Correct 219 ms 38680 KB Output is correct
25 Correct 237 ms 39400 KB Output is correct
26 Correct 219 ms 38692 KB Output is correct
27 Correct 10 ms 12928 KB Output is correct
28 Correct 10 ms 13056 KB Output is correct
29 Correct 10 ms 13056 KB Output is correct
30 Correct 10 ms 12928 KB Output is correct
31 Correct 10 ms 12928 KB Output is correct
32 Correct 10 ms 12980 KB Output is correct
33 Correct 10 ms 13056 KB Output is correct
34 Correct 10 ms 13056 KB Output is correct
35 Correct 10 ms 12928 KB Output is correct
36 Correct 30 ms 16376 KB Output is correct
37 Correct 268 ms 49656 KB Output is correct
38 Correct 247 ms 46072 KB Output is correct
39 Correct 253 ms 43384 KB Output is correct
40 Correct 248 ms 42488 KB Output is correct
41 Correct 252 ms 41468 KB Output is correct
42 Correct 217 ms 38136 KB Output is correct
43 Correct 262 ms 49020 KB Output is correct
44 Correct 267 ms 48760 KB Output is correct
45 Correct 209 ms 50168 KB Output is correct
46 Correct 242 ms 40184 KB Output is correct
47 Correct 45 ms 16504 KB Output is correct
48 Correct 499 ms 49888 KB Output is correct
49 Correct 527 ms 47456 KB Output is correct
50 Correct 496 ms 46052 KB Output is correct
51 Correct 501 ms 45292 KB Output is correct
52 Correct 478 ms 42792 KB Output is correct
53 Correct 366 ms 34540 KB Output is correct
54 Correct 529 ms 49376 KB Output is correct
55 Correct 506 ms 50400 KB Output is correct
56 Correct 423 ms 53600 KB Output is correct
57 Correct 487 ms 43360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 12672 KB Output is correct
2 Correct 9 ms 12672 KB Output is correct
3 Correct 9 ms 12672 KB Output is correct
4 Correct 8 ms 12672 KB Output is correct
5 Correct 9 ms 12800 KB Output is correct
6 Correct 10 ms 13056 KB Output is correct
7 Correct 10 ms 12996 KB Output is correct
8 Correct 10 ms 13056 KB Output is correct
9 Correct 10 ms 13056 KB Output is correct
10 Correct 194 ms 41028 KB Output is correct
11 Correct 245 ms 49144 KB Output is correct
12 Correct 251 ms 47992 KB Output is correct
13 Correct 271 ms 50552 KB Output is correct
14 Correct 210 ms 52472 KB Output is correct
15 Correct 199 ms 40312 KB Output is correct
16 Correct 318 ms 50820 KB Output is correct
17 Correct 310 ms 47656 KB Output is correct
18 Correct 352 ms 54624 KB Output is correct
19 Correct 292 ms 52352 KB Output is correct
20 Correct 220 ms 38960 KB Output is correct
21 Correct 227 ms 39888 KB Output is correct
22 Correct 230 ms 39424 KB Output is correct
23 Correct 224 ms 39780 KB Output is correct
24 Correct 235 ms 39784 KB Output is correct
25 Correct 219 ms 38680 KB Output is correct
26 Correct 237 ms 39400 KB Output is correct
27 Correct 219 ms 38692 KB Output is correct
28 Correct 10 ms 12928 KB Output is correct
29 Correct 10 ms 13056 KB Output is correct
30 Correct 10 ms 13056 KB Output is correct
31 Correct 10 ms 12928 KB Output is correct
32 Correct 10 ms 12928 KB Output is correct
33 Correct 10 ms 12980 KB Output is correct
34 Correct 10 ms 13056 KB Output is correct
35 Correct 10 ms 13056 KB Output is correct
36 Correct 10 ms 12928 KB Output is correct
37 Correct 30 ms 16376 KB Output is correct
38 Correct 268 ms 49656 KB Output is correct
39 Correct 247 ms 46072 KB Output is correct
40 Correct 253 ms 43384 KB Output is correct
41 Correct 248 ms 42488 KB Output is correct
42 Correct 252 ms 41468 KB Output is correct
43 Correct 217 ms 38136 KB Output is correct
44 Correct 262 ms 49020 KB Output is correct
45 Correct 267 ms 48760 KB Output is correct
46 Correct 209 ms 50168 KB Output is correct
47 Correct 242 ms 40184 KB Output is correct
48 Correct 45 ms 16504 KB Output is correct
49 Correct 499 ms 49888 KB Output is correct
50 Correct 527 ms 47456 KB Output is correct
51 Correct 496 ms 46052 KB Output is correct
52 Correct 501 ms 45292 KB Output is correct
53 Correct 478 ms 42792 KB Output is correct
54 Correct 366 ms 34540 KB Output is correct
55 Correct 529 ms 49376 KB Output is correct
56 Correct 506 ms 50400 KB Output is correct
57 Correct 423 ms 53600 KB Output is correct
58 Correct 487 ms 43360 KB Output is correct
59 Correct 173 ms 22520 KB Output is correct
60 Correct 457 ms 51064 KB Output is correct
61 Correct 455 ms 48820 KB Output is correct
62 Correct 501 ms 52824 KB Output is correct
63 Correct 430 ms 53008 KB Output is correct
64 Correct 10 ms 12928 KB Output is correct
65 Correct 10 ms 13056 KB Output is correct
66 Correct 9 ms 12928 KB Output is correct
67 Correct 10 ms 12896 KB Output is correct
68 Correct 9 ms 12928 KB Output is correct
69 Correct 10 ms 13056 KB Output is correct
70 Correct 10 ms 13056 KB Output is correct
71 Correct 11 ms 13056 KB Output is correct
72 Correct 10 ms 13056 KB Output is correct
73 Correct 10 ms 13056 KB Output is correct
74 Correct 240 ms 36840 KB Output is correct
75 Correct 252 ms 44792 KB Output is correct
76 Correct 243 ms 42188 KB Output is correct
77 Correct 242 ms 40696 KB Output is correct
78 Correct 96 ms 20772 KB Output is correct
79 Correct 90 ms 19960 KB Output is correct
80 Correct 203 ms 32716 KB Output is correct
81 Correct 324 ms 47044 KB Output is correct
82 Correct 321 ms 43500 KB Output is correct
83 Correct 396 ms 51136 KB Output is correct
84 Correct 220 ms 51832 KB Output is correct
85 Correct 320 ms 42872 KB Output is correct
86 Correct 130 ms 22444 KB Output is correct
87 Correct 497 ms 46944 KB Output is correct
88 Correct 509 ms 47328 KB Output is correct
89 Correct 414 ms 41256 KB Output is correct
90 Correct 249 ms 22996 KB Output is correct
91 Correct 295 ms 24480 KB Output is correct
92 Correct 417 ms 35672 KB Output is correct
93 Correct 560 ms 49880 KB Output is correct
94 Correct 527 ms 45908 KB Output is correct
95 Correct 614 ms 52472 KB Output is correct
96 Correct 427 ms 50784 KB Output is correct
97 Correct 538 ms 44224 KB Output is correct