Submission #235054

# Submission time Handle Problem Language Result Execution time Memory
235054 2020-05-26T21:07:46 Z Blagojce Factories (JOI14_factories) C++11
100 / 100
4974 ms 179024 KB
#include <bits/stdc++.h> 
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
#include <time.h>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const ll inf = 1e17;
const ll mod = 1000000007;
const ld eps = 1e-13;
const ld pi  = 3.14159265359;

mt19937 _rand(time(NULL));
clock_t timer = clock();
const int mxn = 5e5 + 5;

#include "factories.h"

vector<pii> g[mxn];

int sub[mxn];
bool deleted[mxn];
int n;
int sparse[mxn][20];
ll dist[mxn][20];

void dfs0(int u, int p){
	sub[u] = 1;
	for(auto e : g[u]){
		if(e.st == p || deleted[e.st]) continue;
		dfs0(e.st, u);
		sub[u] += sub[e.st];
	}
}
int dfs1(int u, int p){
	for(auto e : g[u]){
		if(e.st != p && !deleted[e.st] && sub[e.st] > n / 2){
			return dfs1(e.st, u);
		}
	}
	return u;
}
void dfs3(int u, int p, int dep){
	for(auto e : g[u]){
		if(e.st == p || deleted[e.st]) continue;
		dist[e.st][dep] = dist[u][dep] + e.nd;
		dfs3(e.st, u, dep);
	}
}

int parent[mxn];
ll opt[mxn];
int depth[mxn];

void decompose(int root, int p, int dep){
	
	dfs0(root, root);
	n = sub[root];
	int centroid = dfs1(root, root);
	opt[centroid] = inf;

	depth[centroid] = dep;
	dfs3(centroid, centroid, dep);
	
	if(root != p) parent[centroid] = p;
	else parent[centroid] = centroid;
	
	deleted[centroid] = true;
	for(auto u : g[centroid]){
		if(deleted[u.st]) continue;
		decompose(u.st, centroid, dep + 1);		
	}
}
	
void Init(int N, int A[], int B[], int D[]) {
	fr(i, 0, N - 1){
		g[A[i]].pb({B[i], D[i]});
		g[B[i]].pb({A[i], D[i]});
	}
	decompose(0, 0, 0);
}


void update(int u){
	opt[u] = 0;
	int x = u;
	while(parent[x] != x){		
		x = parent[x];
		opt[x] = min(opt[x], dist[u][depth[x]]);
	}
}
void rupdate(int u){
	opt[u] = inf;
	int x = u;
	while(parent[x] != x){
		x = parent[x];
		opt[x] = inf;
	}
}
ll query(int u){
	ll ret = opt[u];
	int x = u;
	while(parent[x] != x){
		
		x = parent[x];
		ret = min(ret, opt[x] + dist[u][depth[x]]);
	}
	return ret;
}


ll Query(int S, int X[], int T, int Y[]) {
	fr(i, 0, S){
		update(X[i]);
	}
	
	
	ll ret = inf;
	fr(i, 0, T){
		ret = min(ret, query(Y[i]));
	}
	fr(i, 0, S){
		rupdate(X[i]);
	}
	
	return ret;
}

# Verdict Execution time Memory Grader output
1 Correct 20 ms 12672 KB Output is correct
2 Correct 423 ms 21856 KB Output is correct
3 Correct 425 ms 22044 KB Output is correct
4 Correct 421 ms 22136 KB Output is correct
5 Correct 430 ms 22136 KB Output is correct
6 Correct 319 ms 21880 KB Output is correct
7 Correct 411 ms 21880 KB Output is correct
8 Correct 419 ms 21880 KB Output is correct
9 Correct 453 ms 22264 KB Output is correct
10 Correct 343 ms 21880 KB Output is correct
11 Correct 427 ms 21880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 12416 KB Output is correct
2 Correct 2037 ms 132344 KB Output is correct
3 Correct 2991 ms 133936 KB Output is correct
4 Correct 783 ms 151196 KB Output is correct
5 Correct 4003 ms 174904 KB Output is correct
6 Correct 3089 ms 154048 KB Output is correct
7 Correct 1020 ms 58544 KB Output is correct
8 Correct 493 ms 59316 KB Output is correct
9 Correct 1177 ms 62748 KB Output is correct
10 Correct 1038 ms 59956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 12672 KB Output is correct
2 Correct 423 ms 21856 KB Output is correct
3 Correct 425 ms 22044 KB Output is correct
4 Correct 421 ms 22136 KB Output is correct
5 Correct 430 ms 22136 KB Output is correct
6 Correct 319 ms 21880 KB Output is correct
7 Correct 411 ms 21880 KB Output is correct
8 Correct 419 ms 21880 KB Output is correct
9 Correct 453 ms 22264 KB Output is correct
10 Correct 343 ms 21880 KB Output is correct
11 Correct 427 ms 21880 KB Output is correct
12 Correct 14 ms 12416 KB Output is correct
13 Correct 2037 ms 132344 KB Output is correct
14 Correct 2991 ms 133936 KB Output is correct
15 Correct 783 ms 151196 KB Output is correct
16 Correct 4003 ms 174904 KB Output is correct
17 Correct 3089 ms 154048 KB Output is correct
18 Correct 1020 ms 58544 KB Output is correct
19 Correct 493 ms 59316 KB Output is correct
20 Correct 1177 ms 62748 KB Output is correct
21 Correct 1038 ms 59956 KB Output is correct
22 Correct 2271 ms 158400 KB Output is correct
23 Correct 2355 ms 160876 KB Output is correct
24 Correct 3718 ms 160156 KB Output is correct
25 Correct 3602 ms 163860 KB Output is correct
26 Correct 3580 ms 160224 KB Output is correct
27 Correct 4974 ms 179024 KB Output is correct
28 Correct 1008 ms 161480 KB Output is correct
29 Correct 3814 ms 159716 KB Output is correct
30 Correct 3876 ms 159460 KB Output is correct
31 Correct 3742 ms 159668 KB Output is correct
32 Correct 1154 ms 63096 KB Output is correct
33 Correct 503 ms 59756 KB Output is correct
34 Correct 779 ms 56276 KB Output is correct
35 Correct 804 ms 56184 KB Output is correct
36 Correct 990 ms 56776 KB Output is correct
37 Correct 1004 ms 56828 KB Output is correct