#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int i = (a); i <= (b); i++)
#define repa(i,a,b) for (int i = (a); i >= (b); i--)
#define lli long long int
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " << a << ", "
#define MAX 100000
#define w first
#define u second.first
#define v second.second
struct x{
lli padre;
lli flag;
lli prof;
lli val;
lli l;
lli r;
};
lli dis[MAX+2];
lli n,m,a,b,res;
vector< pair<lli, pair<lli,lli> > > orden;
x uf[MAX+2];
lli dfs(lli pos) {
if (dis[pos] > 0) return dis[pos]+1;
if (uf[pos].padre == pos) {
dis[pos] = 1;
return 2;
}
dis[pos] = dfs(uf[pos].padre);
return dis[pos]+1;
}
void init(int N, int M,std::vector<int> U, std::vector<int> V, std::vector<int> W) {
n = N;
m = M;
rep(i,0,m-1) orden.push_back({W[i], {U[i]+1,V[i]+1} });
sort(orden.begin(), orden.end());
rep(i,1,n) uf[i] = {i,0,1,0,i,i};
for (auto act : orden) {
a = uf[act.u].padre;
while (a != uf[a].padre) a = uf[a].padre;
b = uf[act.v].padre;
while (b != uf[b].padre) b = uf[b].padre;
if (a == b) {
if (uf[a].flag) continue;
uf[a].flag = act.w;
}
else {
if (uf[b].prof > uf[a].prof) {
swap(a,b);
swap(act.u, act.v);
}
uf[b].val = act.w;
uf[b].padre = a;
if (uf[b].prof == uf[a].prof) uf[a].prof++;
if (uf[a].flag) continue;
if (uf[b].flag) uf[a].flag = act.w;
else {
if (uf[a].l == act.u && uf[b].l == act.v) uf[a].l = uf[b].r;
else if (uf[a].l == act.u && uf[b].r == act.v) uf[a].l = uf[b].l;
else if (uf[a].r == act.u && uf[b].l == act.v) uf[a].r = uf[b].r;
else if (uf[a].r == act.u && uf[b].r == act.v) uf[a].r = uf[b].l;
else {
uf[a].flag = act.w;
}
}
}
}
rep(i,1,n) if (dis[i] == 0) {
if (uf[i].padre == i) dis[i] = 1;
else dis[i] = dfs(uf[i].padre);
}
}
int getMinimumFuelCapacity(int X, int Y) {
X++;
Y++;
if (dis[X] < dis[Y]) swap(X,Y);
a = dis[X] - dis[Y];
res = -1;
rep(i,1,a) {
res = max(res,uf[X].val);
X = uf[X].padre;
}
while (X != Y) {
res = max(res,uf[X].val);
X = uf[X].padre;
res = max(res,uf[Y].val);
Y = uf[Y].padre;
}
while (uf[X].flag == 0) {
res = max(res,uf[X].val);
if (X != uf[X].padre) X = uf[X].padre;
else return -1;
}
res = max(res,uf[X].flag);
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
452 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
38 ms |
10044 KB |
Output is correct |
10 |
Correct |
45 ms |
12120 KB |
Output is correct |
11 |
Correct |
55 ms |
12008 KB |
Output is correct |
12 |
Correct |
50 ms |
12716 KB |
Output is correct |
13 |
Correct |
50 ms |
12724 KB |
Output is correct |
14 |
Correct |
43 ms |
10416 KB |
Output is correct |
15 |
Correct |
114 ms |
16204 KB |
Output is correct |
16 |
Correct |
121 ms |
16024 KB |
Output is correct |
17 |
Correct |
140 ms |
16580 KB |
Output is correct |
18 |
Correct |
108 ms |
16628 KB |
Output is correct |
19 |
Correct |
58 ms |
7504 KB |
Output is correct |
20 |
Correct |
114 ms |
17300 KB |
Output is correct |
21 |
Correct |
153 ms |
17232 KB |
Output is correct |
22 |
Correct |
119 ms |
18072 KB |
Output is correct |
23 |
Correct |
106 ms |
18056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
116 ms |
14732 KB |
Output is correct |
4 |
Correct |
98 ms |
15084 KB |
Output is correct |
5 |
Correct |
102 ms |
15064 KB |
Output is correct |
6 |
Correct |
111 ms |
15152 KB |
Output is correct |
7 |
Correct |
95 ms |
15112 KB |
Output is correct |
8 |
Correct |
94 ms |
14696 KB |
Output is correct |
9 |
Correct |
105 ms |
15032 KB |
Output is correct |
10 |
Correct |
93 ms |
14788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
452 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
452 KB |
Output is correct |
11 |
Correct |
1 ms |
448 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
456 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
452 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
452 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
448 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
452 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
38 ms |
10044 KB |
Output is correct |
11 |
Correct |
45 ms |
12120 KB |
Output is correct |
12 |
Correct |
55 ms |
12008 KB |
Output is correct |
13 |
Correct |
50 ms |
12716 KB |
Output is correct |
14 |
Correct |
50 ms |
12724 KB |
Output is correct |
15 |
Correct |
1 ms |
452 KB |
Output is correct |
16 |
Correct |
1 ms |
448 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
456 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
452 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
452 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
448 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
6 ms |
2000 KB |
Output is correct |
35 |
Correct |
45 ms |
12372 KB |
Output is correct |
36 |
Correct |
50 ms |
12344 KB |
Output is correct |
37 |
Correct |
58 ms |
12340 KB |
Output is correct |
38 |
Correct |
53 ms |
12220 KB |
Output is correct |
39 |
Correct |
48 ms |
12192 KB |
Output is correct |
40 |
Correct |
41 ms |
11152 KB |
Output is correct |
41 |
Correct |
52 ms |
12604 KB |
Output is correct |
42 |
Correct |
46 ms |
12424 KB |
Output is correct |
43 |
Correct |
57 ms |
12328 KB |
Output is correct |
44 |
Correct |
78 ms |
12708 KB |
Output is correct |
45 |
Correct |
91 ms |
14672 KB |
Output is correct |
46 |
Correct |
45 ms |
12344 KB |
Output is correct |
47 |
Correct |
46 ms |
12340 KB |
Output is correct |
48 |
Correct |
49 ms |
12936 KB |
Output is correct |
49 |
Correct |
63 ms |
13016 KB |
Output is correct |
50 |
Correct |
51 ms |
8744 KB |
Output is correct |
51 |
Correct |
81 ms |
12664 KB |
Output is correct |
52 |
Correct |
86 ms |
16880 KB |
Output is correct |
53 |
Correct |
86 ms |
17988 KB |
Output is correct |
54 |
Correct |
134 ms |
19124 KB |
Output is correct |
55 |
Correct |
52 ms |
12332 KB |
Output is correct |
56 |
Correct |
89 ms |
17552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
452 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
38 ms |
10044 KB |
Output is correct |
10 |
Correct |
45 ms |
12120 KB |
Output is correct |
11 |
Correct |
55 ms |
12008 KB |
Output is correct |
12 |
Correct |
50 ms |
12716 KB |
Output is correct |
13 |
Correct |
50 ms |
12724 KB |
Output is correct |
14 |
Correct |
43 ms |
10416 KB |
Output is correct |
15 |
Correct |
114 ms |
16204 KB |
Output is correct |
16 |
Correct |
121 ms |
16024 KB |
Output is correct |
17 |
Correct |
140 ms |
16580 KB |
Output is correct |
18 |
Correct |
108 ms |
16628 KB |
Output is correct |
19 |
Correct |
116 ms |
14732 KB |
Output is correct |
20 |
Correct |
98 ms |
15084 KB |
Output is correct |
21 |
Correct |
102 ms |
15064 KB |
Output is correct |
22 |
Correct |
111 ms |
15152 KB |
Output is correct |
23 |
Correct |
95 ms |
15112 KB |
Output is correct |
24 |
Correct |
94 ms |
14696 KB |
Output is correct |
25 |
Correct |
105 ms |
15032 KB |
Output is correct |
26 |
Correct |
93 ms |
14788 KB |
Output is correct |
27 |
Correct |
1 ms |
452 KB |
Output is correct |
28 |
Correct |
1 ms |
448 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
320 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
456 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
2 ms |
452 KB |
Output is correct |
36 |
Correct |
6 ms |
2000 KB |
Output is correct |
37 |
Correct |
45 ms |
12372 KB |
Output is correct |
38 |
Correct |
50 ms |
12344 KB |
Output is correct |
39 |
Correct |
58 ms |
12340 KB |
Output is correct |
40 |
Correct |
53 ms |
12220 KB |
Output is correct |
41 |
Correct |
48 ms |
12192 KB |
Output is correct |
42 |
Correct |
41 ms |
11152 KB |
Output is correct |
43 |
Correct |
52 ms |
12604 KB |
Output is correct |
44 |
Correct |
46 ms |
12424 KB |
Output is correct |
45 |
Correct |
57 ms |
12328 KB |
Output is correct |
46 |
Correct |
78 ms |
12708 KB |
Output is correct |
47 |
Correct |
12 ms |
2384 KB |
Output is correct |
48 |
Correct |
122 ms |
17172 KB |
Output is correct |
49 |
Correct |
149 ms |
17164 KB |
Output is correct |
50 |
Correct |
136 ms |
17068 KB |
Output is correct |
51 |
Correct |
127 ms |
17036 KB |
Output is correct |
52 |
Correct |
129 ms |
16424 KB |
Output is correct |
53 |
Correct |
137 ms |
14252 KB |
Output is correct |
54 |
Correct |
129 ms |
18056 KB |
Output is correct |
55 |
Correct |
126 ms |
17180 KB |
Output is correct |
56 |
Correct |
125 ms |
17128 KB |
Output is correct |
57 |
Correct |
132 ms |
18056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
452 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
38 ms |
10044 KB |
Output is correct |
11 |
Correct |
45 ms |
12120 KB |
Output is correct |
12 |
Correct |
55 ms |
12008 KB |
Output is correct |
13 |
Correct |
50 ms |
12716 KB |
Output is correct |
14 |
Correct |
50 ms |
12724 KB |
Output is correct |
15 |
Correct |
43 ms |
10416 KB |
Output is correct |
16 |
Correct |
114 ms |
16204 KB |
Output is correct |
17 |
Correct |
121 ms |
16024 KB |
Output is correct |
18 |
Correct |
140 ms |
16580 KB |
Output is correct |
19 |
Correct |
108 ms |
16628 KB |
Output is correct |
20 |
Correct |
116 ms |
14732 KB |
Output is correct |
21 |
Correct |
98 ms |
15084 KB |
Output is correct |
22 |
Correct |
102 ms |
15064 KB |
Output is correct |
23 |
Correct |
111 ms |
15152 KB |
Output is correct |
24 |
Correct |
95 ms |
15112 KB |
Output is correct |
25 |
Correct |
94 ms |
14696 KB |
Output is correct |
26 |
Correct |
105 ms |
15032 KB |
Output is correct |
27 |
Correct |
93 ms |
14788 KB |
Output is correct |
28 |
Correct |
1 ms |
452 KB |
Output is correct |
29 |
Correct |
1 ms |
448 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
320 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
456 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
2 ms |
452 KB |
Output is correct |
37 |
Correct |
6 ms |
2000 KB |
Output is correct |
38 |
Correct |
45 ms |
12372 KB |
Output is correct |
39 |
Correct |
50 ms |
12344 KB |
Output is correct |
40 |
Correct |
58 ms |
12340 KB |
Output is correct |
41 |
Correct |
53 ms |
12220 KB |
Output is correct |
42 |
Correct |
48 ms |
12192 KB |
Output is correct |
43 |
Correct |
41 ms |
11152 KB |
Output is correct |
44 |
Correct |
52 ms |
12604 KB |
Output is correct |
45 |
Correct |
46 ms |
12424 KB |
Output is correct |
46 |
Correct |
57 ms |
12328 KB |
Output is correct |
47 |
Correct |
78 ms |
12708 KB |
Output is correct |
48 |
Correct |
12 ms |
2384 KB |
Output is correct |
49 |
Correct |
122 ms |
17172 KB |
Output is correct |
50 |
Correct |
149 ms |
17164 KB |
Output is correct |
51 |
Correct |
136 ms |
17068 KB |
Output is correct |
52 |
Correct |
127 ms |
17036 KB |
Output is correct |
53 |
Correct |
129 ms |
16424 KB |
Output is correct |
54 |
Correct |
137 ms |
14252 KB |
Output is correct |
55 |
Correct |
129 ms |
18056 KB |
Output is correct |
56 |
Correct |
126 ms |
17180 KB |
Output is correct |
57 |
Correct |
125 ms |
17128 KB |
Output is correct |
58 |
Correct |
132 ms |
18056 KB |
Output is correct |
59 |
Correct |
58 ms |
7504 KB |
Output is correct |
60 |
Correct |
114 ms |
17300 KB |
Output is correct |
61 |
Correct |
153 ms |
17232 KB |
Output is correct |
62 |
Correct |
119 ms |
18072 KB |
Output is correct |
63 |
Correct |
106 ms |
18056 KB |
Output is correct |
64 |
Correct |
1 ms |
340 KB |
Output is correct |
65 |
Correct |
1 ms |
340 KB |
Output is correct |
66 |
Correct |
1 ms |
452 KB |
Output is correct |
67 |
Correct |
1 ms |
468 KB |
Output is correct |
68 |
Correct |
1 ms |
340 KB |
Output is correct |
69 |
Correct |
1 ms |
468 KB |
Output is correct |
70 |
Correct |
1 ms |
468 KB |
Output is correct |
71 |
Correct |
1 ms |
448 KB |
Output is correct |
72 |
Correct |
1 ms |
340 KB |
Output is correct |
73 |
Correct |
1 ms |
468 KB |
Output is correct |
74 |
Correct |
91 ms |
14672 KB |
Output is correct |
75 |
Correct |
45 ms |
12344 KB |
Output is correct |
76 |
Correct |
46 ms |
12340 KB |
Output is correct |
77 |
Correct |
49 ms |
12936 KB |
Output is correct |
78 |
Correct |
63 ms |
13016 KB |
Output is correct |
79 |
Correct |
51 ms |
8744 KB |
Output is correct |
80 |
Correct |
81 ms |
12664 KB |
Output is correct |
81 |
Correct |
86 ms |
16880 KB |
Output is correct |
82 |
Correct |
86 ms |
17988 KB |
Output is correct |
83 |
Correct |
134 ms |
19124 KB |
Output is correct |
84 |
Correct |
52 ms |
12332 KB |
Output is correct |
85 |
Correct |
89 ms |
17552 KB |
Output is correct |
86 |
Correct |
41 ms |
6412 KB |
Output is correct |
87 |
Correct |
130 ms |
17068 KB |
Output is correct |
88 |
Correct |
143 ms |
17076 KB |
Output is correct |
89 |
Correct |
113 ms |
16916 KB |
Output is correct |
90 |
Correct |
153 ms |
16820 KB |
Output is correct |
91 |
Correct |
161 ms |
16492 KB |
Output is correct |
92 |
Correct |
119 ms |
17328 KB |
Output is correct |
93 |
Correct |
186 ms |
21328 KB |
Output is correct |
94 |
Correct |
172 ms |
22412 KB |
Output is correct |
95 |
Correct |
178 ms |
22992 KB |
Output is correct |
96 |
Correct |
108 ms |
17172 KB |
Output is correct |
97 |
Correct |
140 ms |
20028 KB |
Output is correct |