답안 #886239

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
886239 2023-12-11T15:34:56 Z javotaz 공장들 (JOI14_factories) C++17
0 / 100
8000 ms 429172 KB
// In the Name of Allah
 
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

#define F first
#define S second
#define pii pair<int, int>
#define pb push_back
#define pp pop_back
#define all(x) x.begin(), x.end()
#define cln(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define unq(a, n); {vector<int> v; for(int i = 0; i < n; i++) v.pb(a[i]); cln(v); for (int i = 0; i < n; i++) a[i] = lower_bound(all(v), a[i]) - v.begin();}

const ll N = 5e5 + 12, inf = 1e18 + 12;
vector<pii> g[N];
int sz[N], cpar[N], q, n;
ll val[N];
set<pair<int, ll>> cdis[N];
bool mrk[N];

void dfs(int u, int par = -1) {
	sz[u] = 1;
	for (auto i: g[u])
		if (i.F != par && !mrk[i.F]) {
			dfs(i.F, u);
			sz[u] += sz[i.F];
		}
}

int find_centroid(int u, int c, int par = -1) {
	for (auto i: g[u])
		if (!mrk[i.F] && i.F != par && sz[i.F] > (c - 1) / 2)
			return find_centroid(i.F, c, u);
	return u;
}

void fill_cdis(int u, int cent, ll c = 0, int par = -1) {
	cdis[cent].insert({u, c});
	for (auto i: g[u])
		if (!mrk[i.F] && i.F != par)
			fill_cdis(i.F, cent, c + i.S, u);
}

void decompose(int u, int par = -1) {
	dfs(u);
	u = find_centroid(u, sz[u]);
	fill_cdis(u, u);
	cpar[u] = par;
	mrk[u] = true;
	for (auto i: g[u])
		if (!mrk[i.F])
			decompose(i.F, u);
}

void add(int u, int v) {
	if (u == -1)
		return;
	val[u] = min(val[u], cdis[u].lower_bound({v, -1})->S);
	add(cpar[u], v);
}

ll ask(int u, int v) {
	if (u == -1)
		return inf;
	ll tmp = cdis[u].lower_bound({v, -1})->S;
	return min(ask(cpar[u], v), tmp + val[u]);
}

void rem(int u) {
	val[u] = inf;
	if (cpar[u] != -1)
		rem(cpar[u]);
}

void Init(int c, int a[], int b[], int d[]) {
	n = c;
	for (int i = 0; i < n - 1; i++) {
		g[a[i]].pb({b[i], d[i]});
		g[b[i]].pb({a[i], d[i]});
	}
	decompose(0);
	fill(val, val + n, inf);
}

int Query(int s, int x[], int t, int y[]) {
	for (int i = 0; i < s; i++)
		add(x[i], x[i]);
	ll res = inf;
	for (int i = 0; i < t; i++)
		res = min(res, ask(y[i], y[i]));
	for (int i = 0; i < s; i++)
		rem(x[i]);
	return res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 60248 KB Output is correct
2 Correct 1177 ms 75740 KB Output is correct
3 Correct 1751 ms 76676 KB Output is correct
4 Incorrect 1798 ms 76588 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 59992 KB Output is correct
2 Execution timed out 8106 ms 429172 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 60248 KB Output is correct
2 Correct 1177 ms 75740 KB Output is correct
3 Correct 1751 ms 76676 KB Output is correct
4 Incorrect 1798 ms 76588 KB Output isn't correct
5 Halted 0 ms 0 KB -