Submission #102479

# Submission time Handle Problem Language Result Execution time Memory
102479 2019-03-25T08:56:40 Z Dat160601 Cats or Dogs (JOI18_catdog) C++17
100 / 100
441 ms 31864 KB
#include "catdog.h"
#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second

const int N = 1e5 + 7, inf = 1e6;

int n, par[N], head[N], chain[N], pos[N], sz[N], st[N];
vector <int> edge[N];

struct node{
	int dp[2][2];
	node(){
		dp[0][0] = dp[1][1] = 0;
		dp[0][1] = dp[1][0] = inf;
	}
};

node merge(node a, node b){
	node ret;
	for(int i = 0; i <= 1; i++){
		for(int j = 0; j <= 1; j++){
			ret.dp[i][j] = inf;
			for(int k = 0; k <= 1; k++){
				for(int l = 0; l <= 1; l++){
					ret.dp[i][j] = min(ret.dp[i][j], a.dp[i][k] + b.dp[l][j] + (k ^ l));
				}
			}
		}
	}
	return ret;
}

struct segtree{
	vector <node> it;
	int sz;

	void init(int k, int l, int r){
		if(l == r) return;
		int mid = (l + r) >> 1;
		init(k << 1, l, mid);
		init(k << 1 | 1, mid + 1, r);
		it[k] = merge(it[k << 1], it[k << 1 | 1]);
	}

	void init(int pos){
		sz = pos;
		it.resize(4 * sz + 5);
		init(1, 1, sz);
	}

	void update(int k, int l, int r, int pos, int v0, int v1){
		if(l > pos || r < pos) return;
		if(l == r){
			it[k].dp[0][0] += v0;
			it[k].dp[1][1] += v1;
			return;
		}
		int mid = (l + r) >> 1;
		update(k << 1, l, mid, pos, v0, v1);
		update(k << 1 | 1, mid + 1, r, pos, v0, v1);
		it[k] = merge(it[k << 1], it[k << 1 | 1]);
	}

	void update(int pos, int v0, int v1){
		update(1, 1, sz, pos, v0, v1);
	}

	pair <int, int> get(){
		int f1 = min(it[1].dp[0][0], it[1].dp[0][1]);
		int f2 = min(it[1].dp[1][0], it[1].dp[1][1]);
		return mp(min(f1, f2 + 1), min(f1 + 1, f2));
	}
} tree[N];

void dfs(int u, int p){
	sz[u] = 1;
	for(int v : edge[u]){
		if(v == p) continue;
		par[v] = u;
		dfs(v, u);
		sz[u] += sz[v];
	}
}

void hld(int u, int p){
	int nxt = -1;
	for(int v : edge[u]){
		if(v == p) continue;
		if(nxt < 0 || sz[nxt] < sz[v]){
			nxt = v;
		}
	}
	if(nxt < 0){
		tree[chain[u]].init(pos[u]);
		return;
	}
	chain[nxt] = chain[u];
	pos[nxt] = pos[u] + 1;
	hld(nxt, u);
	for(int v : edge[u]){
		if(v == p || v == nxt) continue;
		chain[v] = v;
		head[v] = v;
		pos[v] = 1;
		hld(v, u);
	}
}

void initialize(int p, vector <int> A, vector <int> B){
	n = p;
	for(int i = 0; i < n - 1; i++){
		edge[A[i]].pb(B[i]);
		edge[B[i]].pb(A[i]);
	}
	dfs(1, 1);
	head[1] = 1, chain[1] = 1, pos[1] = 1;
	hld(1, 1);
}

void solve(int u, int v0, int v1){
	while(u){
		int cur = chain[u];
		//cout << cur << endl;
		int pv0, pv1;
		pair <int, int> now = tree[cur].get();
		pv0 = now.fi, pv1 = now.se;
		tree[cur].update(pos[u], v0, v1);
		now = tree[cur].get();
		v0 = now.fi - pv0, v1 = now.se - pv1;
		u = head[cur];
		u = par[u];
	}
}

int getans(){
	return min(min(tree[1].it[1].dp[0][0], tree[1].it[1].dp[0][1]), min(tree[1].it[1].dp[1][0], tree[1].it[1].dp[1][1]));
}

int cat(int v) {
	st[v] = 1;
	solve(v, 0, inf);
	return getans();
}

int dog(int v) {
	st[v] = 2;
	solve(v, inf, 0);
	return getans();
}

