Submission #134140

# Submission time Handle Problem Language Result Execution time Memory
134140 2019-07-22T06:46:10 Z ainta Cats or Dogs (JOI18_catdog) C++17
100 / 100
362 ms 38140 KB
#include "catdog.h"
#include<vector>
#include<algorithm>
#define N_ 101000
#define pii pair<int,int>
using namespace std;

int w[N_], n, par[N_], C[N_], HL[N_][2], INF = 1e9;
vector<int>E[N_], Ch[N_];

struct Mat {
	int A[2][2];
	Mat() {
		for (int i = 0; i < 2; i++)for (int j = 0; j < 2; j++)A[i][j] = 0;
	}
};
Mat Merge(Mat a, Mat b) {
	int i, k, j;
	Mat T;
	for (i = 0; i < 2; i++)for (j = 0; j < 2; j++)T.A[i][j] = INF;
	for (i = 0; i < 2; i++) for (k = 0; k < 2; k++) for (j = 0; j < 2; j++) T.A[i][j] = min(T.A[i][j], a.A[i][k] + b.A[k][j]);
	return T;
}

struct Tree {
	vector<int>V;
	vector<Mat>IT;
	vector<pii>U;
	int n, sz;
	void UDT(int nd) {
		IT[nd] = Merge(IT[nd * 2], IT[nd * 2 + 1]);
	}
	void init(int nd, int b, int e) {
		if (b == e) {
			IT[nd].A[0][0] = IT[nd].A[1][1] = 0;
			IT[nd].A[0][1] = IT[nd].A[1][0] = 1;
			return;
		}
		int m = (b + e) >> 1;
		init(nd * 2, b, m);
		init(nd * 2 + 1, m + 1, e);
		UDT(nd);
	}
	void init() {
		if (V.empty())return;
		n = V.size();
		U.resize(n);
		sz = 1;
		while (sz <= n)sz *= 2;
		IT.resize(sz * 2);
		init(1, 0, n - 1);
	}
	void Put(int nd, int b, int e, int x, int cur) {
		if (b == e) {
			IT[nd].A[0][0] = U[b].first;
			IT[nd].A[1][1] = U[b].second;
			IT[nd].A[0][1] = U[b].first + 1;
			IT[nd].A[1][0] = U[b].second + 1;
			if (cur == 1) IT[nd].A[1][0] = IT[nd].A[1][1] = INF;
			if (cur == 2) IT[nd].A[0][0] = IT[nd].A[0][1] = INF;
			return;
		}
		int m = (b + e) >> 1;
		if (x <= m)Put(nd * 2, b, m, x, cur);
		else Put(nd * 2 + 1, m + 1, e, x, cur);
		UDT(nd);
	}
	pii Get() {
		return { min(IT[1].A[0][0],IT[1].A[1][0]), min(IT[1].A[0][1], IT[1].A[1][1]) };
	}
	void Put(int x, int cur) {
		Put(1, 0, n - 1, x, cur);
	}
}T[N_];

void DFS(int a, int pp) {
	par[a] = pp;
	C[a] = 1;
	for (auto &x : E[a]) {
		if (x == pp)continue;
		DFS(x, a);

		Ch[a].push_back(x);
		C[a] += C[x];
	}
}

void HLD(int a, int ppp) {
	T[ppp].V.push_back(a);
	int pv = -1, Mx = -1;
	for (auto &x : Ch[a]) {
		if (Mx < C[x]) {
			Mx = C[x];
			pv = x;
		}
	}
	if (pv == -1)return;
	HLD(pv, ppp);
	for (auto &x : Ch[a]) {
		if(x!=pv)HLD(x, x);
	}
}

void initialize(int N, std::vector<int> A, std::vector<int> B) {
	n = N;
	int i;
	for (i = 0; i < n - 1; i++) {
		E[A[i]].push_back(B[i]);
		E[B[i]].push_back(A[i]);
	}
	DFS(1, 0);
	HLD(1, 1);
	for (i = 1; i <= n; i++) {
		if (T[i].V.empty())continue;
		reverse(T[i].V.begin(), T[i].V.end());
		for (int j = 0; j < T[i].V.size(); j++) {
			int a = T[i].V[j];
			HL[a][0] = i, HL[a][1] = j;
		}
		T[i].init();
	}
}

