답안 #399887

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
399887 2021-05-06T20:01:39 Z nikatamliani 공장들 (JOI14_factories) C++14
100 / 100
4402 ms 199936 KB
#include "factories.h"
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll N = 5e5+10, LOG = 20, oo = 1e18;
int sub[N], p[N], d[N];
ll dst[N][LOG], bst[N];
ll n, q;
bool vis[N];
vector<pair<int, ll>> edges[N];
void find_dists(int x, int p, int depth, int ct) {
	for(pair<int, ll> to : edges[x]) {
		if(to.first != p && !vis[to.first]) {
			dst[to.first][depth] = dst[x][depth] + to.second;
			find_dists(to.first, x, depth, ct); 
		}
	}
}
void find_sizes(int x, int p) {
	sub[x] = 1;
	for(pair<int, ll> to : edges[x]) {
		if(to.first != p && !vis[to.first]) {
			find_sizes(to.first, x);
			sub[x] += sub[to.first];
		}
	}
}
int find_centroid(int x, int p, int all) {
	for(pair<int, ll> to : edges[x]) {
		if(to.first != p && !vis[to.first] && sub[to.first] > all/2) {
			return find_centroid(to.first, x, all);
		}
	}
	return x;
}
void dfs(int x, int parent, int depth) {
	find_sizes(x, -1);
	int c = find_centroid(x, -1, sub[x]);
	d[c] = depth;
	p[c] = parent;
	find_dists(c, -1, depth, c);
	vis[c] = 1;
	for(pair<int, ll> to : edges[c]) {
		if(!vis[to.first]) {
			dfs(to.first, c, depth+1);
		}
	}
}
void add_edge(int u, int v, int w) {
	edges[u].push_back({v, w});
	edges[v].push_back({u, w});
}
void mini(ll &x, ll y) {
	if(x > y) x = y; 
}
void Init(int N, int A[], int B[], int D[]) {
	for(int i = 0; i < N-1; ++i) {
		add_edge(A[i], B[i], D[i]);
	}
	for(int i = 0; i < N; ++i) {
		bst[i] = oo;
	}
	dfs(0, -1, 0);
}

long long Query(int S, int X[], int T, int Y[]) {
	vector<int> visited;
	ll ans = oo;
	for(int i = 0; i < S; ++i) {
		int x = X[i];
		int dt = d[x];
		int c = x;
		while(~c) {
			visited.push_back(c);
			mini(bst[c], dst[x][dt]);
			c = p[c];
			--dt;
		}
	}
	for(int i = 0; i < T; ++i) {
		int x = Y[i];
		int dt = d[x];
		int c = x;
		while(~c) {
			ans = min(ans, bst[c] + dst[x][dt]);
			c = p[c];
			--dt;
		}
	}
	for(int c : visited) bst[c] = oo;
	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 12532 KB Output is correct
2 Correct 358 ms 30748 KB Output is correct
3 Correct 404 ms 30788 KB Output is correct
4 Correct 394 ms 31216 KB Output is correct
5 Correct 429 ms 31332 KB Output is correct
6 Correct 271 ms 30676 KB Output is correct
7 Correct 399 ms 30828 KB Output is correct
8 Correct 400 ms 30788 KB Output is correct
9 Correct 427 ms 31524 KB Output is correct
10 Correct 269 ms 30716 KB Output is correct
11 Correct 404 ms 30944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12272 KB Output is correct
2 Correct 1958 ms 157688 KB Output is correct
3 Correct 2983 ms 160732 KB Output is correct
4 Correct 805 ms 154952 KB Output is correct
5 Correct 3773 ms 191984 KB Output is correct
6 Correct 3137 ms 162900 KB Output is correct
7 Correct 977 ms 60236 KB Output is correct
8 Correct 486 ms 59908 KB Output is correct
9 Correct 1150 ms 65044 KB Output is correct
10 Correct 1022 ms 61624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 12532 KB Output is correct
2 Correct 358 ms 30748 KB Output is correct
3 Correct 404 ms 30788 KB Output is correct
4 Correct 394 ms 31216 KB Output is correct
5 Correct 429 ms 31332 KB Output is correct
6 Correct 271 ms 30676 KB Output is correct
7 Correct 399 ms 30828 KB Output is correct
8 Correct 400 ms 30788 KB Output is correct
9 Correct 427 ms 31524 KB Output is correct
10 Correct 269 ms 30716 KB Output is correct
11 Correct 404 ms 30944 KB Output is correct
12 Correct 9 ms 12272 KB Output is correct
13 Correct 1958 ms 157688 KB Output is correct
14 Correct 2983 ms 160732 KB Output is correct
15 Correct 805 ms 154952 KB Output is correct
16 Correct 3773 ms 191984 KB Output is correct
17 Correct 3137 ms 162900 KB Output is correct
18 Correct 977 ms 60236 KB Output is correct
19 Correct 486 ms 59908 KB Output is correct
20 Correct 1150 ms 65044 KB Output is correct
21 Correct 1022 ms 61624 KB Output is correct
22 Correct 2343 ms 168480 KB Output is correct
23 Correct 2580 ms 173528 KB Output is correct
24 Correct 3641 ms 176048 KB Output is correct
25 Correct 3671 ms 177912 KB Output is correct
26 Correct 3455 ms 169180 KB Output is correct
27 Correct 4402 ms 199936 KB Output is correct
28 Correct 994 ms 166308 KB Output is correct
29 Correct 3502 ms 168992 KB Output is correct
30 Correct 3536 ms 168176 KB Output is correct
31 Correct 3567 ms 168840 KB Output is correct
32 Correct 1122 ms 70648 KB Output is correct
33 Correct 489 ms 61232 KB Output is correct
34 Correct 751 ms 57672 KB Output is correct
35 Correct 769 ms 57716 KB Output is correct
36 Correct 948 ms 58564 KB Output is correct
37 Correct 949 ms 58448 KB Output is correct