답안 #164573

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
164573 2019-11-21T15:03:46 Z faremy 공장들 (JOI14_factories) C++14
100 / 100
6150 ms 189680 KB
#include "factories.h"

#include <vector>


struct Edge
{
	Edge(int e, long long w) : end(e), weight(w) {}
	int end;
	long long weight;
};

const int MAXN = 5e5;
const int LOGN = 20;
const long long INFTY = 1e18;

std::vector<Edge> tree[MAXN];
bool seen[MAXN];
int size[MAXN];

int centrParent[MAXN], centrDepth[MAXN];
long long centrDist[MAXN][LOGN];

long long minActive[MAXN];


void calcsize(int node)
{
	seen[node] = true;
	size[node] = 1;

	for (Edge& edge : tree[node])
		if (!seen[edge.end])
		{
			calcsize(edge.end);
			size[node] += size[edge.end];
		}
	
	seen[node] = false;
}

int findcentr(int node, int treesize)
{
	if (seen[node])
		return 0;
	seen[node] = true;

	int maxSubSize = treesize - size[node];
	for (Edge &edge : tree[node])
	{
		int ans = findcentr(edge.end, treesize);
		if (ans < 0)
		{
			seen[node] = false;
			return ans;
		}

		maxSubSize = std::max(maxSubSize, ans);
	}

	seen[node] = false;
	if (maxSubSize <= treesize / 2)
		return (-1 - node);
	return size[node];
}

void calcdist(int node, long long dist)
{
	if (seen[node])
		return;
	seen[node] = true;

	centrDist[node][centrDepth[node]] = dist;
	centrDepth[node]++;

	for (Edge &edge : tree[node])
		calcdist(edge.end, dist + edge.weight);
	seen[node] = false;
}

void buildcentr(int node, int parent)
{
	calcsize(node);
	int centroid = (-1 - findcentr(node, size[node]));

	centrParent[centroid] = parent;
	seen[centroid] = true;

	for (Edge &edge : tree[centroid])
		if (!seen[edge.end])
			buildcentr(edge.end, centroid);

	seen[centroid] = false;
	calcdist(centroid, 0);
	minActive[centroid] = INFTY;
}

void Init(int N, int A[], int B[], int D[])
{
	for (int iEdge = 0; iEdge < N - 1; iEdge++)
	{
		tree[A[iEdge]].emplace_back(B[iEdge], D[iEdge]);
		tree[B[iEdge]].emplace_back(A[iEdge], D[iEdge]);
	}

	buildcentr(0, -1);
}

long long Query(int S, int X[], int T, int Y[])
{
	for (int iNode = 0; iNode < S; iNode++)
	{
		int ancester = X[iNode];
		for (int iAnc = 0; iAnc < centrDepth[X[iNode]]; iAnc++)
		{
			minActive[ancester] = std::min(minActive[ancester], centrDist[X[iNode]][iAnc]);
			ancester = centrParent[ancester];
		}
	}

	long long minDist = INFTY;
	for (int iNode = 0; iNode < T; iNode++)
	{
		int ancester = Y[iNode];
		for (int iAnc = 0; iAnc < centrDepth[Y[iNode]]; iAnc++)
		{
			minDist = std::min(minDist, minActive[ancester] + centrDist[Y[iNode]][iAnc]);
			ancester = centrParent[ancester];
		}
	}

	for (int iNode = 0; iNode < S; iNode++)
		for (int ancester = X[iNode]; ancester != -1; ancester = centrParent[ancester])
			minActive[ancester] = INFTY;

	return minDist;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 12664 KB Output is correct
2 Correct 411 ms 31040 KB Output is correct
3 Correct 380 ms 30840 KB Output is correct
4 Correct 383 ms 30968 KB Output is correct
5 Correct 397 ms 31096 KB Output is correct
6 Correct 298 ms 30944 KB Output is correct
7 Correct 377 ms 30840 KB Output is correct
8 Correct 383 ms 30924 KB Output is correct
9 Correct 400 ms 31096 KB Output is correct
10 Correct 308 ms 31096 KB Output is correct
11 Correct 387 ms 30880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 12408 KB Output is correct
2 Correct 2839 ms 157916 KB Output is correct
3 Correct 4535 ms 160092 KB Output is correct
4 Correct 1028 ms 155164 KB Output is correct
5 Correct 5763 ms 185640 KB Output is correct
6 Correct 4634 ms 162156 KB Output is correct
7 Correct 1230 ms 60292 KB Output is correct
8 Correct 516 ms 59992 KB Output is correct
9 Correct 1406 ms 64248 KB Output is correct
10 Correct 1277 ms 61508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 12664 KB Output is correct
2 Correct 411 ms 31040 KB Output is correct
3 Correct 380 ms 30840 KB Output is correct
4 Correct 383 ms 30968 KB Output is correct
5 Correct 397 ms 31096 KB Output is correct
6 Correct 298 ms 30944 KB Output is correct
7 Correct 377 ms 30840 KB Output is correct
8 Correct 383 ms 30924 KB Output is correct
9 Correct 400 ms 31096 KB Output is correct
10 Correct 308 ms 31096 KB Output is correct
11 Correct 387 ms 30880 KB Output is correct
12 Correct 16 ms 12408 KB Output is correct
13 Correct 2839 ms 157916 KB Output is correct
14 Correct 4535 ms 160092 KB Output is correct
15 Correct 1028 ms 155164 KB Output is correct
16 Correct 5763 ms 185640 KB Output is correct
17 Correct 4634 ms 162156 KB Output is correct
18 Correct 1230 ms 60292 KB Output is correct
19 Correct 516 ms 59992 KB Output is correct
20 Correct 1406 ms 64248 KB Output is correct
21 Correct 1277 ms 61508 KB Output is correct
22 Correct 3195 ms 165076 KB Output is correct
23 Correct 3573 ms 167820 KB Output is correct
24 Correct 5420 ms 168344 KB Output is correct
25 Correct 5354 ms 172176 KB Output is correct
26 Correct 5172 ms 168604 KB Output is correct
27 Correct 6150 ms 189680 KB Output is correct
28 Correct 1224 ms 165712 KB Output is correct
29 Correct 5118 ms 168216 KB Output is correct
30 Correct 5118 ms 167816 KB Output is correct
31 Correct 5371 ms 168216 KB Output is correct
32 Correct 1501 ms 65116 KB Output is correct
33 Correct 567 ms 60516 KB Output is correct
34 Correct 910 ms 57776 KB Output is correct
35 Correct 924 ms 57720 KB Output is correct
36 Correct 1203 ms 58556 KB Output is correct
37 Correct 1249 ms 58388 KB Output is correct