답안 #798809

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
798809 2023-07-31T04:35:50 Z OrazB 공장들 (JOI14_factories) C++14
100 / 100
5266 ms 245052 KB
#include <iostream>
#include <vector>
#include "factories.h"
using namespace std;
#define ll long long int
#define pii pair <int, int>
#define pb push_back
#define ff first
#define ss second

const int N = 5e5+2;
const ll inf = 1e18;
int rem[N], sub[N], par[N], sp[N][20], lvl[N];
ll w[N], cur[N], dis[N][25];
vector<pii> E[N];

void dfs(int nd, int pr){
	sub[nd] = 1;
	for (auto i : E[nd]){
		if (i.ff == pr or rem[i.ff]) continue;
		dfs(i.ff, nd);
		sub[nd] += sub[i.ff];
	}
}
int centroid(int nd, int pr, int sz){
	for (auto i : E[nd]){
		if (i.ff == pr or rem[i.ff]) continue;
		if (sub[i.ff]*2 > sz) return centroid(i.ff, nd, sz);
	}
	return nd;
}
void build(int nd, int pr){
	int centr = centroid(nd, 0, sub[nd]);
	rem[centr] = 1;
	par[centr] = pr;
	for (auto i : E[centr]){
		if (!rem[i.ff]){
			dfs(i.ff, 0);
			build(i.ff, centr);
		}
	}
}
void dfs2(int nd, int pr){
	sp[nd][0] = pr;
	for (auto i : E[nd]){
		if (i.ff == pr) continue;
		lvl[i.ff] = lvl[nd]+1;
		w[i.ff] = w[nd]+i.ss;
		dfs2(i.ff, nd);
	}
}
int LCA(int u, int v){
	if (lvl[u] < lvl[v]) swap(u, v);
	int diff = lvl[u]-lvl[v];
	for (int i = 19; i >= 0; i--){
		if (diff&(1<<i)) u = sp[u][i];
	}
	if (u == v) return v;
	for (int i = 19; i >= 0; i--){
		if (sp[u][i] != sp[v][i]){
			u = sp[u][i];
			v = sp[v][i];
		}
	}
	return sp[u][0];
}
ll dist(int u, int v){
	if (u == v) return 0ll;
	return w[u]+w[v]-2*w[LCA(u,v)];
}
void Init(int n, int A[], int B[], int D[]){
	for (int i = 0; i < n-1; i++){
		A[i]++;
		B[i]++;
		E[A[i]].pb({B[i], D[i]});
		E[B[i]].pb({A[i], D[i]});
	}
	dfs(1, 0);
	build(1, 0);
	dfs2(1, 0);
	for (int j = 1; j <= 19; j++){
		for (int i = 1; i <= n; i++) sp[i][j] = sp[sp[i][j-1]][j-1];
	}
	for (int i = 1; i <= n; i++){
		int u = i, k = 0;
		while(u){
			dis[i][k] = dist(u, i);
			u = par[u];
			k++; 
		}
	}
	for (int i = 1; i <= n; i++) cur[i] = inf;
}
void remv(int nd){
	while(nd){
		cur[nd] = inf;
		nd = par[nd];
	}
}
void paint(int nd){
	int u = nd, k = 0;
	while(u){
		cur[u] = min(cur[u], dis[nd][k]);
		u = par[u];
		k++;
	}
}
ll find(int nd){
	ll mn = inf;
	int u = nd, k = 0;
	while(u){
		mn = min(mn, dis[nd][k]+cur[u]);
		u = par[u];
		k++;
	}
	return mn;
}
ll Query(int S, int X[], int T, int Y[]){
	for (int i = 0; i < S; i++){
		X[i]++;
		paint(X[i]);
	}
	ll ans = inf;
	for (int i = 0; i < T; i++){
		Y[i]++;
		ans = min(ans, find(Y[i]));
	}
	for (int i = 0; i < S; i++) remv(X[i]);
	return ans;
}
// int main ()
// {
// 	int n, q;
// 	cin >> n >> q;
// 	int A[n], B[n], D[n];
// 	for (int i = 0; i < n-1; i++){
// 		cin >> A[i] >> B[i] >> D[i];
// 	}
// 	Init(n, A, B, D);
// 	for (int i = 0; i < q; i++){
// 		int S, T;
// 		cin >> S >> T;
// 		int X[S], Y[T];
// 		for (int j = 0; j < S; j++) cin >> X[j];
// 		for (int j = 0; j < T; j++) cin >> Y[j];
// 		cout << Query(S, X, T, Y) << '\n';
// 	}
// }
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 12500 KB Output is correct
2 Correct 241 ms 23620 KB Output is correct
3 Correct 237 ms 22300 KB Output is correct
4 Correct 237 ms 22392 KB Output is correct
5 Correct 262 ms 22700 KB Output is correct
6 Correct 165 ms 22368 KB Output is correct
7 Correct 273 ms 22348 KB Output is correct
8 Correct 259 ms 22376 KB Output is correct
9 Correct 257 ms 22640 KB Output is correct
10 Correct 165 ms 22616 KB Output is correct
11 Correct 233 ms 22596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 12372 KB Output is correct
2 Correct 1481 ms 214304 KB Output is correct
3 Correct 3050 ms 215952 KB Output is correct
4 Correct 590 ms 214628 KB Output is correct
5 Correct 4868 ms 245052 KB Output is correct
6 Correct 3243 ms 218484 KB Output is correct
7 Correct 652 ms 70004 KB Output is correct
8 Correct 297 ms 70676 KB Output is correct
9 Correct 766 ms 74280 KB Output is correct
10 Correct 621 ms 71400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 12500 KB Output is correct
2 Correct 241 ms 23620 KB Output is correct
3 Correct 237 ms 22300 KB Output is correct
4 Correct 237 ms 22392 KB Output is correct
5 Correct 262 ms 22700 KB Output is correct
6 Correct 165 ms 22368 KB Output is correct
7 Correct 273 ms 22348 KB Output is correct
8 Correct 259 ms 22376 KB Output is correct
9 Correct 257 ms 22640 KB Output is correct
10 Correct 165 ms 22616 KB Output is correct
11 Correct 233 ms 22596 KB Output is correct
12 Correct 7 ms 12372 KB Output is correct
13 Correct 1481 ms 214304 KB Output is correct
14 Correct 3050 ms 215952 KB Output is correct
15 Correct 590 ms 214628 KB Output is correct
16 Correct 4868 ms 245052 KB Output is correct
17 Correct 3243 ms 218484 KB Output is correct
18 Correct 652 ms 70004 KB Output is correct
19 Correct 297 ms 70676 KB Output is correct
20 Correct 766 ms 74280 KB Output is correct
21 Correct 621 ms 71400 KB Output is correct
22 Correct 1661 ms 198084 KB Output is correct
23 Correct 1651 ms 200288 KB Output is correct
24 Correct 3606 ms 200576 KB Output is correct
25 Correct 3465 ms 204580 KB Output is correct
26 Correct 3374 ms 201208 KB Output is correct
27 Correct 5266 ms 227904 KB Output is correct
28 Correct 643 ms 225508 KB Output is correct
29 Correct 3645 ms 224324 KB Output is correct
30 Correct 3231 ms 223588 KB Output is correct
31 Correct 3268 ms 224424 KB Output is correct
32 Correct 745 ms 76668 KB Output is correct
33 Correct 307 ms 72296 KB Output is correct
34 Correct 453 ms 68944 KB Output is correct
35 Correct 476 ms 68940 KB Output is correct
36 Correct 683 ms 69584 KB Output is correct
37 Correct 687 ms 69588 KB Output is correct