Submission #882721

# Submission time Handle Problem Language Result Execution time Memory
882721 2023-12-03T14:12:27 Z 12345678 Swapping Cities (APIO20_swap) C++17
100 / 100
257 ms 62912 KB
#include "swap.h"
#include <bits/stdc++.h>

using namespace std;

const int nx=3e5+5, kx=19;
int lvl[nx], pa[nx][kx], dsu[nx], cnt, vl[nx], c[nx], f, l[nx];
vector<tuple<int, int, int>> v;
vector<int> d[nx];

int find(int x)
{
    if (x==dsu[x]) return x;
    return dsu[x]=find(dsu[x]); 
}

void dfs(int u, int p)
{
    lvl[u]=lvl[p]+1; pa[u][0]=p;
    for (int i=1; i<kx; i++) pa[u][i]=pa[pa[u][i-1]][i-1];
    for (auto v:d[u])
    {
        if (!c[v]) vl[v]=vl[u];
        dfs(v, u);
    }
}

int lca(int u, int v)
{
    if (lvl[u]>lvl[v]) swap(u, v);
    for (int i=kx-1; i>=0; i--) if (lvl[pa[v][i]]>=lvl[u]) v=pa[v][i];
    if (u==v) return u;
    for (int i=kx-1; i>=0; i--) if (pa[u][i]!=pa[v][i]) u=pa[u][i], v=pa[v][i];
    return pa[u][0];
}

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
    cnt=N;
    for (int i=0; i<nx; i++) dsu[i]=i;
    for (int i=0; i<M; i++) v.push_back({W[i], U[i], V[i]});
    sort(v.begin(), v.end());
    for (auto [z, x, y]:v)
    {
        vl[cnt]=z;
        l[x]++; l[y]++;
        if (find(x)==find(y)) d[cnt].push_back(find(x)), dsu[find(x)]=cnt, c[cnt++]=1;
        else
        {
            int px=find(x), py=find(y);
            dsu[px]=cnt; dsu[py]=cnt;
            d[cnt].push_back(px);
            d[cnt].push_back(py);
            //cout<<px<<' '<<py<<' '<<d[px].size()<<' '<<d[py].size()<<'\n';
            c[cnt++]=c[px]||c[py]||l[x]>=3||l[y]>=3;
        }
    }
    if (!c[cnt-1]) f=1;
    else dfs(cnt-1, cnt-1);
}

