Submission #1093167

# Submission time Handle Problem Language Result Execution time Memory
1093167 2024-09-26T06:45:58 Z vjudge1 Swapping Cities (APIO20_swap) C++17
100 / 100
208 ms 72128 KB
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 1e6 + 2, LG = 20, inf = 1e18;
int n, m, pu, pv, u[maxn], v[maxn], w[maxn], d[maxn], par[maxn], id[maxn], deg[maxn], st[LG][maxn], res[maxn];
bool good[maxn];
vector<int> adj[maxn];
bool cmp (int x, int y)
{
    return w[x] < w[y];
}
int find_set (int u)
{
    if (par[u] < 0) return u;
    return (par[u] = find_set(par[u]));
}
void union_set (int i)
{
    deg[u[id[i]]]++; deg[v[id[i]]]++;
    pu = find_set(u[id[i]]); pv = find_set(v[id[i]]);
    if (pu == pv)
    {
        par[n + i] += par[pu];
        par[pu] = n + i;
        adj[n + i].push_back(pu);
        good[n + i] = true;
    }
    else
    {
        par[n + i] += par[pu];
        par[n + i] += par[pv];
        par[pu] = par[pv] = n + i;
        adj[n + i].push_back(pu);
        adj[n + i].push_back(pv);
        if (deg[u[id[i]]] > 2 || deg[v[id[i]]] > 2 || good[pu] || good[pv]) good[n + i] = true;
    }
}
void dfs (int u, int p)
{
    st[0][u] = p;
    if (good[u]) res[u] = w[id[u - n]];
    else res[u] = res[p];
    for (int v : adj[u])
    {
        d[v] = d[u] + 1;
        dfs(v, u);
    }
}
int lca (int u, int v)
{
    if (d[u] < d[v]) swap(u, v);
    int diff = d[u] - d[v];
    for (int j = LG - 1; j >= 0; j--)
        if ((diff >> j) & 1)
        {
            diff ^= (1 << j);
            u = st[j][u];
        }
    if (u == v) return u;
    for (int j = LG - 1; j >= 0; j--)
        if (st[j][u] != st[j][v])
        {
            u = st[j][u];
            v = st[j][v];
        }
    return st[0][u];
}
void init (int N, int M, vector<int> U, vector<int> V, vector<int> W)
{
    n = N; m = M;
    for (int i = 1; i <= n; i++) par[i] = -1;
    for (int i = m; i >= 1; i--)
    {
        u[i] = U[i - 1] + 1;
        v[i] = V[i - 1] + 1;
        w[i] = W[i - 1];
        id[i] = i;
    }
    sort(id + 1, id + m + 1, cmp);
    for (int i = 1; i <= m; i++) union_set(i);
    res[0] = -1;
    dfs(n + m, 0);
    for (int j = 1; j < LG; j++)
        for (int i = 1; i <= n + m; i++)
            st[j][i] = st[j - 1][st[j - 1][i]];
}
int getMinimumFuelCapacity (int X, int Y)
{
    X++; Y++;
    return res[lca(X, Y)];
}
#ifdef rtgsp
int main() {
    //freopen(".inp", "r", stdin);
    //freopen(".out", "w", stdout);
    int N, M, Q;
	cin >> N >> M >> Q;
	vector<int> U(M), V(M), W(M);
	for (int i = 0; i < M; i++) cin >> U[i];
	for (int i = 0; i < M; i++) cin >> V[i];
	for (int i = 0; i < M; i++) cin >> W[i];
	init(N, M, U, V, W);
    while (Q--)
    {
        int X, Y;
        cin >> X >> Y;
        cout << getMinimumFuelCapacity(X, Y) << '\n';
    }
}
#endif
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23900 KB Output is correct
2 Correct 11 ms 23900 KB Output is correct
3 Correct 12 ms 23900 KB Output is correct
4 Correct 14 ms 24156 KB Output is correct
5 Correct 13 ms 24156 KB Output is correct
6 Correct 11 ms 24156 KB Output is correct
7 Correct 12 ms 24368 KB Output is correct
8 Correct 14 ms 24412 KB Output is correct
9 Correct 57 ms 43860 KB Output is correct
10 Correct 68 ms 48336 KB Output is correct
11 Correct 64 ms 47952 KB Output is correct
12 Correct 69 ms 49488 KB Output is correct
13 Correct 64 ms 51792 KB Output is correct
14 Correct 65 ms 44200 KB Output is correct
15 Correct 166 ms 50564 KB Output is correct
16 Correct 132 ms 49664 KB Output is correct
17 Correct 136 ms 51124 KB Output is correct
18 Correct 174 ms 53688 KB Output is correct
19 Correct 67 ms 31572 KB Output is correct
20 Correct 152 ms 51764 KB Output is correct
21 Correct 127 ms 50960 KB Output is correct
22 Correct 137 ms 52604 KB Output is correct
23 Correct 174 ms 55020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23900 KB Output is correct
2 Correct 11 ms 23900 KB Output is correct
3 Correct 144 ms 55552 KB Output is correct
4 Correct 145 ms 57016 KB Output is correct
5 Correct 151 ms 56180 KB Output is correct
6 Correct 166 ms 56964 KB Output is correct
7 Correct 152 ms 56788 KB Output is correct
8 Correct 171 ms 55376 KB Output is correct
9 Correct 161 ms 56372 KB Output is correct
10 Correct 156 ms 55052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23900 KB Output is correct
2 Correct 11 ms 23900 KB Output is correct
3 Correct 12 ms 23900 KB Output is correct
4 Correct 14 ms 24156 KB Output is correct
5 Correct 13 ms 24156 KB Output is correct
6 Correct 11 ms 24156 KB Output is correct
7 Correct 12 ms 24368 KB Output is correct
8 Correct 14 ms 24412 KB Output is correct
9 Correct 11 ms 24096 KB Output is correct
10 Correct 11 ms 24412 KB Output is correct
11 Correct 11 ms 24152 KB Output is correct
12 Correct 14 ms 24152 KB Output is correct
13 Correct 10 ms 24156 KB Output is correct
14 Correct 11 ms 24196 KB Output is correct
15 Correct 12 ms 24412 KB Output is correct
16 Correct 11 ms 24208 KB Output is correct
17 Correct 11 ms 24412 KB Output is correct
18 Correct 11 ms 24408 KB Output is correct
19 Correct 11 ms 24156 KB Output is correct
20 Correct 12 ms 24156 KB Output is correct
21 Correct 11 ms 24384 KB Output is correct
22 Correct 11 ms 24412 KB Output is correct
23 Correct 12 ms 24156 KB Output is correct
24 Correct 11 ms 24412 KB Output is correct
25 Correct 11 ms 24412 KB Output is correct
26 Correct 11 ms 24412 KB Output is correct
27 Correct 12 ms 24412 KB Output is correct
28 Correct 12 ms 24412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 24096 KB Output is correct
2 Correct 11 ms 23900 KB Output is correct
3 Correct 11 ms 23900 KB Output is correct
4 Correct 12 ms 23900 KB Output is correct
5 Correct 14 ms 24156 KB Output is correct
6 Correct 13 ms 24156 KB Output is correct
7 Correct 11 ms 24156 KB Output is correct
8 Correct 12 ms 24368 KB Output is correct
9 Correct 14 ms 24412 KB Output is correct
10 Correct 57 ms 43860 KB Output is correct
11 Correct 68 ms 48336 KB Output is correct
12 Correct 64 ms 47952 KB Output is correct
13 Correct 69 ms 49488 KB Output is correct
14 Correct 64 ms 51792 KB Output is correct
15 Correct 11 ms 24412 KB Output is correct
16 Correct 11 ms 24152 KB Output is correct
17 Correct 14 ms 24152 KB Output is correct
18 Correct 10 ms 24156 KB Output is correct
19 Correct 11 ms 24196 KB Output is correct
20 Correct 12 ms 24412 KB Output is correct
21 Correct 11 ms 24208 KB Output is correct
22 Correct 11 ms 24412 KB Output is correct
23 Correct 11 ms 24408 KB Output is correct
24 Correct 11 ms 24156 KB Output is correct
25 Correct 12 ms 24156 KB Output is correct
26 Correct 11 ms 24384 KB Output is correct
27 Correct 11 ms 24412 KB Output is correct
28 Correct 12 ms 24156 KB Output is correct
29 Correct 11 ms 24412 KB Output is correct
30 Correct 11 ms 24412 KB Output is correct
31 Correct 11 ms 24412 KB Output is correct
32 Correct 12 ms 24412 KB Output is correct
33 Correct 12 ms 24412 KB Output is correct
34 Correct 18 ms 27484 KB Output is correct
35 Correct 69 ms 49420 KB Output is correct
36 Correct 73 ms 49428 KB Output is correct
37 Correct 74 ms 49488 KB Output is correct
38 Correct 70 ms 49160 KB Output is correct
39 Correct 74 ms 49184 KB Output is correct
40 Correct 63 ms 46928 KB Output is correct
41 Correct 78 ms 49496 KB Output is correct
42 Correct 67 ms 49428 KB Output is correct
43 Correct 64 ms 52752 KB Output is correct
44 Correct 74 ms 49492 KB Output is correct
45 Correct 103 ms 59476 KB Output is correct
46 Correct 68 ms 49488 KB Output is correct
47 Correct 73 ms 49672 KB Output is correct
48 Correct 79 ms 52816 KB Output is correct
49 Correct 87 ms 57032 KB Output is correct
50 Correct 70 ms 49492 KB Output is correct
51 Correct 93 ms 54632 KB Output is correct
52 Correct 113 ms 62304 KB Output is correct
53 Correct 123 ms 64852 KB Output is correct
54 Correct 141 ms 70224 KB Output is correct
55 Correct 64 ms 52496 KB Output is correct
56 Correct 121 ms 65872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23900 KB Output is correct
2 Correct 11 ms 23900 KB Output is correct
3 Correct 12 ms 23900 KB Output is correct
4 Correct 14 ms 24156 KB Output is correct
5 Correct 13 ms 24156 KB Output is correct
6 Correct 11 ms 24156 KB Output is correct
7 Correct 12 ms 24368 KB Output is correct
8 Correct 14 ms 24412 KB Output is correct
9 Correct 57 ms 43860 KB Output is correct
10 Correct 68 ms 48336 KB Output is correct
11 Correct 64 ms 47952 KB Output is correct
12 Correct 69 ms 49488 KB Output is correct
13 Correct 64 ms 51792 KB Output is correct
14 Correct 65 ms 44200 KB Output is correct
15 Correct 166 ms 50564 KB Output is correct
16 Correct 132 ms 49664 KB Output is correct
17 Correct 136 ms 51124 KB Output is correct
18 Correct 174 ms 53688 KB Output is correct
19 Correct 144 ms 55552 KB Output is correct
20 Correct 145 ms 57016 KB Output is correct
21 Correct 151 ms 56180 KB Output is correct
22 Correct 166 ms 56964 KB Output is correct
23 Correct 152 ms 56788 KB Output is correct
24 Correct 171 ms 55376 KB Output is correct
25 Correct 161 ms 56372 KB Output is correct
26 Correct 156 ms 55052 KB Output is correct
27 Correct 11 ms 24412 KB Output is correct
28 Correct 11 ms 24152 KB Output is correct
29 Correct 14 ms 24152 KB Output is correct
30 Correct 10 ms 24156 KB Output is correct
31 Correct 11 ms 24196 KB Output is correct
32 Correct 12 ms 24412 KB Output is correct
33 Correct 11 ms 24208 KB Output is correct
34 Correct 11 ms 24412 KB Output is correct
35 Correct 11 ms 24408 KB Output is correct
36 Correct 18 ms 27484 KB Output is correct
37 Correct 69 ms 49420 KB Output is correct
38 Correct 73 ms 49428 KB Output is correct
39 Correct 74 ms 49488 KB Output is correct
40 Correct 70 ms 49160 KB Output is correct
41 Correct 74 ms 49184 KB Output is correct
42 Correct 63 ms 46928 KB Output is correct
43 Correct 78 ms 49496 KB Output is correct
44 Correct 67 ms 49428 KB Output is correct
45 Correct 64 ms 52752 KB Output is correct
46 Correct 74 ms 49492 KB Output is correct
47 Correct 23 ms 27484 KB Output is correct
48 Correct 151 ms 52120 KB Output is correct
49 Correct 136 ms 52092 KB Output is correct
50 Correct 132 ms 52148 KB Output is correct
51 Correct 140 ms 51908 KB Output is correct
52 Correct 150 ms 50432 KB Output is correct
53 Correct 128 ms 44608 KB Output is correct
54 Correct 160 ms 52660 KB Output is correct
55 Correct 143 ms 52152 KB Output is correct
56 Correct 186 ms 54712 KB Output is correct
57 Correct 149 ms 52916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 24096 KB Output is correct
2 Correct 11 ms 23900 KB Output is correct
3 Correct 11 ms 23900 KB Output is correct
4 Correct 12 ms 23900 KB Output is correct
5 Correct 14 ms 24156 KB Output is correct
6 Correct 13 ms 24156 KB Output is correct
7 Correct 11 ms 24156 KB Output is correct
8 Correct 12 ms 24368 KB Output is correct
9 Correct 14 ms 24412 KB Output is correct
10 Correct 57 ms 43860 KB Output is correct
11 Correct 68 ms 48336 KB Output is correct
12 Correct 64 ms 47952 KB Output is correct
13 Correct 69 ms 49488 KB Output is correct
14 Correct 64 ms 51792 KB Output is correct
15 Correct 65 ms 44200 KB Output is correct
16 Correct 166 ms 50564 KB Output is correct
17 Correct 132 ms 49664 KB Output is correct
18 Correct 136 ms 51124 KB Output is correct
19 Correct 174 ms 53688 KB Output is correct
20 Correct 144 ms 55552 KB Output is correct
21 Correct 145 ms 57016 KB Output is correct
22 Correct 151 ms 56180 KB Output is correct
23 Correct 166 ms 56964 KB Output is correct
24 Correct 152 ms 56788 KB Output is correct
25 Correct 171 ms 55376 KB Output is correct
26 Correct 161 ms 56372 KB Output is correct
27 Correct 156 ms 55052 KB Output is correct
28 Correct 11 ms 24412 KB Output is correct
29 Correct 11 ms 24152 KB Output is correct
30 Correct 14 ms 24152 KB Output is correct
31 Correct 10 ms 24156 KB Output is correct
32 Correct 11 ms 24196 KB Output is correct
33 Correct 12 ms 24412 KB Output is correct
34 Correct 11 ms 24208 KB Output is correct
35 Correct 11 ms 24412 KB Output is correct
36 Correct 11 ms 24408 KB Output is correct
37 Correct 18 ms 27484 KB Output is correct
38 Correct 69 ms 49420 KB Output is correct
39 Correct 73 ms 49428 KB Output is correct
40 Correct 74 ms 49488 KB Output is correct
41 Correct 70 ms 49160 KB Output is correct
42 Correct 74 ms 49184 KB Output is correct
43 Correct 63 ms 46928 KB Output is correct
44 Correct 78 ms 49496 KB Output is correct
45 Correct 67 ms 49428 KB Output is correct
46 Correct 64 ms 52752 KB Output is correct
47 Correct 74 ms 49492 KB Output is correct
48 Correct 23 ms 27484 KB Output is correct
49 Correct 151 ms 52120 KB Output is correct
50 Correct 136 ms 52092 KB Output is correct
51 Correct 132 ms 52148 KB Output is correct
52 Correct 140 ms 51908 KB Output is correct
53 Correct 150 ms 50432 KB Output is correct
54 Correct 128 ms 44608 KB Output is correct
55 Correct 160 ms 52660 KB Output is correct
56 Correct 143 ms 52152 KB Output is correct
57 Correct 186 ms 54712 KB Output is correct
58 Correct 149 ms 52916 KB Output is correct
59 Correct 67 ms 31572 KB Output is correct
60 Correct 152 ms 51764 KB Output is correct
61 Correct 127 ms 50960 KB Output is correct
62 Correct 137 ms 52604 KB Output is correct
63 Correct 174 ms 55020 KB Output is correct
64 Correct 11 ms 24156 KB Output is correct
65 Correct 12 ms 24156 KB Output is correct
66 Correct 11 ms 24384 KB Output is correct
67 Correct 11 ms 24412 KB Output is correct
68 Correct 12 ms 24156 KB Output is correct
69 Correct 11 ms 24412 KB Output is correct
70 Correct 11 ms 24412 KB Output is correct
71 Correct 11 ms 24412 KB Output is correct
72 Correct 12 ms 24412 KB Output is correct
73 Correct 12 ms 24412 KB Output is correct
74 Correct 103 ms 59476 KB Output is correct
75 Correct 68 ms 49488 KB Output is correct
76 Correct 73 ms 49672 KB Output is correct
77 Correct 79 ms 52816 KB Output is correct
78 Correct 87 ms 57032 KB Output is correct
79 Correct 70 ms 49492 KB Output is correct
80 Correct 93 ms 54632 KB Output is correct
81 Correct 113 ms 62304 KB Output is correct
82 Correct 123 ms 64852 KB Output is correct
83 Correct 141 ms 70224 KB Output is correct
84 Correct 64 ms 52496 KB Output is correct
85 Correct 121 ms 65872 KB Output is correct
86 Correct 60 ms 36872 KB Output is correct
87 Correct 134 ms 52184 KB Output is correct
88 Correct 132 ms 52156 KB Output is correct
89 Correct 164 ms 54128 KB Output is correct
90 Correct 144 ms 61104 KB Output is correct
91 Correct 151 ms 58228 KB Output is correct
92 Correct 173 ms 56440 KB Output is correct
93 Correct 208 ms 64140 KB Output is correct
94 Correct 202 ms 67216 KB Output is correct
95 Correct 198 ms 72128 KB Output is correct
96 Correct 172 ms 54716 KB Output is correct
97 Correct 202 ms 61084 KB Output is correct