#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
const int M = 2e5;
const int inf = 2e9;
int id[M],deg[N];
struct nodedata
{
int parent,active;
};
vector <nodedata> nodes;///parent,active
vector <int> e[2*N - 1];
int p2[2*N - 1][20];
int dpth[2*N - 1];
struct dsu
{
int id,parent,active,sz;
};
dsu v[N];
int leader(int node)
{
if(node == v[node].parent)return node;
int l = leader(v[node].parent);
v[node].parent = l;
return v[node].parent;
}
void connect(int a,int b,int w)
{
deg[a]++;
deg[b]++;
int active = 0;
if(deg[a] >= 3 || deg[b] >= 3)active = 1;
a = leader(a);
b = leader(b);
if(a == b)
{
if(nodes[v[a].id].active == inf)nodes[v[a].id].active = w;
v[a].active = min(v[a].active,w);
}
else
{
nodes[v[a].id].parent = nodes.size();
nodes[v[b].id].parent = nodes.size();
if(v[a].active != inf || v[b].active != inf)active = 1;
if(v[a].sz < v[b].sz)swap(a,b);
v[b].parent = a;
v[a].sz+=v[b].sz;
v[a].id = nodes.size();
nodes.push_back({-1,active?w:inf});
v[a].active = active?w:inf;
}
}
void dfs(int node,int p = -1,int d = 0)
{
dpth[node] = d;
p2[node][0] = p;
for(auto i:e[node])
{
if(i == p)continue;
dfs(i,node,d + 1);
}
}
void init(int n, int m,vector<int> u,vector<int> u2,vector<int> w)
{
int i,j;
for(i = 0; i < m; i++)id[i] = i;
for(i = 0; i < n; i++)
{
nodes.push_back({-1,inf});
v[i] = {i,i,inf,1};
}
sort(id,id + m,[&](int a,int b)
{
return w[a] < w[b];
});
for(i = 0; i < m; i++)
{
///connect u[id[i]] with v[id[i]]
connect(u[id[i]],u2[id[i]],w[id[i]]);
//cout<<u[id[i]]<<' '<<u2[id[i]]<<'\n';
}
int cnt = 0;
for(auto i:nodes)
{
if(i.parent != -1)
{
e[i.parent].push_back(cnt);
}
cnt++;
}
dfs(nodes.size() - 1,nodes.size() - 1);
for(j = 1; j < 20; j++)
{
for(i = 0; i < 2*n - 1; i++)
{
p2[i][j] = p2[p2[i][j - 1]][j - 1];
}
}
/*for(i = 0;i < 2*n - 1;i++){
for(j = 0;j < 20;j++)cout<<p2[i][j]<<' ';
cout<<'\n';
}
for(i = 0;i < 2*n - 1;i++){
cout<<p2[i][0]<<' '<<dpth[i]<<' '<<nodes[i].active<<' '<<i<<'\n';
}*/
}
int getMinimumFuelCapacity(int x, int y)
{
if(nodes[nodes.size() - 1].active == inf)return -1;
if(dpth[x] < dpth[y])swap(x,y);
/*for(int i = 19; i >= 0; i--)
{
if(dpth[x] - (1<<i) >= dpth[y])
{
x = p2[x][i];
}
}
for(int i = 19; i >= 0; i--)
{
if(p2[x][i] != p2[y][i])
{
x = p2[x][i];
y = p2[y][i];
}
}
if(x != y)x = p2[x][0];
for(int i = 19; i >= 0; i--)
{
if(nodes[p2[x][i]].active == inf)
{
x = p2[x][i];
}
}
if(nodes[x].active == inf)
{
x = p2[x][0];
}*/
while(dpth[x] > dpth[y])x = p2[x][0];
while(x != y)x = p2[x][0],y = p2[y][0];
while(nodes[x].active == inf)x = p2[x][0];
return nodes[x].active;
}
/**
5 6
0 1 4
0 2 4
1 2 1
1 3 2
1 4 10
2 3 3
3
1 2
2 4
0 1
3 2
0 1 5
0 2 5
1
1 2
**/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
4 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
5 ms |
5204 KB |
Output is correct |
9 |
Correct |
88 ms |
25240 KB |
Output is correct |
10 |
Correct |
135 ms |
29756 KB |
Output is correct |
11 |
Correct |
114 ms |
29508 KB |
Output is correct |
12 |
Correct |
125 ms |
30744 KB |
Output is correct |
13 |
Correct |
114 ms |
32108 KB |
Output is correct |
14 |
Correct |
102 ms |
25360 KB |
Output is correct |
15 |
Correct |
179 ms |
31804 KB |
Output is correct |
16 |
Correct |
165 ms |
31108 KB |
Output is correct |
17 |
Correct |
206 ms |
32532 KB |
Output is correct |
18 |
Correct |
154 ms |
33912 KB |
Output is correct |
19 |
Correct |
89 ms |
12480 KB |
Output is correct |
20 |
Correct |
269 ms |
33004 KB |
Output is correct |
21 |
Correct |
269 ms |
32236 KB |
Output is correct |
22 |
Correct |
309 ms |
33952 KB |
Output is correct |
23 |
Execution timed out |
2063 ms |
33764 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Execution timed out |
2048 ms |
33588 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
4 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
5 ms |
5204 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 ms |
5272 KB |
Output is correct |
11 |
Correct |
4 ms |
5204 KB |
Output is correct |
12 |
Correct |
4 ms |
5204 KB |
Output is correct |
13 |
Correct |
4 ms |
5204 KB |
Output is correct |
14 |
Correct |
4 ms |
5160 KB |
Output is correct |
15 |
Correct |
4 ms |
5284 KB |
Output is correct |
16 |
Correct |
4 ms |
5204 KB |
Output is correct |
17 |
Correct |
4 ms |
5284 KB |
Output is correct |
18 |
Correct |
3 ms |
5300 KB |
Output is correct |
19 |
Correct |
4 ms |
5204 KB |
Output is correct |
20 |
Correct |
3 ms |
5204 KB |
Output is correct |
21 |
Correct |
3 ms |
5204 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5156 KB |
Output is correct |
24 |
Correct |
6 ms |
5332 KB |
Output is correct |
25 |
Correct |
6 ms |
5332 KB |
Output is correct |
26 |
Correct |
5 ms |
5284 KB |
Output is correct |
27 |
Correct |
4 ms |
5324 KB |
Output is correct |
28 |
Correct |
4 ms |
5284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
4 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
5 ms |
5204 KB |
Output is correct |
10 |
Correct |
88 ms |
25240 KB |
Output is correct |
11 |
Correct |
135 ms |
29756 KB |
Output is correct |
12 |
Correct |
114 ms |
29508 KB |
Output is correct |
13 |
Correct |
125 ms |
30744 KB |
Output is correct |
14 |
Correct |
114 ms |
32108 KB |
Output is correct |
15 |
Correct |
4 ms |
5272 KB |
Output is correct |
16 |
Correct |
4 ms |
5204 KB |
Output is correct |
17 |
Correct |
4 ms |
5204 KB |
Output is correct |
18 |
Correct |
4 ms |
5204 KB |
Output is correct |
19 |
Correct |
4 ms |
5160 KB |
Output is correct |
20 |
Correct |
4 ms |
5284 KB |
Output is correct |
21 |
Correct |
4 ms |
5204 KB |
Output is correct |
22 |
Correct |
4 ms |
5284 KB |
Output is correct |
23 |
Correct |
3 ms |
5300 KB |
Output is correct |
24 |
Correct |
4 ms |
5204 KB |
Output is correct |
25 |
Correct |
3 ms |
5204 KB |
Output is correct |
26 |
Correct |
3 ms |
5204 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5156 KB |
Output is correct |
29 |
Correct |
6 ms |
5332 KB |
Output is correct |
30 |
Correct |
6 ms |
5332 KB |
Output is correct |
31 |
Correct |
5 ms |
5284 KB |
Output is correct |
32 |
Correct |
4 ms |
5324 KB |
Output is correct |
33 |
Correct |
4 ms |
5284 KB |
Output is correct |
34 |
Correct |
15 ms |
8864 KB |
Output is correct |
35 |
Correct |
115 ms |
32668 KB |
Output is correct |
36 |
Correct |
118 ms |
32652 KB |
Output is correct |
37 |
Correct |
128 ms |
32644 KB |
Output is correct |
38 |
Correct |
120 ms |
32336 KB |
Output is correct |
39 |
Correct |
159 ms |
32008 KB |
Output is correct |
40 |
Correct |
130 ms |
29912 KB |
Output is correct |
41 |
Correct |
130 ms |
32948 KB |
Output is correct |
42 |
Correct |
127 ms |
32660 KB |
Output is correct |
43 |
Correct |
125 ms |
34392 KB |
Output is correct |
44 |
Correct |
129 ms |
33104 KB |
Output is correct |
45 |
Correct |
144 ms |
30896 KB |
Output is correct |
46 |
Correct |
122 ms |
32624 KB |
Output is correct |
47 |
Correct |
143 ms |
32708 KB |
Output is correct |
48 |
Correct |
148 ms |
33176 KB |
Output is correct |
49 |
Correct |
79 ms |
12356 KB |
Output is correct |
50 |
Correct |
56 ms |
12408 KB |
Output is correct |
51 |
Correct |
132 ms |
26536 KB |
Output is correct |
52 |
Correct |
175 ms |
35828 KB |
Output is correct |
53 |
Correct |
181 ms |
36984 KB |
Output is correct |
54 |
Correct |
177 ms |
37416 KB |
Output is correct |
55 |
Correct |
119 ms |
34312 KB |
Output is correct |
56 |
Correct |
164 ms |
37296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
4 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
5 ms |
5204 KB |
Output is correct |
9 |
Correct |
88 ms |
25240 KB |
Output is correct |
10 |
Correct |
135 ms |
29756 KB |
Output is correct |
11 |
Correct |
114 ms |
29508 KB |
Output is correct |
12 |
Correct |
125 ms |
30744 KB |
Output is correct |
13 |
Correct |
114 ms |
32108 KB |
Output is correct |
14 |
Correct |
102 ms |
25360 KB |
Output is correct |
15 |
Correct |
179 ms |
31804 KB |
Output is correct |
16 |
Correct |
165 ms |
31108 KB |
Output is correct |
17 |
Correct |
206 ms |
32532 KB |
Output is correct |
18 |
Correct |
154 ms |
33912 KB |
Output is correct |
19 |
Execution timed out |
2048 ms |
33588 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
4 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
5 ms |
5204 KB |
Output is correct |
10 |
Correct |
88 ms |
25240 KB |
Output is correct |
11 |
Correct |
135 ms |
29756 KB |
Output is correct |
12 |
Correct |
114 ms |
29508 KB |
Output is correct |
13 |
Correct |
125 ms |
30744 KB |
Output is correct |
14 |
Correct |
114 ms |
32108 KB |
Output is correct |
15 |
Correct |
102 ms |
25360 KB |
Output is correct |
16 |
Correct |
179 ms |
31804 KB |
Output is correct |
17 |
Correct |
165 ms |
31108 KB |
Output is correct |
18 |
Correct |
206 ms |
32532 KB |
Output is correct |
19 |
Correct |
154 ms |
33912 KB |
Output is correct |
20 |
Execution timed out |
2048 ms |
33588 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |