답안 #160504

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
160504 2019-10-28T07:38:44 Z iefnah06 공장들 (JOI14_factories) C++11
100 / 100
7477 ms 185244 KB
#include "factories.h"

#include<bits/stdc++.h>
using namespace std;

using ll = long long;
const ll INF = 1e18;

const int MAXN = 5.1e5;
struct edge_t {
	int c[2];
	ll w;
	int other(int a) {
		assert(a == c[0] || a == c[1]);
		return c[0] ^ c[1] ^ a;
	}
} E[MAXN];
vector<int> adj[MAXN];

bool isDead[MAXN];
int par[MAXN];
ll dists[MAXN][20];
int ptrs[MAXN];

int dfsSz(int cur, int prv) {
	if (isDead[cur]) return 0;
	int ans = 1;
	for (int e : adj[cur]) {
		int nxt = E[e].other(cur);
		if (nxt == prv) continue;
		ans += dfsSz(nxt, cur);
	}
	return ans;
}

int getCentroid(int cur, int prv, int sz) {
	if (isDead[cur]) return -1;
	int curSz = 1;
	for (int e : adj[cur]) {
		int nxt = E[e].other(cur);
		if (nxt == prv) continue;
		int ch = getCentroid(nxt, cur, sz);
		if (ch >= 0) {
			return ch;
		} else {
			curSz += -1-ch;
		}
	}

	if (curSz * 2 >= sz) {
		return cur;
	} else {
		return -1-curSz;
	}
}

void genDists(int cur, int centroid, int prv, ll dist) {
	if (isDead[cur]) return;
	if (cur != centroid) {
		par[cur] = centroid;
	}
	dists[cur][ptrs[cur]++] = dist;
	for (int e : adj[cur]) {
		int nxt = E[e].other(cur);
		if (nxt == prv) continue;
		genDists(nxt, centroid, cur, dist + E[e].w);
	}
}

void buildCentroid(int cur) {
	int sz = dfsSz(cur, -1);
	cur = getCentroid(cur, -1, sz);
	genDists(cur, cur, -1, 0);
	isDead[cur] = true;

	for (int e : adj[cur]) {
		int nxt = E[e].other(cur);
		if (isDead[nxt]) continue;
		buildCentroid(nxt);
	}
}

ll minDist[MAXN];

void Init(int N, int A[], int B[], int D[]) {
	for (int e = 0; e < N-1; e++) {
		E[e] = {A[e], B[e], D[e]};
		adj[A[e]].push_back(e);
		adj[B[e]].push_back(e);
	}

	for (int i = 0; i < N; i++) {
		par[i] = -1;
	}
	buildCentroid(0);
	for (int i = 0; i < N; i++) {
		minDist[i] = INF;
	}
}


void update(int v, bool add = true) {
	for (int cur = v, i = ptrs[cur]-1; cur != -1; cur = par[cur], i--) {
		assert(i >= 0);
		if (add) {
			minDist[cur] = min(minDist[cur], dists[v][i]);
		} else {
			minDist[cur] = INF;
		}
	}
}

ll query(int v) {
	ll ans = INF;
	for (int cur = v, i = ptrs[cur]-1; cur != -1; cur = par[cur], i--) {
		assert(i >= 0);
		ans = min(ans, minDist[cur] + dists[v][i]);
	}
	return ans;
}

long long Query(int S, int X[], int T, int Y[]) {
	for (int i = 0; i < S; i++) {
		update(X[i]);
	}
	ll ans = INF;
	for (int i = 0; i < T; i++) {
		ans = min(ans, query(Y[i]));
	}
	for (int i = 0; i < S; i++) {
		update(X[i], false);
	}
	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 12820 KB Output is correct
2 Correct 379 ms 21612 KB Output is correct
3 Correct 414 ms 21624 KB Output is correct
4 Correct 415 ms 21612 KB Output is correct
5 Correct 426 ms 21752 KB Output is correct
6 Correct 316 ms 21628 KB Output is correct
7 Correct 405 ms 21668 KB Output is correct
8 Correct 511 ms 21496 KB Output is correct
9 Correct 426 ms 21752 KB Output is correct
10 Correct 301 ms 21664 KB Output is correct
11 Correct 410 ms 21496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 12536 KB Output is correct
2 Correct 3313 ms 135812 KB Output is correct
3 Correct 5300 ms 137496 KB Output is correct
4 Correct 1018 ms 137084 KB Output is correct
5 Correct 7075 ms 162428 KB Output is correct
6 Correct 5303 ms 157624 KB Output is correct
7 Correct 1336 ms 59436 KB Output is correct
8 Correct 540 ms 60320 KB Output is correct
9 Correct 1609 ms 63496 KB Output is correct
10 Correct 1368 ms 60920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 12820 KB Output is correct
2 Correct 379 ms 21612 KB Output is correct
3 Correct 414 ms 21624 KB Output is correct
4 Correct 415 ms 21612 KB Output is correct
5 Correct 426 ms 21752 KB Output is correct
6 Correct 316 ms 21628 KB Output is correct
7 Correct 405 ms 21668 KB Output is correct
8 Correct 511 ms 21496 KB Output is correct
9 Correct 426 ms 21752 KB Output is correct
10 Correct 301 ms 21664 KB Output is correct
11 Correct 410 ms 21496 KB Output is correct
12 Correct 14 ms 12536 KB Output is correct
13 Correct 3313 ms 135812 KB Output is correct
14 Correct 5300 ms 137496 KB Output is correct
15 Correct 1018 ms 137084 KB Output is correct
16 Correct 7075 ms 162428 KB Output is correct
17 Correct 5303 ms 157624 KB Output is correct
18 Correct 1336 ms 59436 KB Output is correct
19 Correct 540 ms 60320 KB Output is correct
20 Correct 1609 ms 63496 KB Output is correct
21 Correct 1368 ms 60920 KB Output is correct
22 Correct 3612 ms 161768 KB Output is correct
23 Correct 3791 ms 164488 KB Output is correct
24 Correct 6084 ms 163760 KB Output is correct
25 Correct 6046 ms 167548 KB Output is correct
26 Correct 5695 ms 163916 KB Output is correct
27 Correct 7477 ms 185244 KB Output is correct
28 Correct 1183 ms 165924 KB Output is correct
29 Correct 5824 ms 163448 KB Output is correct
30 Correct 6340 ms 162952 KB Output is correct
31 Correct 6069 ms 163616 KB Output is correct
32 Correct 1729 ms 64504 KB Output is correct
33 Correct 563 ms 60624 KB Output is correct
34 Correct 1008 ms 57400 KB Output is correct
35 Correct 1000 ms 57464 KB Output is correct
36 Correct 1286 ms 57840 KB Output is correct
37 Correct 1369 ms 57848 KB Output is correct