답안 #760111

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
760111 2023-06-17T07:58:42 Z denniskim 갈라파고스 여행 (FXCUP4_island) C++17
0 / 100
411 ms 524288 KB
#include "island.h"
#include <bits/stdc++.h>

using namespace std;
typedef int ll;
typedef __int128 lll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
#define MAX 9223372036854775807LL
#define MIN -9223372036854775807LL
#define INF 0x3f3f3f3f3f3f3f3f
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << fixed; cout.precision(10);
#define sp << " "
#define en << "\n"
#define compress(v) sort(v.begin(), v.end()), v.erase(unique(v.begin(), v.end()), v.end())

ll n, m;
ll a[100010];
ll pa[100010], ra[100010];
vector<pll> vec[100010];
ll spa[100010][21], mspa[100010][21];
ll dep[100010];

ll ffind(ll here)
{
	if(here == pa[here])
		return pa[here];
	
	return pa[here] = ffind(pa[here]);
}

void uunion(ll X, ll Y)
{
	X = ffind(X);
	Y = ffind(Y);
	
	if(X == Y)
		return;
	
	if(ra[X] < ra[Y])
		pa[X] = Y;
	else if(ra[X] > ra[Y])
		pa[Y] = X;
	
	else
	{
		pa[X] = Y;
		ra[Y]++;
	}
}

void dfs(ll here, ll p)
{
	for(auto &i : vec[here])
	{
		if(i.fi == p)
			continue;
		
		dep[i.fi] = dep[here] + 1;
		spa[i.fi][0] = here;
		mspa[i.fi][0] = i.se;
		dfs(i.fi, here);
	}
}

void Init(ll K, vector<ll> F, vector<ll> S, vector<ll> E)
{
	n = (ll)F.size();
	m = (ll)S.size();
	
	for(ll i = 0 ; i < n ; i++)
		a[i] = F[i];
	
	for(ll i = 0 ; i < n ; i++)
		pa[i] = i;
	
	for(ll i = m - 1 ; i >= 0 ; i--)
	{
		if(ffind(S[i]) == ffind(E[i]))
			continue;
		
		uunion(F[S[i]], F[E[i]]);
		vec[F[S[i]]].push_back({F[E[i]], i + 1});
		vec[F[E[i]]].push_back({F[S[i]], i + 1});
	}
	
	dfs(0, -1);
	
	for(ll i = 1 ; i <= 20 ; i++)
	{
		for(ll j = 0 ; j < n ; j++)
		{
			spa[j][i] = spa[spa[j][i - 1]][i - 1];
			mspa[j][i] = min(mspa[j][i - 1], mspa[spa[j][i - 1]][i - 1]);
		}
	}
}

ll Separate(ll A, ll B)
{
	ll ans = 987654321;
	
	if(dep[A] < dep[B])
		swap(A, B);
	
	ll cha = dep[A] - dep[B];
	
	for(ll i = 20 ; i >= 0 ; i--)
	{
		if(cha >= (1LL << i))
		{
			cha -= (1LL << i);
			ans = min(ans, mspa[A][i]);
			A = spa[A][i];
		}
	}
	
	if(A == B)
		return ans;
	
	for(ll i = 20 ; i >= 0 ; i--)
	{
		if(spa[A][i] != spa[B][i])
		{
			ans = min(ans, mspa[A][i]);
			ans = min(ans, mspa[B][i]);
			A = spa[A][i];
			B = spa[B][i];
		}
	}
	
	ans = min(ans, mspa[A][0]);
	ans = min(ans, mspa[B][0]);
	
	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Runtime error 411 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 230 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 230 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -