Submission #780225

# Submission time Handle Problem Language Result Execution time Memory
780225 2023-07-12T07:26:11 Z daoquanglinh2007 Factories (JOI14_factories) C++17
100 / 100
3421 ms 168876 KB
#include "factories.h"
#include <bits/stdc++.h>
using namespace std;

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

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

int n;
vector <pil> adj[NM+5], son[NM+5];
int parent[NM+5], h[NM+5], jump[NM+5][LOG+5];
ll d[NM+5];
int t, s[NM+5], e[NM+5];
vector <int> L;
stack <int> st;
int col[NM+5];
ll dp1[NM+5], dp2[NM+5], res;

void DFS(int u){
	s[u] = ++t;
	for (int i = 0; i < 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] = d[u]+(ll)adj[u][i].se;
		DFS(v);
	}
	e[u] = t;
}

void build(){
	for (int i = 0; i < n; i++) jump[i][0] = parent[i];
	for (int j = 1; (1<<j) < n; j++)
		for (int i = 0; i < n; i++)
			jump[i][j] = (jump[i][j-1] == -1 ? -1 : jump[jump[i][j-1]][j-1]);
}

void Init(int N, int A[], int B[], int D[]){
	n = N;
	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], (ll)D[i]));
		adj[B[i]].push_back(mp(A[i], (ll)D[i]));
	}
	parent[0] = -1; h[1] = 0; d[1] = 0;
	for (int i = 1; i < n; i++) h[i] = -1;
	t = 0;
	DFS(0);
	build();
}

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

int LCA(int u, int v){
	if (h[u] < h[v]) swap(u, v);
	for (int i = __lg(h[u]); i >= 0; i--)
		if (h[u]-(1<<i) >= h[v]) u = jump[u][i];
	if (u == v) return u;
	for (int i = __lg(h[u]); 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];
}

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

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

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);
	for (int i = 0; i+1 < S+T; i++){
		int a = LCA(L[i], L[i+1]);
		if (col[a] == 0){
			col[a] = 3;
			L.push_back(a);
		}
	}
	sort(L.begin(), L.end(), cmp);
	for (int i = 0; i < (int)L.size(); i++)
		son[L[i]].clear();
	while (!st.empty()) st.pop();
	st.push(L[0]);
	for (int i = 1; i < (int)L.size(); i++){
		while (!is_ancestor(st.top(), L[i])) st.pop();
		son[st.top()].push_back(mp(L[i], d[L[i]]-d[st.top()]));
		st.push(L[i]);
	}
	res = +inf;
	DFS2(L[0]);
	for (int i = 0; i < (int)L.size(); i++)
		col[L[i]] = 0;
	return res;
}

Compilation message

factories.cpp: In function 'void DFS(int)':
factories.cpp:26:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |  for (int i = 0; i < adj[u].size(); i++){
      |                  ~~^~~~~~~~~~~~~~~
factories.cpp: In function 'void DFS2(int)':
factories.cpp:82:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |  for (int i = 0; i < son[u].size(); i++){
      |                  ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 24 ms 24148 KB Output is correct
2 Correct 652 ms 33028 KB Output is correct
3 Correct 749 ms 32932 KB Output is correct
4 Correct 635 ms 33084 KB Output is correct
5 Correct 550 ms 33228 KB Output is correct
6 Correct 496 ms 32880 KB Output is correct
7 Correct 720 ms 33060 KB Output is correct
8 Correct 666 ms 32976 KB Output is correct
9 Correct 597 ms 33208 KB Output is correct
10 Correct 515 ms 32896 KB Output is correct
11 Correct 822 ms 32980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 24072 KB Output is correct
2 Correct 1314 ms 128924 KB Output is correct
3 Correct 2029 ms 133280 KB Output is correct
4 Correct 865 ms 126412 KB Output is correct
5 Correct 2003 ms 167692 KB Output is correct
6 Correct 2292 ms 134608 KB Output is correct
7 Correct 1458 ms 53992 KB Output is correct
8 Correct 519 ms 53284 KB Output is correct
9 Correct 1094 ms 60232 KB Output is correct
10 Correct 1270 ms 55236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 24148 KB Output is correct
2 Correct 652 ms 33028 KB Output is correct
3 Correct 749 ms 32932 KB Output is correct
4 Correct 635 ms 33084 KB Output is correct
5 Correct 550 ms 33228 KB Output is correct
6 Correct 496 ms 32880 KB Output is correct
7 Correct 720 ms 33060 KB Output is correct
8 Correct 666 ms 32976 KB Output is correct
9 Correct 597 ms 33208 KB Output is correct
10 Correct 515 ms 32896 KB Output is correct
11 Correct 822 ms 32980 KB Output is correct
12 Correct 14 ms 24072 KB Output is correct
13 Correct 1314 ms 128924 KB Output is correct
14 Correct 2029 ms 133280 KB Output is correct
15 Correct 865 ms 126412 KB Output is correct
16 Correct 2003 ms 167692 KB Output is correct
17 Correct 2292 ms 134608 KB Output is correct
18 Correct 1458 ms 53992 KB Output is correct
19 Correct 519 ms 53284 KB Output is correct
20 Correct 1094 ms 60232 KB Output is correct
21 Correct 1270 ms 55236 KB Output is correct
22 Correct 2347 ms 139252 KB Output is correct
23 Correct 2126 ms 139980 KB Output is correct
24 Correct 2845 ms 143556 KB Output is correct
25 Correct 2840 ms 146624 KB Output is correct
26 Correct 3255 ms 137300 KB Output is correct
27 Correct 2765 ms 168876 KB Output is correct
28 Correct 1416 ms 134188 KB Output is correct
29 Correct 3055 ms 135948 KB Output is correct
30 Correct 3163 ms 135364 KB Output is correct
31 Correct 3421 ms 135876 KB Output is correct
32 Correct 1112 ms 61936 KB Output is correct
33 Correct 760 ms 57292 KB Output is correct
34 Correct 1006 ms 53068 KB Output is correct
35 Correct 1064 ms 52848 KB Output is correct
36 Correct 1270 ms 53820 KB Output is correct
37 Correct 1253 ms 53756 KB Output is correct