Submission #1065225

# Submission time Handle Problem Language Result Execution time Memory
1065225 2024-08-19T03:51:12 Z vjudge1 Swapping Cities (APIO20_swap) C++17
100 / 100
273 ms 52860 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 7512 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 7516 KB Output is correct
6 Correct 3 ms 7516 KB Output is correct
7 Correct 3 ms 7516 KB Output is correct
8 Correct 5 ms 7768 KB Output is correct
9 Correct 58 ms 25784 KB Output is correct
10 Correct 95 ms 29616 KB Output is correct
11 Correct 74 ms 29356 KB Output is correct
12 Correct 83 ms 30548 KB Output is correct
13 Correct 60 ms 33000 KB Output is correct
14 Correct 58 ms 25832 KB Output is correct
15 Correct 197 ms 31708 KB Output is correct
16 Correct 202 ms 30972 KB Output is correct
17 Correct 198 ms 32324 KB Output is correct
18 Correct 264 ms 34888 KB Output is correct
19 Correct 81 ms 14236 KB Output is correct
20 Correct 187 ms 32972 KB Output is correct
21 Correct 185 ms 32268 KB Output is correct
22 Correct 187 ms 33724 KB Output is correct
23 Correct 256 ms 36120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7512 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 215 ms 36908 KB Output is correct
4 Correct 225 ms 38072 KB Output is correct
5 Correct 232 ms 37556 KB Output is correct
6 Correct 200 ms 38068 KB Output is correct
7 Correct 218 ms 37836 KB Output is correct
8 Correct 213 ms 36696 KB Output is correct
9 Correct 214 ms 37632 KB Output is correct
10 Correct 214 ms 36484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7512 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 7516 KB Output is correct
6 Correct 3 ms 7516 KB Output is correct
7 Correct 3 ms 7516 KB Output is correct
8 Correct 5 ms 7768 KB Output is correct
9 Correct 3 ms 7512 KB Output is correct
10 Correct 4 ms 7516 KB Output is correct
11 Correct 4 ms 7516 KB Output is correct
12 Correct 4 ms 7532 KB Output is correct
13 Correct 4 ms 7516 KB Output is correct
14 Correct 3 ms 7512 KB Output is correct
15 Correct 3 ms 7516 KB Output is correct
16 Correct 3 ms 7516 KB Output is correct
17 Correct 3 ms 7676 KB Output is correct
18 Correct 3 ms 7516 KB Output is correct
19 Correct 3 ms 7516 KB Output is correct
20 Correct 3 ms 7760 KB Output is correct
21 Correct 3 ms 7772 KB Output is correct
22 Correct 3 ms 7772 KB Output is correct
23 Correct 3 ms 7624 KB Output is correct
24 Correct 3 ms 7768 KB Output is correct
25 Correct 3 ms 7772 KB Output is correct
26 Correct 4 ms 7772 KB Output is correct
27 Correct 5 ms 7772 KB Output is correct
28 Correct 3 ms 7768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7512 KB Output is correct
2 Correct 3 ms 7512 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 7516 KB Output is correct
6 Correct 3 ms 7516 KB Output is correct
7 Correct 3 ms 7516 KB Output is correct
8 Correct 3 ms 7516 KB Output is correct
9 Correct 5 ms 7768 KB Output is correct
10 Correct 58 ms 25784 KB Output is correct
11 Correct 95 ms 29616 KB Output is correct
12 Correct 74 ms 29356 KB Output is correct
13 Correct 83 ms 30548 KB Output is correct
14 Correct 60 ms 33000 KB Output is correct
15 Correct 4 ms 7516 KB Output is correct
16 Correct 4 ms 7516 KB Output is correct
17 Correct 4 ms 7532 KB Output is correct
18 Correct 4 ms 7516 KB Output is correct
19 Correct 3 ms 7512 KB Output is correct
20 Correct 3 ms 7516 KB Output is correct
21 Correct 3 ms 7516 KB Output is correct
22 Correct 3 ms 7676 KB Output is correct
23 Correct 3 ms 7516 KB Output is correct
24 Correct 3 ms 7516 KB Output is correct
25 Correct 3 ms 7760 KB Output is correct
26 Correct 3 ms 7772 KB Output is correct
27 Correct 3 ms 7772 KB Output is correct
28 Correct 3 ms 7624 KB Output is correct
29 Correct 3 ms 7768 KB Output is correct
30 Correct 3 ms 7772 KB Output is correct
31 Correct 4 ms 7772 KB Output is correct
32 Correct 5 ms 7772 KB Output is correct
33 Correct 3 ms 7768 KB Output is correct
34 Correct 10 ms 10332 KB Output is correct
35 Correct 66 ms 30764 KB Output is correct
36 Correct 63 ms 30544 KB Output is correct
37 Correct 68 ms 30548 KB Output is correct
38 Correct 71 ms 30288 KB Output is correct
39 Correct 75 ms 30160 KB Output is correct
40 Correct 62 ms 28240 KB Output is correct
41 Correct 69 ms 30684 KB Output is correct
42 Correct 69 ms 30548 KB Output is correct
43 Correct 66 ms 33616 KB Output is correct
44 Correct 68 ms 30684 KB Output is correct
45 Correct 88 ms 40272 KB Output is correct
46 Correct 75 ms 30764 KB Output is correct
47 Correct 76 ms 30892 KB Output is correct
48 Correct 77 ms 33876 KB Output is correct
49 Correct 72 ms 38480 KB Output is correct
50 Correct 57 ms 31184 KB Output is correct
51 Correct 78 ms 35920 KB Output is correct
52 Correct 111 ms 42832 KB Output is correct
53 Correct 116 ms 46192 KB Output is correct
54 Correct 122 ms 51328 KB Output is correct
55 Correct 58 ms 33360 KB Output is correct
56 Correct 112 ms 47188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7512 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 7516 KB Output is correct
6 Correct 3 ms 7516 KB Output is correct
7 Correct 3 ms 7516 KB Output is correct
8 Correct 5 ms 7768 KB Output is correct
9 Correct 58 ms 25784 KB Output is correct
10 Correct 95 ms 29616 KB Output is correct
11 Correct 74 ms 29356 KB Output is correct
12 Correct 83 ms 30548 KB Output is correct
13 Correct 60 ms 33000 KB Output is correct
14 Correct 58 ms 25832 KB Output is correct
15 Correct 197 ms 31708 KB Output is correct
16 Correct 202 ms 30972 KB Output is correct
17 Correct 198 ms 32324 KB Output is correct
18 Correct 264 ms 34888 KB Output is correct
19 Correct 215 ms 36908 KB Output is correct
20 Correct 225 ms 38072 KB Output is correct
21 Correct 232 ms 37556 KB Output is correct
22 Correct 200 ms 38068 KB Output is correct
23 Correct 218 ms 37836 KB Output is correct
24 Correct 213 ms 36696 KB Output is correct
25 Correct 214 ms 37632 KB Output is correct
26 Correct 214 ms 36484 KB Output is correct
27 Correct 4 ms 7516 KB Output is correct
28 Correct 4 ms 7516 KB Output is correct
29 Correct 4 ms 7532 KB Output is correct
30 Correct 4 ms 7516 KB Output is correct
31 Correct 3 ms 7512 KB Output is correct
32 Correct 3 ms 7516 KB Output is correct
33 Correct 3 ms 7516 KB Output is correct
34 Correct 3 ms 7676 KB Output is correct
35 Correct 3 ms 7516 KB Output is correct
36 Correct 10 ms 10332 KB Output is correct
37 Correct 66 ms 30764 KB Output is correct
38 Correct 63 ms 30544 KB Output is correct
39 Correct 68 ms 30548 KB Output is correct
40 Correct 71 ms 30288 KB Output is correct
41 Correct 75 ms 30160 KB Output is correct
42 Correct 62 ms 28240 KB Output is correct
43 Correct 69 ms 30684 KB Output is correct
44 Correct 69 ms 30548 KB Output is correct
45 Correct 66 ms 33616 KB Output is correct
46 Correct 68 ms 30684 KB Output is correct
47 Correct 17 ms 10332 KB Output is correct
48 Correct 180 ms 33132 KB Output is correct
49 Correct 181 ms 33208 KB Output is correct
50 Correct 173 ms 33204 KB Output is correct
51 Correct 165 ms 33132 KB Output is correct
52 Correct 154 ms 31744 KB Output is correct
53 Correct 146 ms 26688 KB Output is correct
54 Correct 184 ms 33816 KB Output is correct
55 Correct 192 ms 33120 KB Output is correct
56 Correct 251 ms 35560 KB Output is correct
57 Correct 166 ms 33972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7512 KB Output is correct
2 Correct 3 ms 7512 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 7516 KB Output is correct
6 Correct 3 ms 7516 KB Output is correct
7 Correct 3 ms 7516 KB Output is correct
8 Correct 3 ms 7516 KB Output is correct
9 Correct 5 ms 7768 KB Output is correct
10 Correct 58 ms 25784 KB Output is correct
11 Correct 95 ms 29616 KB Output is correct
12 Correct 74 ms 29356 KB Output is correct
13 Correct 83 ms 30548 KB Output is correct
14 Correct 60 ms 33000 KB Output is correct
15 Correct 58 ms 25832 KB Output is correct
16 Correct 197 ms 31708 KB Output is correct
17 Correct 202 ms 30972 KB Output is correct
18 Correct 198 ms 32324 KB Output is correct
19 Correct 264 ms 34888 KB Output is correct
20 Correct 215 ms 36908 KB Output is correct
21 Correct 225 ms 38072 KB Output is correct
22 Correct 232 ms 37556 KB Output is correct
23 Correct 200 ms 38068 KB Output is correct
24 Correct 218 ms 37836 KB Output is correct
25 Correct 213 ms 36696 KB Output is correct
26 Correct 214 ms 37632 KB Output is correct
27 Correct 214 ms 36484 KB Output is correct
28 Correct 4 ms 7516 KB Output is correct
29 Correct 4 ms 7516 KB Output is correct
30 Correct 4 ms 7532 KB Output is correct
31 Correct 4 ms 7516 KB Output is correct
32 Correct 3 ms 7512 KB Output is correct
33 Correct 3 ms 7516 KB Output is correct
34 Correct 3 ms 7516 KB Output is correct
35 Correct 3 ms 7676 KB Output is correct
36 Correct 3 ms 7516 KB Output is correct
37 Correct 10 ms 10332 KB Output is correct
38 Correct 66 ms 30764 KB Output is correct
39 Correct 63 ms 30544 KB Output is correct
40 Correct 68 ms 30548 KB Output is correct
41 Correct 71 ms 30288 KB Output is correct
42 Correct 75 ms 30160 KB Output is correct
43 Correct 62 ms 28240 KB Output is correct
44 Correct 69 ms 30684 KB Output is correct
45 Correct 69 ms 30548 KB Output is correct
46 Correct 66 ms 33616 KB Output is correct
47 Correct 68 ms 30684 KB Output is correct
48 Correct 17 ms 10332 KB Output is correct
49 Correct 180 ms 33132 KB Output is correct
50 Correct 181 ms 33208 KB Output is correct
51 Correct 173 ms 33204 KB Output is correct
52 Correct 165 ms 33132 KB Output is correct
53 Correct 154 ms 31744 KB Output is correct
54 Correct 146 ms 26688 KB Output is correct
55 Correct 184 ms 33816 KB Output is correct
56 Correct 192 ms 33120 KB Output is correct
57 Correct 251 ms 35560 KB Output is correct
58 Correct 166 ms 33972 KB Output is correct
59 Correct 81 ms 14236 KB Output is correct
60 Correct 187 ms 32972 KB Output is correct
61 Correct 185 ms 32268 KB Output is correct
62 Correct 187 ms 33724 KB Output is correct
63 Correct 256 ms 36120 KB Output is correct
64 Correct 3 ms 7516 KB Output is correct
65 Correct 3 ms 7760 KB Output is correct
66 Correct 3 ms 7772 KB Output is correct
67 Correct 3 ms 7772 KB Output is correct
68 Correct 3 ms 7624 KB Output is correct
69 Correct 3 ms 7768 KB Output is correct
70 Correct 3 ms 7772 KB Output is correct
71 Correct 4 ms 7772 KB Output is correct
72 Correct 5 ms 7772 KB Output is correct
73 Correct 3 ms 7768 KB Output is correct
74 Correct 88 ms 40272 KB Output is correct
75 Correct 75 ms 30764 KB Output is correct
76 Correct 76 ms 30892 KB Output is correct
77 Correct 77 ms 33876 KB Output is correct
78 Correct 72 ms 38480 KB Output is correct
79 Correct 57 ms 31184 KB Output is correct
80 Correct 78 ms 35920 KB Output is correct
81 Correct 111 ms 42832 KB Output is correct
82 Correct 116 ms 46192 KB Output is correct
83 Correct 122 ms 51328 KB Output is correct
84 Correct 58 ms 33360 KB Output is correct
85 Correct 112 ms 47188 KB Output is correct
86 Correct 62 ms 19036 KB Output is correct
87 Correct 173 ms 33208 KB Output is correct
88 Correct 165 ms 33228 KB Output is correct
89 Correct 224 ms 35180 KB Output is correct
90 Correct 162 ms 42584 KB Output is correct
91 Correct 181 ms 39748 KB Output is correct
92 Correct 228 ms 37548 KB Output is correct
93 Correct 209 ms 44612 KB Output is correct
94 Correct 273 ms 48436 KB Output is correct
95 Correct 258 ms 52860 KB Output is correct
96 Correct 237 ms 36024 KB Output is correct
97 Correct 257 ms 41748 KB Output is correct