int neighbor(int v){
	if(st[v] == 0) return getans();
	if(st[v] == 1) solve(v, 0, -inf);
	if(st[v] == 2) solve(v, -inf, 0);
	st[v] = 0;
	return getans();
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5888 KB Output is correct
2 Correct 7 ms 5888 KB Output is correct
3 Correct 7 ms 5760 KB Output is correct
4 Correct 7 ms 5760 KB Output is correct
5 Correct 8 ms 5888 KB Output is correct
6 Correct 8 ms 5888 KB Output is correct
7 Correct 8 ms 5888 KB Output is correct
8 Correct 10 ms 5888 KB Output is correct
9 Correct 8 ms 5888 KB Output is correct
10 Correct 8 ms 5888 KB Output is correct
11 Correct 8 ms 5888 KB Output is correct
12 Correct 7 ms 5888 KB Output is correct
13 Correct 8 ms 5860 KB Output is correct
14 Correct 7 ms 5888 KB Output is correct
15 Correct 9 ms 5888 KB Output is correct
16 Correct 8 ms 5888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5888 KB Output is correct
2 Correct 7 ms 5888 KB Output is correct
3 Correct 7 ms 5760 KB Output is correct
4 Correct 7 ms 5760 KB Output is correct
5 Correct 8 ms 5888 KB Output is correct
6 Correct 8 ms 5888 KB Output is correct
7 Correct 8 ms 5888 KB Output is correct
8 Correct 10 ms 5888 KB Output is correct
9 Correct 8 ms 5888 KB Output is correct
10 Correct 8 ms 5888 KB Output is correct
11 Correct 8 ms 5888 KB Output is correct
12 Correct 7 ms 5888 KB Output is correct
13 Correct 8 ms 5860 KB Output is correct
14 Correct 7 ms 5888 KB Output is correct
15 Correct 9 ms 5888 KB Output is correct
16 Correct 8 ms 5888 KB Output is correct
17 Correct 8 ms 6016 KB Output is correct
18 Correct 11 ms 6016 KB Output is correct
19 Correct 9 ms 6016 KB Output is correct
20 Correct 8 ms 5888 KB Output is correct
21 Correct 8 ms 5888 KB Output is correct
22 Correct 8 ms 5888 KB Output is correct
23 Correct 9 ms 6016 KB Output is correct
24 Correct 9 ms 6016 KB Output is correct
25 Correct 10 ms 5888 KB Output is correct
26 Correct 10 ms 5888 KB Output is correct
27 Correct 8 ms 5888 KB Output is correct
28 Correct 7 ms 6016 KB Output is correct
29 Correct 10 ms 6016 KB Output is correct
30 Correct 10 ms 5888 KB Output is correct
31 Correct 9 ms 6016 KB Output is correct
32 Correct 10 ms 5888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5888 KB Output is correct
2 Correct 7 ms 5888 KB Output is correct
3 Correct 7 ms 5760 KB Output is correct
4 Correct 7 ms 5760 KB Output is correct
5 Correct 8 ms 5888 KB Output is correct
6 Correct 8 ms 5888 KB Output is correct
7 Correct 8 ms 5888 KB Output is correct
8 Correct 10 ms 5888 KB Output is correct
9 Correct 8 ms 5888 KB Output is correct
10 Correct 8 ms 5888 KB Output is correct
11 Correct 8 ms 5888 KB Output is correct
12 Correct 7 ms 5888 KB Output is correct
13 Correct 8 ms 5860 KB Output is correct
14 Correct 7 ms 5888 KB Output is correct
15 Correct 9 ms 5888 KB Output is correct
16 Correct 8 ms 5888 KB Output is correct
17 Correct 8 ms 6016 KB Output is correct
18 Correct 11 ms 6016 KB Output is correct
19 Correct 9 ms 6016 KB Output is correct
20 Correct 8 ms 5888 KB Output is correct
21 Correct 8 ms 5888 KB Output is correct
22 Correct 8 ms 5888 KB Output is correct
23 Correct 9 ms 6016 KB Output is correct
24 Correct 9 ms 6016 KB Output is correct
25 Correct 10 ms 5888 KB Output is correct
26 Correct 10 ms 5888 KB Output is correct
27 Correct 8 ms 5888 KB Output is correct
28 Correct 7 ms 6016 KB Output is correct
29 Correct 10 ms 6016 KB Output is correct
30 Correct 10 ms 5888 KB Output is correct
31 Correct 9 ms 6016 KB Output is correct
32 Correct 10 ms 5888 KB Output is correct
33 Correct 205 ms 15408 KB Output is correct
34 Correct 128 ms 17788 KB Output is correct
35 Correct 162 ms 13680 KB Output is correct
36 Correct 348 ms 24000 KB Output is correct
37 Correct 42 ms 11388 KB Output is correct
38 Correct 410 ms 25832 KB Output is correct
39 Correct 410 ms 25956 KB Output is correct
40 Correct 438 ms 25968 KB Output is correct
41 Correct 400 ms 25960 KB Output is correct
42 Correct 389 ms 25932 KB Output is correct
43 Correct 362 ms 25960 KB Output is correct
44 Correct 358 ms 25832 KB Output is correct
45 Correct 441 ms 25960 KB Output is correct
46 Correct 368 ms 25940 KB Output is correct
47 Correct 392 ms 25928 KB Output is correct
48 Correct 143 ms 24260 KB Output is correct
49 Correct 184 ms 29188 KB Output is correct
50 Correct 45 ms 10744 KB Output is correct
51 Correct 70 ms 14888 KB Output is correct
52 Correct 33 ms 10496 KB Output is correct
53 Correct 223 ms 24312 KB Output is correct
54 Correct 118 ms 14000 KB Output is correct
55 Correct 282 ms 19952 KB Output is correct
56 Correct 173 ms 15164 KB Output is correct
57 Correct 275 ms 23224 KB Output is correct
58 Correct 59 ms 15860 KB Output is correct
59 Correct 63 ms 14072 KB Output is correct
60 Correct 175 ms 26516 KB Output is correct
61 Correct 189 ms 27232 KB Output is correct
62 Correct 128 ms 22992 KB Output is correct
63 Correct 99 ms 17272 KB Output is correct
64 Correct 82 ms 19576 KB Output is correct
65 Correct 135 ms 27256 KB Output is correct
66 Correct 77 ms 11120 KB Output is correct
67 Correct 119 ms 20972 KB Output is correct
68 Correct 225 ms 27644 KB Output is correct
69 Correct 35 ms 7808 KB Output is correct
70 Correct 18 ms 6144 KB Output is correct
71 Correct 75 ms 15928 KB Output is correct
72 Correct 131 ms 24312 KB Output is correct
73 Correct 242 ms 31864 KB Output is correct
74 Correct 259 ms 27208 KB Output is correct
75 Correct 306 ms 31736 KB Output is correct
76 Correct 281 ms 30072 KB Output is correct
77 Correct 335 ms 27616 KB Output is correct