#include <bits/stdc++.h>
using namespace std;
#define bit(n,i) ((n>>i)&1)
#define all(a) (a).begin(),(a).end()
#define pb push_back
#define ep emplace_back
#define pii pair<int,int>
#define piii pair<int,pii>
#define fi first
#define se second
#define ll long long
#define debug(x) cerr << #x << ' ' << x << '\n'
#define dbg(x) cerr<<#x<<' '<<x<<' '
const int base=31;
const int MOD=1e9+7;
const int N=3e5+5;
int ans[N],value[N],lab[N],deg[N],p[N][20],depth[N],n,m;
bool ok[N];
vector<int>adj[N];
int find_set(int v)
{
return (lab[v]==v?v:lab[v]=find_set(lab[v]));
}
bool join(int u,int v,int w)
{
value[n]=w;
deg[u]++;
deg[v]++;
if (deg[u]>=3||deg[v]>=3) ok[n]=1;
u=find_set(u);
v=find_set(v);
adj[n].pb(u);
lab[u]=n;
lab[n]=n;
if (u!=v)
{
adj[n].pb(v);
lab[v]=n;
}
ok[n]|=ok[u]|ok[v];
++n;
}
int lca(int u,int v)
{
if (depth[u]<depth[v]) swap(u,v);
for (int i=18;~i;i--)
{
if (depth[u]-(1<<i)>=depth[v]) u=p[u][i];
}
if (u==v) return u;
for (int i=18;~i;i--)
{
if (~p[u][i]&&p[u][i]!=p[v][i])
{
u=p[u][i];
v=p[v][i];
}
}
return p[u][0];
}
void dfs(int u,int par)
{
if (ok[u]||adj[u].size()==1) ans[u]=value[u];
else ans[u]=ans[par];
for (int v:adj[u])
{
if (v==par) continue;
depth[v]=depth[u]+1;
p[v][0]=u;
dfs(v,u);
}
}
void init(int N,int M,vector<int>U,vector<int>V,vector<int>W)
{
memset(p,-1,sizeof p);
n=N,m=M;
for (int i=0;i<n;i++) lab[i]=i;
vector<int>id;
for (int i=0;i<m;i++) id.pb(i);
sort(all(id),[&](int x,int y){return W[x]<W[y];});
for (auto idx:id)
{
join(U[idx],V[idx],W[idx]);
}
dfs(n-1,n-1);
for (int j=1;(1<<j)<n;j++)
{
for (int i=0;i<n;i++)
{
if (~p[i][j-1])
{
p[i][j]=p[p[i][j-1]][j-1];
}
}
}
}
int getMinimumFuelCapacity(int x,int y)
{
return ans[lca(x,y)];
}
Compilation message
swap.cpp: In function 'bool join(int, int, int)':
swap.cpp:42:1: warning: no return statement in function returning non-void [-Wreturn-type]
42 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
30 ms |
71508 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
30 ms |
71508 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
30 ms |
71508 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
30 ms |
71508 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
30 ms |
71508 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
30 ms |
71508 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |