Submission #779995

# Submission time Handle Problem Language Result Execution time Memory
779995 2023-07-12T04:56:31 Z daoquanglinh2007 Factories (JOI14_factories) C++17
0 / 100
43 ms 48956 KB
#include "factories.h"

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

#define ll long long
#define pii pair <int, int>
#define pil pair <int, ll>
#define fi first
#define se second
#define mp make_pair

const int NM = 5e5, LOG = 18;
const ll inf = 1e18;

int T = 1, N, Q, parent[NM+5], h[NM+5], jump[NM+5][LOG+5];
ll d[NM+5][LOG+5];
vector <pii> adj[NM+5];
int t, s[NM+5], e[NM+5];
int A[NM+5], B[NM+5], D[NM+5], X[NM+5], Y[NM+5];
vector <int> L, L1;
int col[NM+5];
stack <int> st;
vector <pil> son[NM+5];
ll dp1[NM+5], dp2[NM+5], ans;

void DFS(int u){
	s[u] = ++t;
	for (int i = 0; i < (int)adj[u].size(); i++){
		int v = adj[u][i].fi;
		if (h[v] != -1) continue;
		parent[v] = u;
		h[v] = h[u]+1;
		d[v][0] = adj[u][i].se;
		DFS(v);
	}
	e[u] = t;
}

void build(){
	d[0][0] = -1;
	for (int i = 0; i < N; i++)
		jump[i][0] = parent[i];
	for (int j = 1; j <= LOG; j++)
		for (int i = 0; i < N; i++)
			if (jump[i][j-1] != -1){
				jump[i][j] = jump[jump[i][j-1]][j-1];
				d[i][j] = d[i][j-1]+d[jump[i][j-1]][j-1];
			}
			else{
				jump[i][j] = -1;
				d[i][j] = -1;
			}
}

bool is_ancestor(int a, int u){
	return s[a] <= s[u] && e[a] >= e[u];
}

int LCA(int u, int v){
	if (h[u] < h[v]) swap(u, v);
	for (int i = LOG; i >= 0; i--)
		if (h[u]-(1<<i) >= h[v]) u = jump[u][i];
	if (u == v) return u;
	for (int i = LOG; i >= 0; i--)
		if (jump[u][i] != -1 && jump[u][i] != jump[v][i]){
			u = jump[u][i];
			v = jump[v][i];
		}
	return parent[u];
}

ll get(int a, int u){
	ll res = 0;
	for (int i = LOG; i >= 0; i--)
		if (h[u]-(1<<i) >= h[a]){
			res += d[u][i];
			u = jump[u][i];
		}
	return res;
}

bool cmp(int x, int y){
	return s[x] < s[y];
}

void DFS2(int u){
	dp1[u] = +inf, dp2[u] = +inf;
	for (int i = 0; i < (int)son[u].size(); i++){
		int v = son[u][i].fi;
		DFS2(v);
		ans = min(ans, dp1[u]+dp2[v]+son[u][i].se);
		ans = min(ans, dp2[u]+dp1[v]+son[u][i].se);
		dp1[u] = min(dp1[u], dp1[v]+son[u][i].se);
		dp2[u] = min(dp2[u], dp2[v]+son[u][i].se);
	}
	if (col[u] == 1){
		dp1[u] = 0;
		ans = min(ans, dp2[u]);
	}
	if (col[u] == 2){
		dp2[u] = 0;
		ans = min(ans, dp1[u]);
	}
}

void Init(int N, int A[], int B[], int D[]){
	for (int i = 0; i < N; i++) adj[i].clear();
	for (int i = 0; i < N-1; i++){
		adj[A[i]].push_back(mp(B[i], D[i]));
		adj[B[i]].push_back(mp(A[i], D[i]));
	}
	parent[0] = -1; h[0] = 0;
	for (int i = 1; i < N; i++) h[i] = -1;
	t = 0;
	DFS(0);
	build();
}

ll Query(int S, int X[], int T, int Y[]){
	L.clear();
	for (int i = 0; i < S; i++){
		col[X[i]] = 1;
		L.push_back(X[i]);
	}
	for (int i = 0; i < T; i++){
		col[Y[i]] = 2;
		L.push_back(Y[i]);
	}
	sort(L.begin(), L.end(), cmp);
	L1.clear();
	for (int i = 0; i < (int)L.size(); i++){
		L1.push_back(L[i]);
		if (i < (int)L.size()-1){
			int a = LCA(L[i], L[i+1]);
			if (col[a] == 0){
				col[a] = 3;
				L1.push_back(a);
			}
		}
	}
	sort(L1.begin(), L1.end(), cmp);
	for (int i = 0; i < (int)L1.size(); i++)
		son[L1[i]].clear();
	while (!st.empty()) st.pop();
	st.push(L1[0]);
	for (int i = 1; i < (int)L1.size(); i++){
		while (!is_ancestor(st.top(), L1[i])) st.pop();
		son[st.top()].push_back(mp(L1[i], get(st.top(), L1[i])));
		st.push(L1[i]);
	}
	ans = +inf;
	DFS2(L1[0]);
	for (int i = 0; i < (int)L1.size(); i++)
		col[L1[i]] = 0;
	return ans;
}
# Verdict Execution time Memory Grader output
1 Runtime error 43 ms 48956 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 31 ms 48528 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 43 ms 48956 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -