Submission #369522

# Submission time Handle Problem Language Result Execution time Memory
369522 2021-02-21T19:48:33 Z soroush Factories (JOI14_factories) C++17
100 / 100
3180 ms 150356 KB
#include "factories.h"
#include <bits/stdc++.h>

using namespace std;

const int maxn = 5e5 + 100;

typedef long long ll;
typedef pair < int , int > pii;

#define pb push_back

int st[maxn] , ft[maxn] , ti = 0;
vector < pii > adj[maxn];
ll h[maxn];

const ll inf = 1e18;

ll red[maxn] , blue[maxn];

void dfs(int v = 1 , int p = 0){
	st[v] = ++ti;
	for(auto [u , w] : adj[v])if(u ^ p)
		h[u] = h[v] + w , dfs(u , v);
	ft[v] = ti;
}

int sz[maxn] , H[maxn] , head[maxn] , par[maxn] , mx[maxn];

void szdfs(int v = 1){
	sz[v] = 1;
	for(auto [u , w] : adj[v])if(u ^ par[v]){
		par[u] = v , H[u] = H[v] + 1 , szdfs(u) , sz[v] += sz[u];
		if(sz[u] > sz[mx[v]])mx[v] = u;
	}
}

void chaindfs(int v = 1){
	if(mx[v])head[mx[v]] = head[v] , chaindfs(mx[v]);
	for(auto [u , w] : adj[v]) if(u ^ par[v] and u ^ mx[v])
		head[u] = u , chaindfs(u);
	
}

int lca(int u , int v){
	while(head[u] != head[v]){
		if(h[head[u]] < h[head[v]])swap(u , v);
		u = par[head[u]];
	}
	if(h[u] < h[v])swap(u , v);
	return(v);
}

void Init(int N, int A[], int B[], int D[]) {
	for(int i = 0 ; i < N - 1 ; i ++)
		adj[A[i]+1].pb({B[i]+1 , D[i]}),
		adj[B[i]+1].pb({A[i]+1 , D[i]});
	dfs();
	szdfs();
	head[1] = 1;
	chaindfs();
}

int stk[maxn] , ptr = 0;

vector < int > Adj[maxn];
bool b[maxn] , r[maxn];


void solve(int v){
	red[v] = blue[v] = inf;
	if(b[v])blue[v] = 0;
	if(r[v])red[v] = 0;
	for(auto u : Adj[v]){
		solve(u);
		blue[v] = min(blue[v] , blue[u] + h[u] - h[v]);
		red[v] = min(red[v] , red[u] + h[u] - h[v]);
	}
}


ll Query(int S, int X[], int T, int Y[]) {
	ll ans = inf;
	vector < int > vec;
	for(int i = 0 ; i < S ; i ++)vec.pb(X[i]+1) , b[X[i] + 1] = 1;
	for(int i = 0 ; i < T ; i ++)vec.pb(Y[i]+1) , r[Y[i] + 1] = 1;
	sort(vec.begin() , vec.end() , [](int a , int b){return st[a] < st[b];});
	for(int i = 1 ; i < S + T ; i ++)vec.pb(lca(vec[i] , vec[i - 1]));
	sort(vec.begin() , vec.end() , [](int a , int b){return st[a] < st[b];});
	vec.resize(unique(vec.begin() , vec.end()) - vec.begin());
	ptr = 0;
	stk[++ptr] = vec[0];
	for(int i = 1 ; i < (int)vec.size() ; i++){
		int v = vec[i];
		while(st[stk[ptr]] > st[v] or ft[v] > ft[stk[ptr]])ptr--;
		Adj[stk[ptr]].pb(v);
		stk[++ptr] = v;
	}
	solve(vec[0]);
    for(int i : vec)Adj[i].clear() , ans = min(ans , red[i] + blue[i]) , b[i] = r[i] = 0;
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 33 ms 24300 KB Output is correct
2 Correct 862 ms 42092 KB Output is correct
3 Correct 852 ms 42212 KB Output is correct
4 Correct 876 ms 42252 KB Output is correct
5 Correct 775 ms 42516 KB Output is correct
6 Correct 567 ms 42092 KB Output is correct
7 Correct 864 ms 42220 KB Output is correct
8 Correct 860 ms 42304 KB Output is correct
9 Correct 761 ms 42484 KB Output is correct
10 Correct 564 ms 42020 KB Output is correct
11 Correct 842 ms 42152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 24044 KB Output is correct
2 Correct 1929 ms 100756 KB Output is correct
3 Correct 1794 ms 103888 KB Output is correct
4 Correct 1082 ms 98956 KB Output is correct
5 Correct 1617 ms 144236 KB Output is correct
6 Correct 1889 ms 105840 KB Output is correct
7 Correct 1320 ms 58476 KB Output is correct
8 Correct 855 ms 58300 KB Output is correct
9 Correct 1016 ms 65480 KB Output is correct
10 Correct 1239 ms 59912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 24300 KB Output is correct
2 Correct 862 ms 42092 KB Output is correct
3 Correct 852 ms 42212 KB Output is correct
4 Correct 876 ms 42252 KB Output is correct
5 Correct 775 ms 42516 KB Output is correct
6 Correct 567 ms 42092 KB Output is correct
7 Correct 864 ms 42220 KB Output is correct
8 Correct 860 ms 42304 KB Output is correct
9 Correct 761 ms 42484 KB Output is correct
10 Correct 564 ms 42020 KB Output is correct
11 Correct 842 ms 42152 KB Output is correct
12 Correct 17 ms 24044 KB Output is correct
13 Correct 1929 ms 100756 KB Output is correct
14 Correct 1794 ms 103888 KB Output is correct
15 Correct 1082 ms 98956 KB Output is correct
16 Correct 1617 ms 144236 KB Output is correct
17 Correct 1889 ms 105840 KB Output is correct
18 Correct 1320 ms 58476 KB Output is correct
19 Correct 855 ms 58300 KB Output is correct
20 Correct 1016 ms 65480 KB Output is correct
21 Correct 1239 ms 59912 KB Output is correct
22 Correct 3025 ms 114348 KB Output is correct
23 Correct 2886 ms 115800 KB Output is correct
24 Correct 3180 ms 119492 KB Output is correct
25 Correct 2980 ms 122196 KB Output is correct
26 Correct 3074 ms 113636 KB Output is correct
27 Correct 2963 ms 150356 KB Output is correct
28 Correct 1655 ms 111960 KB Output is correct
29 Correct 3110 ms 112612 KB Output is correct
30 Correct 3048 ms 111876 KB Output is correct
31 Correct 2970 ms 112544 KB Output is correct
32 Correct 1489 ms 67764 KB Output is correct
33 Correct 889 ms 61236 KB Output is correct
34 Correct 1479 ms 57096 KB Output is correct
35 Correct 1398 ms 56812 KB Output is correct
36 Correct 1424 ms 57708 KB Output is correct
37 Correct 1515 ms 57984 KB Output is correct