답안 #170343

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
170343 2019-12-24T20:17:34 Z ngmh 공장들 (JOI14_factories) C++11
100 / 100
4260 ms 196028 KB
#include "factories.h"

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pi;

#define f first
#define s second;

ll n, m, a, b, d, v, par[500005], sub[500005], ban[500005], ans[500005], dst[500005][19];
vector<pi> adj[500005];
stack<ll> st;

ll dfs1(ll u, ll p, ll l){
	sub[u] = 1;
	for(auto it : adj[u]){
		if(ban[it.f] != -1) continue;
		if(it.f == p) continue;
		if(l) dst[it.f][l-1] = dst[u][l-1]+it.s;
		sub[u] += dfs1(it.f, u, l);
	}
	return sub[u];
}

ll dfs2(ll u, ll p, ll n){
	for(auto it : adj[u]){
		if(ban[it.f] != -1) continue;
		if(it.f != p && sub[it.f] > n/2){
			return dfs2(it.f, u, n);
		}
	}
	return u;
}

void build(ll u, ll p, ll l){
	ll n = dfs1(u, p, l);
	ll cent = dfs2(u, p, n);
	if(p == -1) p = cent;
	par[cent] = p;
	ban[cent] = l;
	for(auto it : adj[cent]){
		if(ban[it.f] != -1) continue;
		dst[it.f][l] = it.s;
		build(it.f, cent, l+1);
	}
}

void update(ll x){
	ll lvl = ban[x];
	ll y = x;
	while(lvl != -1){
		ans[y] = min(ans[y], dst[x][lvl]);
		st.push(y);
		y = par[y];
		lvl--;
	}
}

ll query(ll x){
	ll res = LLONG_MAX/3;
	ll lvl = ban[x];
	ll y = x;
	while(lvl != -1){
		res = min(res, ans[y]+dst[x][lvl]);
		y = par[y];
		lvl--;
	}
	return res;
}

void Init(int N, int A[], int B[], int D[]) {
	memset(ban, -1, sizeof(ban));
    n = N;
	for(ll i = 0; i < n-1; i++){
	    a = A[i]; b = B[i]; d = D[i];
		adj[a].push_back(pi(b, d));
		adj[b].push_back(pi(a, d));
	}
	build(0, -1, 0);
	for(ll i = 0; i < n; i++) ans[i] = LLONG_MAX/3;
}

ll Query(int S, int X[], int T, int Y[]) {
    for(ll i = 0; i < S; i++) update(X[i]);
    v = LLONG_MAX;
    for(ll i = 0; i < T; i++) v = min(v, query(Y[i]));
    while(!st.empty()){ ans[st.top()] = LLONG_MAX/3; st.pop(); }
    return v;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 16632 KB Output is correct
2 Correct 384 ms 34776 KB Output is correct
3 Correct 424 ms 34808 KB Output is correct
4 Correct 420 ms 34976 KB Output is correct
5 Correct 454 ms 35168 KB Output is correct
6 Correct 308 ms 34780 KB Output is correct
7 Correct 425 ms 34680 KB Output is correct
8 Correct 427 ms 34900 KB Output is correct
9 Correct 454 ms 35188 KB Output is correct
10 Correct 309 ms 34808 KB Output is correct
11 Correct 425 ms 34808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 16248 KB Output is correct
2 Correct 1699 ms 159384 KB Output is correct
3 Correct 2367 ms 161528 KB Output is correct
4 Correct 873 ms 156588 KB Output is correct
5 Correct 3425 ms 186236 KB Output is correct
6 Correct 2669 ms 163588 KB Output is correct
7 Correct 993 ms 63404 KB Output is correct
8 Correct 495 ms 63516 KB Output is correct
9 Correct 1150 ms 67688 KB Output is correct
10 Correct 957 ms 64948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 16632 KB Output is correct
2 Correct 384 ms 34776 KB Output is correct
3 Correct 424 ms 34808 KB Output is correct
4 Correct 420 ms 34976 KB Output is correct
5 Correct 454 ms 35168 KB Output is correct
6 Correct 308 ms 34780 KB Output is correct
7 Correct 425 ms 34680 KB Output is correct
8 Correct 427 ms 34900 KB Output is correct
9 Correct 454 ms 35188 KB Output is correct
10 Correct 309 ms 34808 KB Output is correct
11 Correct 425 ms 34808 KB Output is correct
12 Correct 17 ms 16248 KB Output is correct
13 Correct 1699 ms 159384 KB Output is correct
14 Correct 2367 ms 161528 KB Output is correct
15 Correct 873 ms 156588 KB Output is correct
16 Correct 3425 ms 186236 KB Output is correct
17 Correct 2669 ms 163588 KB Output is correct
18 Correct 993 ms 63404 KB Output is correct
19 Correct 495 ms 63516 KB Output is correct
20 Correct 1150 ms 67688 KB Output is correct
21 Correct 957 ms 64948 KB Output is correct
22 Correct 2715 ms 170284 KB Output is correct
23 Correct 2451 ms 175004 KB Output is correct
24 Correct 3410 ms 176328 KB Output is correct
25 Correct 3417 ms 177880 KB Output is correct
26 Correct 3141 ms 170028 KB Output is correct
27 Correct 4260 ms 196028 KB Output is correct
28 Correct 1131 ms 167920 KB Output is correct
29 Correct 3224 ms 169752 KB Output is correct
30 Correct 3179 ms 169124 KB Output is correct
31 Correct 3268 ms 169756 KB Output is correct
32 Correct 1359 ms 72168 KB Output is correct
33 Correct 517 ms 64612 KB Output is correct
34 Correct 806 ms 61304 KB Output is correct
35 Correct 824 ms 61176 KB Output is correct
36 Correct 1025 ms 62072 KB Output is correct
37 Correct 1025 ms 62072 KB Output is correct