int getMinimumFuelCapacity(int X, int Y) {
    if (f) return -1;
    else return vl[lca(X, Y)];
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12888 KB Output is correct
2 Correct 2 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 2 ms 12892 KB Output is correct
6 Correct 3 ms 12892 KB Output is correct
7 Correct 3 ms 12888 KB Output is correct
8 Correct 2 ms 12888 KB Output is correct
9 Correct 33 ms 18060 KB Output is correct
10 Correct 40 ms 19136 KB Output is correct
11 Correct 39 ms 19144 KB Output is correct
12 Correct 42 ms 19396 KB Output is correct
13 Correct 42 ms 20164 KB Output is correct
14 Correct 39 ms 18116 KB Output is correct
15 Correct 80 ms 21004 KB Output is correct
16 Correct 83 ms 20856 KB Output is correct
17 Correct 90 ms 21324 KB Output is correct
18 Correct 79 ms 21764 KB Output is correct
19 Correct 82 ms 19416 KB Output is correct
20 Correct 213 ms 37440 KB Output is correct
21 Correct 177 ms 37472 KB Output is correct
22 Correct 198 ms 37876 KB Output is correct
23 Correct 229 ms 40836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12888 KB Output is correct
2 Correct 2 ms 12892 KB Output is correct
3 Correct 198 ms 43272 KB Output is correct
4 Correct 218 ms 47608 KB Output is correct
5 Correct 194 ms 47472 KB Output is correct
6 Correct 170 ms 47492 KB Output is correct
7 Correct 177 ms 47560 KB Output is correct
8 Correct 165 ms 46940 KB Output is correct
9 Correct 200 ms 47444 KB Output is correct
10 Correct 195 ms 46824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12888 KB Output is correct
2 Correct 2 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 2 ms 12892 KB Output is correct
6 Correct 3 ms 12892 KB Output is correct
7 Correct 3 ms 12888 KB Output is correct
8 Correct 2 ms 12888 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 12936 KB Output is correct
12 Correct 3 ms 12892 KB Output is correct
13 Correct 3 ms 12892 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 12928 KB Output is correct
19 Correct 3 ms 12892 KB Output is correct
20 Correct 3 ms 12892 KB Output is correct
21 Correct 4 ms 12892 KB Output is correct
22 Correct 3 ms 13148 KB Output is correct
23 Correct 3 ms 12892 KB Output is correct
24 Correct 3 ms 12892 KB Output is correct
25 Correct 3 ms 12956 KB Output is correct
26 Correct 3 ms 13148 KB Output is correct
27 Correct 4 ms 12892 KB Output is correct
28 Correct 3 ms 13148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12892 KB Output is correct
2 Correct 3 ms 12888 KB Output is correct
3 Correct 2 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 2 ms 12892 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 3 ms 12888 KB Output is correct
9 Correct 2 ms 12888 KB Output is correct
10 Correct 33 ms 18060 KB Output is correct
11 Correct 40 ms 19136 KB Output is correct
12 Correct 39 ms 19144 KB Output is correct
13 Correct 42 ms 19396 KB Output is correct
14 Correct 42 ms 20164 KB Output is correct
15 Correct 3 ms 12892 KB Output is correct
16 Correct 3 ms 12936 KB Output is correct
17 Correct 3 ms 12892 KB Output is correct
18 Correct 3 ms 12892 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 12928 KB Output is correct
24 Correct 3 ms 12892 KB Output is correct
25 Correct 3 ms 12892 KB Output is correct
26 Correct 4 ms 12892 KB Output is correct
27 Correct 3 ms 13148 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 13148 KB Output is correct
32 Correct 4 ms 12892 KB Output is correct
33 Correct 3 ms 13148 KB Output is correct
34 Correct 13 ms 16440 KB Output is correct
35 Correct 99 ms 36332 KB Output is correct
36 Correct 90 ms 36552 KB Output is correct
37 Correct 88 ms 36516 KB Output is correct
38 Correct 94 ms 36436 KB Output is correct
39 Correct 86 ms 36300 KB Output is correct
40 Correct 78 ms 35524 KB Output is correct
41 Correct 100 ms 36604 KB Output is correct
42 Correct 95 ms 36564 KB Output is correct
43 Correct 75 ms 40656 KB Output is correct
44 Correct 99 ms 36732 KB Output is correct
45 Correct 107 ms 48924 KB Output is correct
46 Correct 89 ms 36340 KB Output is correct
47 Correct 105 ms 36932 KB Output is correct
48 Correct 108 ms 42016 KB Output is correct
49 Correct 83 ms 48748 KB Output is correct
50 Correct 67 ms 41180 KB Output is correct
51 Correct 94 ms 44484 KB Output is correct
52 Correct 122 ms 53440 KB Output is correct
53 Correct 148 ms 54808 KB Output is correct
54 Correct 153 ms 59088 KB Output is correct
55 Correct 76 ms 40388 KB Output is correct
56 Correct 132 ms 57032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12888 KB Output is correct
2 Correct 2 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 2 ms 12892 KB Output is correct
6 Correct 3 ms 12892 KB Output is correct
7 Correct 3 ms 12888 KB Output is correct
8 Correct 2 ms 12888 KB Output is correct
9 Correct 33 ms 18060 KB Output is correct
10 Correct 40 ms 19136 KB Output is correct
11 Correct 39 ms 19144 KB Output is correct
12 Correct 42 ms 19396 KB Output is correct
13 Correct 42 ms 20164 KB Output is correct
14 Correct 39 ms 18116 KB Output is correct
15 Correct 80 ms 21004 KB Output is correct
16 Correct 83 ms 20856 KB Output is correct
17 Correct 90 ms 21324 KB Output is correct
18 Correct 79 ms 21764 KB Output is correct
19 Correct 198 ms 43272 KB Output is correct
20 Correct 218 ms 47608 KB Output is correct
21 Correct 194 ms 47472 KB Output is correct
22 Correct 170 ms 47492 KB Output is correct
23 Correct 177 ms 47560 KB Output is correct
24 Correct 165 ms 46940 KB Output is correct
25 Correct 200 ms 47444 KB Output is correct
26 Correct 195 ms 46824 KB Output is correct
27 Correct 3 ms 12892 KB Output is correct
28 Correct 3 ms 12936 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 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 12928 KB Output is correct
36 Correct 13 ms 16440 KB Output is correct
37 Correct 99 ms 36332 KB Output is correct
38 Correct 90 ms 36552 KB Output is correct
39 Correct 88 ms 36516 KB Output is correct
40 Correct 94 ms 36436 KB Output is correct
41 Correct 86 ms 36300 KB Output is correct
42 Correct 78 ms 35524 KB Output is correct
43 Correct 100 ms 36604 KB Output is correct
44 Correct 95 ms 36564 KB Output is correct
45 Correct 75 ms 40656 KB Output is correct
46 Correct 99 ms 36732 KB Output is correct
47 Correct 18 ms 16660 KB Output is correct
48 Correct 213 ms 41592 KB Output is correct
49 Correct 200 ms 41308 KB Output is correct
50 Correct 189 ms 41424 KB Output is correct
51 Correct 172 ms 41256 KB Output is correct
52 Correct 179 ms 40796 KB Output is correct
53 Correct 162 ms 35204 KB Output is correct
54 Correct 190 ms 42108 KB Output is correct
55 Correct 181 ms 41336 KB Output is correct
56 Correct 230 ms 44748 KB Output is correct
57 Correct 200 ms 42212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12892 KB Output is correct
2 Correct 3 ms 12888 KB Output is correct
3 Correct 2 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 2 ms 12892 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 3 ms 12888 KB Output is correct
9 Correct 2 ms 12888 KB Output is correct
10 Correct 33 ms 18060 KB Output is correct
11 Correct 40 ms 19136 KB Output is correct
12 Correct 39 ms 19144 KB Output is correct
13 Correct 42 ms 19396 KB Output is correct
14 Correct 42 ms 20164 KB Output is correct
15 Correct 39 ms 18116 KB Output is correct
16 Correct 80 ms 21004 KB Output is correct
17 Correct 83 ms 20856 KB Output is correct
18 Correct 90 ms 21324 KB Output is correct
19 Correct 79 ms 21764 KB Output is correct
20 Correct 198 ms 43272 KB Output is correct
21 Correct 218 ms 47608 KB Output is correct
22 Correct 194 ms 47472 KB Output is correct
23 Correct 170 ms 47492 KB Output is correct
24 Correct 177 ms 47560 KB Output is correct
25 Correct 165 ms 46940 KB Output is correct
26 Correct 200 ms 47444 KB Output is correct
27 Correct 195 ms 46824 KB Output is correct
28 Correct 3 ms 12892 KB Output is correct
29 Correct 3 ms 12936 KB Output is correct
30 Correct 3 ms 12892 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 3 ms 12928 KB Output is correct
37 Correct 13 ms 16440 KB Output is correct
38 Correct 99 ms 36332 KB Output is correct
39 Correct 90 ms 36552 KB Output is correct
40 Correct 88 ms 36516 KB Output is correct
41 Correct 94 ms 36436 KB Output is correct
42 Correct 86 ms 36300 KB Output is correct
43 Correct 78 ms 35524 KB Output is correct
44 Correct 100 ms 36604 KB Output is correct
45 Correct 95 ms 36564 KB Output is correct
46 Correct 75 ms 40656 KB Output is correct
47 Correct 99 ms 36732 KB Output is correct
48 Correct 18 ms 16660 KB Output is correct
49 Correct 213 ms 41592 KB Output is correct
50 Correct 200 ms 41308 KB Output is correct
51 Correct 189 ms 41424 KB Output is correct
52 Correct 172 ms 41256 KB Output is correct
53 Correct 179 ms 40796 KB Output is correct
54 Correct 162 ms 35204 KB Output is correct
55 Correct 190 ms 42108 KB Output is correct
56 Correct 181 ms 41336 KB Output is correct
57 Correct 230 ms 44748 KB Output is correct
58 Correct 200 ms 42212 KB Output is correct
59 Correct 82 ms 19416 KB Output is correct
60 Correct 213 ms 37440 KB Output is correct
61 Correct 177 ms 37472 KB Output is correct
62 Correct 198 ms 37876 KB Output is correct
63 Correct 229 ms 40836 KB Output is correct
64 Correct 3 ms 12892 KB Output is correct
65 Correct 3 ms 12892 KB Output is correct
66 Correct 4 ms 12892 KB Output is correct
67 Correct 3 ms 13148 KB Output is correct
68 Correct 3 ms 12892 KB Output is correct
69 Correct 3 ms 12892 KB Output is correct
70 Correct 3 ms 12956 KB Output is correct
71 Correct 3 ms 13148 KB Output is correct
72 Correct 4 ms 12892 KB Output is correct
73 Correct 3 ms 13148 KB Output is correct
74 Correct 107 ms 48924 KB Output is correct
75 Correct 89 ms 36340 KB Output is correct
76 Correct 105 ms 36932 KB Output is correct
77 Correct 108 ms 42016 KB Output is correct
78 Correct 83 ms 48748 KB Output is correct
79 Correct 67 ms 41180 KB Output is correct
80 Correct 94 ms 44484 KB Output is correct
81 Correct 122 ms 53440 KB Output is correct
82 Correct 148 ms 54808 KB Output is correct
83 Correct 153 ms 59088 KB Output is correct
84 Correct 76 ms 40388 KB Output is correct
85 Correct 132 ms 57032 KB Output is correct
86 Correct 66 ms 26532 KB Output is correct
87 Correct 182 ms 41208 KB Output is correct
88 Correct 196 ms 41208 KB Output is correct
89 Correct 204 ms 44148 KB Output is correct
90 Correct 185 ms 54464 KB Output is correct
91 Correct 181 ms 51828 KB Output is correct
92 Correct 257 ms 48816 KB Output is correct
93 Correct 223 ms 57624 KB Output is correct
94 Correct 252 ms 59484 KB Output is correct
95 Correct 246 ms 62912 KB Output is correct
96 Correct 207 ms 44848 KB Output is correct
97 Correct 253 ms 53068 KB Output is correct