#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<bitset>
#include<string>
#include<stack>
#include<set>
#include<unordered_set>
#include<map>
#include<unordered_map>
#include<cstring>
#include<complex>
#include<cmath>
#include<iomanip>
#include<numeric>
#include<algorithm>
#include<list>
#include<functional>
#include<cassert>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define y0 y12
#define y1 y22
#define INF 987654321
#define PI 3.141592653589793238462643383279502884
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset((a),0,sizeof(a));
#define MEM_1(a) memset((a),-1,sizeof(a));
#define ALL(a) a.begin(),a.end()
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<ld, ld> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<double> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef tuple<int, int, int> iii;
typedef tuple<int,int,int,int> iiii;
typedef tuple<ll, ll, ll> LLL;
typedef vector<iii> Viii;
typedef vector<LLL> VLLL;
typedef complex<double> base;
const ll MOD = 1000000007;
ll POW(ll a, ll b, ll MMM = MOD) {ll ret=1; for(;b;b>>=1,a=(a*a)%MMM)if(b&1)ret=(ret*a)% MMM; return ret; }
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll lcm(ll a, ll b) { if (a == 0 || b == 0)return a + b; return a*(b / gcd(a, b)); }
int dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 };
int parent[100000];
int find(int a)
{
if (parent[a] < 0)return a;
return parent[a] = find(parent[a]);
}
void merge(int a, int b)
{
a = find(a), b = find(b);
if (a != b)
{
parent[b] = a;
}
}
int f[100000],a[100000],s[300000],e[300000];
VPi v[100000];
int p[100000][17],mn[100000][17],depth[100000];
void dfs(int N,int pp,int d)
{
depth[N]=d;
p[N][0]=pp;
for(auto next:v[N])
{
if(next.X==pp)continue;
mn[next.X][0]=next.Y;
dfs(next.X,N,d+1);
}
}
int LCA(int x, int y)
{
if (depth[x] < depth[y])swap(x, y);
int dif = depth[x] - depth[y];
for (int j = 0; dif > 0; j++)
{
if (dif & 1)x = p[x][j];
dif /= 2;
}
if (x != y)
{
fdn(j,16,0,1)
{
if (p[x][j] != -1 && p[x][j] != p[y][j])
{
x = p[x][j];
y = p[y][j];
}
}
x = p[x][0];
}
return x;
}
int query(int U,int V)
{
int ans=INF;
int t=depth[V]-depth[U];
fdn(i,16,0,1)
{
if(t&(1<<i))
{
ans=min(ans,mn[V][i]);
V=p[V][i];
}
}
return ans;
}
void Init(int K, Vi F, Vi S, Vi E)
{
MEM_1(parent);
fill(&mn[0][0],&mn[99999][17],INF);
int N=F.size(),M=S.size();
fup(i,0,N-1,1)
{
f[i]=F[i];
a[f[i]]=i;
}
fdn(i,M-1,0,1)
{
s[i]=S[i],e[i]=E[i];
if(find(s[i])!=find(e[i]))
{
merge(s[i],e[i]);
v[s[i]].pb(mp(e[i],i));
v[e[i]].pb(mp(s[i],i));
}
}
dfs(0,-1,0);
fup(j,0,15,1)
fup(i,0,N-1,1)
{
if(p[i][j]!=-1)
{
p[i][j+1]=p[p[i][j]][j];
mn[i][j+1]=min(mn[i][j],mn[p[i][j]][j]);
}
}
}
int Separate(int A, int B)
{
A=a[A],B=a[B];
int U=LCA(A,B);
return min(query(U,A),query(U,B))+1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
284 ms |
29024 KB |
Output is correct |
2 |
Correct |
302 ms |
29024 KB |
Output is correct |
3 |
Correct |
279 ms |
29152 KB |
Output is correct |
4 |
Correct |
345 ms |
29024 KB |
Output is correct |
5 |
Correct |
283 ms |
29028 KB |
Output is correct |
6 |
Correct |
277 ms |
29024 KB |
Output is correct |
7 |
Correct |
334 ms |
29028 KB |
Output is correct |
8 |
Correct |
296 ms |
29024 KB |
Output is correct |
9 |
Correct |
301 ms |
29032 KB |
Output is correct |
10 |
Correct |
303 ms |
29024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
9856 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
9856 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |