Submission #1065224

# Submission time Handle Problem Language Result Execution time Memory
1065224 2024-08-19T03:50:50 Z HiepVu217 Swapping Cities (APIO20_swap) C++17
100 / 100
318 ms 59132 KB
#include <bits/stdc++.h>
#include "swap.h"
using namespace std;
const int Z = 3e5 + 17;
int n, m, q, lg, c[Z], ans[Z];
int r[Z], d[Z], p[20][Z];
tuple <int, int, int> e[Z];
vector <int> adj[Z];
bool ex[Z];
int root (int u)
{
    if (u == r[u])
    {
        return u;
    }
    return r[u] = root(r[u]);
}
void join (int x, int y, int z)
{
    x = root(x);
    y = root(y);
    ++n, r[n] = n;
    if (x == y)
    {
        ex[n] = 1, r[x] = n;
        adj[n].push_back(x);
    }
    else
    {
        ex[n] = ex[x] | ex[y] | z;
        r[x] = r[y] = n;
        adj[n].push_back(x);
        adj[n].push_back(y);
    }
}
int lca (int u, int v)
{
    if (d[u] < d[v])
    {
        swap (u, v);
    }
    for (int i = lg; i >= 0; --i)
    {
        if (d[p[i][u]] >= d[v])
        {
            u = p[i][u];
        }
    }
    if (u == v)
    {
        return u;
    }
    for (int i = lg; i >= 0; --i)
    {
        if (p[i][u] != p[i][v])
        {
            u = p[i][u], v = p[i][v];
        }
    }
    return p[0][u];
}
void dfs (int u, int pr)
{
    d[u] = d[pr] + 1, p[0][u] = pr;
    for (int i = 1; i <= lg; ++i)
    {
        p[i][u] = p[i - 1][p[i - 1][u]];
    }
    for (int v: adj[u])
    {
        if (v != pr)
        {
            dfs (v, u);
        }
    }
}
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W)
{
    n = N, m = M;
    lg = __lg(n + m);
    for (int i = 0; i < m; ++i)
    {
        int u = U[i], v = V[i], w = W[i];
        ++u, ++v;
        e[i + 1] = {w, u, v};
    }
    for (int i = 1; i <= n; ++i)
    {
        r[i] = i;
    }
    sort (e + 1, e + m + 1);
    for (int i = 1; i <= m; ++i)
    {
        auto [w, u, v] = e[i];
        ++c[u], ++c[v];
        int z = (c[u] > 2) | (c[v] > 2);
        join (u, v, z);
        ans[n] = w;
    }
    for (int i = n; i >= 0; --i)
    {
        if (!d[i])
        {
            dfs (i, 0);
        }
    }
}
int getMinimumFuelCapacity(int u, int v)
{
    ++u, ++v;
    int z = lca (u, v);
    if (ex[z])
    {
        return ans[z];
    }
    for (int i = lg; i >= 0; --i)
    {
        if (p[i][z] && !ex[p[i][z]])
        {
            z = p[i][z];
        }
    }
    z = p[0][z];
    if (!z)
    {
        return -1;
    }
    return ans[z];
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7516 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 3 ms 7628 KB Output is correct
5 Correct 3 ms 7772 KB Output is correct
6 Correct 7 ms 7760 KB Output is correct
7 Correct 4 ms 7772 KB Output is correct
8 Correct 5 ms 7772 KB Output is correct
9 Correct 55 ms 26708 KB Output is correct
10 Correct 67 ms 31316 KB Output is correct
11 Correct 74 ms 30544 KB Output is correct
12 Correct 74 ms 31956 KB Output is correct
13 Correct 90 ms 34280 KB Output is correct
14 Correct 71 ms 26964 KB Output is correct
15 Correct 184 ms 34136 KB Output is correct
16 Correct 183 ms 33300 KB Output is correct
17 Correct 203 ms 35048 KB Output is correct
18 Correct 270 ms 37404 KB Output is correct
19 Correct 79 ms 15440 KB Output is correct
20 Correct 203 ms 37216 KB Output is correct
21 Correct 187 ms 36552 KB Output is correct
22 Correct 219 ms 38324 KB Output is correct
23 Correct 282 ms 40628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7516 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 225 ms 38900 KB Output is correct
4 Correct 222 ms 41884 KB Output is correct
5 Correct 240 ms 41448 KB Output is correct
6 Correct 229 ms 41668 KB Output is correct
7 Correct 218 ms 41680 KB Output is correct
8 Correct 218 ms 40756 KB Output is correct
9 Correct 226 ms 41420 KB Output is correct
10 Correct 228 ms 40204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7516 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 3 ms 7628 KB Output is correct
5 Correct 3 ms 7772 KB Output is correct
6 Correct 7 ms 7760 KB Output is correct
7 Correct 4 ms 7772 KB Output is correct
8 Correct 5 ms 7772 KB Output is correct
9 Correct 3 ms 7516 KB Output is correct
10 Correct 3 ms 7772 KB Output is correct
11 Correct 3 ms 7772 KB Output is correct
12 Correct 4 ms 7772 KB Output is correct
13 Correct 3 ms 7516 KB Output is correct
14 Correct 5 ms 7516 KB Output is correct
15 Correct 3 ms 7772 KB Output is correct
16 Correct 3 ms 7700 KB Output is correct
17 Correct 4 ms 7772 KB Output is correct
18 Correct 4 ms 7772 KB Output is correct
19 Correct 4 ms 7768 KB Output is correct
20 Correct 3 ms 7768 KB Output is correct
21 Correct 6 ms 7772 KB Output is correct
22 Correct 4 ms 7772 KB Output is correct
23 Correct 3 ms 7552 KB Output is correct
24 Correct 4 ms 7768 KB Output is correct
25 Correct 4 ms 7764 KB Output is correct
26 Correct 4 ms 7772 KB Output is correct
27 Correct 3 ms 7772 KB Output is correct
28 Correct 4 ms 7864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7516 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 3 ms 7628 KB Output is correct
6 Correct 3 ms 7772 KB Output is correct
7 Correct 7 ms 7760 KB Output is correct
8 Correct 4 ms 7772 KB Output is correct
9 Correct 5 ms 7772 KB Output is correct
10 Correct 55 ms 26708 KB Output is correct
11 Correct 67 ms 31316 KB Output is correct
12 Correct 74 ms 30544 KB Output is correct
13 Correct 74 ms 31956 KB Output is correct
14 Correct 90 ms 34280 KB Output is correct
15 Correct 3 ms 7772 KB Output is correct
16 Correct 3 ms 7772 KB Output is correct
17 Correct 4 ms 7772 KB Output is correct
18 Correct 3 ms 7516 KB Output is correct
19 Correct 5 ms 7516 KB Output is correct
20 Correct 3 ms 7772 KB Output is correct
21 Correct 3 ms 7700 KB Output is correct
22 Correct 4 ms 7772 KB Output is correct
23 Correct 4 ms 7772 KB Output is correct
24 Correct 4 ms 7768 KB Output is correct
25 Correct 3 ms 7768 KB Output is correct
26 Correct 6 ms 7772 KB Output is correct
27 Correct 4 ms 7772 KB Output is correct
28 Correct 3 ms 7552 KB Output is correct
29 Correct 4 ms 7768 KB Output is correct
30 Correct 4 ms 7764 KB Output is correct
31 Correct 4 ms 7772 KB Output is correct
32 Correct 3 ms 7772 KB Output is correct
33 Correct 4 ms 7864 KB Output is correct
34 Correct 10 ms 10588 KB Output is correct
35 Correct 76 ms 32340 KB Output is correct
36 Correct 63 ms 32336 KB Output is correct
37 Correct 66 ms 32604 KB Output is correct
38 Correct 72 ms 32084 KB Output is correct
39 Correct 69 ms 32084 KB Output is correct
40 Correct 68 ms 30036 KB Output is correct
41 Correct 81 ms 32852 KB Output is correct
42 Correct 73 ms 32592 KB Output is correct
43 Correct 61 ms 35676 KB Output is correct
44 Correct 71 ms 32852 KB Output is correct
45 Correct 88 ms 43348 KB Output is correct
46 Correct 67 ms 32500 KB Output is correct
47 Correct 67 ms 32460 KB Output is correct
48 Correct 86 ms 35920 KB Output is correct
49 Correct 67 ms 41300 KB Output is correct
50 Correct 55 ms 33364 KB Output is correct
51 Correct 86 ms 38480 KB Output is correct
52 Correct 98 ms 46164 KB Output is correct
53 Correct 122 ms 50000 KB Output is correct
54 Correct 119 ms 55384 KB Output is correct
55 Correct 63 ms 35272 KB Output is correct
56 Correct 114 ms 50844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7516 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 3 ms 7628 KB Output is correct
5 Correct 3 ms 7772 KB Output is correct
6 Correct 7 ms 7760 KB Output is correct
7 Correct 4 ms 7772 KB Output is correct
8 Correct 5 ms 7772 KB Output is correct
9 Correct 55 ms 26708 KB Output is correct
10 Correct 67 ms 31316 KB Output is correct
11 Correct 74 ms 30544 KB Output is correct
12 Correct 74 ms 31956 KB Output is correct
13 Correct 90 ms 34280 KB Output is correct
14 Correct 71 ms 26964 KB Output is correct
15 Correct 184 ms 34136 KB Output is correct
16 Correct 183 ms 33300 KB Output is correct
17 Correct 203 ms 35048 KB Output is correct
18 Correct 270 ms 37404 KB Output is correct
19 Correct 225 ms 38900 KB Output is correct
20 Correct 222 ms 41884 KB Output is correct
21 Correct 240 ms 41448 KB Output is correct
22 Correct 229 ms 41668 KB Output is correct
23 Correct 218 ms 41680 KB Output is correct
24 Correct 218 ms 40756 KB Output is correct
25 Correct 226 ms 41420 KB Output is correct
26 Correct 228 ms 40204 KB Output is correct
27 Correct 3 ms 7772 KB Output is correct
28 Correct 3 ms 7772 KB Output is correct
29 Correct 4 ms 7772 KB Output is correct
30 Correct 3 ms 7516 KB Output is correct
31 Correct 5 ms 7516 KB Output is correct
32 Correct 3 ms 7772 KB Output is correct
33 Correct 3 ms 7700 KB Output is correct
34 Correct 4 ms 7772 KB Output is correct
35 Correct 4 ms 7772 KB Output is correct
36 Correct 10 ms 10588 KB Output is correct
37 Correct 76 ms 32340 KB Output is correct
38 Correct 63 ms 32336 KB Output is correct
39 Correct 66 ms 32604 KB Output is correct
40 Correct 72 ms 32084 KB Output is correct
41 Correct 69 ms 32084 KB Output is correct
42 Correct 68 ms 30036 KB Output is correct
43 Correct 81 ms 32852 KB Output is correct
44 Correct 73 ms 32592 KB Output is correct
45 Correct 61 ms 35676 KB Output is correct
46 Correct 71 ms 32852 KB Output is correct
47 Correct 17 ms 10832 KB Output is correct
48 Correct 207 ms 37304 KB Output is correct
49 Correct 174 ms 37300 KB Output is correct
50 Correct 170 ms 37428 KB Output is correct
51 Correct 167 ms 37320 KB Output is correct
52 Correct 178 ms 35664 KB Output is correct
53 Correct 139 ms 30144 KB Output is correct
54 Correct 174 ms 38324 KB Output is correct
55 Correct 211 ms 37300 KB Output is correct
56 Correct 233 ms 39864 KB Output is correct
57 Correct 170 ms 38320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7516 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 3 ms 7628 KB Output is correct
6 Correct 3 ms 7772 KB Output is correct
7 Correct 7 ms 7760 KB Output is correct
8 Correct 4 ms 7772 KB Output is correct
9 Correct 5 ms 7772 KB Output is correct
10 Correct 55 ms 26708 KB Output is correct
11 Correct 67 ms 31316 KB Output is correct
12 Correct 74 ms 30544 KB Output is correct
13 Correct 74 ms 31956 KB Output is correct
14 Correct 90 ms 34280 KB Output is correct
15 Correct 71 ms 26964 KB Output is correct
16 Correct 184 ms 34136 KB Output is correct
17 Correct 183 ms 33300 KB Output is correct
18 Correct 203 ms 35048 KB Output is correct
19 Correct 270 ms 37404 KB Output is correct
20 Correct 225 ms 38900 KB Output is correct
21 Correct 222 ms 41884 KB Output is correct
22 Correct 240 ms 41448 KB Output is correct
23 Correct 229 ms 41668 KB Output is correct
24 Correct 218 ms 41680 KB Output is correct
25 Correct 218 ms 40756 KB Output is correct
26 Correct 226 ms 41420 KB Output is correct
27 Correct 228 ms 40204 KB Output is correct
28 Correct 3 ms 7772 KB Output is correct
29 Correct 3 ms 7772 KB Output is correct
30 Correct 4 ms 7772 KB Output is correct
31 Correct 3 ms 7516 KB Output is correct
32 Correct 5 ms 7516 KB Output is correct
33 Correct 3 ms 7772 KB Output is correct
34 Correct 3 ms 7700 KB Output is correct
35 Correct 4 ms 7772 KB Output is correct
36 Correct 4 ms 7772 KB Output is correct
37 Correct 10 ms 10588 KB Output is correct
38 Correct 76 ms 32340 KB Output is correct
39 Correct 63 ms 32336 KB Output is correct
40 Correct 66 ms 32604 KB Output is correct
41 Correct 72 ms 32084 KB Output is correct
42 Correct 69 ms 32084 KB Output is correct
43 Correct 68 ms 30036 KB Output is correct
44 Correct 81 ms 32852 KB Output is correct
45 Correct 73 ms 32592 KB Output is correct
46 Correct 61 ms 35676 KB Output is correct
47 Correct 71 ms 32852 KB Output is correct
48 Correct 17 ms 10832 KB Output is correct
49 Correct 207 ms 37304 KB Output is correct
50 Correct 174 ms 37300 KB Output is correct
51 Correct 170 ms 37428 KB Output is correct
52 Correct 167 ms 37320 KB Output is correct
53 Correct 178 ms 35664 KB Output is correct
54 Correct 139 ms 30144 KB Output is correct
55 Correct 174 ms 38324 KB Output is correct
56 Correct 211 ms 37300 KB Output is correct
57 Correct 233 ms 39864 KB Output is correct
58 Correct 170 ms 38320 KB Output is correct
59 Correct 79 ms 15440 KB Output is correct
60 Correct 203 ms 37216 KB Output is correct
61 Correct 187 ms 36552 KB Output is correct
62 Correct 219 ms 38324 KB Output is correct
63 Correct 282 ms 40628 KB Output is correct
64 Correct 4 ms 7768 KB Output is correct
65 Correct 3 ms 7768 KB Output is correct
66 Correct 6 ms 7772 KB Output is correct
67 Correct 4 ms 7772 KB Output is correct
68 Correct 3 ms 7552 KB Output is correct
69 Correct 4 ms 7768 KB Output is correct
70 Correct 4 ms 7764 KB Output is correct
71 Correct 4 ms 7772 KB Output is correct
72 Correct 3 ms 7772 KB Output is correct
73 Correct 4 ms 7864 KB Output is correct
74 Correct 88 ms 43348 KB Output is correct
75 Correct 67 ms 32500 KB Output is correct
76 Correct 67 ms 32460 KB Output is correct
77 Correct 86 ms 35920 KB Output is correct
78 Correct 67 ms 41300 KB Output is correct
79 Correct 55 ms 33364 KB Output is correct
80 Correct 86 ms 38480 KB Output is correct
81 Correct 98 ms 46164 KB Output is correct
82 Correct 122 ms 50000 KB Output is correct
83 Correct 119 ms 55384 KB Output is correct
84 Correct 63 ms 35272 KB Output is correct
85 Correct 114 ms 50844 KB Output is correct
86 Correct 61 ms 20744 KB Output is correct
87 Correct 185 ms 37308 KB Output is correct
88 Correct 169 ms 37328 KB Output is correct
89 Correct 261 ms 39484 KB Output is correct
90 Correct 154 ms 47272 KB Output is correct
91 Correct 209 ms 44664 KB Output is correct
92 Correct 229 ms 42296 KB Output is correct
93 Correct 209 ms 50188 KB Output is correct
94 Correct 318 ms 54320 KB Output is correct
95 Correct 288 ms 59132 KB Output is correct
96 Correct 248 ms 40024 KB Output is correct
97 Correct 275 ms 46760 KB Output is correct