Submission #86981

# Submission time Handle Problem Language Result Execution time Memory
86981 2018-11-29T03:22:08 Z wzy Factories (JOI14_factories) C++11
100 / 100
5289 ms 261344 KB
#include <bits/stdc++.h>

#include"factories.h"

using namespace std;

#define ll long long
const int N = 500005;

vector< array<int, 2 > > adj[N];
int paiC[N];
bool blocked[N];
int sz[N];
ll vl[N];
ll maxint = 1000000000000000000ll;
int n;
ll dist[N][20];
int layer[N];
void computa(int x , int y){
	sz[x] = 1;
	for(auto w : adj[x]){
		if(blocked[w[0]] || y == w[0]) continue;
		computa(w[0] , x);
		sz[x] += sz[w[0]];
	}
}

int find_C(int x , int y , int root){
	int centroid = x;
	for(auto w : adj[x]){
		if(blocked[w[0]] || y == w[0]) continue;
		if(sz[w[0]] > sz[root]/2) centroid = find_C(w[0] , x , root);
	}
	return centroid;
}

void fill_dp(int x , int y , int pai , int root , ll dis){
	dist[x][layer[root]] = dis;
	for(auto w : adj[x]){
		if(blocked[w[0]] || y == w[0]) continue;
		fill_dp(w[0] , x , pai, root ,(ll)dis + 1ll*w[1]);
	}
}


void decompose(int x  = 0, int pai = -1){
	computa(x , x);
	x = find_C(x , x , x);
	paiC[x] = pai;
	if(pai == -1) layer[x] = 0;
	else layer[x] = layer[pai] + 1;
	fill_dp(x , x , pai , x,0);
	blocked[x] = true;
	for(auto w : adj[x]){
		if(!blocked[w[0]])
		decompose(w[0] , x);
	}
}



void Init(int N, int A[], int B[], int D[]){
	for(int i = 0 ; i < N - 1 ; i ++){
		adj[A[i]].push_back({B[i] , D[i]});
		adj[B[i]].push_back({A[i] , D[i]});
	}
	n = N;
	decompose();
	for(int i = 0 ; i < N ; i ++){
		vl[i] = maxint;
	} 
}

long long Query(int S, int X[], int T, int Y[]){
	for(int i = 0 ; i < S ; i ++){
		ll currdis = 0;
		int x = X[i];
		while(x != -1){
			vl[x] = min(vl[x] , dist[X[i]][layer[x]]);
			x = paiC[x];
		}
	}
	ll ans = maxint;
	for(int i = 0 ; i < T ; i ++){
		ll currdis = 0;
		int x = Y[i];
		while(x != -1){
			ans = min(ans , dist[Y[i]][layer[x]] + vl[x]);
			x = paiC[x];
		}
	}
	for(int i = 0 ; i < S ; i ++){
		int x = X[i];
		while(x != -1){
			vl[x] = maxint;
			x = paiC[x];
		}
	}	
	return ans;
}

Compilation message

factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:76:6: warning: unused variable 'currdis' [-Wunused-variable]
   ll currdis = 0;
      ^~~~~~~
factories.cpp:85:6: warning: unused variable 'currdis' [-Wunused-variable]
   ll currdis = 0;
      ^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 21 ms 12664 KB Output is correct
2 Correct 369 ms 30912 KB Output is correct
3 Correct 438 ms 40508 KB Output is correct
4 Correct 431 ms 44788 KB Output is correct
5 Correct 436 ms 45116 KB Output is correct
6 Correct 291 ms 45116 KB Output is correct
7 Correct 400 ms 45116 KB Output is correct
8 Correct 392 ms 45116 KB Output is correct
9 Correct 443 ms 45160 KB Output is correct
10 Correct 294 ms 45160 KB Output is correct
11 Correct 421 ms 45176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 45176 KB Output is correct
2 Correct 2544 ms 155948 KB Output is correct
3 Correct 3675 ms 157376 KB Output is correct
4 Correct 1119 ms 157376 KB Output is correct
5 Correct 4691 ms 183704 KB Output is correct
6 Correct 3749 ms 183704 KB Output is correct
7 Correct 1081 ms 183704 KB Output is correct
8 Correct 509 ms 183704 KB Output is correct
9 Correct 1389 ms 183704 KB Output is correct
10 Correct 1094 ms 183704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 12664 KB Output is correct
2 Correct 369 ms 30912 KB Output is correct
3 Correct 438 ms 40508 KB Output is correct
4 Correct 431 ms 44788 KB Output is correct
5 Correct 436 ms 45116 KB Output is correct
6 Correct 291 ms 45116 KB Output is correct
7 Correct 400 ms 45116 KB Output is correct
8 Correct 392 ms 45116 KB Output is correct
9 Correct 443 ms 45160 KB Output is correct
10 Correct 294 ms 45160 KB Output is correct
11 Correct 421 ms 45176 KB Output is correct
12 Correct 18 ms 45176 KB Output is correct
13 Correct 2544 ms 155948 KB Output is correct
14 Correct 3675 ms 157376 KB Output is correct
15 Correct 1119 ms 157376 KB Output is correct
16 Correct 4691 ms 183704 KB Output is correct
17 Correct 3749 ms 183704 KB Output is correct
18 Correct 1081 ms 183704 KB Output is correct
19 Correct 509 ms 183704 KB Output is correct
20 Correct 1389 ms 183704 KB Output is correct
21 Correct 1094 ms 183704 KB Output is correct
22 Correct 2802 ms 183704 KB Output is correct
23 Correct 2858 ms 183704 KB Output is correct
24 Correct 4078 ms 183704 KB Output is correct
25 Correct 4066 ms 183704 KB Output is correct
26 Correct 4270 ms 183704 KB Output is correct
27 Correct 5289 ms 183704 KB Output is correct
28 Correct 1278 ms 190640 KB Output is correct
29 Correct 4696 ms 213688 KB Output is correct
30 Correct 4519 ms 237052 KB Output is correct
31 Correct 4510 ms 261344 KB Output is correct
32 Correct 1381 ms 261344 KB Output is correct
33 Correct 546 ms 261344 KB Output is correct
34 Correct 931 ms 261344 KB Output is correct
35 Correct 991 ms 261344 KB Output is correct
36 Correct 1331 ms 261344 KB Output is correct
37 Correct 1290 ms 261344 KB Output is correct