#include <bits/stdc++.h>
using namespace std;
#include "swap.h"
//#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
vector <int> waiting[200005];
int par[200005], val[200005], deg[200005], good[200005], up[20][200005], dep[200005];
int cnt = 1;
pi adj[200005];
int getr(int x){return par[x] == x ? x : par[x] = getr(par[x]);}
void merge(int a, int b, int c){
a = getr(a); b = getr(b);
if(a == b)return;
par[a] = par[b] = up[0][a] = up[0][b] = par[cnt];
if((int)waiting[a].size() < (int)waiting[b].size())swap(a, b);
swap(waiting[cnt], waiting[a]);
for(auto j : waiting[b])waiting[cnt].push_back(j);
val[cnt] = c;
adj[cnt] = {a, b};
cnt++;
}
int par2[200005];
int g2(int x){return par2[x] == x ? x : par2[x] = g2(par2[x]);}
void m2(int a, int b){par2[g2(b)] = g2(a);}
void dfs(int x, int d){
//cout << x << '\n';
dep[x] = d;
if(adj[x].fi == adj[x].se)return;
dfs(adj[x].fi, d + 1);
dfs(adj[x].se, d + 1);
}
int lca(int u, int v){
if(dep[u] > dep[v])swap(u, v);
int df = dep[v] -dep[u];
for(int i = 0; i <= 19; i++)if(df >> i & 1)v = up[i][v];
if(u ==v )return u;
for(int i = 19; 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,
std::vector<int> U, std::vector<int> V, std::vector<int> W) {
vector <pii> bb;
cnt = N;
for(int i = 0; i < 2 * N; i++)par[i] = par2[i] = i;
for(int i = 0; i < M; i++)bb.push_back({W[i], {U[i], V[i]}});
sort(bb.begin(), bb.end());
for(auto i : bb){
int a = i.se.fi, b = i.se.se, w = i.fi;
if(g2(a) == g2(b)){
good[getr(a)] = 1;
good[getr(b)] = 1;
queue <int> q;
q.push(getr(a));
q.push(getr(b));
merge(a, b, w);
while(!q.empty()){
int x = q.front(); q.pop();
x = getr(x);
vector <int> aa = waiting[x];
waiting[x].clear();
for(auto j : aa){
if(getr(j) != getr(x)){
merge(x, j, w);
q.push(j);
}
}
}
}
else{
m2(a, b);
if(good[getr(a)] || good[getr(b)])merge(a, b, w);
else{
deg[a]++; deg[b]++;
if(max(deg[a], deg[b]) >= 3){
good[getr(a)] = good[getr(b)] = 1;
queue <int> q;
q.push(getr(a));
q.push(getr(b));
merge(a, b, w);
while(!q.empty()){
int x = q.front(); q.pop();
x = getr(x);
vector <int> aa = waiting[x];
waiting[x].clear();
for(auto j : aa){
if(getr(j) != getr(x)){
merge(x, j, w);
q.push(j);
}
}
}
}
else waiting[getr(a)].push_back(getr(b)), waiting[getr(b)].push_back(getr(a));
}
}
}
for(int i = cnt - 1; i >= N; i--)if(!dep[i])dfs(i, 1);
for(int i = 1; i <= 19; i++)for(int j = 0; j < 2 * N; j++)up[i][j] = up[i - 1][up[i - 1][j]];
}
int getMinimumFuelCapacity(int X, int Y) {
if(getr(X) != getr(Y))return -1;
//cout << lca(X, Y) << ' ';
return val[lca(X, Y)];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20696 KB |
Output is correct |
2 |
Correct |
4 ms |
20572 KB |
Output is correct |
3 |
Correct |
4 ms |
20572 KB |
Output is correct |
4 |
Correct |
4 ms |
20572 KB |
Output is correct |
5 |
Correct |
7 ms |
20828 KB |
Output is correct |
6 |
Correct |
4 ms |
20828 KB |
Output is correct |
7 |
Correct |
4 ms |
20828 KB |
Output is correct |
8 |
Correct |
6 ms |
20716 KB |
Output is correct |
9 |
Correct |
50 ms |
30408 KB |
Output is correct |
10 |
Correct |
50 ms |
32224 KB |
Output is correct |
11 |
Correct |
51 ms |
32032 KB |
Output is correct |
12 |
Correct |
61 ms |
32452 KB |
Output is correct |
13 |
Correct |
51 ms |
32712 KB |
Output is correct |
14 |
Correct |
47 ms |
30720 KB |
Output is correct |
15 |
Correct |
90 ms |
36032 KB |
Output is correct |
16 |
Correct |
88 ms |
35776 KB |
Output is correct |
17 |
Correct |
105 ms |
36296 KB |
Output is correct |
18 |
Correct |
93 ms |
36724 KB |
Output is correct |
19 |
Correct |
68 ms |
31492 KB |
Output is correct |
20 |
Correct |
180 ms |
38600 KB |
Output is correct |
21 |
Correct |
197 ms |
38644 KB |
Output is correct |
22 |
Correct |
226 ms |
39572 KB |
Output is correct |
23 |
Correct |
198 ms |
39224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20696 KB |
Output is correct |
2 |
Correct |
4 ms |
20572 KB |
Output is correct |
3 |
Correct |
168 ms |
36496 KB |
Output is correct |
4 |
Correct |
178 ms |
36696 KB |
Output is correct |
5 |
Correct |
177 ms |
36832 KB |
Output is correct |
6 |
Correct |
217 ms |
36660 KB |
Output is correct |
7 |
Correct |
166 ms |
36832 KB |
Output is correct |
8 |
Correct |
175 ms |
36564 KB |
Output is correct |
9 |
Correct |
176 ms |
36712 KB |
Output is correct |
10 |
Correct |
184 ms |
36412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20696 KB |
Output is correct |
2 |
Correct |
4 ms |
20572 KB |
Output is correct |
3 |
Correct |
4 ms |
20572 KB |
Output is correct |
4 |
Correct |
4 ms |
20572 KB |
Output is correct |
5 |
Correct |
7 ms |
20828 KB |
Output is correct |
6 |
Correct |
4 ms |
20828 KB |
Output is correct |
7 |
Correct |
4 ms |
20828 KB |
Output is correct |
8 |
Correct |
6 ms |
20716 KB |
Output is correct |
9 |
Correct |
4 ms |
24668 KB |
Output is correct |
10 |
Correct |
6 ms |
25120 KB |
Output is correct |
11 |
Incorrect |
5 ms |
24920 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
24668 KB |
Output is correct |
2 |
Correct |
4 ms |
20696 KB |
Output is correct |
3 |
Correct |
4 ms |
20572 KB |
Output is correct |
4 |
Correct |
4 ms |
20572 KB |
Output is correct |
5 |
Correct |
4 ms |
20572 KB |
Output is correct |
6 |
Correct |
7 ms |
20828 KB |
Output is correct |
7 |
Correct |
4 ms |
20828 KB |
Output is correct |
8 |
Correct |
4 ms |
20828 KB |
Output is correct |
9 |
Correct |
6 ms |
20716 KB |
Output is correct |
10 |
Correct |
50 ms |
30408 KB |
Output is correct |
11 |
Correct |
50 ms |
32224 KB |
Output is correct |
12 |
Correct |
51 ms |
32032 KB |
Output is correct |
13 |
Correct |
61 ms |
32452 KB |
Output is correct |
14 |
Correct |
51 ms |
32712 KB |
Output is correct |
15 |
Correct |
6 ms |
25120 KB |
Output is correct |
16 |
Incorrect |
5 ms |
24920 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20696 KB |
Output is correct |
2 |
Correct |
4 ms |
20572 KB |
Output is correct |
3 |
Correct |
4 ms |
20572 KB |
Output is correct |
4 |
Correct |
4 ms |
20572 KB |
Output is correct |
5 |
Correct |
7 ms |
20828 KB |
Output is correct |
6 |
Correct |
4 ms |
20828 KB |
Output is correct |
7 |
Correct |
4 ms |
20828 KB |
Output is correct |
8 |
Correct |
6 ms |
20716 KB |
Output is correct |
9 |
Correct |
50 ms |
30408 KB |
Output is correct |
10 |
Correct |
50 ms |
32224 KB |
Output is correct |
11 |
Correct |
51 ms |
32032 KB |
Output is correct |
12 |
Correct |
61 ms |
32452 KB |
Output is correct |
13 |
Correct |
51 ms |
32712 KB |
Output is correct |
14 |
Correct |
47 ms |
30720 KB |
Output is correct |
15 |
Correct |
90 ms |
36032 KB |
Output is correct |
16 |
Correct |
88 ms |
35776 KB |
Output is correct |
17 |
Correct |
105 ms |
36296 KB |
Output is correct |
18 |
Correct |
93 ms |
36724 KB |
Output is correct |
19 |
Correct |
168 ms |
36496 KB |
Output is correct |
20 |
Correct |
178 ms |
36696 KB |
Output is correct |
21 |
Correct |
177 ms |
36832 KB |
Output is correct |
22 |
Correct |
217 ms |
36660 KB |
Output is correct |
23 |
Correct |
166 ms |
36832 KB |
Output is correct |
24 |
Correct |
175 ms |
36564 KB |
Output is correct |
25 |
Correct |
176 ms |
36712 KB |
Output is correct |
26 |
Correct |
184 ms |
36412 KB |
Output is correct |
27 |
Correct |
6 ms |
25120 KB |
Output is correct |
28 |
Incorrect |
5 ms |
24920 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
24668 KB |
Output is correct |
2 |
Correct |
4 ms |
20696 KB |
Output is correct |
3 |
Correct |
4 ms |
20572 KB |
Output is correct |
4 |
Correct |
4 ms |
20572 KB |
Output is correct |
5 |
Correct |
4 ms |
20572 KB |
Output is correct |
6 |
Correct |
7 ms |
20828 KB |
Output is correct |
7 |
Correct |
4 ms |
20828 KB |
Output is correct |
8 |
Correct |
4 ms |
20828 KB |
Output is correct |
9 |
Correct |
6 ms |
20716 KB |
Output is correct |
10 |
Correct |
50 ms |
30408 KB |
Output is correct |
11 |
Correct |
50 ms |
32224 KB |
Output is correct |
12 |
Correct |
51 ms |
32032 KB |
Output is correct |
13 |
Correct |
61 ms |
32452 KB |
Output is correct |
14 |
Correct |
51 ms |
32712 KB |
Output is correct |
15 |
Correct |
47 ms |
30720 KB |
Output is correct |
16 |
Correct |
90 ms |
36032 KB |
Output is correct |
17 |
Correct |
88 ms |
35776 KB |
Output is correct |
18 |
Correct |
105 ms |
36296 KB |
Output is correct |
19 |
Correct |
93 ms |
36724 KB |
Output is correct |
20 |
Correct |
168 ms |
36496 KB |
Output is correct |
21 |
Correct |
178 ms |
36696 KB |
Output is correct |
22 |
Correct |
177 ms |
36832 KB |
Output is correct |
23 |
Correct |
217 ms |
36660 KB |
Output is correct |
24 |
Correct |
166 ms |
36832 KB |
Output is correct |
25 |
Correct |
175 ms |
36564 KB |
Output is correct |
26 |
Correct |
176 ms |
36712 KB |
Output is correct |
27 |
Correct |
184 ms |
36412 KB |
Output is correct |
28 |
Correct |
6 ms |
25120 KB |
Output is correct |
29 |
Incorrect |
5 ms |
24920 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |