답안 #937581

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
937581 2024-03-04T09:13:33 Z ByeWorld 공장들 (JOI14_factories) C++14
0 / 100
37 ms 71248 KB
#include "factories.h"
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define ll long long
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
#define ld long double
using namespace std;
typedef pair<ll,ll> pii;
const ll INF = 1e18+10;
const int MAXN = 5e5+10;
const int LOG = 21;

int n;
vector <pii> adj[MAXN];
ll dep[MAXN], up[MAXN], anc[MAXN][LOG+5];
ll x[MAXN], y[MAXN];

void dfs(ll nw, ll par){
	anc[nw][0] = par;
	for(auto nx : adj[nw]){
		if(nx.fi == par) continue;
		up[nx.fi] = up[nw]+nx.se;
		dep[nx.fi] = dep[nw]+1;
		dfs(nx.fi, nw);
	}
}

ll LCA(ll x, ll y){
	if(dep[x] > dep[y]) swap(x, y); //gerakin y
	for(int i=LOG-1; i>=0; i--){
		if(dep[anc[y][i]] >= dep[x]) y = anc[y][i]; // dibawah ato sama dengan
	}
	if(x == y) return x;
	for(int i=LOG-1; i>=0; i--){
		if(anc[y][i] != anc[x][i]){
			x = anc[x][i];
			y = anc[y][i];
		}
	}
	return anc[x][0];
}
ll DIST(ll x, ll y){
	ll lca = LCA(x, y);
	return (ll)up[x]+up[y]-2*up[lca];
}

void Init(int N, int A[], int B[], int D[]) {
	n = N;
	for(int i=0; i<n-1; i++){
		A[i]++; B[i]++;
		adj[A[i]].pb({B[i], D[i]}); adj[A[i]].pb({B[i], D[i]});
	}
	dfs(1, -1);
	for(int i=1; i<LOG; i++){
		for(int j=1; j<=n; j++){
			anc[j][i] = anc[anc[j][i-1]][i-1];
		}
	}
	//for(int i=1; i<=n; i++) cout << up[i] << " p\n";
}

long long Query(int S, int X[], int T, int Y[]) {
	int s = S, t = T;
	for(int i=1; i<=s; i++) x[i] = X[i-1]+1;
	for(int i=1; i<=t; i++) y[i] = Y[i-1]+1;

	//for(int i=1; i<=s; i++) cout << x[i] << "p\n";
	//for(int i=1; i<=t; i++) cout << y[i] << "pp\n";

	ll mn = INF;
	for(int i=1; i<=s; i++){
		for(int j=1; j<=t; j++){
			mn = min(mn, DIST(x[i], y[j]));
			//cout << x[i] << ' ' << y[j] << ' '<< DIST(x[i], y[j]) << ' '<< up[x[i]] << ' ' << " x\n";
			assert(DIST(x[i], y[i]) != 0);
		}
	}
	return mn;
}
# 결과 실행 시간 메모리 Grader output
1 Runtime error 37 ms 71136 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 36 ms 71248 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 37 ms 71136 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -