답안 #288220

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
288220 2020-09-01T10:19:11 Z kig9981 공장들 (JOI14_factories) C++17
100 / 100
6485 ms 166676 KB
#include <bits/stdc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

vector<pair<int,int>> adj[500000];
int query_num, root, P[500000], W[500000], num[500000], cnt[500000];
long long V[500000], dist[500000][19];
bool chk[500000];

void reset(int c)
{
	W[c]=0;
	for(auto[n,w]: adj[c]) if(!chk[n] && W[n]) reset(n);
}

int dfs(int c)
{
	W[c]=1; V[c]=-1;
	for(auto[n,w]: adj[c]) if(!chk[n] && W[n]==0) W[c]+=dfs(n);
	return W[c];
}

int dfs2(int c, int v)
{
	for(auto[n,w]: adj[c]) if(W[n]<W[c] && 2*W[n]>v) return dfs2(n,v);
	return c;
}

void dfs3(int c)
{
	dist[c][cnt[c]++]=V[c];
	for(auto[n,w]: adj[c]) if(!chk[n] && V[n]==-1) {
		V[n]=V[c]+w;
		dfs3(n);
	}
}

int get_centroid(int c)
{
	reset(c);
	chk[c=dfs2(c,dfs(c))]=true;
	dist[c][cnt[c]++]=V[c]=0;
	for(auto[n,w]: adj[c]) if(!chk[n]) {
		V[n]=w; dfs3(n);
		P[get_centroid(n)]=c;
	}
	return c;
}

void Init(int N, int *A, int *B, int *D)
{
	for(int i=0;i<N-1;i++) {
		adj[A[i]].emplace_back(B[i],D[i]);
		adj[B[i]].emplace_back(A[i],D[i]);
	}
	root=get_centroid(0);
}

long long Query(int S, int *X, int T, int *Y)
{
	long long ret=0x7fffffffffffffffLL;
	int v; ++query_num;
	for(int i=0;i<S;i++) {
		v=cnt[X[i]];
		for(int j=X[i];j!=root;j=P[j]) {
			if(num[j]==query_num) V[j]=min(V[j],dist[X[i]][--v]);
			else {
				num[j]=query_num;
				V[j]=dist[X[i]][--v];
			}
		}
		if(num[root]==query_num) V[root]=min(V[root],dist[X[i]][--v]);
		else {
			num[root]=query_num;
			V[root]=dist[X[i]][--v];
		}
	}
	for(int i=0;i<T;i++) {
		v=cnt[Y[i]];
		for(int j=Y[i];j!=root;j=P[j]) {
			v--;
			if(num[j]!=query_num) continue;
			ret=min(ret,V[j]+dist[Y[i]][v]);
		}
		ret=min(ret,V[root]+dist[Y[i]][--v]);
	}
	return ret;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 12672 KB Output is correct
2 Correct 342 ms 30712 KB Output is correct
3 Correct 376 ms 30712 KB Output is correct
4 Correct 363 ms 30832 KB Output is correct
5 Correct 430 ms 30968 KB Output is correct
6 Correct 270 ms 30712 KB Output is correct
7 Correct 396 ms 30712 KB Output is correct
8 Correct 377 ms 30684 KB Output is correct
9 Correct 424 ms 30968 KB Output is correct
10 Correct 289 ms 30712 KB Output is correct
11 Correct 387 ms 30680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12416 KB Output is correct
2 Correct 2631 ms 132464 KB Output is correct
3 Correct 4368 ms 147868 KB Output is correct
4 Correct 857 ms 132572 KB Output is correct
5 Correct 5776 ms 142612 KB Output is correct
6 Correct 4426 ms 133248 KB Output is correct
7 Correct 1052 ms 57720 KB Output is correct
8 Correct 502 ms 58860 KB Output is correct
9 Correct 1213 ms 60152 KB Output is correct
10 Correct 1061 ms 59464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 12672 KB Output is correct
2 Correct 342 ms 30712 KB Output is correct
3 Correct 376 ms 30712 KB Output is correct
4 Correct 363 ms 30832 KB Output is correct
5 Correct 430 ms 30968 KB Output is correct
6 Correct 270 ms 30712 KB Output is correct
7 Correct 396 ms 30712 KB Output is correct
8 Correct 377 ms 30684 KB Output is correct
9 Correct 424 ms 30968 KB Output is correct
10 Correct 289 ms 30712 KB Output is correct
11 Correct 387 ms 30680 KB Output is correct
12 Correct 12 ms 12416 KB Output is correct
13 Correct 2631 ms 132464 KB Output is correct
14 Correct 4368 ms 147868 KB Output is correct
15 Correct 857 ms 132572 KB Output is correct
16 Correct 5776 ms 142612 KB Output is correct
17 Correct 4426 ms 133248 KB Output is correct
18 Correct 1052 ms 57720 KB Output is correct
19 Correct 502 ms 58860 KB Output is correct
20 Correct 1213 ms 60152 KB Output is correct
21 Correct 1061 ms 59464 KB Output is correct
22 Correct 2968 ms 136212 KB Output is correct
23 Correct 3093 ms 138632 KB Output is correct
24 Correct 5165 ms 136560 KB Output is correct
25 Correct 5049 ms 139704 KB Output is correct
26 Correct 4935 ms 136404 KB Output is correct
27 Correct 6485 ms 166676 KB Output is correct
28 Correct 1045 ms 159840 KB Output is correct
29 Correct 4916 ms 156464 KB Output is correct
30 Correct 4947 ms 156408 KB Output is correct
31 Correct 4944 ms 156476 KB Output is correct
32 Correct 1214 ms 60924 KB Output is correct
33 Correct 495 ms 59372 KB Output is correct
34 Correct 764 ms 55928 KB Output is correct
35 Correct 788 ms 56184 KB Output is correct
36 Correct 1034 ms 56176 KB Output is correct
37 Correct 1033 ms 56224 KB Output is correct