#include <bits/stdc++.h>
#include "swap.h"
using namespace std;
#define mp(a,b) make_pair(a,b)
#define ff first
#define setp(a) setprecision(a)<<fixed
#define ss second
#define fori(v) for(ll i=0; i<v; i++)
#define forj(v) for(ll j=0; j<v; j++)
#define fork(v) for(ll k=0; k<v; k++)
#define forl(v) for(ll l=0; l<v; l++)
#define fort(v) for(ll t=0; t<v; t++)
#define forz(v) for(ll z=0; z<v; z++)
#define forx(v) for(ll x=0; x<v; x++)
#define fory(v) for(ll y=0; y<v; y++)
#define ll long long
#define ld long double
#define pb(a) push_back(a)
const ll INF = 0x3f3f3f3f;
const ll inf = 1*pow(10,9) + 100;
ll modulo = pow(10,9) + 7;
#define MAX (int)(pow(10,5) + 10)
namespace dsu{
int aid[MAX];
vector<int> dsu[MAX];
vector<pair<int,int> > nodeBelongs[MAX]; // nodes history of components
int when[MAX]; // when did a component become 2 tracable
int deg[MAX]; // degree of nodes
void init(int n){
fori(n){
aid[i] = i, dsu[i].clear(), dsu[i].push_back(i),
when[i] = inf, deg[i] = 0, nodeBelongs[i].pb(mp(i, 0));
}
}
bool join(int a,int b, int w){ // 1 - if joined, 0 - if they were already joined
deg[a]++;
deg[b]++;
int aid1 = aid[a], aid2 = aid[b];
if(deg[a] > 2 || deg[b] > 2){
when[aid1] = min(when[aid1], w);
when[aid2] = min(when[aid2], w);
}
if(dsu[aid2].size() > dsu[aid1].size())
swap(aid1,aid2);
if(aid1==aid2){
when[aid1] = min(when[aid1], w);
return 0;
}
if(when[aid2]!=inf && when[aid1] == inf){
when[aid1] = w;
}
for(auto hd : dsu[aid2]){
aid[hd] = aid1;
dsu[aid1].push_back(hd);
nodeBelongs[hd].pb(mp(aid1, w));
}
dsu[aid2].clear();
return 1;
}
};
bool srt(vector<ll>& a, vector<ll>& b){
return a[2] < b[2];
}
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
ll n = N, m = M;
vector<vector<ll> > edges;
fori(m){
ll a = U[i], b = V[i], w = W[i];
if(a > b){
swap(a, b);
}
edges.pb(vector<ll>({a,b,w}));
}
sort(edges.begin(), edges.end(), srt);
set<pair<ll,ll> > seen;
dsu::init(n);
for(auto& el : edges){
dsu::join(el[0], el[1], el[2]);
}
}
int getMinimumFuelCapacity(int X, int Y) {
ll x = X, y = Y;
if(dsu::when[dsu::aid[x]] == inf ){
return -1;
}
ll cmp1 = x, cmp2 = y;
ll pt1 = 0, pt2 = 0;
int ans = 0;
while( (cmp1 != cmp2) || (dsu::when[cmp1] == inf) ){
if(pt1 == (ll)dsu::nodeBelongs[x].size() ){
cmp2 = dsu::nodeBelongs[y][pt2].ff;
ans = max(ans, dsu::nodeBelongs[y][pt2].ss);
++pt2;
}
else if(pt2 == (ll)dsu::nodeBelongs[y].size()){
cmp1 = dsu::nodeBelongs[x][pt1].ff;
ans = max(ans, dsu::nodeBelongs[x][pt1].ss);
++pt1;
}
else if(dsu::nodeBelongs[x][pt1].ss <= dsu::nodeBelongs[y][pt2].ss){
cmp1 = dsu::nodeBelongs[x][pt1].ff;
ans = max(ans, dsu::nodeBelongs[x][pt1].ss);
++pt1;
}
else{
cmp2 = dsu::nodeBelongs[y][pt2].ff;
ans = max(ans, dsu::nodeBelongs[y][pt2].ss);
++pt2;
}
}
ans = max(ans, dsu::when[cmp1]);
return ans;
}
/*
void deal(){
int n, m;
cin>>n>>m;
vector<int> u(m), v(m), w(m);
fori(m){
cin>>u[i];
}
fori(m){
cin>>v[i];
}
fori(m){
cin>>w[i];
}
init(n, m, u, v, w);
ll q;
cin>>q;
forl(q){
ll ui, vi;
cin>>ui>>vi;
cout<<getMinimumFuelCapacity(ui, vi)<<'\n';
}
}
int main(){
cin.tie(0);
ios_base::sync_with_stdio(0);
deal();
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4996 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
5068 KB |
Output is correct |
5 |
Correct |
4 ms |
5196 KB |
Output is correct |
6 |
Correct |
4 ms |
5196 KB |
Output is correct |
7 |
Correct |
4 ms |
5196 KB |
Output is correct |
8 |
Correct |
4 ms |
5196 KB |
Output is correct |
9 |
Correct |
186 ms |
26100 KB |
Output is correct |
10 |
Correct |
265 ms |
30316 KB |
Output is correct |
11 |
Correct |
251 ms |
29936 KB |
Output is correct |
12 |
Correct |
270 ms |
31272 KB |
Output is correct |
13 |
Correct |
169 ms |
23804 KB |
Output is correct |
14 |
Correct |
188 ms |
26152 KB |
Output is correct |
15 |
Correct |
334 ms |
32240 KB |
Output is correct |
16 |
Correct |
329 ms |
31564 KB |
Output is correct |
17 |
Correct |
345 ms |
33080 KB |
Output is correct |
18 |
Correct |
224 ms |
25536 KB |
Output is correct |
19 |
Correct |
95 ms |
12608 KB |
Output is correct |
20 |
Correct |
430 ms |
32260 KB |
Output is correct |
21 |
Correct |
387 ms |
31296 KB |
Output is correct |
22 |
Correct |
490 ms |
33140 KB |
Output is correct |
23 |
Correct |
301 ms |
25460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4996 KB |
Output is correct |
3 |
Correct |
269 ms |
22368 KB |
Output is correct |
4 |
Correct |
266 ms |
23220 KB |
Output is correct |
5 |
Correct |
252 ms |
22640 KB |
Output is correct |
6 |
Correct |
278 ms |
23152 KB |
Output is correct |
7 |
Correct |
244 ms |
22996 KB |
Output is correct |
8 |
Correct |
277 ms |
22244 KB |
Output is correct |
9 |
Correct |
230 ms |
22940 KB |
Output is correct |
10 |
Correct |
238 ms |
22128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4996 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
5068 KB |
Output is correct |
5 |
Correct |
4 ms |
5196 KB |
Output is correct |
6 |
Correct |
4 ms |
5196 KB |
Output is correct |
7 |
Correct |
4 ms |
5196 KB |
Output is correct |
8 |
Correct |
4 ms |
5196 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 ms |
5196 KB |
Output is correct |
11 |
Correct |
5 ms |
5196 KB |
Output is correct |
12 |
Correct |
6 ms |
5196 KB |
Output is correct |
13 |
Correct |
5 ms |
5196 KB |
Output is correct |
14 |
Correct |
4 ms |
5136 KB |
Output is correct |
15 |
Correct |
6 ms |
5196 KB |
Output is correct |
16 |
Correct |
6 ms |
5196 KB |
Output is correct |
17 |
Correct |
5 ms |
5196 KB |
Output is correct |
18 |
Correct |
6 ms |
5196 KB |
Output is correct |
19 |
Correct |
4 ms |
5068 KB |
Output is correct |
20 |
Correct |
4 ms |
5196 KB |
Output is correct |
21 |
Correct |
4 ms |
5196 KB |
Output is correct |
22 |
Correct |
5 ms |
5196 KB |
Output is correct |
23 |
Correct |
5 ms |
5140 KB |
Output is correct |
24 |
Correct |
5 ms |
5324 KB |
Output is correct |
25 |
Correct |
5 ms |
5196 KB |
Output is correct |
26 |
Correct |
6 ms |
5272 KB |
Output is correct |
27 |
Correct |
4 ms |
5196 KB |
Output is correct |
28 |
Correct |
6 ms |
5196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4996 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
6 |
Correct |
4 ms |
5196 KB |
Output is correct |
7 |
Correct |
4 ms |
5196 KB |
Output is correct |
8 |
Correct |
4 ms |
5196 KB |
Output is correct |
9 |
Correct |
4 ms |
5196 KB |
Output is correct |
10 |
Correct |
186 ms |
26100 KB |
Output is correct |
11 |
Correct |
265 ms |
30316 KB |
Output is correct |
12 |
Correct |
251 ms |
29936 KB |
Output is correct |
13 |
Correct |
270 ms |
31272 KB |
Output is correct |
14 |
Correct |
169 ms |
23804 KB |
Output is correct |
15 |
Correct |
4 ms |
5196 KB |
Output is correct |
16 |
Correct |
5 ms |
5196 KB |
Output is correct |
17 |
Correct |
6 ms |
5196 KB |
Output is correct |
18 |
Correct |
5 ms |
5196 KB |
Output is correct |
19 |
Correct |
4 ms |
5136 KB |
Output is correct |
20 |
Correct |
6 ms |
5196 KB |
Output is correct |
21 |
Correct |
6 ms |
5196 KB |
Output is correct |
22 |
Correct |
5 ms |
5196 KB |
Output is correct |
23 |
Correct |
6 ms |
5196 KB |
Output is correct |
24 |
Correct |
4 ms |
5068 KB |
Output is correct |
25 |
Correct |
4 ms |
5196 KB |
Output is correct |
26 |
Correct |
4 ms |
5196 KB |
Output is correct |
27 |
Correct |
5 ms |
5196 KB |
Output is correct |
28 |
Correct |
5 ms |
5140 KB |
Output is correct |
29 |
Correct |
5 ms |
5324 KB |
Output is correct |
30 |
Correct |
5 ms |
5196 KB |
Output is correct |
31 |
Correct |
6 ms |
5272 KB |
Output is correct |
32 |
Correct |
4 ms |
5196 KB |
Output is correct |
33 |
Correct |
6 ms |
5196 KB |
Output is correct |
34 |
Correct |
21 ms |
8380 KB |
Output is correct |
35 |
Correct |
264 ms |
31236 KB |
Output is correct |
36 |
Correct |
256 ms |
31040 KB |
Output is correct |
37 |
Correct |
341 ms |
30560 KB |
Output is correct |
38 |
Correct |
245 ms |
29720 KB |
Output is correct |
39 |
Correct |
270 ms |
29328 KB |
Output is correct |
40 |
Correct |
216 ms |
26868 KB |
Output is correct |
41 |
Correct |
256 ms |
31520 KB |
Output is correct |
42 |
Correct |
264 ms |
32832 KB |
Output is correct |
43 |
Correct |
165 ms |
24292 KB |
Output is correct |
44 |
Correct |
219 ms |
30580 KB |
Output is correct |
45 |
Correct |
204 ms |
28504 KB |
Output is correct |
46 |
Correct |
255 ms |
32236 KB |
Output is correct |
47 |
Correct |
207 ms |
28756 KB |
Output is correct |
48 |
Correct |
189 ms |
26612 KB |
Output is correct |
49 |
Correct |
132 ms |
21728 KB |
Output is correct |
50 |
Correct |
86 ms |
17796 KB |
Output is correct |
51 |
Correct |
162 ms |
24820 KB |
Output is correct |
52 |
Correct |
302 ms |
37636 KB |
Output is correct |
53 |
Correct |
285 ms |
35020 KB |
Output is correct |
54 |
Correct |
359 ms |
42696 KB |
Output is correct |
55 |
Correct |
160 ms |
24388 KB |
Output is correct |
56 |
Correct |
289 ms |
33236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4996 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
5068 KB |
Output is correct |
5 |
Correct |
4 ms |
5196 KB |
Output is correct |
6 |
Correct |
4 ms |
5196 KB |
Output is correct |
7 |
Correct |
4 ms |
5196 KB |
Output is correct |
8 |
Correct |
4 ms |
5196 KB |
Output is correct |
9 |
Correct |
186 ms |
26100 KB |
Output is correct |
10 |
Correct |
265 ms |
30316 KB |
Output is correct |
11 |
Correct |
251 ms |
29936 KB |
Output is correct |
12 |
Correct |
270 ms |
31272 KB |
Output is correct |
13 |
Correct |
169 ms |
23804 KB |
Output is correct |
14 |
Correct |
188 ms |
26152 KB |
Output is correct |
15 |
Correct |
334 ms |
32240 KB |
Output is correct |
16 |
Correct |
329 ms |
31564 KB |
Output is correct |
17 |
Correct |
345 ms |
33080 KB |
Output is correct |
18 |
Correct |
224 ms |
25536 KB |
Output is correct |
19 |
Correct |
269 ms |
22368 KB |
Output is correct |
20 |
Correct |
266 ms |
23220 KB |
Output is correct |
21 |
Correct |
252 ms |
22640 KB |
Output is correct |
22 |
Correct |
278 ms |
23152 KB |
Output is correct |
23 |
Correct |
244 ms |
22996 KB |
Output is correct |
24 |
Correct |
277 ms |
22244 KB |
Output is correct |
25 |
Correct |
230 ms |
22940 KB |
Output is correct |
26 |
Correct |
238 ms |
22128 KB |
Output is correct |
27 |
Correct |
4 ms |
5196 KB |
Output is correct |
28 |
Correct |
5 ms |
5196 KB |
Output is correct |
29 |
Correct |
6 ms |
5196 KB |
Output is correct |
30 |
Correct |
5 ms |
5196 KB |
Output is correct |
31 |
Correct |
4 ms |
5136 KB |
Output is correct |
32 |
Correct |
6 ms |
5196 KB |
Output is correct |
33 |
Correct |
6 ms |
5196 KB |
Output is correct |
34 |
Correct |
5 ms |
5196 KB |
Output is correct |
35 |
Correct |
6 ms |
5196 KB |
Output is correct |
36 |
Correct |
21 ms |
8380 KB |
Output is correct |
37 |
Correct |
264 ms |
31236 KB |
Output is correct |
38 |
Correct |
256 ms |
31040 KB |
Output is correct |
39 |
Correct |
341 ms |
30560 KB |
Output is correct |
40 |
Correct |
245 ms |
29720 KB |
Output is correct |
41 |
Correct |
270 ms |
29328 KB |
Output is correct |
42 |
Correct |
216 ms |
26868 KB |
Output is correct |
43 |
Correct |
256 ms |
31520 KB |
Output is correct |
44 |
Correct |
264 ms |
32832 KB |
Output is correct |
45 |
Correct |
165 ms |
24292 KB |
Output is correct |
46 |
Correct |
219 ms |
30580 KB |
Output is correct |
47 |
Correct |
26 ms |
8428 KB |
Output is correct |
48 |
Correct |
410 ms |
36468 KB |
Output is correct |
49 |
Correct |
390 ms |
35700 KB |
Output is correct |
50 |
Correct |
359 ms |
35660 KB |
Output is correct |
51 |
Correct |
386 ms |
35004 KB |
Output is correct |
52 |
Correct |
330 ms |
33136 KB |
Output is correct |
53 |
Correct |
275 ms |
25716 KB |
Output is correct |
54 |
Correct |
387 ms |
36476 KB |
Output is correct |
55 |
Correct |
362 ms |
36984 KB |
Output is correct |
56 |
Correct |
257 ms |
28708 KB |
Output is correct |
57 |
Correct |
356 ms |
34080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4996 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
6 |
Correct |
4 ms |
5196 KB |
Output is correct |
7 |
Correct |
4 ms |
5196 KB |
Output is correct |
8 |
Correct |
4 ms |
5196 KB |
Output is correct |
9 |
Correct |
4 ms |
5196 KB |
Output is correct |
10 |
Correct |
186 ms |
26100 KB |
Output is correct |
11 |
Correct |
265 ms |
30316 KB |
Output is correct |
12 |
Correct |
251 ms |
29936 KB |
Output is correct |
13 |
Correct |
270 ms |
31272 KB |
Output is correct |
14 |
Correct |
169 ms |
23804 KB |
Output is correct |
15 |
Correct |
188 ms |
26152 KB |
Output is correct |
16 |
Correct |
334 ms |
32240 KB |
Output is correct |
17 |
Correct |
329 ms |
31564 KB |
Output is correct |
18 |
Correct |
345 ms |
33080 KB |
Output is correct |
19 |
Correct |
224 ms |
25536 KB |
Output is correct |
20 |
Correct |
269 ms |
22368 KB |
Output is correct |
21 |
Correct |
266 ms |
23220 KB |
Output is correct |
22 |
Correct |
252 ms |
22640 KB |
Output is correct |
23 |
Correct |
278 ms |
23152 KB |
Output is correct |
24 |
Correct |
244 ms |
22996 KB |
Output is correct |
25 |
Correct |
277 ms |
22244 KB |
Output is correct |
26 |
Correct |
230 ms |
22940 KB |
Output is correct |
27 |
Correct |
238 ms |
22128 KB |
Output is correct |
28 |
Correct |
4 ms |
5196 KB |
Output is correct |
29 |
Correct |
5 ms |
5196 KB |
Output is correct |
30 |
Correct |
6 ms |
5196 KB |
Output is correct |
31 |
Correct |
5 ms |
5196 KB |
Output is correct |
32 |
Correct |
4 ms |
5136 KB |
Output is correct |
33 |
Correct |
6 ms |
5196 KB |
Output is correct |
34 |
Correct |
6 ms |
5196 KB |
Output is correct |
35 |
Correct |
5 ms |
5196 KB |
Output is correct |
36 |
Correct |
6 ms |
5196 KB |
Output is correct |
37 |
Correct |
21 ms |
8380 KB |
Output is correct |
38 |
Correct |
264 ms |
31236 KB |
Output is correct |
39 |
Correct |
256 ms |
31040 KB |
Output is correct |
40 |
Correct |
341 ms |
30560 KB |
Output is correct |
41 |
Correct |
245 ms |
29720 KB |
Output is correct |
42 |
Correct |
270 ms |
29328 KB |
Output is correct |
43 |
Correct |
216 ms |
26868 KB |
Output is correct |
44 |
Correct |
256 ms |
31520 KB |
Output is correct |
45 |
Correct |
264 ms |
32832 KB |
Output is correct |
46 |
Correct |
165 ms |
24292 KB |
Output is correct |
47 |
Correct |
219 ms |
30580 KB |
Output is correct |
48 |
Correct |
26 ms |
8428 KB |
Output is correct |
49 |
Correct |
410 ms |
36468 KB |
Output is correct |
50 |
Correct |
390 ms |
35700 KB |
Output is correct |
51 |
Correct |
359 ms |
35660 KB |
Output is correct |
52 |
Correct |
386 ms |
35004 KB |
Output is correct |
53 |
Correct |
330 ms |
33136 KB |
Output is correct |
54 |
Correct |
275 ms |
25716 KB |
Output is correct |
55 |
Correct |
387 ms |
36476 KB |
Output is correct |
56 |
Correct |
362 ms |
36984 KB |
Output is correct |
57 |
Correct |
257 ms |
28708 KB |
Output is correct |
58 |
Correct |
356 ms |
34080 KB |
Output is correct |
59 |
Correct |
95 ms |
12608 KB |
Output is correct |
60 |
Correct |
430 ms |
32260 KB |
Output is correct |
61 |
Correct |
387 ms |
31296 KB |
Output is correct |
62 |
Correct |
490 ms |
33140 KB |
Output is correct |
63 |
Correct |
301 ms |
25460 KB |
Output is correct |
64 |
Correct |
4 ms |
5068 KB |
Output is correct |
65 |
Correct |
4 ms |
5196 KB |
Output is correct |
66 |
Correct |
4 ms |
5196 KB |
Output is correct |
67 |
Correct |
5 ms |
5196 KB |
Output is correct |
68 |
Correct |
5 ms |
5140 KB |
Output is correct |
69 |
Correct |
5 ms |
5324 KB |
Output is correct |
70 |
Correct |
5 ms |
5196 KB |
Output is correct |
71 |
Correct |
6 ms |
5272 KB |
Output is correct |
72 |
Correct |
4 ms |
5196 KB |
Output is correct |
73 |
Correct |
6 ms |
5196 KB |
Output is correct |
74 |
Correct |
204 ms |
28504 KB |
Output is correct |
75 |
Correct |
255 ms |
32236 KB |
Output is correct |
76 |
Correct |
207 ms |
28756 KB |
Output is correct |
77 |
Correct |
189 ms |
26612 KB |
Output is correct |
78 |
Correct |
132 ms |
21728 KB |
Output is correct |
79 |
Correct |
86 ms |
17796 KB |
Output is correct |
80 |
Correct |
162 ms |
24820 KB |
Output is correct |
81 |
Correct |
302 ms |
37636 KB |
Output is correct |
82 |
Correct |
285 ms |
35020 KB |
Output is correct |
83 |
Correct |
359 ms |
42696 KB |
Output is correct |
84 |
Correct |
160 ms |
24388 KB |
Output is correct |
85 |
Correct |
289 ms |
33236 KB |
Output is correct |
86 |
Correct |
76 ms |
14320 KB |
Output is correct |
87 |
Correct |
349 ms |
35268 KB |
Output is correct |
88 |
Correct |
394 ms |
35404 KB |
Output is correct |
89 |
Correct |
276 ms |
29424 KB |
Output is correct |
90 |
Correct |
194 ms |
25452 KB |
Output is correct |
91 |
Correct |
218 ms |
25100 KB |
Output is correct |
92 |
Correct |
275 ms |
28148 KB |
Output is correct |
93 |
Correct |
413 ms |
40552 KB |
Output is correct |
94 |
Correct |
392 ms |
38976 KB |
Output is correct |
95 |
Correct |
512 ms |
46996 KB |
Output is correct |
96 |
Correct |
291 ms |
28592 KB |
Output is correct |
97 |
Correct |
316 ms |
33760 KB |
Output is correct |