Submission #52217

# Submission time Handle Problem Language Result Execution time Memory
52217 2018-06-25T05:07:04 Z 윤교준(#1348) Factories (JOI14_factories) C++11
100 / 100
3835 ms 200924 KB
#include "factories.h"
#include <bits/stdc++.h>
#define pb push_back
#define upmin(a,b) (a)=min((a),(b))
#define INF (0x3f3f3f3f)
#define INFLL (0x3f3f3f3f3f3f3f3fll)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
void fg(vector<pii> G[], int a, int b, int c) { G[a].pb({b, c}); G[b].pb({a, c}); }
void fg(vector<int> G[], int a, int b) { G[a].pb(b); G[b].pb(a); }
void init();

const int MAXN = 500005;

vector<pii> G[MAXN];

int prt[MAXN][19];
ll dl[MAXN];
int dep[MAXN], dfsi[MAXN];

int A[MAXN], B[MAXN], C[MAXN];

int N;

int lca(int a, int b) {
	if(dep[a] > dep[b]) swap(a, b);
	int dt = dep[b] - dep[a];
	for(int i = 0; i < 19; i++) if(dt & (1<<i))
		b = prt[b][i];
	if(a == b) return a;
	for(int i = 18; ~i; i--) if(prt[a][i] != prt[b][i]) {
		a = prt[a][i]; b = prt[b][i];
	}
	return prt[a][0];
}

void dfs(int idx, int &c) {
	for(pii e : G[idx]) {
		int v = e.first;
		if(dep[v]) continue;
		dep[v] = dep[idx] + 1;
		dl[v] = dl[idx] + e.second;
		prt[v][0] = idx;
		dfs(v, c);
	}
	c++; dfsi[idx] = c;
}

void Init(int N, int A[], int B[], int D[]) {
	::N = N;
	for(int i = 0; i < N-1; i++) {
		::A[i+1] = A[i]+1;
		::B[i+1] = B[i]+1;
		::C[i+1] = D[i];
		fg(G, A[i]+1, B[i]+1, D[i]);
	}
	init();
}

vector<int> SG[MAXN];

ll mnt1[MAXN], mnt2[MAXN];
int TV[MAXN], TL[MAXN], TLO[MAXN];
int ud[MAXN];
int typ[MAXN];

ll Ans;

/*
void f(int idx) {
	if(1 == typ[idx]) mnt1[idx] = 0;
	if(2 == typ[idx]) mnt2[idx] = 0;

	for(int v : SG[idx]) if(dfsi[v] < dfsi[idx]) {
		f(v);

		ll l = dl[v] - dl[idx];
		upmin(Ans, mnt1[idx] + mnt2[v] + l);
		upmin(Ans, mnt1[v] + mnt2[idx] + l);
		upmin(mnt1[idx], mnt1[v] + l);
		upmin(mnt2[idx], mnt2[v] + l);
	}
}
*/

void doit(int a, int b) {
	ll l = dl[b] - dl[a];
	upmin(Ans, mnt1[a] + mnt2[b] + l);
	upmin(Ans, mnt1[b] + mnt2[a] + l);
	upmin(mnt1[a], mnt1[b] + l);
	upmin(mnt2[a], mnt2[b] + l);
}

int uf(int i) { return ud[i] == i ? i : (ud[i] = uf(ud[i])); }
void uf(int a, int b) { ud[uf(b)] = uf(a); }
long long Query(int Xn, int X[], int Yn, int Y[]) {
	Ans = INFLL;

	for(int i = 0; i < Xn; i++) X[i]++;
	for(int i = 0; i < Yn; i++) Y[i]++;

	for(int i = 0; i < Xn; i++) typ[X[i]] = 1;
	for(int i = 0; i < Yn; i++) typ[Y[i]] = 2;
	for(int i = 0; i < Xn; i++) mnt1[X[i]] = 0;
	for(int i = 0; i < Yn; i++) mnt2[Y[i]] = 0;
	
	for(int i = 0; i < Xn; i++) TV[i] = X[i];
	for(int i = 0; i < Yn; i++) TV[Xn+i] = Y[i];
	sort(TV, TV+Xn+Yn, [&](int a, int b) {
		return dfsi[a] < dfsi[b];
	});

	for(int i = 1; i < Xn+Yn; i++)
		TL[i] = lca(TV[i-1], TV[i]);
	
	iota(TLO, TLO+Xn+Yn, 0);
	sort(TLO+1, TLO+Xn+Yn, [&](int a, int b) {
		return TL[a] == TL[b] ? a < b : dfsi[TL[a]] < dfsi[TL[b]];
	});

	for(int oi = 1; oi < Xn+Yn; oi++) {
		int idx = TLO[oi];
		int a = TV[idx-1], b = TV[idx];
		a = uf(a); b = uf(b);
		if(a == b) continue;
		if(a == TL[idx]) {
			//fg(SG, a, b);
			doit(a, b);
			uf(a, b);
		} else if(b == TL[idx]) {
			//fg(SG, a, b);
			doit(b, a);
			uf(b, a);
		} else {
			//fg(SG, a, TL[idx]);
			//fg(SG, b, TL[idx]);
			doit(TL[idx], a);
			doit(TL[idx], b);
			uf(TL[idx], a);
			uf(TL[idx], b);
		}
	}

	//int ridx = TL[TLO[Xn+Yn-1]];
	//f(ridx);

	

	for(int i = 0; i < Xn; i++) typ[X[i]] = 0;
	for(int i = 0; i < Yn; i++) typ[Y[i]] = 0;
	for(int i = 0; i < Xn+Yn; i++) ud[TV[i]] = TV[i];
	for(int i = 1; i < Xn+Yn; i++) ud[TL[i]] = TL[i];
	//for(int i = 0; i < Xn+Yn; i++) SG[TV[i]].clear();
	//for(int i = 1; i < Xn+Yn; i++) SG[TL[i]].clear();
	for(int i = 0; i < Xn+Yn; i++) mnt1[TV[i]] = mnt2[TV[i]] = INFLL;
	for(int i = 1; i < Xn+Yn; i++) mnt1[TL[i]] = mnt2[TL[i]] = INFLL;

	return Ans;
}

void init() {
	{
		int c = 0;
		dep[1] = 1;
		dfs(1, c);
	}
	for(int j = 1; j < 19; j++) for(int i = 1; i <= N; i++)
		prt[i][j] = prt[prt[i][j-1]][j-1];
	
	iota(ud, ud+MAXN, 0);
	fill(mnt1, mnt1+MAXN, INFLL);
	fill(mnt2, mnt2+MAXN, INFLL);
}
# Verdict Execution time Memory Grader output
1 Correct 50 ms 34040 KB Output is correct
2 Correct 824 ms 42736 KB Output is correct
3 Correct 871 ms 42744 KB Output is correct
4 Correct 852 ms 42756 KB Output is correct
5 Correct 613 ms 42920 KB Output is correct
6 Correct 658 ms 42920 KB Output is correct
7 Correct 922 ms 42920 KB Output is correct
8 Correct 1018 ms 42920 KB Output is correct
9 Correct 681 ms 43132 KB Output is correct
10 Correct 740 ms 43132 KB Output is correct
11 Correct 986 ms 43132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 43132 KB Output is correct
2 Correct 2009 ms 118444 KB Output is correct
3 Correct 2580 ms 119008 KB Output is correct
4 Correct 1495 ms 119240 KB Output is correct
5 Correct 2759 ms 135972 KB Output is correct
6 Correct 2730 ms 135972 KB Output is correct
7 Correct 1962 ms 135972 KB Output is correct
8 Correct 1068 ms 135972 KB Output is correct
9 Correct 1613 ms 135972 KB Output is correct
10 Correct 1837 ms 135972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 34040 KB Output is correct
2 Correct 824 ms 42736 KB Output is correct
3 Correct 871 ms 42744 KB Output is correct
4 Correct 852 ms 42756 KB Output is correct
5 Correct 613 ms 42920 KB Output is correct
6 Correct 658 ms 42920 KB Output is correct
7 Correct 922 ms 42920 KB Output is correct
8 Correct 1018 ms 42920 KB Output is correct
9 Correct 681 ms 43132 KB Output is correct
10 Correct 740 ms 43132 KB Output is correct
11 Correct 986 ms 43132 KB Output is correct
12 Correct 35 ms 43132 KB Output is correct
13 Correct 2009 ms 118444 KB Output is correct
14 Correct 2580 ms 119008 KB Output is correct
15 Correct 1495 ms 119240 KB Output is correct
16 Correct 2759 ms 135972 KB Output is correct
17 Correct 2730 ms 135972 KB Output is correct
18 Correct 1962 ms 135972 KB Output is correct
19 Correct 1068 ms 135972 KB Output is correct
20 Correct 1613 ms 135972 KB Output is correct
21 Correct 1837 ms 135972 KB Output is correct
22 Correct 3260 ms 135972 KB Output is correct
23 Correct 2958 ms 135972 KB Output is correct
24 Correct 3423 ms 135972 KB Output is correct
25 Correct 3648 ms 150028 KB Output is correct
26 Correct 3565 ms 170044 KB Output is correct
27 Correct 3280 ms 197012 KB Output is correct
28 Correct 2472 ms 197012 KB Output is correct
29 Correct 3787 ms 197012 KB Output is correct
30 Correct 3813 ms 197012 KB Output is correct
31 Correct 3835 ms 197012 KB Output is correct
32 Correct 1507 ms 197012 KB Output is correct
33 Correct 1323 ms 197012 KB Output is correct
34 Correct 1839 ms 197012 KB Output is correct
35 Correct 1719 ms 197012 KB Output is correct
36 Correct 1990 ms 197012 KB Output is correct
37 Correct 1869 ms 200924 KB Output is correct