void Add(int v, pii bef, pii aft) {
	int fi = aft.first - bef.first;
	int se = aft.second - bef.second;
	int a = HL[v][0], b = HL[v][1];
	T[a].U[b].first += fi;
	T[a].U[b].second += se;
}

int Go(int v, int ck) {
	int a = HL[v][0], b = HL[v][1];
	w[v] = ck;
	while (1) {
		pii bef = T[a].Get();
		T[a].Put(b, ck);
		pii aft = T[a].Get();
		int pp = par[a];
		if (!pp)break;
		Add(pp, bef, aft);
		a = HL[pp][0], b = HL[pp][1];
		ck = w[pp];
	}
	pii z = T[1].Get();
	return min(z.first, z.second);
}

int cat(int v) {
	return Go(v, 1);
}

int dog(int v) {
	return Go(v, 2);
}

int neighbor(int v) {
	return Go(v, 0);
}

Compilation message

catdog.cpp: In function 'void initialize(int, std::vector<int>, std::vector<int>)':
catdog.cpp:116:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 0; j < T[i].V.size(); j++) {
                   ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 12920 KB Output is correct
2 Correct 13 ms 12920 KB Output is correct
3 Correct 16 ms 12920 KB Output is correct
4 Correct 13 ms 13048 KB Output is correct
5 Correct 12 ms 13048 KB Output is correct
6 Correct 13 ms 12920 KB Output is correct
7 Correct 13 ms 13048 KB Output is correct
8 Correct 13 ms 13052 KB Output is correct
9 Correct 13 ms 13048 KB Output is correct
10 Correct 13 ms 12920 KB Output is correct
11 Correct 13 ms 12944 KB Output is correct
12 Correct 13 ms 13048 KB Output is correct
13 Correct 16 ms 12920 KB Output is correct
14 Correct 16 ms 12956 KB Output is correct
15 Correct 14 ms 13012 KB Output is correct
16 Correct 14 ms 12920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 12920 KB Output is correct
2 Correct 13 ms 12920 KB Output is correct
3 Correct 16 ms 12920 KB Output is correct
4 Correct 13 ms 13048 KB Output is correct
5 Correct 12 ms 13048 KB Output is correct
6 Correct 13 ms 12920 KB Output is correct
7 Correct 13 ms 13048 KB Output is correct
8 Correct 13 ms 13052 KB Output is correct
9 Correct 13 ms 13048 KB Output is correct
10 Correct 13 ms 12920 KB Output is correct
11 Correct 13 ms 12944 KB Output is correct
12 Correct 13 ms 13048 KB Output is correct
13 Correct 16 ms 12920 KB Output is correct
14 Correct 16 ms 12956 KB Output is correct
15 Correct 14 ms 13012 KB Output is correct
16 Correct 14 ms 12920 KB Output is correct
17 Correct 14 ms 13176 KB Output is correct
18 Correct 14 ms 13064 KB Output is correct
19 Correct 14 ms 13176 KB Output is correct
20 Correct 13 ms 13048 KB Output is correct
21 Correct 13 ms 13048 KB Output is correct
22 Correct 14 ms 13176 KB Output is correct
23 Correct 15 ms 13096 KB Output is correct
24 Correct 14 ms 13180 KB Output is correct
25 Correct 14 ms 13048 KB Output is correct
26 Correct 14 ms 13104 KB Output is correct
27 Correct 13 ms 13084 KB Output is correct
28 Correct 14 ms 13180 KB Output is correct
29 Correct 14 ms 13176 KB Output is correct
30 Correct 13 ms 13048 KB Output is correct
31 Correct 14 ms 13132 KB Output is correct
32 Correct 13 ms 13048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 12920 KB Output is correct
2 Correct 13 ms 12920 KB Output is correct
3 Correct 16 ms 12920 KB Output is correct
4 Correct 13 ms 13048 KB Output is correct
5 Correct 12 ms 13048 KB Output is correct
6 Correct 13 ms 12920 KB Output is correct
7 Correct 13 ms 13048 KB Output is correct
8 Correct 13 ms 13052 KB Output is correct
9 Correct 13 ms 13048 KB Output is correct
10 Correct 13 ms 12920 KB Output is correct
11 Correct 13 ms 12944 KB Output is correct
12 Correct 13 ms 13048 KB Output is correct
13 Correct 16 ms 12920 KB Output is correct
14 Correct 16 ms 12956 KB Output is correct
15 Correct 14 ms 13012 KB Output is correct
16 Correct 14 ms 12920 KB Output is correct
17 Correct 14 ms 13176 KB Output is correct
18 Correct 14 ms 13064 KB Output is correct
19 Correct 14 ms 13176 KB Output is correct
20 Correct 13 ms 13048 KB Output is correct
21 Correct 13 ms 13048 KB Output is correct
22 Correct 14 ms 13176 KB Output is correct
23 Correct 15 ms 13096 KB Output is correct
24 Correct 14 ms 13180 KB Output is correct
25 Correct 14 ms 13048 KB Output is correct
26 Correct 14 ms 13104 KB Output is correct
27 Correct 13 ms 13084 KB Output is correct
28 Correct 14 ms 13180 KB Output is correct
29 Correct 14 ms 13176 KB Output is correct
30 Correct 13 ms 13048 KB Output is correct
31 Correct 14 ms 13132 KB Output is correct
32 Correct 13 ms 13048 KB Output is correct
33 Correct 152 ms 23808 KB Output is correct
34 Correct 104 ms 25080 KB Output is correct
35 Correct 130 ms 20924 KB Output is correct
36 Correct 246 ms 31424 KB Output is correct
37 Correct 44 ms 18680 KB Output is correct
38 Correct 267 ms 33472 KB Output is correct
39 Correct 278 ms 33512 KB Output is correct
40 Correct 277 ms 33508 KB Output is correct
41 Correct 276 ms 33508 KB Output is correct
42 Correct 280 ms 33384 KB Output is correct
43 Correct 284 ms 33512 KB Output is correct
44 Correct 276 ms 33384 KB Output is correct
45 Correct 285 ms 33512 KB Output is correct
46 Correct 362 ms 33508 KB Output is correct
47 Correct 272 ms 33484 KB Output is correct
48 Correct 134 ms 30332 KB Output is correct
49 Correct 153 ms 34720 KB Output is correct
50 Correct 45 ms 17528 KB Output is correct
51 Correct 61 ms 21440 KB Output is correct
52 Correct 36 ms 17400 KB Output is correct
53 Correct 170 ms 31736 KB Output is correct
54 Correct 96 ms 21296 KB Output is correct
55 Correct 203 ms 27284 KB Output is correct
56 Correct 133 ms 22428 KB Output is correct
57 Correct 207 ms 30700 KB Output is correct
58 Correct 58 ms 22388 KB Output is correct
59 Correct 75 ms 20728 KB Output is correct
60 Correct 139 ms 32296 KB Output is correct
61 Correct 150 ms 33164 KB Output is correct
62 Correct 107 ms 29040 KB Output is correct
63 Correct 76 ms 24332 KB Output is correct
64 Correct 80 ms 26744 KB Output is correct
65 Correct 121 ms 35568 KB Output is correct
66 Correct 74 ms 18652 KB Output is correct
67 Correct 108 ms 28404 KB Output is correct
68 Correct 164 ms 34932 KB Output is correct
69 Correct 37 ms 14968 KB Output is correct
70 Correct 18 ms 13304 KB Output is correct
71 Correct 76 ms 23488 KB Output is correct
72 Correct 126 ms 32116 KB Output is correct
73 Correct 195 ms 38128 KB Output is correct
74 Correct 209 ms 34832 KB Output is correct
75 Correct 177 ms 38140 KB Output is correct
76 Correct 178 ms 37872 KB Output is correct
77 Correct 214 ms 35316 KB Output is correct