Submission #412451

# Submission time Handle Problem Language Result Execution time Memory
412451 2021-05-26T22:54:21 Z couplefire Swapping Cities (APIO20_swap) C++17
100 / 100
408 ms 47844 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;

const int N = 100005;
const int M = 200005;

int n, m;
array<int, 3> edges[M];
int fa[N+M], par[N+M];
bool isLine[N+M];
int e1[N+M], e2[N+M], weight[N+M];
int cur_id;
vector<int> adj[N+M];
int up[N+M][20], tin[N+M], tout[N+M], timer = -1;

int find(int v){
    return v==fa[v]?v:fa[v]=find(fa[v]);
}

int unite(int u, int v){
    int _u = u, _v = v;
    u = find(u), v = find(v);
    if(u == v){
        if(!isLine[u]) return -1;
        isLine[u] = 0;
        e1[u] = e2[u] = -1;
        return u;
    }
    fa[u] = fa[v] = par[u] = par[v] = cur_id++;
    if(!isLine[u] || !isLine[v]) return cur_id-1;
    for(int i = 0; i<2; i++){
        if(e1[u] == _u && e1[v] == _v) e1[fa[u]] = e2[u], e2[fa[u]] = e2[v];
        else if(e1[u] == _u && e2[v] == _v) e1[fa[u]] = e2[u], e2[fa[u]] = e1[v];
        else if(e2[u] == _u && e1[v] == _v) e1[fa[u]] = e1[u], e2[fa[u]] = e2[v];
        else if(e2[u] == _u && e2[v] == _v) e1[fa[u]] = e1[u], e2[fa[u]] = e1[v];
        swap(_u, _v);
    }
    if(e1[fa[u]] == -1) return cur_id-1;
    isLine[fa[u]] = 1;
    // if(_u == 3 && _v == 1) cout << isLine[6] << endl;
    return cur_id-1;
}

void dfs(int v){
    tin[v] = ++timer;
    up[v][0] = par[v];
    for(int i = 1; i<20; i++)
        up[v][i] = up[up[v][i-1]][i-1];
    for(auto u : adj[v])
        dfs(u);
    tout[v] = timer;
}

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

int LCA(int u, int v){
    if(isPar(u, v)) return u;
    if(isPar(v, u)) return v;
    for(int i = 19; i>=0; i--)
        if(!isPar(up[u][i], v)) u = up[u][i];
    return up[u][0];
}

int query(int v){
    if(!isLine[v]) return weight[v];
    for(int i = 19; i>=0; i--)
        if(isLine[up[v][i]]) v = up[v][i];
    v = up[v][0];
    if(isLine[v]) return -1;
    return weight[v];
}

void init(int _n, int _m, vector<int> U, vector<int> V, vector<int> W){
    n = _n, m = _m;
    for(int i = 0; i<m; i++)
        edges[i] = {W[i], V[i], U[i]};
    sort(edges, edges+m);
    for(int i = 0; i<n+m; i++) fa[i] = i, e1[i] = e2[i] = -1, par[i] = i;
    for(int i = 0; i<n; i++) isLine[i] = 1, e1[i] = e2[i] = i;
    cur_id = n;
    for(int i = 0; i<m; i++){
        int v = unite(edges[i][1], edges[i][2]);
        if(v==-1) continue;
        weight[v] = edges[i][0];
    }
    // cout << cur_id << endl;
    for(int i = 0; i<cur_id-1; i++)
        adj[par[i]].push_back(i);
    dfs(cur_id-1);
    // cout << weight[7] << endl;
}

