Submission #26545

# Submission time Handle Problem Language Result Execution time Memory
26545 2017-07-02T17:43:40 Z rubabredwan Factories (JOI14_factories) C++14
15 / 100
6000 ms 236552 KB
/*  Bismillahir Rahmanir Rahim  */

#include <bits/stdc++.h>
#include "factories.h"

using namespace std;

typedef pair <int, long long> pii;

const int N = 500005;
const long long oo = 1e18;

vector <pii> g[N];
int n;
int tot;
int sub[N];
int anc[N];
int vis[N];
long long mn[N];
vector <long long> pre[N];
stack <int> rem;

void dfs0(int at, int par){
	++tot;
	sub[at] = 1;
	for(auto u : g[at]){
		if(vis[u.first] || u.first == par) continue;
		dfs0(u.first, at);
		sub[at] += sub[u.first];
	}
}

int get(int at, int par){
	for(auto u : g[at]){
		if(vis[u.first] || u.first == par) continue;
		if(sub[u.first] > tot / 2) return get(u.first, at);
	}
	return at;
}

void get_dis(int at, int par, long long dis){
	pre[at].push_back(dis);
	for(auto u : g[at]){
		if(vis[u.first] || u.first == par) continue;
		get_dis(u.first, at, dis + u.second);
	}
}

void decompose(int at, int par){
	tot = 0;
	dfs0(at, at);
	int cen = get(at, at);
	vis[cen] = 1;
	if(par) anc[cen] = par;
	get_dis(cen, cen, 0);
	for(auto u : g[cen]){
		if(vis[u.first]) continue;
		decompose(u.first, cen);
	}
}

void update(int from){
	int cur = from;
	for(auto u : pre[from]){
		mn[cur] = min(mn[cur], u);
		rem.push(cur);
		cur = anc[cur];
	}
}

long long query(int from){
	int cur = from;
	long long ret = oo;
	for(auto u : pre[from]){
		ret = min(ret, mn[cur] + u);
		cur = anc[cur];
	}
	return ret;
}

void reset(){
	while(!rem.empty()){
		mn[rem.top()] = oo;
		rem.pop();
	}
}

long long Query(int S, int X[], int T, int Y[]){
	reset();
	for(int i = 0; i < S; ++i){
		update(X[i] + 1);
	}	
	long long ret = oo;
	for(int i = 0; i < T; ++i){
		ret = min(ret, query(Y[i] + 1));
	}
	return ret;
}

void Init(int N, int A[], int B[], int D[]){
	n = N;
	for(int i = 0; i < N - 1; ++i){
		g[A[i] + 1].push_back({B[i] + 1, D[i]});
		g[B[i] + 1].push_back({A[i] + 1, D[i]});
	}
	decompose(1, 0);
	for(int i = 1; i <= n; ++i) mn[i] = oo;
	for(int i = 1; i <= n; ++i){
		reverse(pre[i].begin(), pre[i].end());
	}
}
# Verdict Execution time Memory Grader output
1 Correct 19 ms 57628 KB Output is correct
2 Correct 366 ms 58288 KB Output is correct
3 Correct 403 ms 58420 KB Output is correct
4 Correct 476 ms 58552 KB Output is correct
5 Correct 476 ms 58708 KB Output is correct
6 Correct 283 ms 57916 KB Output is correct
7 Correct 443 ms 58420 KB Output is correct
8 Correct 443 ms 58420 KB Output is correct
9 Correct 419 ms 58704 KB Output is correct
10 Correct 316 ms 57916 KB Output is correct
11 Correct 453 ms 58420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 57628 KB Output is correct
2 Correct 3733 ms 149500 KB Output is correct
3 Correct 5563 ms 187860 KB Output is correct
4 Correct 1276 ms 97000 KB Output is correct
5 Execution timed out 6000 ms 236552 KB Execution timed out
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3993 ms 150660 KB Output is correct
2 Correct 4633 ms 153576 KB Output is correct
3 Correct 5763 ms 189848 KB Output is correct
4 Correct 5623 ms 190704 KB Output is correct
5 Correct 5779 ms 187576 KB Output is correct
6 Execution timed out 6000 ms 234260 KB Execution timed out
7 Halted 0 ms 0 KB -