답안 #984080

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
984080 2024-05-16T09:56:25 Z SmuggingSpun 공장들 (JOI14_factories) C++17
100 / 100
4729 ms 222396 KB
#include "factories.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>void minimize(T& a, T b){
	if(a > b){
		a = b;
	}
}
const int lim = 5e5 + 5;
const ll INF = 1e18;
vector<pair<int, int>>e[lim];
int root = -1, cnt[lim], par[lim], h[lim], up[lim][19];
ll dis[lim], value[lim], par_d[lim][19];
bitset<lim>vis;
void dfs_1(int s){
	for(auto& [d, w] : e[s]){
		if(d != up[s][0]){
			h[d] = h[up[d][0] = s] + 1;
			dis[d] = dis[s] + w;
			for(int i = 1; i < 19; i++){
				up[d][i] = up[up[d][i - 1]][i - 1];
			}
			dfs_1(d);
		}
	}
}
void dfs_2(int s, int p = -1){
	cnt[s] = 1;
	for(auto& [d, w] : e[s]){
		if(d != p && !vis.test(d)){
			dfs_2(d, s);
			cnt[s] += cnt[d];
		}
	}
}
int get_centroid(int s, const int& n, int p = -1){
	for(auto& [d, w] : e[s]){
		if(d != p && !vis.test(d) && cnt[d] > (n >> 1)){
			return get_centroid(d, n, s);
		}
	}
	return s;
}
int centroid_decomposition(int s){
	dfs_2(s);
	int centroid = get_centroid(s, cnt[s]);
	vis.set(centroid);
	if(root == -1){
		root = centroid;
		par[centroid] = -1;
	}
	for(auto& [d, w] : e[centroid]){
		if(!vis.test(d)){
			par[centroid_decomposition(d)] = centroid;
		}
	}
	return centroid;
}
int lca(int u, int v){
	if(h[u] < h[v]){
		swap(u, v);
	}
	int k = h[u] - h[v];
	for(int i = 0; i < 19; i++){
		if(1 << i & k){
			u = up[u][i];
		}
	}
	if(u == v){
		return u; 
	}
	for(int i = 18; i > -1; i--){
		if(up[u][i] != up[v][i]){
			u = up[u][i];
			v = up[v][i];
		}
	}
	return up[u][0];
}
ll get_distance(int u, int v){
	return dis[u] + dis[v] - (dis[lca(u, v)] << 1LL);
}
void Init(int n, int a[], int b[], int d[]){
	for(int i = 0; i + 1 < n; i++){
		e[a[i]].emplace_back(b[i], d[i]);
		e[b[i]].emplace_back(a[i], d[i]);
	}
	memset(up, dis[0] = 0, sizeof(up));
	dfs_1(h[0] = 0);
	vis.reset();
	centroid_decomposition(0);
	for(int i = 0; i < n; value[i++] = INF){
		int u = i;
		for(int cnt = 0; u != -1; cnt++, u = par[u]){
			par_d[i][cnt] = get_distance(u, i);
		}
	}
}
ll Query(int S, int X[], int T, int Y[]){
	ll ans = INF;
	for(int i = 0; i < S; i++){
		int u = X[i];
		for(int cnt = 0; u != -1; cnt++, u = par[u]){
			minimize(value[u], par_d[X[i]][cnt]);
		}
	}
	for(int i = 0; i < T; i++){
		int u = Y[i];
		for(int cnt = 0; u != -1; cnt++, u = par[u]){
			minimize(ans, value[u] + par_d[Y[i]][cnt]);
		}
	}
	for(int i = 0; i < S; i++){
		int u = X[i];
		for(int cnt = 0; u != -1; cnt++, u = par[u]){
			value[u] = INF;
		}
	}
	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 80468 KB Output is correct
2 Correct 204 ms 85160 KB Output is correct
3 Correct 235 ms 94288 KB Output is correct
4 Correct 221 ms 94292 KB Output is correct
5 Correct 242 ms 94544 KB Output is correct
6 Correct 149 ms 94284 KB Output is correct
7 Correct 226 ms 94288 KB Output is correct
8 Correct 229 ms 94388 KB Output is correct
9 Correct 241 ms 94596 KB Output is correct
10 Correct 146 ms 94416 KB Output is correct
11 Correct 235 ms 94284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 78428 KB Output is correct
2 Correct 1467 ms 179504 KB Output is correct
3 Correct 2816 ms 200008 KB Output is correct
4 Correct 540 ms 198724 KB Output is correct
5 Correct 4729 ms 222256 KB Output is correct
6 Correct 2989 ms 201060 KB Output is correct
7 Correct 622 ms 117576 KB Output is correct
8 Correct 251 ms 117712 KB Output is correct
9 Correct 679 ms 120512 KB Output is correct
10 Correct 619 ms 118336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 80468 KB Output is correct
2 Correct 204 ms 85160 KB Output is correct
3 Correct 235 ms 94288 KB Output is correct
4 Correct 221 ms 94292 KB Output is correct
5 Correct 242 ms 94544 KB Output is correct
6 Correct 149 ms 94284 KB Output is correct
7 Correct 226 ms 94288 KB Output is correct
8 Correct 229 ms 94388 KB Output is correct
9 Correct 241 ms 94596 KB Output is correct
10 Correct 146 ms 94416 KB Output is correct
11 Correct 235 ms 94284 KB Output is correct
12 Correct 12 ms 78428 KB Output is correct
13 Correct 1467 ms 179504 KB Output is correct
14 Correct 2816 ms 200008 KB Output is correct
15 Correct 540 ms 198724 KB Output is correct
16 Correct 4729 ms 222256 KB Output is correct
17 Correct 2989 ms 201060 KB Output is correct
18 Correct 622 ms 117576 KB Output is correct
19 Correct 251 ms 117712 KB Output is correct
20 Correct 679 ms 120512 KB Output is correct
21 Correct 619 ms 118336 KB Output is correct
22 Correct 1586 ms 202828 KB Output is correct
23 Correct 1642 ms 204188 KB Output is correct
24 Correct 3398 ms 204720 KB Output is correct
25 Correct 3413 ms 207100 KB Output is correct
26 Correct 3084 ms 205260 KB Output is correct
27 Correct 4582 ms 222396 KB Output is correct
28 Correct 602 ms 204908 KB Output is correct
29 Correct 3077 ms 204936 KB Output is correct
30 Correct 3086 ms 204708 KB Output is correct
31 Correct 3224 ms 205348 KB Output is correct
32 Correct 687 ms 121172 KB Output is correct
33 Correct 254 ms 117832 KB Output is correct
34 Correct 431 ms 116356 KB Output is correct
35 Correct 452 ms 116556 KB Output is correct
36 Correct 558 ms 116820 KB Output is correct
37 Correct 609 ms 116836 KB Output is correct