int getMinimumFuelCapacity(int u, int v){
    return query(LCA(u, v));
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 5 ms 7356 KB Output is correct
3 Correct 5 ms 7352 KB Output is correct
4 Correct 5 ms 7488 KB Output is correct
5 Correct 6 ms 7628 KB Output is correct
6 Correct 6 ms 7628 KB Output is correct
7 Correct 8 ms 7628 KB Output is correct
8 Correct 6 ms 7704 KB Output is correct
9 Correct 136 ms 30660 KB Output is correct
10 Correct 145 ms 35908 KB Output is correct
11 Correct 152 ms 35384 KB Output is correct
12 Correct 151 ms 37064 KB Output is correct
13 Correct 146 ms 38624 KB Output is correct
14 Correct 135 ms 31024 KB Output is correct
15 Correct 271 ms 40140 KB Output is correct
16 Correct 290 ms 39540 KB Output is correct
17 Correct 285 ms 41088 KB Output is correct
18 Correct 367 ms 42652 KB Output is correct
19 Correct 115 ms 17216 KB Output is correct
20 Correct 277 ms 41272 KB Output is correct
21 Correct 277 ms 40620 KB Output is correct
22 Correct 312 ms 42536 KB Output is correct
23 Correct 383 ms 44096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 5 ms 7356 KB Output is correct
3 Correct 354 ms 43120 KB Output is correct
4 Correct 318 ms 44616 KB Output is correct
5 Correct 340 ms 43992 KB Output is correct
6 Correct 314 ms 44452 KB Output is correct
7 Correct 339 ms 44480 KB Output is correct
8 Correct 337 ms 43020 KB Output is correct
9 Correct 361 ms 44004 KB Output is correct
10 Correct 319 ms 42640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 5 ms 7356 KB Output is correct
3 Correct 5 ms 7352 KB Output is correct
4 Correct 5 ms 7488 KB Output is correct
5 Correct 6 ms 7628 KB Output is correct
6 Correct 6 ms 7628 KB Output is correct
7 Correct 8 ms 7628 KB Output is correct
8 Correct 6 ms 7704 KB Output is correct
9 Correct 5 ms 7372 KB Output is correct
10 Correct 6 ms 7628 KB Output is correct
11 Correct 6 ms 7624 KB Output is correct
12 Correct 6 ms 7624 KB Output is correct
13 Correct 6 ms 7616 KB Output is correct
14 Correct 6 ms 7628 KB Output is correct
15 Correct 7 ms 7624 KB Output is correct
16 Correct 6 ms 7628 KB Output is correct
17 Correct 6 ms 7628 KB Output is correct
18 Correct 6 ms 7620 KB Output is correct
19 Correct 6 ms 7628 KB Output is correct
20 Correct 6 ms 7612 KB Output is correct
21 Correct 6 ms 7628 KB Output is correct
22 Correct 6 ms 7500 KB Output is correct
23 Correct 6 ms 7628 KB Output is correct
24 Correct 8 ms 7752 KB Output is correct
25 Correct 6 ms 7756 KB Output is correct
26 Correct 9 ms 7756 KB Output is correct
27 Correct 6 ms 7628 KB Output is correct
28 Correct 6 ms 7756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 5 ms 7372 KB Output is correct
3 Correct 5 ms 7356 KB Output is correct
4 Correct 5 ms 7352 KB Output is correct
5 Correct 5 ms 7488 KB Output is correct
6 Correct 6 ms 7628 KB Output is correct
7 Correct 6 ms 7628 KB Output is correct
8 Correct 8 ms 7628 KB Output is correct
9 Correct 6 ms 7704 KB Output is correct
10 Correct 136 ms 30660 KB Output is correct
11 Correct 145 ms 35908 KB Output is correct
12 Correct 152 ms 35384 KB Output is correct
13 Correct 151 ms 37064 KB Output is correct
14 Correct 146 ms 38624 KB Output is correct
15 Correct 6 ms 7628 KB Output is correct
16 Correct 6 ms 7624 KB Output is correct
17 Correct 6 ms 7624 KB Output is correct
18 Correct 6 ms 7616 KB Output is correct
19 Correct 6 ms 7628 KB Output is correct
20 Correct 7 ms 7624 KB Output is correct
21 Correct 6 ms 7628 KB Output is correct
22 Correct 6 ms 7628 KB Output is correct
23 Correct 6 ms 7620 KB Output is correct
24 Correct 6 ms 7628 KB Output is correct
25 Correct 6 ms 7612 KB Output is correct
26 Correct 6 ms 7628 KB Output is correct
27 Correct 6 ms 7500 KB Output is correct
28 Correct 6 ms 7628 KB Output is correct
29 Correct 8 ms 7752 KB Output is correct
30 Correct 6 ms 7756 KB Output is correct
31 Correct 9 ms 7756 KB Output is correct
32 Correct 6 ms 7628 KB Output is correct
33 Correct 6 ms 7756 KB Output is correct
34 Correct 18 ms 11340 KB Output is correct
35 Correct 150 ms 36824 KB Output is correct
36 Correct 171 ms 36832 KB Output is correct
37 Correct 167 ms 36668 KB Output is correct
38 Correct 145 ms 36408 KB Output is correct
39 Correct 144 ms 36248 KB Output is correct
40 Correct 134 ms 33732 KB Output is correct
41 Correct 156 ms 37064 KB Output is correct
42 Correct 148 ms 36808 KB Output is correct
43 Correct 132 ms 38852 KB Output is correct
44 Correct 162 ms 37152 KB Output is correct
45 Correct 154 ms 36336 KB Output is correct
46 Correct 149 ms 36796 KB Output is correct
47 Correct 147 ms 36720 KB Output is correct
48 Correct 176 ms 37692 KB Output is correct
49 Correct 77 ms 18416 KB Output is correct
50 Correct 69 ms 17480 KB Output is correct
51 Correct 130 ms 31568 KB Output is correct
52 Correct 205 ms 41424 KB Output is correct
53 Correct 237 ms 43028 KB Output is correct
54 Correct 206 ms 44092 KB Output is correct
55 Correct 132 ms 38700 KB Output is correct
56 Correct 208 ms 43204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 5 ms 7356 KB Output is correct
3 Correct 5 ms 7352 KB Output is correct
4 Correct 5 ms 7488 KB Output is correct
5 Correct 6 ms 7628 KB Output is correct
6 Correct 6 ms 7628 KB Output is correct
7 Correct 8 ms 7628 KB Output is correct
8 Correct 6 ms 7704 KB Output is correct
9 Correct 136 ms 30660 KB Output is correct
10 Correct 145 ms 35908 KB Output is correct
11 Correct 152 ms 35384 KB Output is correct
12 Correct 151 ms 37064 KB Output is correct
13 Correct 146 ms 38624 KB Output is correct
14 Correct 135 ms 31024 KB Output is correct
15 Correct 271 ms 40140 KB Output is correct
16 Correct 290 ms 39540 KB Output is correct
17 Correct 285 ms 41088 KB Output is correct
18 Correct 367 ms 42652 KB Output is correct
19 Correct 354 ms 43120 KB Output is correct
20 Correct 318 ms 44616 KB Output is correct
21 Correct 340 ms 43992 KB Output is correct
22 Correct 314 ms 44452 KB Output is correct
23 Correct 339 ms 44480 KB Output is correct
24 Correct 337 ms 43020 KB Output is correct
25 Correct 361 ms 44004 KB Output is correct
26 Correct 319 ms 42640 KB Output is correct
27 Correct 6 ms 7628 KB Output is correct
28 Correct 6 ms 7624 KB Output is correct
29 Correct 6 ms 7624 KB Output is correct
30 Correct 6 ms 7616 KB Output is correct
31 Correct 6 ms 7628 KB Output is correct
32 Correct 7 ms 7624 KB Output is correct
33 Correct 6 ms 7628 KB Output is correct
34 Correct 6 ms 7628 KB Output is correct
35 Correct 6 ms 7620 KB Output is correct
36 Correct 18 ms 11340 KB Output is correct
37 Correct 150 ms 36824 KB Output is correct
38 Correct 171 ms 36832 KB Output is correct
39 Correct 167 ms 36668 KB Output is correct
40 Correct 145 ms 36408 KB Output is correct
41 Correct 144 ms 36248 KB Output is correct
42 Correct 134 ms 33732 KB Output is correct
43 Correct 156 ms 37064 KB Output is correct
44 Correct 148 ms 36808 KB Output is correct
45 Correct 132 ms 38852 KB Output is correct
46 Correct 162 ms 37152 KB Output is correct
47 Correct 28 ms 11588 KB Output is correct
48 Correct 261 ms 41644 KB Output is correct
49 Correct 280 ms 41684 KB Output is correct
50 Correct 256 ms 41632 KB Output is correct
51 Correct 278 ms 41448 KB Output is correct
52 Correct 249 ms 39788 KB Output is correct
53 Correct 231 ms 32856 KB Output is correct
54 Correct 308 ms 42536 KB Output is correct
55 Correct 276 ms 41792 KB Output is correct
56 Correct 310 ms 43336 KB Output is correct
57 Correct 279 ms 42628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 5 ms 7372 KB Output is correct
3 Correct 5 ms 7356 KB Output is correct
4 Correct 5 ms 7352 KB Output is correct
5 Correct 5 ms 7488 KB Output is correct
6 Correct 6 ms 7628 KB Output is correct
7 Correct 6 ms 7628 KB Output is correct
8 Correct 8 ms 7628 KB Output is correct
9 Correct 6 ms 7704 KB Output is correct
10 Correct 136 ms 30660 KB Output is correct
11 Correct 145 ms 35908 KB Output is correct
12 Correct 152 ms 35384 KB Output is correct
13 Correct 151 ms 37064 KB Output is correct
14 Correct 146 ms 38624 KB Output is correct
15 Correct 135 ms 31024 KB Output is correct
16 Correct 271 ms 40140 KB Output is correct
17 Correct 290 ms 39540 KB Output is correct
18 Correct 285 ms 41088 KB Output is correct
19 Correct 367 ms 42652 KB Output is correct
20 Correct 354 ms 43120 KB Output is correct
21 Correct 318 ms 44616 KB Output is correct
22 Correct 340 ms 43992 KB Output is correct
23 Correct 314 ms 44452 KB Output is correct
24 Correct 339 ms 44480 KB Output is correct
25 Correct 337 ms 43020 KB Output is correct
26 Correct 361 ms 44004 KB Output is correct
27 Correct 319 ms 42640 KB Output is correct
28 Correct 6 ms 7628 KB Output is correct
29 Correct 6 ms 7624 KB Output is correct
30 Correct 6 ms 7624 KB Output is correct
31 Correct 6 ms 7616 KB Output is correct
32 Correct 6 ms 7628 KB Output is correct
33 Correct 7 ms 7624 KB Output is correct
34 Correct 6 ms 7628 KB Output is correct
35 Correct 6 ms 7628 KB Output is correct
36 Correct 6 ms 7620 KB Output is correct
37 Correct 18 ms 11340 KB Output is correct
38 Correct 150 ms 36824 KB Output is correct
39 Correct 171 ms 36832 KB Output is correct
40 Correct 167 ms 36668 KB Output is correct
41 Correct 145 ms 36408 KB Output is correct
42 Correct 144 ms 36248 KB Output is correct
43 Correct 134 ms 33732 KB Output is correct
44 Correct 156 ms 37064 KB Output is correct
45 Correct 148 ms 36808 KB Output is correct
46 Correct 132 ms 38852 KB Output is correct
47 Correct 162 ms 37152 KB Output is correct
48 Correct 28 ms 11588 KB Output is correct
49 Correct 261 ms 41644 KB Output is correct
50 Correct 280 ms 41684 KB Output is correct
51 Correct 256 ms 41632 KB Output is correct
52 Correct 278 ms 41448 KB Output is correct
53 Correct 249 ms 39788 KB Output is correct
54 Correct 231 ms 32856 KB Output is correct
55 Correct 308 ms 42536 KB Output is correct
56 Correct 276 ms 41792 KB Output is correct
57 Correct 310 ms 43336 KB Output is correct
58 Correct 279 ms 42628 KB Output is correct
59 Correct 115 ms 17216 KB Output is correct
60 Correct 277 ms 41272 KB Output is correct
61 Correct 277 ms 40620 KB Output is correct
62 Correct 312 ms 42536 KB Output is correct
63 Correct 383 ms 44096 KB Output is correct
64 Correct 6 ms 7628 KB Output is correct
65 Correct 6 ms 7612 KB Output is correct
66 Correct 6 ms 7628 KB Output is correct
67 Correct 6 ms 7500 KB Output is correct
68 Correct 6 ms 7628 KB Output is correct
69 Correct 8 ms 7752 KB Output is correct
70 Correct 6 ms 7756 KB Output is correct
71 Correct 9 ms 7756 KB Output is correct
72 Correct 6 ms 7628 KB Output is correct
73 Correct 6 ms 7756 KB Output is correct
74 Correct 154 ms 36336 KB Output is correct
75 Correct 149 ms 36796 KB Output is correct
76 Correct 147 ms 36720 KB Output is correct
77 Correct 176 ms 37692 KB Output is correct
78 Correct 77 ms 18416 KB Output is correct
79 Correct 69 ms 17480 KB Output is correct
80 Correct 130 ms 31568 KB Output is correct
81 Correct 205 ms 41424 KB Output is correct
82 Correct 237 ms 43028 KB Output is correct
83 Correct 206 ms 44092 KB Output is correct
84 Correct 132 ms 38700 KB Output is correct
85 Correct 208 ms 43204 KB Output is correct
86 Correct 90 ms 19492 KB Output is correct
87 Correct 274 ms 41636 KB Output is correct
88 Correct 265 ms 41640 KB Output is correct
89 Correct 318 ms 40740 KB Output is correct
90 Correct 182 ms 23136 KB Output is correct
91 Correct 202 ms 24868 KB Output is correct
92 Correct 266 ms 36384 KB Output is correct
93 Correct 317 ms 46184 KB Output is correct
94 Correct 353 ms 47436 KB Output is correct
95 Correct 319 ms 47844 KB Output is correct
96 Correct 315 ms 43428 KB Output is correct
97 Correct 408 ms 45648 KB Output is correct