#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 |
12 ms |
23896 KB |
Output is correct |
2 |
Correct |
11 ms |
24156 KB |
Output is correct |
3 |
Correct |
11 ms |
23900 KB |
Output is correct |
4 |
Correct |
13 ms |
24260 KB |
Output is correct |
5 |
Correct |
10 ms |
24152 KB |
Output is correct |
6 |
Correct |
11 ms |
24196 KB |
Output is correct |
7 |
Correct |
11 ms |
24412 KB |
Output is correct |
8 |
Correct |
12 ms |
24416 KB |
Output is correct |
9 |
Correct |
59 ms |
45660 KB |
Output is correct |
10 |
Correct |
69 ms |
50528 KB |
Output is correct |
11 |
Correct |
84 ms |
50012 KB |
Output is correct |
12 |
Correct |
73 ms |
51704 KB |
Output is correct |
13 |
Correct |
65 ms |
53948 KB |
Output is correct |
14 |
Correct |
70 ms |
45920 KB |
Output is correct |
15 |
Correct |
148 ms |
54760 KB |
Output is correct |
16 |
Correct |
139 ms |
54028 KB |
Output is correct |
17 |
Correct |
151 ms |
55748 KB |
Output is correct |
18 |
Correct |
176 ms |
57968 KB |
Output is correct |
19 |
Correct |
65 ms |
33368 KB |
Output is correct |
20 |
Correct |
132 ms |
56012 KB |
Output is correct |
21 |
Correct |
132 ms |
55176 KB |
Output is correct |
22 |
Correct |
189 ms |
56960 KB |
Output is correct |
23 |
Correct |
181 ms |
59320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23896 KB |
Output is correct |
2 |
Correct |
11 ms |
24156 KB |
Output is correct |
3 |
Correct |
152 ms |
59472 KB |
Output is correct |
4 |
Correct |
155 ms |
60852 KB |
Output is correct |
5 |
Correct |
160 ms |
60136 KB |
Output is correct |
6 |
Correct |
159 ms |
60600 KB |
Output is correct |
7 |
Correct |
162 ms |
60744 KB |
Output is correct |
8 |
Correct |
161 ms |
59396 KB |
Output is correct |
9 |
Correct |
167 ms |
60192 KB |
Output is correct |
10 |
Correct |
155 ms |
58896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23896 KB |
Output is correct |
2 |
Correct |
11 ms |
24156 KB |
Output is correct |
3 |
Correct |
11 ms |
23900 KB |
Output is correct |
4 |
Correct |
13 ms |
24260 KB |
Output is correct |
5 |
Correct |
10 ms |
24152 KB |
Output is correct |
6 |
Correct |
11 ms |
24196 KB |
Output is correct |
7 |
Correct |
11 ms |
24412 KB |
Output is correct |
8 |
Correct |
12 ms |
24416 KB |
Output is correct |
9 |
Correct |
11 ms |
24156 KB |
Output is correct |
10 |
Correct |
11 ms |
24348 KB |
Output is correct |
11 |
Correct |
13 ms |
24204 KB |
Output is correct |
12 |
Correct |
12 ms |
24196 KB |
Output is correct |
13 |
Correct |
11 ms |
24156 KB |
Output is correct |
14 |
Correct |
11 ms |
24152 KB |
Output is correct |
15 |
Correct |
10 ms |
24284 KB |
Output is correct |
16 |
Correct |
12 ms |
24412 KB |
Output is correct |
17 |
Correct |
12 ms |
24408 KB |
Output is correct |
18 |
Correct |
12 ms |
24412 KB |
Output is correct |
19 |
Correct |
11 ms |
24180 KB |
Output is correct |
20 |
Correct |
10 ms |
24228 KB |
Output is correct |
21 |
Correct |
10 ms |
24412 KB |
Output is correct |
22 |
Correct |
14 ms |
24324 KB |
Output is correct |
23 |
Correct |
11 ms |
24152 KB |
Output is correct |
24 |
Correct |
11 ms |
24440 KB |
Output is correct |
25 |
Correct |
11 ms |
24412 KB |
Output is correct |
26 |
Correct |
17 ms |
24412 KB |
Output is correct |
27 |
Correct |
11 ms |
24464 KB |
Output is correct |
28 |
Correct |
12 ms |
24508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
24156 KB |
Output is correct |
2 |
Correct |
12 ms |
23896 KB |
Output is correct |
3 |
Correct |
11 ms |
24156 KB |
Output is correct |
4 |
Correct |
11 ms |
23900 KB |
Output is correct |
5 |
Correct |
13 ms |
24260 KB |
Output is correct |
6 |
Correct |
10 ms |
24152 KB |
Output is correct |
7 |
Correct |
11 ms |
24196 KB |
Output is correct |
8 |
Correct |
11 ms |
24412 KB |
Output is correct |
9 |
Correct |
12 ms |
24416 KB |
Output is correct |
10 |
Correct |
59 ms |
45660 KB |
Output is correct |
11 |
Correct |
69 ms |
50528 KB |
Output is correct |
12 |
Correct |
84 ms |
50012 KB |
Output is correct |
13 |
Correct |
73 ms |
51704 KB |
Output is correct |
14 |
Correct |
65 ms |
53948 KB |
Output is correct |
15 |
Correct |
11 ms |
24348 KB |
Output is correct |
16 |
Correct |
13 ms |
24204 KB |
Output is correct |
17 |
Correct |
12 ms |
24196 KB |
Output is correct |
18 |
Correct |
11 ms |
24156 KB |
Output is correct |
19 |
Correct |
11 ms |
24152 KB |
Output is correct |
20 |
Correct |
10 ms |
24284 KB |
Output is correct |
21 |
Correct |
12 ms |
24412 KB |
Output is correct |
22 |
Correct |
12 ms |
24408 KB |
Output is correct |
23 |
Correct |
12 ms |
24412 KB |
Output is correct |
24 |
Correct |
11 ms |
24180 KB |
Output is correct |
25 |
Correct |
10 ms |
24228 KB |
Output is correct |
26 |
Correct |
10 ms |
24412 KB |
Output is correct |
27 |
Correct |
14 ms |
24324 KB |
Output is correct |
28 |
Correct |
11 ms |
24152 KB |
Output is correct |
29 |
Correct |
11 ms |
24440 KB |
Output is correct |
30 |
Correct |
11 ms |
24412 KB |
Output is correct |
31 |
Correct |
17 ms |
24412 KB |
Output is correct |
32 |
Correct |
11 ms |
24464 KB |
Output is correct |
33 |
Correct |
12 ms |
24508 KB |
Output is correct |
34 |
Correct |
21 ms |
27884 KB |
Output is correct |
35 |
Correct |
68 ms |
51296 KB |
Output is correct |
36 |
Correct |
74 ms |
51296 KB |
Output is correct |
37 |
Correct |
71 ms |
51296 KB |
Output is correct |
38 |
Correct |
70 ms |
50912 KB |
Output is correct |
39 |
Correct |
70 ms |
50788 KB |
Output is correct |
40 |
Correct |
63 ms |
48740 KB |
Output is correct |
41 |
Correct |
74 ms |
51664 KB |
Output is correct |
42 |
Correct |
70 ms |
51364 KB |
Output is correct |
43 |
Correct |
66 ms |
54368 KB |
Output is correct |
44 |
Correct |
74 ms |
51808 KB |
Output is correct |
45 |
Correct |
106 ms |
62560 KB |
Output is correct |
46 |
Correct |
73 ms |
51292 KB |
Output is correct |
47 |
Correct |
70 ms |
51552 KB |
Output is correct |
48 |
Correct |
83 ms |
54880 KB |
Output is correct |
49 |
Correct |
92 ms |
59744 KB |
Output is correct |
50 |
Correct |
70 ms |
52064 KB |
Output is correct |
51 |
Correct |
89 ms |
57684 KB |
Output is correct |
52 |
Correct |
114 ms |
65620 KB |
Output is correct |
53 |
Correct |
128 ms |
68664 KB |
Output is correct |
54 |
Correct |
139 ms |
74280 KB |
Output is correct |
55 |
Correct |
70 ms |
54096 KB |
Output is correct |
56 |
Correct |
129 ms |
69456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23896 KB |
Output is correct |
2 |
Correct |
11 ms |
24156 KB |
Output is correct |
3 |
Correct |
11 ms |
23900 KB |
Output is correct |
4 |
Correct |
13 ms |
24260 KB |
Output is correct |
5 |
Correct |
10 ms |
24152 KB |
Output is correct |
6 |
Correct |
11 ms |
24196 KB |
Output is correct |
7 |
Correct |
11 ms |
24412 KB |
Output is correct |
8 |
Correct |
12 ms |
24416 KB |
Output is correct |
9 |
Correct |
59 ms |
45660 KB |
Output is correct |
10 |
Correct |
69 ms |
50528 KB |
Output is correct |
11 |
Correct |
84 ms |
50012 KB |
Output is correct |
12 |
Correct |
73 ms |
51704 KB |
Output is correct |
13 |
Correct |
65 ms |
53948 KB |
Output is correct |
14 |
Correct |
70 ms |
45920 KB |
Output is correct |
15 |
Correct |
148 ms |
54760 KB |
Output is correct |
16 |
Correct |
139 ms |
54028 KB |
Output is correct |
17 |
Correct |
151 ms |
55748 KB |
Output is correct |
18 |
Correct |
176 ms |
57968 KB |
Output is correct |
19 |
Correct |
152 ms |
59472 KB |
Output is correct |
20 |
Correct |
155 ms |
60852 KB |
Output is correct |
21 |
Correct |
160 ms |
60136 KB |
Output is correct |
22 |
Correct |
159 ms |
60600 KB |
Output is correct |
23 |
Correct |
162 ms |
60744 KB |
Output is correct |
24 |
Correct |
161 ms |
59396 KB |
Output is correct |
25 |
Correct |
167 ms |
60192 KB |
Output is correct |
26 |
Correct |
155 ms |
58896 KB |
Output is correct |
27 |
Correct |
11 ms |
24348 KB |
Output is correct |
28 |
Correct |
13 ms |
24204 KB |
Output is correct |
29 |
Correct |
12 ms |
24196 KB |
Output is correct |
30 |
Correct |
11 ms |
24156 KB |
Output is correct |
31 |
Correct |
11 ms |
24152 KB |
Output is correct |
32 |
Correct |
10 ms |
24284 KB |
Output is correct |
33 |
Correct |
12 ms |
24412 KB |
Output is correct |
34 |
Correct |
12 ms |
24408 KB |
Output is correct |
35 |
Correct |
12 ms |
24412 KB |
Output is correct |
36 |
Correct |
21 ms |
27884 KB |
Output is correct |
37 |
Correct |
68 ms |
51296 KB |
Output is correct |
38 |
Correct |
74 ms |
51296 KB |
Output is correct |
39 |
Correct |
71 ms |
51296 KB |
Output is correct |
40 |
Correct |
70 ms |
50912 KB |
Output is correct |
41 |
Correct |
70 ms |
50788 KB |
Output is correct |
42 |
Correct |
63 ms |
48740 KB |
Output is correct |
43 |
Correct |
74 ms |
51664 KB |
Output is correct |
44 |
Correct |
70 ms |
51364 KB |
Output is correct |
45 |
Correct |
66 ms |
54368 KB |
Output is correct |
46 |
Correct |
74 ms |
51808 KB |
Output is correct |
47 |
Correct |
25 ms |
27992 KB |
Output is correct |
48 |
Correct |
144 ms |
56248 KB |
Output is correct |
49 |
Correct |
150 ms |
56316 KB |
Output is correct |
50 |
Correct |
144 ms |
56292 KB |
Output is correct |
51 |
Correct |
142 ms |
56064 KB |
Output is correct |
52 |
Correct |
134 ms |
54528 KB |
Output is correct |
53 |
Correct |
120 ms |
48092 KB |
Output is correct |
54 |
Correct |
164 ms |
57016 KB |
Output is correct |
55 |
Correct |
138 ms |
56192 KB |
Output is correct |
56 |
Correct |
196 ms |
58812 KB |
Output is correct |
57 |
Correct |
149 ms |
57272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
24156 KB |
Output is correct |
2 |
Correct |
12 ms |
23896 KB |
Output is correct |
3 |
Correct |
11 ms |
24156 KB |
Output is correct |
4 |
Correct |
11 ms |
23900 KB |
Output is correct |
5 |
Correct |
13 ms |
24260 KB |
Output is correct |
6 |
Correct |
10 ms |
24152 KB |
Output is correct |
7 |
Correct |
11 ms |
24196 KB |
Output is correct |
8 |
Correct |
11 ms |
24412 KB |
Output is correct |
9 |
Correct |
12 ms |
24416 KB |
Output is correct |
10 |
Correct |
59 ms |
45660 KB |
Output is correct |
11 |
Correct |
69 ms |
50528 KB |
Output is correct |
12 |
Correct |
84 ms |
50012 KB |
Output is correct |
13 |
Correct |
73 ms |
51704 KB |
Output is correct |
14 |
Correct |
65 ms |
53948 KB |
Output is correct |
15 |
Correct |
70 ms |
45920 KB |
Output is correct |
16 |
Correct |
148 ms |
54760 KB |
Output is correct |
17 |
Correct |
139 ms |
54028 KB |
Output is correct |
18 |
Correct |
151 ms |
55748 KB |
Output is correct |
19 |
Correct |
176 ms |
57968 KB |
Output is correct |
20 |
Correct |
152 ms |
59472 KB |
Output is correct |
21 |
Correct |
155 ms |
60852 KB |
Output is correct |
22 |
Correct |
160 ms |
60136 KB |
Output is correct |
23 |
Correct |
159 ms |
60600 KB |
Output is correct |
24 |
Correct |
162 ms |
60744 KB |
Output is correct |
25 |
Correct |
161 ms |
59396 KB |
Output is correct |
26 |
Correct |
167 ms |
60192 KB |
Output is correct |
27 |
Correct |
155 ms |
58896 KB |
Output is correct |
28 |
Correct |
11 ms |
24348 KB |
Output is correct |
29 |
Correct |
13 ms |
24204 KB |
Output is correct |
30 |
Correct |
12 ms |
24196 KB |
Output is correct |
31 |
Correct |
11 ms |
24156 KB |
Output is correct |
32 |
Correct |
11 ms |
24152 KB |
Output is correct |
33 |
Correct |
10 ms |
24284 KB |
Output is correct |
34 |
Correct |
12 ms |
24412 KB |
Output is correct |
35 |
Correct |
12 ms |
24408 KB |
Output is correct |
36 |
Correct |
12 ms |
24412 KB |
Output is correct |
37 |
Correct |
21 ms |
27884 KB |
Output is correct |
38 |
Correct |
68 ms |
51296 KB |
Output is correct |
39 |
Correct |
74 ms |
51296 KB |
Output is correct |
40 |
Correct |
71 ms |
51296 KB |
Output is correct |
41 |
Correct |
70 ms |
50912 KB |
Output is correct |
42 |
Correct |
70 ms |
50788 KB |
Output is correct |
43 |
Correct |
63 ms |
48740 KB |
Output is correct |
44 |
Correct |
74 ms |
51664 KB |
Output is correct |
45 |
Correct |
70 ms |
51364 KB |
Output is correct |
46 |
Correct |
66 ms |
54368 KB |
Output is correct |
47 |
Correct |
74 ms |
51808 KB |
Output is correct |
48 |
Correct |
25 ms |
27992 KB |
Output is correct |
49 |
Correct |
144 ms |
56248 KB |
Output is correct |
50 |
Correct |
150 ms |
56316 KB |
Output is correct |
51 |
Correct |
144 ms |
56292 KB |
Output is correct |
52 |
Correct |
142 ms |
56064 KB |
Output is correct |
53 |
Correct |
134 ms |
54528 KB |
Output is correct |
54 |
Correct |
120 ms |
48092 KB |
Output is correct |
55 |
Correct |
164 ms |
57016 KB |
Output is correct |
56 |
Correct |
138 ms |
56192 KB |
Output is correct |
57 |
Correct |
196 ms |
58812 KB |
Output is correct |
58 |
Correct |
149 ms |
57272 KB |
Output is correct |
59 |
Correct |
65 ms |
33368 KB |
Output is correct |
60 |
Correct |
132 ms |
56012 KB |
Output is correct |
61 |
Correct |
132 ms |
55176 KB |
Output is correct |
62 |
Correct |
189 ms |
56960 KB |
Output is correct |
63 |
Correct |
181 ms |
59320 KB |
Output is correct |
64 |
Correct |
11 ms |
24180 KB |
Output is correct |
65 |
Correct |
10 ms |
24228 KB |
Output is correct |
66 |
Correct |
10 ms |
24412 KB |
Output is correct |
67 |
Correct |
14 ms |
24324 KB |
Output is correct |
68 |
Correct |
11 ms |
24152 KB |
Output is correct |
69 |
Correct |
11 ms |
24440 KB |
Output is correct |
70 |
Correct |
11 ms |
24412 KB |
Output is correct |
71 |
Correct |
17 ms |
24412 KB |
Output is correct |
72 |
Correct |
11 ms |
24464 KB |
Output is correct |
73 |
Correct |
12 ms |
24508 KB |
Output is correct |
74 |
Correct |
106 ms |
62560 KB |
Output is correct |
75 |
Correct |
73 ms |
51292 KB |
Output is correct |
76 |
Correct |
70 ms |
51552 KB |
Output is correct |
77 |
Correct |
83 ms |
54880 KB |
Output is correct |
78 |
Correct |
92 ms |
59744 KB |
Output is correct |
79 |
Correct |
70 ms |
52064 KB |
Output is correct |
80 |
Correct |
89 ms |
57684 KB |
Output is correct |
81 |
Correct |
114 ms |
65620 KB |
Output is correct |
82 |
Correct |
128 ms |
68664 KB |
Output is correct |
83 |
Correct |
139 ms |
74280 KB |
Output is correct |
84 |
Correct |
70 ms |
54096 KB |
Output is correct |
85 |
Correct |
129 ms |
69456 KB |
Output is correct |
86 |
Correct |
62 ms |
38404 KB |
Output is correct |
87 |
Correct |
148 ms |
56300 KB |
Output is correct |
88 |
Correct |
133 ms |
56248 KB |
Output is correct |
89 |
Correct |
181 ms |
58220 KB |
Output is correct |
90 |
Correct |
150 ms |
65628 KB |
Output is correct |
91 |
Correct |
161 ms |
63096 KB |
Output is correct |
92 |
Correct |
182 ms |
61180 KB |
Output is correct |
93 |
Correct |
212 ms |
69864 KB |
Output is correct |
94 |
Correct |
218 ms |
73264 KB |
Output is correct |
95 |
Correct |
207 ms |
78160 KB |
Output is correct |
96 |
Correct |
182 ms |
59068 KB |
Output is correct |
97 |
Correct |
196 ms |
66032 KB |
Output is correct |