#include <bits/stdc++.h>
using namespace std;
using lli = long long;
const int maxN = 1e6 + 5;
const int LG = 21;
int n,m,node,dsu[maxN],val[maxN],deg[maxN];
int up[LG][maxN],lg[maxN],res[maxN],dep[maxN];
bool swp[maxN];
vector<int> adj[maxN];
struct Tedge
{
int x,y,w;
}
e[maxN];
int findset(int u)
{
return dsu[u] == u ? u : dsu[u] = findset(dsu[u]);
}
void add(int u,int v,int w)
{
int v1 = v,u1 = u;
deg[u]++;deg[v]++;
++node;
u = findset(u);v = findset(v);
dsu[u] = dsu[v] = node;
dsu[node] = node;
val[node] = w;
adj[node].push_back(u);
if (u != v) adj[node].push_back(v);
if (u == v || swp[u] || swp[v] || deg[u1] > 2 || deg[v1] > 2) swp[node] = true;
else swp[node] = false;
}
bool cmp(const Tedge& a,const Tedge& b)
{
return a.w < b.w;
}
void dfs(int u,int par)
{
if (swp[u]) res[u] = val[u];
else res[u] = res[par];
up[0][u] = par;
for (int v : adj[u])
{
dep[v] = dep[u] + 1;
dfs(v,u);
}
}
void presolve()
{
lg[1] = 0;
for (int i = 2;i <= node;++i) lg[i] = lg[i / 2] + 1;
for (int j = 1;j < LG;++j)
for (int i = 1;i <= node;++i)
up[j][i] = up[j - 1][up[j - 1][i]];
}
int lca(int u,int v)
{
int u1 = u,v1 = v;
if (dep[u] < dep[v]) swap(u,v);
int k = dep[u] - dep[v];
for (int i = lg[k];i >= 0;--i)
if (k & (1 << i)) u = up[i][u];
if (u == v) return u;
k = dep[u];
for (int i = lg[k];i >= 0;--i)
{
if (up[i][u] != up[i][v])
{
u = up[i][u];
v = up[i][v];
}
}
return up[0][u];
}
void init(int N,int M,vector<int> U,vector<int> V,vector<int> W)
{
n = N;m = M;
node = n;
for (int i = 1;i <= M;++i)
{
e[i].x = U[i - 1] + 1;
e[i].y = V[i - 1] + 1;
e[i].w = W[i - 1];
}
sort(e + 1,e + m + 1,cmp);
for (int i = 1;i <= n;++i)
{
dsu[i] = i;
if (deg[i] >= 3) swp[i] = true;
else swp[i] = false;
}
for (int i = 1;i <= m;++i)
{
//cout << e[i].x << " " << e[i].y << " " << e[i].w << "\n";
add(e[i].x,e[i].y,e[i].w);
}
//cout << "A\n";
//for (int i = 1;i <= node;++i) for (int v : adj[i]) cout << i << " " << v << "\n";
res[0] = -1;dep[node] = 0;
dfs(node,0);
presolve();
}
int getMinimumFuelCapacity(int X, int Y)
{
++X,++Y;
//cout << X << " " << Y << " " << swp[lca(X,Y)] << " ";
return res[lca(X,Y)];
}
Compilation message
swap.cpp: In function 'int lca(int, int)':
swap.cpp:58:9: warning: unused variable 'u1' [-Wunused-variable]
58 | int u1 = u,v1 = v;
| ^~
swap.cpp:58:16: warning: unused variable 'v1' [-Wunused-variable]
58 | int u1 = u,v1 = v;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24152 KB |
Output is correct |
2 |
Correct |
11 ms |
23896 KB |
Output is correct |
3 |
Correct |
12 ms |
24156 KB |
Output is correct |
4 |
Correct |
12 ms |
24156 KB |
Output is correct |
5 |
Correct |
11 ms |
24412 KB |
Output is correct |
6 |
Correct |
11 ms |
24156 KB |
Output is correct |
7 |
Correct |
11 ms |
24412 KB |
Output is correct |
8 |
Correct |
11 ms |
24412 KB |
Output is correct |
9 |
Correct |
60 ms |
46932 KB |
Output is correct |
10 |
Correct |
74 ms |
52060 KB |
Output is correct |
11 |
Correct |
63 ms |
51800 KB |
Output is correct |
12 |
Correct |
69 ms |
53388 KB |
Output is correct |
13 |
Correct |
64 ms |
55804 KB |
Output is correct |
14 |
Correct |
65 ms |
47440 KB |
Output is correct |
15 |
Correct |
116 ms |
56544 KB |
Output is correct |
16 |
Correct |
121 ms |
55808 KB |
Output is correct |
17 |
Correct |
122 ms |
57524 KB |
Output is correct |
18 |
Correct |
185 ms |
59788 KB |
Output is correct |
19 |
Correct |
60 ms |
33876 KB |
Output is correct |
20 |
Correct |
116 ms |
57552 KB |
Output is correct |
21 |
Correct |
124 ms |
56844 KB |
Output is correct |
22 |
Correct |
126 ms |
58804 KB |
Output is correct |
23 |
Correct |
183 ms |
61116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24152 KB |
Output is correct |
2 |
Correct |
11 ms |
23896 KB |
Output is correct |
3 |
Correct |
168 ms |
60916 KB |
Output is correct |
4 |
Correct |
170 ms |
62620 KB |
Output is correct |
5 |
Correct |
168 ms |
61932 KB |
Output is correct |
6 |
Correct |
162 ms |
62392 KB |
Output is correct |
7 |
Correct |
158 ms |
62160 KB |
Output is correct |
8 |
Correct |
160 ms |
60932 KB |
Output is correct |
9 |
Correct |
150 ms |
61788 KB |
Output is correct |
10 |
Correct |
149 ms |
60436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24152 KB |
Output is correct |
2 |
Correct |
11 ms |
23896 KB |
Output is correct |
3 |
Correct |
12 ms |
24156 KB |
Output is correct |
4 |
Correct |
12 ms |
24156 KB |
Output is correct |
5 |
Correct |
11 ms |
24412 KB |
Output is correct |
6 |
Correct |
11 ms |
24156 KB |
Output is correct |
7 |
Correct |
11 ms |
24412 KB |
Output is correct |
8 |
Correct |
11 ms |
24412 KB |
Output is correct |
9 |
Correct |
9 ms |
24156 KB |
Output is correct |
10 |
Correct |
9 ms |
24280 KB |
Output is correct |
11 |
Correct |
10 ms |
24408 KB |
Output is correct |
12 |
Correct |
10 ms |
24412 KB |
Output is correct |
13 |
Correct |
10 ms |
24156 KB |
Output is correct |
14 |
Correct |
10 ms |
24360 KB |
Output is correct |
15 |
Correct |
10 ms |
24412 KB |
Output is correct |
16 |
Correct |
9 ms |
24272 KB |
Output is correct |
17 |
Correct |
10 ms |
24408 KB |
Output is correct |
18 |
Correct |
10 ms |
24412 KB |
Output is correct |
19 |
Correct |
10 ms |
24412 KB |
Output is correct |
20 |
Correct |
9 ms |
24412 KB |
Output is correct |
21 |
Correct |
10 ms |
24304 KB |
Output is correct |
22 |
Correct |
10 ms |
24412 KB |
Output is correct |
23 |
Correct |
10 ms |
24132 KB |
Output is correct |
24 |
Correct |
10 ms |
24412 KB |
Output is correct |
25 |
Correct |
10 ms |
24616 KB |
Output is correct |
26 |
Correct |
10 ms |
24412 KB |
Output is correct |
27 |
Correct |
10 ms |
24412 KB |
Output is correct |
28 |
Correct |
12 ms |
24412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
24156 KB |
Output is correct |
2 |
Correct |
13 ms |
24152 KB |
Output is correct |
3 |
Correct |
11 ms |
23896 KB |
Output is correct |
4 |
Correct |
12 ms |
24156 KB |
Output is correct |
5 |
Correct |
12 ms |
24156 KB |
Output is correct |
6 |
Correct |
11 ms |
24412 KB |
Output is correct |
7 |
Correct |
11 ms |
24156 KB |
Output is correct |
8 |
Correct |
11 ms |
24412 KB |
Output is correct |
9 |
Correct |
11 ms |
24412 KB |
Output is correct |
10 |
Correct |
60 ms |
46932 KB |
Output is correct |
11 |
Correct |
74 ms |
52060 KB |
Output is correct |
12 |
Correct |
63 ms |
51800 KB |
Output is correct |
13 |
Correct |
69 ms |
53388 KB |
Output is correct |
14 |
Correct |
64 ms |
55804 KB |
Output is correct |
15 |
Correct |
9 ms |
24280 KB |
Output is correct |
16 |
Correct |
10 ms |
24408 KB |
Output is correct |
17 |
Correct |
10 ms |
24412 KB |
Output is correct |
18 |
Correct |
10 ms |
24156 KB |
Output is correct |
19 |
Correct |
10 ms |
24360 KB |
Output is correct |
20 |
Correct |
10 ms |
24412 KB |
Output is correct |
21 |
Correct |
9 ms |
24272 KB |
Output is correct |
22 |
Correct |
10 ms |
24408 KB |
Output is correct |
23 |
Correct |
10 ms |
24412 KB |
Output is correct |
24 |
Correct |
10 ms |
24412 KB |
Output is correct |
25 |
Correct |
9 ms |
24412 KB |
Output is correct |
26 |
Correct |
10 ms |
24304 KB |
Output is correct |
27 |
Correct |
10 ms |
24412 KB |
Output is correct |
28 |
Correct |
10 ms |
24132 KB |
Output is correct |
29 |
Correct |
10 ms |
24412 KB |
Output is correct |
30 |
Correct |
10 ms |
24616 KB |
Output is correct |
31 |
Correct |
10 ms |
24412 KB |
Output is correct |
32 |
Correct |
10 ms |
24412 KB |
Output is correct |
33 |
Correct |
12 ms |
24412 KB |
Output is correct |
34 |
Correct |
16 ms |
27992 KB |
Output is correct |
35 |
Correct |
61 ms |
53076 KB |
Output is correct |
36 |
Correct |
60 ms |
52952 KB |
Output is correct |
37 |
Correct |
64 ms |
52864 KB |
Output is correct |
38 |
Correct |
60 ms |
52736 KB |
Output is correct |
39 |
Correct |
63 ms |
52560 KB |
Output is correct |
40 |
Correct |
58 ms |
50180 KB |
Output is correct |
41 |
Correct |
71 ms |
53380 KB |
Output is correct |
42 |
Correct |
67 ms |
53076 KB |
Output is correct |
43 |
Correct |
63 ms |
56172 KB |
Output is correct |
44 |
Correct |
70 ms |
53328 KB |
Output is correct |
45 |
Correct |
98 ms |
64340 KB |
Output is correct |
46 |
Correct |
67 ms |
53076 KB |
Output is correct |
47 |
Correct |
66 ms |
53076 KB |
Output is correct |
48 |
Correct |
82 ms |
56660 KB |
Output is correct |
49 |
Correct |
82 ms |
60988 KB |
Output is correct |
50 |
Correct |
65 ms |
52988 KB |
Output is correct |
51 |
Correct |
80 ms |
58964 KB |
Output is correct |
52 |
Correct |
100 ms |
67828 KB |
Output is correct |
53 |
Correct |
116 ms |
70996 KB |
Output is correct |
54 |
Correct |
121 ms |
76888 KB |
Output is correct |
55 |
Correct |
56 ms |
55892 KB |
Output is correct |
56 |
Correct |
117 ms |
71760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24152 KB |
Output is correct |
2 |
Correct |
11 ms |
23896 KB |
Output is correct |
3 |
Correct |
12 ms |
24156 KB |
Output is correct |
4 |
Correct |
12 ms |
24156 KB |
Output is correct |
5 |
Correct |
11 ms |
24412 KB |
Output is correct |
6 |
Correct |
11 ms |
24156 KB |
Output is correct |
7 |
Correct |
11 ms |
24412 KB |
Output is correct |
8 |
Correct |
11 ms |
24412 KB |
Output is correct |
9 |
Correct |
60 ms |
46932 KB |
Output is correct |
10 |
Correct |
74 ms |
52060 KB |
Output is correct |
11 |
Correct |
63 ms |
51800 KB |
Output is correct |
12 |
Correct |
69 ms |
53388 KB |
Output is correct |
13 |
Correct |
64 ms |
55804 KB |
Output is correct |
14 |
Correct |
65 ms |
47440 KB |
Output is correct |
15 |
Correct |
116 ms |
56544 KB |
Output is correct |
16 |
Correct |
121 ms |
55808 KB |
Output is correct |
17 |
Correct |
122 ms |
57524 KB |
Output is correct |
18 |
Correct |
185 ms |
59788 KB |
Output is correct |
19 |
Correct |
168 ms |
60916 KB |
Output is correct |
20 |
Correct |
170 ms |
62620 KB |
Output is correct |
21 |
Correct |
168 ms |
61932 KB |
Output is correct |
22 |
Correct |
162 ms |
62392 KB |
Output is correct |
23 |
Correct |
158 ms |
62160 KB |
Output is correct |
24 |
Correct |
160 ms |
60932 KB |
Output is correct |
25 |
Correct |
150 ms |
61788 KB |
Output is correct |
26 |
Correct |
149 ms |
60436 KB |
Output is correct |
27 |
Correct |
9 ms |
24280 KB |
Output is correct |
28 |
Correct |
10 ms |
24408 KB |
Output is correct |
29 |
Correct |
10 ms |
24412 KB |
Output is correct |
30 |
Correct |
10 ms |
24156 KB |
Output is correct |
31 |
Correct |
10 ms |
24360 KB |
Output is correct |
32 |
Correct |
10 ms |
24412 KB |
Output is correct |
33 |
Correct |
9 ms |
24272 KB |
Output is correct |
34 |
Correct |
10 ms |
24408 KB |
Output is correct |
35 |
Correct |
10 ms |
24412 KB |
Output is correct |
36 |
Correct |
16 ms |
27992 KB |
Output is correct |
37 |
Correct |
61 ms |
53076 KB |
Output is correct |
38 |
Correct |
60 ms |
52952 KB |
Output is correct |
39 |
Correct |
64 ms |
52864 KB |
Output is correct |
40 |
Correct |
60 ms |
52736 KB |
Output is correct |
41 |
Correct |
63 ms |
52560 KB |
Output is correct |
42 |
Correct |
58 ms |
50180 KB |
Output is correct |
43 |
Correct |
71 ms |
53380 KB |
Output is correct |
44 |
Correct |
67 ms |
53076 KB |
Output is correct |
45 |
Correct |
63 ms |
56172 KB |
Output is correct |
46 |
Correct |
70 ms |
53328 KB |
Output is correct |
47 |
Correct |
22 ms |
28252 KB |
Output is correct |
48 |
Correct |
117 ms |
58040 KB |
Output is correct |
49 |
Correct |
122 ms |
57852 KB |
Output is correct |
50 |
Correct |
121 ms |
57780 KB |
Output is correct |
51 |
Correct |
123 ms |
58052 KB |
Output is correct |
52 |
Correct |
120 ms |
56064 KB |
Output is correct |
53 |
Correct |
107 ms |
49412 KB |
Output is correct |
54 |
Correct |
118 ms |
58808 KB |
Output is correct |
55 |
Correct |
114 ms |
58040 KB |
Output is correct |
56 |
Correct |
171 ms |
60344 KB |
Output is correct |
57 |
Correct |
130 ms |
58808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
24156 KB |
Output is correct |
2 |
Correct |
13 ms |
24152 KB |
Output is correct |
3 |
Correct |
11 ms |
23896 KB |
Output is correct |
4 |
Correct |
12 ms |
24156 KB |
Output is correct |
5 |
Correct |
12 ms |
24156 KB |
Output is correct |
6 |
Correct |
11 ms |
24412 KB |
Output is correct |
7 |
Correct |
11 ms |
24156 KB |
Output is correct |
8 |
Correct |
11 ms |
24412 KB |
Output is correct |
9 |
Correct |
11 ms |
24412 KB |
Output is correct |
10 |
Correct |
60 ms |
46932 KB |
Output is correct |
11 |
Correct |
74 ms |
52060 KB |
Output is correct |
12 |
Correct |
63 ms |
51800 KB |
Output is correct |
13 |
Correct |
69 ms |
53388 KB |
Output is correct |
14 |
Correct |
64 ms |
55804 KB |
Output is correct |
15 |
Correct |
65 ms |
47440 KB |
Output is correct |
16 |
Correct |
116 ms |
56544 KB |
Output is correct |
17 |
Correct |
121 ms |
55808 KB |
Output is correct |
18 |
Correct |
122 ms |
57524 KB |
Output is correct |
19 |
Correct |
185 ms |
59788 KB |
Output is correct |
20 |
Correct |
168 ms |
60916 KB |
Output is correct |
21 |
Correct |
170 ms |
62620 KB |
Output is correct |
22 |
Correct |
168 ms |
61932 KB |
Output is correct |
23 |
Correct |
162 ms |
62392 KB |
Output is correct |
24 |
Correct |
158 ms |
62160 KB |
Output is correct |
25 |
Correct |
160 ms |
60932 KB |
Output is correct |
26 |
Correct |
150 ms |
61788 KB |
Output is correct |
27 |
Correct |
149 ms |
60436 KB |
Output is correct |
28 |
Correct |
9 ms |
24280 KB |
Output is correct |
29 |
Correct |
10 ms |
24408 KB |
Output is correct |
30 |
Correct |
10 ms |
24412 KB |
Output is correct |
31 |
Correct |
10 ms |
24156 KB |
Output is correct |
32 |
Correct |
10 ms |
24360 KB |
Output is correct |
33 |
Correct |
10 ms |
24412 KB |
Output is correct |
34 |
Correct |
9 ms |
24272 KB |
Output is correct |
35 |
Correct |
10 ms |
24408 KB |
Output is correct |
36 |
Correct |
10 ms |
24412 KB |
Output is correct |
37 |
Correct |
16 ms |
27992 KB |
Output is correct |
38 |
Correct |
61 ms |
53076 KB |
Output is correct |
39 |
Correct |
60 ms |
52952 KB |
Output is correct |
40 |
Correct |
64 ms |
52864 KB |
Output is correct |
41 |
Correct |
60 ms |
52736 KB |
Output is correct |
42 |
Correct |
63 ms |
52560 KB |
Output is correct |
43 |
Correct |
58 ms |
50180 KB |
Output is correct |
44 |
Correct |
71 ms |
53380 KB |
Output is correct |
45 |
Correct |
67 ms |
53076 KB |
Output is correct |
46 |
Correct |
63 ms |
56172 KB |
Output is correct |
47 |
Correct |
70 ms |
53328 KB |
Output is correct |
48 |
Correct |
22 ms |
28252 KB |
Output is correct |
49 |
Correct |
117 ms |
58040 KB |
Output is correct |
50 |
Correct |
122 ms |
57852 KB |
Output is correct |
51 |
Correct |
121 ms |
57780 KB |
Output is correct |
52 |
Correct |
123 ms |
58052 KB |
Output is correct |
53 |
Correct |
120 ms |
56064 KB |
Output is correct |
54 |
Correct |
107 ms |
49412 KB |
Output is correct |
55 |
Correct |
118 ms |
58808 KB |
Output is correct |
56 |
Correct |
114 ms |
58040 KB |
Output is correct |
57 |
Correct |
171 ms |
60344 KB |
Output is correct |
58 |
Correct |
130 ms |
58808 KB |
Output is correct |
59 |
Correct |
60 ms |
33876 KB |
Output is correct |
60 |
Correct |
116 ms |
57552 KB |
Output is correct |
61 |
Correct |
124 ms |
56844 KB |
Output is correct |
62 |
Correct |
126 ms |
58804 KB |
Output is correct |
63 |
Correct |
183 ms |
61116 KB |
Output is correct |
64 |
Correct |
10 ms |
24412 KB |
Output is correct |
65 |
Correct |
9 ms |
24412 KB |
Output is correct |
66 |
Correct |
10 ms |
24304 KB |
Output is correct |
67 |
Correct |
10 ms |
24412 KB |
Output is correct |
68 |
Correct |
10 ms |
24132 KB |
Output is correct |
69 |
Correct |
10 ms |
24412 KB |
Output is correct |
70 |
Correct |
10 ms |
24616 KB |
Output is correct |
71 |
Correct |
10 ms |
24412 KB |
Output is correct |
72 |
Correct |
10 ms |
24412 KB |
Output is correct |
73 |
Correct |
12 ms |
24412 KB |
Output is correct |
74 |
Correct |
98 ms |
64340 KB |
Output is correct |
75 |
Correct |
67 ms |
53076 KB |
Output is correct |
76 |
Correct |
66 ms |
53076 KB |
Output is correct |
77 |
Correct |
82 ms |
56660 KB |
Output is correct |
78 |
Correct |
82 ms |
60988 KB |
Output is correct |
79 |
Correct |
65 ms |
52988 KB |
Output is correct |
80 |
Correct |
80 ms |
58964 KB |
Output is correct |
81 |
Correct |
100 ms |
67828 KB |
Output is correct |
82 |
Correct |
116 ms |
70996 KB |
Output is correct |
83 |
Correct |
121 ms |
76888 KB |
Output is correct |
84 |
Correct |
56 ms |
55892 KB |
Output is correct |
85 |
Correct |
117 ms |
71760 KB |
Output is correct |
86 |
Correct |
57 ms |
39176 KB |
Output is correct |
87 |
Correct |
126 ms |
58004 KB |
Output is correct |
88 |
Correct |
111 ms |
58036 KB |
Output is correct |
89 |
Correct |
158 ms |
60020 KB |
Output is correct |
90 |
Correct |
133 ms |
66988 KB |
Output is correct |
91 |
Correct |
149 ms |
64372 KB |
Output is correct |
92 |
Correct |
164 ms |
62640 KB |
Output is correct |
93 |
Correct |
156 ms |
72204 KB |
Output is correct |
94 |
Correct |
192 ms |
75568 KB |
Output is correct |
95 |
Correct |
184 ms |
80684 KB |
Output is correct |
96 |
Correct |
160 ms |
60604 KB |
Output is correct |
97 |
Correct |
208 ms |
67984 KB |
Output is correct |