Submission #527632

# Submission time Handle Problem Language Result Execution time Memory
527632 2022-02-17T21:01:20 Z NekoRolly Factories (JOI14_factories) C++17
100 / 100
3459 ms 170304 KB
#include "factories.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e5+4;
const ll inf = 1e18;

struct Cd{
	vector<pair<int,int>> adj[N];
	int sz[N],up[N],h[N],vis[N];
	ll dis[N][19],val[N];

	int find(int u,int p,int Sz){
		int val = 0,x = 0; sz[u] = 1;
		for (auto [v, w] : adj[u]) if (v != p && !vis[v]){
			x ^= find(v, u, Sz); sz[u] += sz[v];
			val = max(val, sz[v]);
		}
		if (max(val, Sz-sz[u]) <= Sz/2) up[x = u] = p;
		return x;
	}

	void dfs(int u,int p,int H){
		for (auto [v, w] : adj[u]) if (v != p && !vis[v]){
			dis[v][H] = dis[u][H] + w;
			dfs(v, u, H);
		}
	}

	void build(int u,int p,int Sz){
		u = find(u, -1, Sz);
		if (up[u] != -1) sz[up[u]] = Sz-sz[u];
		vis[u] = 1, h[u] = h[p]+1, up[u] = p, val[u] = inf;
		dfs(u, -1, h[u]);
		for (auto [v, w] : adj[u]) if (!vis[v])
			build(v, u, sz[v]);
	}

	void upd(int u,bool f){
		for (int v=u; v; v=up[v]){
			if (f) val[v] = min(val[v], dis[u][h[v]]);
			else val[v] = inf;
		}
	}

	ll que(int u){
		ll ans = inf;
		for (int v=u; v; v=up[v]) ans = min(ans, dis[u][h[v]] + val[v]);
		return ans;
	}
} d;


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

	d.h[0] = -1;
	d.build(1, 0, n);
}

ll Query(int S, int X[], int T, int Y[]) {
	for (int i=0; i<S; i++) d.upd(++X[i], 1);
	ll ans = inf;
	for (int i=0; i<T; i++) ans = min(ans, d.que(++Y[i]));
	for (int i=0; i<S; i++) d.upd(X[i], 0);
	return ans;
}

/*
int n,q,S,T;
int A[N],B[N],D[N],X[N],Y[N];

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> n >> q;

	for (int i=0; i+1<n; i++) cin >> A[i] >> B[i] >> D[i];

	Init(n, A, B, D);

	while (q--){
		cin >> S >> T;
		for (int i=0; i<S; i++) cin >> X[i];
		for (int i=0; i<T; i++) cin >> Y[i];
		cout << Query(S, X, T, Y) << "\n";
	}

	return 0;
}
*/
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12640 KB Output is correct
2 Correct 282 ms 30592 KB Output is correct
3 Correct 290 ms 30612 KB Output is correct
4 Correct 292 ms 30840 KB Output is correct
5 Correct 308 ms 30736 KB Output is correct
6 Correct 212 ms 30588 KB Output is correct
7 Correct 349 ms 30604 KB Output is correct
8 Correct 313 ms 30652 KB Output is correct
9 Correct 317 ms 30812 KB Output is correct
10 Correct 207 ms 30608 KB Output is correct
11 Correct 295 ms 30592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12276 KB Output is correct
2 Correct 1636 ms 148316 KB Output is correct
3 Correct 2549 ms 147984 KB Output is correct
4 Correct 568 ms 148584 KB Output is correct
5 Correct 3160 ms 164200 KB Output is correct
6 Correct 2683 ms 150188 KB Output is correct
7 Correct 764 ms 57564 KB Output is correct
8 Correct 312 ms 58724 KB Output is correct
9 Correct 853 ms 60664 KB Output is correct
10 Correct 769 ms 59092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12640 KB Output is correct
2 Correct 282 ms 30592 KB Output is correct
3 Correct 290 ms 30612 KB Output is correct
4 Correct 292 ms 30840 KB Output is correct
5 Correct 308 ms 30736 KB Output is correct
6 Correct 212 ms 30588 KB Output is correct
7 Correct 349 ms 30604 KB Output is correct
8 Correct 313 ms 30652 KB Output is correct
9 Correct 317 ms 30812 KB Output is correct
10 Correct 207 ms 30608 KB Output is correct
11 Correct 295 ms 30592 KB Output is correct
12 Correct 7 ms 12276 KB Output is correct
13 Correct 1636 ms 148316 KB Output is correct
14 Correct 2549 ms 147984 KB Output is correct
15 Correct 568 ms 148584 KB Output is correct
16 Correct 3160 ms 164200 KB Output is correct
17 Correct 2683 ms 150188 KB Output is correct
18 Correct 764 ms 57564 KB Output is correct
19 Correct 312 ms 58724 KB Output is correct
20 Correct 853 ms 60664 KB Output is correct
21 Correct 769 ms 59092 KB Output is correct
22 Correct 1837 ms 155524 KB Output is correct
23 Correct 1894 ms 158120 KB Output is correct
24 Correct 2939 ms 156676 KB Output is correct
25 Correct 2942 ms 160192 KB Output is correct
26 Correct 2880 ms 156760 KB Output is correct
27 Correct 3459 ms 170304 KB Output is correct
28 Correct 694 ms 158956 KB Output is correct
29 Correct 3034 ms 156268 KB Output is correct
30 Correct 3031 ms 155908 KB Output is correct
31 Correct 2947 ms 156304 KB Output is correct
32 Correct 853 ms 61228 KB Output is correct
33 Correct 323 ms 59152 KB Output is correct
34 Correct 557 ms 55620 KB Output is correct
35 Correct 595 ms 55616 KB Output is correct
36 Correct 723 ms 55892 KB Output is correct
37 Correct 714 ms 55952 KB Output is correct