#include "swap.h"
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 2e5+10;
const int lgmx = 18;
const int inf = 1e9+69;
struct edge
{
int x, y, z;
inline bool operator<(const edge& o) const
{
return z < o.z;
}
} edgs[maxn];
int N, M;
vector<int> comp[maxn];
int root[maxn];
bool ispath[maxn];
int fnp[maxn];
pii sars[maxn];
int epar[maxn];
int dpar[maxn];
vector<int> kids[maxn];
inline void invldp(int r, int z)
{
if(!ispath[r]) return;
for(int u: comp[r])
{
//if(!ispath[u])
// cerr << "fuck" << endl;
ispath[u] = false;
fnp[u] = z;
}
}
inline bool isin(int x, pii p)
{
return x == p.first or x == p.second;
}
inline void mrg(int x, int y, int z)
{
if(comp[root[x]].size() < comp[root[y]].size())
swap(x, y);
int rx = root[x];
int ry = root[y];
if(rx == ry)
{
invldp(rx, z);
}
else
{
if( ispath[rx] and ispath[ry] and isin(x, sars[rx]) and isin(y, sars[ry]) )
{
int a = sars[rx].first^sars[rx].second ^ x;
int b = sars[ry].first^sars[ry].second ^ y;
sars[rx] = pii(a, b);
}
else
{
invldp(rx, z);
invldp(ry, z);
}
epar[ry] = z;
dpar[ry] = rx;
for(int u: comp[ry])
{
root[u] = rx;
comp[rx].push_back(u);
}
comp[ry].clear();
}
}
int tin[maxn];
int tout[maxn];
int timer = 0;
void dfs(int v)
{
tin[v] = timer++;
for(int u: kids[v])
dfs(u);
tout[v] = timer;
}
inline bool isanc(int x, int y)
{
return (tin[x] <= tin[y] and tout[y] <= tout[x]);
}
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W)
{
::N = N;
::M = M;
for(int i = 0; i < N; i++)
{
root[i] = i;
comp[i].push_back(i);
ispath[i] = true;
fnp[i] = inf;
sars[i] = pii(i, i);
dpar[i] = i;
}
for(int i = 0; i < M; i++)
edgs[i] = {V[i], U[i], W[i]};
sort(edgs, edgs+M);
for(int i = 0; i < M; i++)
{
int x = edgs[i].x;
int y = edgs[i].y;
int z = edgs[i].z;
mrg(x, y, z);
}
int r = 0;
for(int i = 0; i < N; i++)
{
if(dpar[i] != i)
kids[dpar[i]].push_back(i);
else
r = i;
}
//cerr << r << endl;
dfs(r);
}
int getMinimumFuelCapacity(int x, int y)
{
int ans = max(fnp[x], fnp[y]);
int v = x;
int t = 0;
while(!isanc(v, y))
{
ans = max(ans, epar[v]);
v = dpar[v];
t++;
}
assert(t <= lgmx);
t = 0;
v = y;
while(!isanc(v, x))
{
ans = max(ans, epar[v]);
v = dpar[v];
t++;
}
//cerr << t << endl;
assert(t <= lgmx);
if(ans == inf)
return -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
7 ms |
9812 KB |
Output is correct |
6 |
Correct |
7 ms |
9748 KB |
Output is correct |
7 |
Correct |
6 ms |
9812 KB |
Output is correct |
8 |
Correct |
6 ms |
9812 KB |
Output is correct |
9 |
Correct |
62 ms |
20612 KB |
Output is correct |
10 |
Correct |
78 ms |
22936 KB |
Output is correct |
11 |
Correct |
81 ms |
22600 KB |
Output is correct |
12 |
Correct |
82 ms |
23500 KB |
Output is correct |
13 |
Correct |
66 ms |
20932 KB |
Output is correct |
14 |
Correct |
83 ms |
20636 KB |
Output is correct |
15 |
Correct |
157 ms |
24932 KB |
Output is correct |
16 |
Correct |
153 ms |
24616 KB |
Output is correct |
17 |
Correct |
157 ms |
25320 KB |
Output is correct |
18 |
Correct |
126 ms |
22768 KB |
Output is correct |
19 |
Correct |
67 ms |
15252 KB |
Output is correct |
20 |
Correct |
156 ms |
26196 KB |
Output is correct |
21 |
Correct |
172 ms |
25864 KB |
Output is correct |
22 |
Correct |
152 ms |
26688 KB |
Output is correct |
23 |
Correct |
134 ms |
24188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
117 ms |
22888 KB |
Output is correct |
4 |
Correct |
121 ms |
23308 KB |
Output is correct |
5 |
Correct |
116 ms |
23264 KB |
Output is correct |
6 |
Correct |
121 ms |
23240 KB |
Output is correct |
7 |
Correct |
122 ms |
23344 KB |
Output is correct |
8 |
Correct |
108 ms |
22896 KB |
Output is correct |
9 |
Correct |
132 ms |
23256 KB |
Output is correct |
10 |
Correct |
116 ms |
22792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
7 ms |
9812 KB |
Output is correct |
6 |
Correct |
7 ms |
9748 KB |
Output is correct |
7 |
Correct |
6 ms |
9812 KB |
Output is correct |
8 |
Correct |
6 ms |
9812 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9812 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
5 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
6 ms |
9812 KB |
Output is correct |
15 |
Correct |
5 ms |
9852 KB |
Output is correct |
16 |
Correct |
5 ms |
9860 KB |
Output is correct |
17 |
Correct |
5 ms |
9812 KB |
Output is correct |
18 |
Correct |
7 ms |
9780 KB |
Output is correct |
19 |
Correct |
7 ms |
9812 KB |
Output is correct |
20 |
Correct |
6 ms |
9812 KB |
Output is correct |
21 |
Correct |
7 ms |
9732 KB |
Output is correct |
22 |
Correct |
6 ms |
9812 KB |
Output is correct |
23 |
Correct |
5 ms |
9756 KB |
Output is correct |
24 |
Correct |
5 ms |
9812 KB |
Output is correct |
25 |
Correct |
6 ms |
9812 KB |
Output is correct |
26 |
Correct |
6 ms |
9812 KB |
Output is correct |
27 |
Correct |
5 ms |
9812 KB |
Output is correct |
28 |
Correct |
6 ms |
9812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
7 ms |
9812 KB |
Output is correct |
7 |
Correct |
7 ms |
9748 KB |
Output is correct |
8 |
Correct |
6 ms |
9812 KB |
Output is correct |
9 |
Correct |
6 ms |
9812 KB |
Output is correct |
10 |
Correct |
62 ms |
20612 KB |
Output is correct |
11 |
Correct |
78 ms |
22936 KB |
Output is correct |
12 |
Correct |
81 ms |
22600 KB |
Output is correct |
13 |
Correct |
82 ms |
23500 KB |
Output is correct |
14 |
Correct |
66 ms |
20932 KB |
Output is correct |
15 |
Correct |
5 ms |
9812 KB |
Output is correct |
16 |
Correct |
5 ms |
9812 KB |
Output is correct |
17 |
Correct |
5 ms |
9812 KB |
Output is correct |
18 |
Correct |
5 ms |
9812 KB |
Output is correct |
19 |
Correct |
6 ms |
9812 KB |
Output is correct |
20 |
Correct |
5 ms |
9852 KB |
Output is correct |
21 |
Correct |
5 ms |
9860 KB |
Output is correct |
22 |
Correct |
5 ms |
9812 KB |
Output is correct |
23 |
Correct |
7 ms |
9780 KB |
Output is correct |
24 |
Correct |
7 ms |
9812 KB |
Output is correct |
25 |
Correct |
6 ms |
9812 KB |
Output is correct |
26 |
Correct |
7 ms |
9732 KB |
Output is correct |
27 |
Correct |
6 ms |
9812 KB |
Output is correct |
28 |
Correct |
5 ms |
9756 KB |
Output is correct |
29 |
Correct |
5 ms |
9812 KB |
Output is correct |
30 |
Correct |
6 ms |
9812 KB |
Output is correct |
31 |
Correct |
6 ms |
9812 KB |
Output is correct |
32 |
Correct |
5 ms |
9812 KB |
Output is correct |
33 |
Correct |
6 ms |
9812 KB |
Output is correct |
34 |
Correct |
17 ms |
11348 KB |
Output is correct |
35 |
Correct |
92 ms |
23488 KB |
Output is correct |
36 |
Correct |
101 ms |
23372 KB |
Output is correct |
37 |
Correct |
81 ms |
23228 KB |
Output is correct |
38 |
Correct |
77 ms |
22776 KB |
Output is correct |
39 |
Correct |
77 ms |
22648 KB |
Output is correct |
40 |
Correct |
78 ms |
21424 KB |
Output is correct |
41 |
Correct |
108 ms |
23540 KB |
Output is correct |
42 |
Correct |
81 ms |
23616 KB |
Output is correct |
43 |
Correct |
64 ms |
21064 KB |
Output is correct |
44 |
Correct |
85 ms |
22552 KB |
Output is correct |
45 |
Correct |
106 ms |
21316 KB |
Output is correct |
46 |
Correct |
101 ms |
23428 KB |
Output is correct |
47 |
Correct |
79 ms |
22244 KB |
Output is correct |
48 |
Correct |
79 ms |
21612 KB |
Output is correct |
49 |
Correct |
60 ms |
15292 KB |
Output is correct |
50 |
Correct |
48 ms |
14488 KB |
Output is correct |
51 |
Correct |
73 ms |
19528 KB |
Output is correct |
52 |
Correct |
109 ms |
25284 KB |
Output is correct |
53 |
Correct |
116 ms |
24468 KB |
Output is correct |
54 |
Correct |
136 ms |
27084 KB |
Output is correct |
55 |
Correct |
66 ms |
21040 KB |
Output is correct |
56 |
Correct |
102 ms |
23972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
7 ms |
9812 KB |
Output is correct |
6 |
Correct |
7 ms |
9748 KB |
Output is correct |
7 |
Correct |
6 ms |
9812 KB |
Output is correct |
8 |
Correct |
6 ms |
9812 KB |
Output is correct |
9 |
Correct |
62 ms |
20612 KB |
Output is correct |
10 |
Correct |
78 ms |
22936 KB |
Output is correct |
11 |
Correct |
81 ms |
22600 KB |
Output is correct |
12 |
Correct |
82 ms |
23500 KB |
Output is correct |
13 |
Correct |
66 ms |
20932 KB |
Output is correct |
14 |
Correct |
83 ms |
20636 KB |
Output is correct |
15 |
Correct |
157 ms |
24932 KB |
Output is correct |
16 |
Correct |
153 ms |
24616 KB |
Output is correct |
17 |
Correct |
157 ms |
25320 KB |
Output is correct |
18 |
Correct |
126 ms |
22768 KB |
Output is correct |
19 |
Correct |
117 ms |
22888 KB |
Output is correct |
20 |
Correct |
121 ms |
23308 KB |
Output is correct |
21 |
Correct |
116 ms |
23264 KB |
Output is correct |
22 |
Correct |
121 ms |
23240 KB |
Output is correct |
23 |
Correct |
122 ms |
23344 KB |
Output is correct |
24 |
Correct |
108 ms |
22896 KB |
Output is correct |
25 |
Correct |
132 ms |
23256 KB |
Output is correct |
26 |
Correct |
116 ms |
22792 KB |
Output is correct |
27 |
Correct |
5 ms |
9812 KB |
Output is correct |
28 |
Correct |
5 ms |
9812 KB |
Output is correct |
29 |
Correct |
5 ms |
9812 KB |
Output is correct |
30 |
Correct |
5 ms |
9812 KB |
Output is correct |
31 |
Correct |
6 ms |
9812 KB |
Output is correct |
32 |
Correct |
5 ms |
9852 KB |
Output is correct |
33 |
Correct |
5 ms |
9860 KB |
Output is correct |
34 |
Correct |
5 ms |
9812 KB |
Output is correct |
35 |
Correct |
7 ms |
9780 KB |
Output is correct |
36 |
Correct |
17 ms |
11348 KB |
Output is correct |
37 |
Correct |
92 ms |
23488 KB |
Output is correct |
38 |
Correct |
101 ms |
23372 KB |
Output is correct |
39 |
Correct |
81 ms |
23228 KB |
Output is correct |
40 |
Correct |
77 ms |
22776 KB |
Output is correct |
41 |
Correct |
77 ms |
22648 KB |
Output is correct |
42 |
Correct |
78 ms |
21424 KB |
Output is correct |
43 |
Correct |
108 ms |
23540 KB |
Output is correct |
44 |
Correct |
81 ms |
23616 KB |
Output is correct |
45 |
Correct |
64 ms |
21064 KB |
Output is correct |
46 |
Correct |
85 ms |
22552 KB |
Output is correct |
47 |
Correct |
18 ms |
11608 KB |
Output is correct |
48 |
Correct |
150 ms |
26028 KB |
Output is correct |
49 |
Correct |
196 ms |
25828 KB |
Output is correct |
50 |
Correct |
151 ms |
25824 KB |
Output is correct |
51 |
Correct |
148 ms |
25500 KB |
Output is correct |
52 |
Correct |
152 ms |
24684 KB |
Output is correct |
53 |
Correct |
117 ms |
21752 KB |
Output is correct |
54 |
Correct |
163 ms |
26668 KB |
Output is correct |
55 |
Correct |
160 ms |
26184 KB |
Output is correct |
56 |
Correct |
131 ms |
23424 KB |
Output is correct |
57 |
Correct |
151 ms |
25660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
7 ms |
9812 KB |
Output is correct |
7 |
Correct |
7 ms |
9748 KB |
Output is correct |
8 |
Correct |
6 ms |
9812 KB |
Output is correct |
9 |
Correct |
6 ms |
9812 KB |
Output is correct |
10 |
Correct |
62 ms |
20612 KB |
Output is correct |
11 |
Correct |
78 ms |
22936 KB |
Output is correct |
12 |
Correct |
81 ms |
22600 KB |
Output is correct |
13 |
Correct |
82 ms |
23500 KB |
Output is correct |
14 |
Correct |
66 ms |
20932 KB |
Output is correct |
15 |
Correct |
83 ms |
20636 KB |
Output is correct |
16 |
Correct |
157 ms |
24932 KB |
Output is correct |
17 |
Correct |
153 ms |
24616 KB |
Output is correct |
18 |
Correct |
157 ms |
25320 KB |
Output is correct |
19 |
Correct |
126 ms |
22768 KB |
Output is correct |
20 |
Correct |
117 ms |
22888 KB |
Output is correct |
21 |
Correct |
121 ms |
23308 KB |
Output is correct |
22 |
Correct |
116 ms |
23264 KB |
Output is correct |
23 |
Correct |
121 ms |
23240 KB |
Output is correct |
24 |
Correct |
122 ms |
23344 KB |
Output is correct |
25 |
Correct |
108 ms |
22896 KB |
Output is correct |
26 |
Correct |
132 ms |
23256 KB |
Output is correct |
27 |
Correct |
116 ms |
22792 KB |
Output is correct |
28 |
Correct |
5 ms |
9812 KB |
Output is correct |
29 |
Correct |
5 ms |
9812 KB |
Output is correct |
30 |
Correct |
5 ms |
9812 KB |
Output is correct |
31 |
Correct |
5 ms |
9812 KB |
Output is correct |
32 |
Correct |
6 ms |
9812 KB |
Output is correct |
33 |
Correct |
5 ms |
9852 KB |
Output is correct |
34 |
Correct |
5 ms |
9860 KB |
Output is correct |
35 |
Correct |
5 ms |
9812 KB |
Output is correct |
36 |
Correct |
7 ms |
9780 KB |
Output is correct |
37 |
Correct |
17 ms |
11348 KB |
Output is correct |
38 |
Correct |
92 ms |
23488 KB |
Output is correct |
39 |
Correct |
101 ms |
23372 KB |
Output is correct |
40 |
Correct |
81 ms |
23228 KB |
Output is correct |
41 |
Correct |
77 ms |
22776 KB |
Output is correct |
42 |
Correct |
77 ms |
22648 KB |
Output is correct |
43 |
Correct |
78 ms |
21424 KB |
Output is correct |
44 |
Correct |
108 ms |
23540 KB |
Output is correct |
45 |
Correct |
81 ms |
23616 KB |
Output is correct |
46 |
Correct |
64 ms |
21064 KB |
Output is correct |
47 |
Correct |
85 ms |
22552 KB |
Output is correct |
48 |
Correct |
18 ms |
11608 KB |
Output is correct |
49 |
Correct |
150 ms |
26028 KB |
Output is correct |
50 |
Correct |
196 ms |
25828 KB |
Output is correct |
51 |
Correct |
151 ms |
25824 KB |
Output is correct |
52 |
Correct |
148 ms |
25500 KB |
Output is correct |
53 |
Correct |
152 ms |
24684 KB |
Output is correct |
54 |
Correct |
117 ms |
21752 KB |
Output is correct |
55 |
Correct |
163 ms |
26668 KB |
Output is correct |
56 |
Correct |
160 ms |
26184 KB |
Output is correct |
57 |
Correct |
131 ms |
23424 KB |
Output is correct |
58 |
Correct |
151 ms |
25660 KB |
Output is correct |
59 |
Correct |
67 ms |
15252 KB |
Output is correct |
60 |
Correct |
156 ms |
26196 KB |
Output is correct |
61 |
Correct |
172 ms |
25864 KB |
Output is correct |
62 |
Correct |
152 ms |
26688 KB |
Output is correct |
63 |
Correct |
134 ms |
24188 KB |
Output is correct |
64 |
Correct |
7 ms |
9812 KB |
Output is correct |
65 |
Correct |
6 ms |
9812 KB |
Output is correct |
66 |
Correct |
7 ms |
9732 KB |
Output is correct |
67 |
Correct |
6 ms |
9812 KB |
Output is correct |
68 |
Correct |
5 ms |
9756 KB |
Output is correct |
69 |
Correct |
5 ms |
9812 KB |
Output is correct |
70 |
Correct |
6 ms |
9812 KB |
Output is correct |
71 |
Correct |
6 ms |
9812 KB |
Output is correct |
72 |
Correct |
5 ms |
9812 KB |
Output is correct |
73 |
Correct |
6 ms |
9812 KB |
Output is correct |
74 |
Correct |
106 ms |
21316 KB |
Output is correct |
75 |
Correct |
101 ms |
23428 KB |
Output is correct |
76 |
Correct |
79 ms |
22244 KB |
Output is correct |
77 |
Correct |
79 ms |
21612 KB |
Output is correct |
78 |
Correct |
60 ms |
15292 KB |
Output is correct |
79 |
Correct |
48 ms |
14488 KB |
Output is correct |
80 |
Correct |
73 ms |
19528 KB |
Output is correct |
81 |
Correct |
109 ms |
25284 KB |
Output is correct |
82 |
Correct |
116 ms |
24468 KB |
Output is correct |
83 |
Correct |
136 ms |
27084 KB |
Output is correct |
84 |
Correct |
66 ms |
21040 KB |
Output is correct |
85 |
Correct |
102 ms |
23972 KB |
Output is correct |
86 |
Correct |
53 ms |
14528 KB |
Output is correct |
87 |
Correct |
217 ms |
25632 KB |
Output is correct |
88 |
Correct |
161 ms |
25744 KB |
Output is correct |
89 |
Correct |
144 ms |
23516 KB |
Output is correct |
90 |
Correct |
125 ms |
17748 KB |
Output is correct |
91 |
Correct |
122 ms |
18296 KB |
Output is correct |
92 |
Correct |
140 ms |
22260 KB |
Output is correct |
93 |
Correct |
214 ms |
27356 KB |
Output is correct |
94 |
Correct |
180 ms |
26844 KB |
Output is correct |
95 |
Correct |
205 ms |
28712 KB |
Output is correct |
96 |
Correct |
152 ms |
23660 KB |
Output is correct |
97 |
Correct |
163 ms |
25568 KB |
Output is correct |