#include <bits/stdc++.h>
#include "swap.h"
#pragma GCC target("sse,sse2,sse4,avx2")
#pragma GCC optimize("unroll-loops,O2")
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define X first
#define Y second
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << " : " << x << endl;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pll;
constexpr int N = 1e5+20,mod = 998244353,inf = 1e9+10;
inline int mkay(int a,int b){
if (a+b >= mod) return a+b-mod;
// if (a+b < 0) return a+b+mod;
return a+b;
}
inline int poww(int a,int k){
if (k < 0) return 0;
int z = 1;
while (k){
if (k&1) z = 1ll*z*a%mod;
a = 1ll*a*a%mod;
k >>= 1;
}
return z;
}
vector<pll> adj[N];
vector<int> be[N],fr[N];
vector<pair<int,pll> > e;
int n,m,maxdeg;
int pr[N],par[N][20],h[N],mx[N][20],mi[N][20][2],bmi[N];
bool good[N];
multiset<int> st[N];
void dfs2(int v,int p){
for (pll u : adj[v]){
if (u.X == p) continue;
dfs2(u.X,v);
if(st[u.X].size() > st[v].size()) swap(st[v],st[u.X]);
while (!st[u.X].empty()){
int x = *(st[u.X].begin());
st[v].insert(x);
st[u.X].erase(st[u.X].begin());
}
}
for (int x : be[v]) st[v].insert(x);
for (int x : fr[v]) st[v].erase(st[v].find(x));
if (st[v].empty()) return;
int g = *(st[v].begin());
bmi[v] = g;
if (g < mi[v][0][0]){
mi[v][0][1] = mi[v][0][0];
mi[v][0][0] = g;
return;
}
if (g < mi[v][0][1]) mi[v][0][1] = g;
}
void dfs(int v,int p){
par[v][0] = p;
int g[3];
rep(i,0,3) g[i] = inf;
for (auto u : adj[v]){
int w = u.Y;
if (u.X == p) continue;
h[u.X] = h[v]+1;
mx[u.X][0] = w;
dfs(u.X,v);
if (w < g[0]){
g[2] = g[1];
g[1] = g[0];
g[0] = w;
continue;
}
if (w < g[1]){
g[2] = g[1];
g[1] = w;
continue;
}
if (w < g[2]) g[2] = w;
}
for (auto u : adj[v]){
if (u.X == p) continue;
int w = u.Y;
if (g[0] < w){
mi[u.X][0][0] = g[0];
if (g[1] < w) mi[u.X][0][1] = g[1];
else mi[u.X][0][1] = g[2];
}
else{
mi[u.X][0][0] = g[1];
mi[u.X][0][1] = g[2];
}
}
}
int getpar(int v,int k){
rep(i,0,20)
if (k&(1 << i))
v = par[v][i];
return v;
}
int lca(int u,int v){
if (h[u] > h[v]) swap(u,v);
if (h[v]-h[u]) v = getpar(v,h[v]-h[u]);
if (u == v) return u;
repr(i,19,0){
if (par[v][i] != par[u][i]){
v = par[v][i];
u = par[u][i];
}
}
return par[v][0];
}
int calc_mx(int v,int k){
int ans = 0;
repr(i,19,0){
if (k >= (1 << i)){
ans = max(ans,mx[v][i]);
v = par[v][i];
k -= (1 << i);
}
}
return ans;
}
int calc_mi(int v,int k){
int ans = inf;
repr(i,19,0){
if (k >= (1 << i)){
ans = min(ans,mi[v][i][0]);
v = par[v][i];
k -= (1 << i);
}
}
return ans;
}
int getpr(int v){
if (pr[v] == v) return v;
return pr[v] = getpr(pr[v]);
}
void init (int nn,int mm,vector<int> U,vector<int> V,vector<int> W){
memset(mi,63,sizeof mi);
memset(bmi,63,sizeof bmi);
n = nn;
m = mm;
rep(i,0,n) pr[i] = i;
rep(i,0,m){
U[i]++;
V[i]++;
e.pb({W[i],{U[i],V[i]}});
}
sort(all(e));
rep(i,0,m){
int x = e[i].Y.X,y = e[i].Y.Y;
if (getpr(x) != getpr(y)){
good[i] = 1;
pr[pr[x]] = pr[y];
adj[x].pb({y,e[i].X});
adj[y].pb({x,e[i].X});
}
}
if (m == n-1){
rep(i,1,n+1) maxdeg = max(maxdeg,(int)adj[i].size());
}
dfs(1,0);
rep(j,1,20){
rep(i,1,n+1){
par[i][j] = par[par[i][j-1]][j-1];
mx[i][j] = max(mx[i][j-1],mx[par[i][j-1]][j-1]);
}
}
rep(i,0,m){
if (good[i]) continue;
int u = e[i].Y.X,v = e[i].Y.Y,w = e[i].X;
if (h[u] > h[v]) swap(u,v);
int g = lca(u,v);
if (u == g){
fr[u].pb(w);
be[v].pb(w);
continue;
}
be[v].pb(w);
be[u].pb(w);
fr[g].pb(w);
fr[g].pb(w);
}
dfs2(1,0);
rep(j,1,20){
rep(i,1,n+1){
int x0 = mi[par[i][j-1]][j-1][0],x1 = mi[par[i][j-1]][j-1][1];
rep(k,0,2) mi[i][j][k] = mi[i][j-1][k];
if (x0 < mi[i][j][0]){
mi[i][j][1] = min(x1,mi[i][j][0]);
mi[i][j][0] = x0;
continue;
}
if (x0 < mi[i][j][1])
mi[i][j][1] = x0;
}
}
debug("end");
}
int getMinimumFuelCapacity(int u, int v){
u++;
v++;
if (m == n-1 && maxdeg == 2) return -1;
if (h[u] > h[v]) swap(u,v);
int w = lca(u,v);
if (u == w){
int ans = calc_mi(v,h[v]-h[u]-1);
int x = getpar(v,h[v]-h[u]-1);
ans = min(ans,bmi[x]);
if (ans >= inf && adj[u].size() <= 2) return -1;
if (ans >= inf){
if (u == 1 || mx[u][0] >= mi[x][0][1]) ans = max(mi[x][0][1],calc_mx(v,h[v]-h[u]));
else ans = max({mx[u][0],mi[x][0][0],calc_mx(v,h[v]-h[u])});
return ans;
}
int mas = calc_mx(v,h[v]-h[u]);
if (adj[u].size() <= 2) return max(mas,ans);
ans = min(ans,mi[x][0][1]);
if (u != 1) ans = min(ans,max(mx[u][0],mi[x][0][0]));
return max(ans,mas);
}
else{
int x = getpar(v,h[v]-h[w]-1),y = getpar(u,h[u]-h[w]-1);
int ans = min(calc_mi(v,h[v]-h[w]-1),calc_mi(u,h[u]-h[w]-1));
ans = min({ans,bmi[x],bmi[y]});
if (w != 1) ans = min(ans,mx[w][0]);
if (mi[x][0][0] != mx[y][0]) ans = min(ans,mi[x][0][0]);
else ans = min(ans,mi[x][0][1]);
if (ans >= inf) return -1;
int mas = max(calc_mx(v,h[v]-h[w]),calc_mx(u,h[u]-h[w]));
return max(ans,mas);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
28116 KB |
Output is correct |
2 |
Correct |
16 ms |
28140 KB |
Output is correct |
3 |
Correct |
12 ms |
28116 KB |
Output is correct |
4 |
Correct |
13 ms |
28240 KB |
Output is correct |
5 |
Correct |
13 ms |
28372 KB |
Output is correct |
6 |
Correct |
15 ms |
28420 KB |
Output is correct |
7 |
Correct |
14 ms |
28336 KB |
Output is correct |
8 |
Correct |
16 ms |
28500 KB |
Output is correct |
9 |
Correct |
139 ms |
54592 KB |
Output is correct |
10 |
Correct |
219 ms |
63424 KB |
Output is correct |
11 |
Correct |
184 ms |
61656 KB |
Output is correct |
12 |
Correct |
192 ms |
64284 KB |
Output is correct |
13 |
Correct |
171 ms |
68332 KB |
Output is correct |
14 |
Correct |
163 ms |
53312 KB |
Output is correct |
15 |
Correct |
264 ms |
64804 KB |
Output is correct |
16 |
Correct |
230 ms |
60292 KB |
Output is correct |
17 |
Correct |
271 ms |
69860 KB |
Output is correct |
18 |
Correct |
277 ms |
66908 KB |
Output is correct |
19 |
Correct |
132 ms |
37900 KB |
Output is correct |
20 |
Correct |
571 ms |
64456 KB |
Output is correct |
21 |
Correct |
579 ms |
61572 KB |
Output is correct |
22 |
Correct |
646 ms |
66264 KB |
Output is correct |
23 |
Correct |
490 ms |
67332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
28116 KB |
Output is correct |
2 |
Correct |
16 ms |
28140 KB |
Output is correct |
3 |
Correct |
251 ms |
53772 KB |
Output is correct |
4 |
Correct |
248 ms |
54812 KB |
Output is correct |
5 |
Correct |
240 ms |
54308 KB |
Output is correct |
6 |
Correct |
241 ms |
54752 KB |
Output is correct |
7 |
Correct |
252 ms |
54616 KB |
Output is correct |
8 |
Correct |
229 ms |
53616 KB |
Output is correct |
9 |
Correct |
277 ms |
54408 KB |
Output is correct |
10 |
Correct |
222 ms |
53384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
28116 KB |
Output is correct |
2 |
Correct |
16 ms |
28140 KB |
Output is correct |
3 |
Correct |
12 ms |
28116 KB |
Output is correct |
4 |
Correct |
13 ms |
28240 KB |
Output is correct |
5 |
Correct |
13 ms |
28372 KB |
Output is correct |
6 |
Correct |
15 ms |
28420 KB |
Output is correct |
7 |
Correct |
14 ms |
28336 KB |
Output is correct |
8 |
Correct |
16 ms |
28500 KB |
Output is correct |
9 |
Correct |
12 ms |
28116 KB |
Output is correct |
10 |
Correct |
14 ms |
28264 KB |
Output is correct |
11 |
Correct |
13 ms |
28324 KB |
Output is correct |
12 |
Correct |
15 ms |
28384 KB |
Output is correct |
13 |
Correct |
16 ms |
28284 KB |
Output is correct |
14 |
Correct |
17 ms |
28356 KB |
Output is correct |
15 |
Incorrect |
14 ms |
28372 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
28116 KB |
Output is correct |
2 |
Correct |
13 ms |
28116 KB |
Output is correct |
3 |
Correct |
16 ms |
28140 KB |
Output is correct |
4 |
Correct |
12 ms |
28116 KB |
Output is correct |
5 |
Correct |
13 ms |
28240 KB |
Output is correct |
6 |
Correct |
13 ms |
28372 KB |
Output is correct |
7 |
Correct |
15 ms |
28420 KB |
Output is correct |
8 |
Correct |
14 ms |
28336 KB |
Output is correct |
9 |
Correct |
16 ms |
28500 KB |
Output is correct |
10 |
Correct |
139 ms |
54592 KB |
Output is correct |
11 |
Correct |
219 ms |
63424 KB |
Output is correct |
12 |
Correct |
184 ms |
61656 KB |
Output is correct |
13 |
Correct |
192 ms |
64284 KB |
Output is correct |
14 |
Correct |
171 ms |
68332 KB |
Output is correct |
15 |
Correct |
14 ms |
28264 KB |
Output is correct |
16 |
Correct |
13 ms |
28324 KB |
Output is correct |
17 |
Correct |
15 ms |
28384 KB |
Output is correct |
18 |
Correct |
16 ms |
28284 KB |
Output is correct |
19 |
Correct |
17 ms |
28356 KB |
Output is correct |
20 |
Incorrect |
14 ms |
28372 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
28116 KB |
Output is correct |
2 |
Correct |
16 ms |
28140 KB |
Output is correct |
3 |
Correct |
12 ms |
28116 KB |
Output is correct |
4 |
Correct |
13 ms |
28240 KB |
Output is correct |
5 |
Correct |
13 ms |
28372 KB |
Output is correct |
6 |
Correct |
15 ms |
28420 KB |
Output is correct |
7 |
Correct |
14 ms |
28336 KB |
Output is correct |
8 |
Correct |
16 ms |
28500 KB |
Output is correct |
9 |
Correct |
139 ms |
54592 KB |
Output is correct |
10 |
Correct |
219 ms |
63424 KB |
Output is correct |
11 |
Correct |
184 ms |
61656 KB |
Output is correct |
12 |
Correct |
192 ms |
64284 KB |
Output is correct |
13 |
Correct |
171 ms |
68332 KB |
Output is correct |
14 |
Correct |
163 ms |
53312 KB |
Output is correct |
15 |
Correct |
264 ms |
64804 KB |
Output is correct |
16 |
Correct |
230 ms |
60292 KB |
Output is correct |
17 |
Correct |
271 ms |
69860 KB |
Output is correct |
18 |
Correct |
277 ms |
66908 KB |
Output is correct |
19 |
Correct |
251 ms |
53772 KB |
Output is correct |
20 |
Correct |
248 ms |
54812 KB |
Output is correct |
21 |
Correct |
240 ms |
54308 KB |
Output is correct |
22 |
Correct |
241 ms |
54752 KB |
Output is correct |
23 |
Correct |
252 ms |
54616 KB |
Output is correct |
24 |
Correct |
229 ms |
53616 KB |
Output is correct |
25 |
Correct |
277 ms |
54408 KB |
Output is correct |
26 |
Correct |
222 ms |
53384 KB |
Output is correct |
27 |
Correct |
14 ms |
28264 KB |
Output is correct |
28 |
Correct |
13 ms |
28324 KB |
Output is correct |
29 |
Correct |
15 ms |
28384 KB |
Output is correct |
30 |
Correct |
16 ms |
28284 KB |
Output is correct |
31 |
Correct |
17 ms |
28356 KB |
Output is correct |
32 |
Incorrect |
14 ms |
28372 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
28116 KB |
Output is correct |
2 |
Correct |
13 ms |
28116 KB |
Output is correct |
3 |
Correct |
16 ms |
28140 KB |
Output is correct |
4 |
Correct |
12 ms |
28116 KB |
Output is correct |
5 |
Correct |
13 ms |
28240 KB |
Output is correct |
6 |
Correct |
13 ms |
28372 KB |
Output is correct |
7 |
Correct |
15 ms |
28420 KB |
Output is correct |
8 |
Correct |
14 ms |
28336 KB |
Output is correct |
9 |
Correct |
16 ms |
28500 KB |
Output is correct |
10 |
Correct |
139 ms |
54592 KB |
Output is correct |
11 |
Correct |
219 ms |
63424 KB |
Output is correct |
12 |
Correct |
184 ms |
61656 KB |
Output is correct |
13 |
Correct |
192 ms |
64284 KB |
Output is correct |
14 |
Correct |
171 ms |
68332 KB |
Output is correct |
15 |
Correct |
163 ms |
53312 KB |
Output is correct |
16 |
Correct |
264 ms |
64804 KB |
Output is correct |
17 |
Correct |
230 ms |
60292 KB |
Output is correct |
18 |
Correct |
271 ms |
69860 KB |
Output is correct |
19 |
Correct |
277 ms |
66908 KB |
Output is correct |
20 |
Correct |
251 ms |
53772 KB |
Output is correct |
21 |
Correct |
248 ms |
54812 KB |
Output is correct |
22 |
Correct |
240 ms |
54308 KB |
Output is correct |
23 |
Correct |
241 ms |
54752 KB |
Output is correct |
24 |
Correct |
252 ms |
54616 KB |
Output is correct |
25 |
Correct |
229 ms |
53616 KB |
Output is correct |
26 |
Correct |
277 ms |
54408 KB |
Output is correct |
27 |
Correct |
222 ms |
53384 KB |
Output is correct |
28 |
Correct |
14 ms |
28264 KB |
Output is correct |
29 |
Correct |
13 ms |
28324 KB |
Output is correct |
30 |
Correct |
15 ms |
28384 KB |
Output is correct |
31 |
Correct |
16 ms |
28284 KB |
Output is correct |
32 |
Correct |
17 ms |
28356 KB |
Output is correct |
33 |
Incorrect |
14 ms |
28372 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |