Submission #848255

# Submission time Handle Problem Language Result Execution time Memory
848255 2023-09-11T20:37:11 Z damot67679 Cats or Dogs (JOI18_catdog) C++14
100 / 100
324 ms 27732 KB
#include "catdog.h"
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5;

vector<int> adj[N];

int par[N], chain[N], head[N], tail[N], tin[N], sz[N];
int tchain = 0, tcur = 0, n;

void dfs_size(int x, int p) {
	sz[x] = 1;
	par[x] = p;
	for (int y: adj[x]) {
		if (y == p) continue;
		dfs_size(y, x);
		sz[x] += sz[y];
	}
}

void dfs(int x) {
	tin[x] = ++tcur;	
	if (!chain[x]) {
		chain[x] = ++tchain;
		head[tchain] = x;
	}
	tail[chain[x]] = x;
	int spec = -1;
	for (int y: adj[x]) {
		if (y == par[x]) continue;
		if (spec == -1 || sz[y] > sz[spec])
			spec = y;
	}
	if (spec != -1) {
		chain[spec] = tchain;
		dfs(spec);
	}
	for (int y: adj[x])
		if (y != par[x] && y != spec)
			dfs(y);
}

const int INF = 1e9;
struct Node {
	int val[2][2];
	
	Node() = default;
	
	Node(bool set_inf) {
		if (set_inf) {
			for (int a: {0, 1})
				for (int b: {0, 1})
					val[a][b] = INF;
		}
	}
	
	void debug() const {
		cerr << string(10, '=') << '\n';
		for (int a: {0, 1}) for (int b: {0, 1})
			fprintf(stderr, "val[%d][%d] = %d\n", a+1, b+1, val[a][b]);
		cerr << string(10, '=') << "\n";
	}
	
	int res() const {
		int ans = INF;
		for (int a: {0, 1})
			for (int b: {0, 1})
				ans = min(ans, val[a][b]);
		// cerr << "THIS IS RES ====\n";
		// debug();
		// cerr << string(15, '=') << "\n\n";
		return ans;
	}
	
	Node operator + (const Node &other) const {
		Node ans;
		for (int a = 0; a < 2; a++) {
			for (int d = 0; d < 2; d++) {
				int cur = INF;
				for (int b = 0; b < 2; b++)
					for (int c = 0; c < 2; c++)
						cur = min(cur, val[a][b] + other.val[c][d] + (b != c));
				ans.val[a][d] = cur;
			}
		}
		return ans;
	}
} IT[N << 2];

int dp[N][2]; // only consider vertices NOT in heavy chain, segment tree will consider the rest + dp
int up_par[N][2]; // for chain_head to update dp[par]'s values

Node node_inf(true);

void build(int id, int l, int r) {
	if (l == r) {
		for (int a: {0, 1})
			for (int b: {0, 1})
				IT[id].val[a][b] = (a != b ? INF : 0);
		return;
	}
	int mid = (l + r) / 2;
	build(id << 1, l, mid);
	build(id << 1 | 1, mid + 1, r);
	IT[id] = IT[id << 1] + IT[id << 1 | 1];
}

void initialize(int n, vector<int> A, vector<int> B) {
	::n = n;
	for (int i = 0; i < n - 1; i++) {
		int x = A[i], y = B[i];
		adj[x].emplace_back(y);
		adj[y].emplace_back(x);
	}
	dfs_size(1, 0);
	dfs(1);
	build(1, 1, n);
	// for (int i = 2; i <= n; i++) {
		// cerr << tin[par[i]] << ' ' << tin[i] << '\n';
	// }
}

void invalid_color(int x, int c, int id, int l, int r) {
	if (l == r) {
		// if (c ^ color) == 0 => skip
		IT[id] = node_inf;
		for (int col: {0, 1})
			if ((col + 1) ^ c)
				IT[id].val[col][col] = dp[x][col];
		// if (x == 2) cerr << string(25, '?') << " " << dp[2][0] << '\n';
		// IT[id].debug();
		return;
	}
	int mid = (l + r) / 2;
	if (x <= mid) invalid_color(x, c, id << 1, l, mid);
	else invalid_color(x, c, id << 1 | 1, mid + 1, r);
	IT[id] = IT[id << 1] + IT[id << 1 | 1];
}

void update_val(int x, int w[], int id, int l, int r) {
	if (l == r) {
		// assert(([&] () -> bool {
			// for (int a: {0, 1})
				// for (int b: {0, 1})
					// if (a != b && IT[id].val[a][b] != INF)
						// return false;
			// return true;
		// }()));
		for (int c: {0, 1})
			if (IT[id].val[c][c] != INF)
				IT[id].val[c][c] += w[c];
		// cerr << "x = " << x << '\n';
		// IT[id].debug();
		return;
	}
	int mid = (l + r) / 2;
	if (x <= mid) update_val(x, w, id << 1, l, mid);
	else update_val(x, w, id << 1 | 1, mid + 1, r);
	IT[id] = IT[id << 1] + IT[id << 1 | 1];
}

Node get_range(int x, int y, int id, int l, int r) {
	if (x <= l && r <= y) return /*fprintf(stderr, "get_range(%d, %d)\n", l, r), IT[id].debug(), */IT[id];
	int mid = (l + r) / 2;
	if (y <= mid) return get_range(x, y, id << 1, l, mid);
	if (x > mid) return get_range(x, y, id << 1 | 1, mid + 1, r);
	return get_range(x, y, id << 1, l, mid) + get_range(x, y, id << 1 | 1, mid + 1, r);
}

int get(int x) {
	int xchain = chain[x], head_ = head[xchain], tail_ = tail[xchain];
	// cerr << "head tail = " << head_ << ' ' << tail_ << '\n';
	while (xchain != 1) {
		auto S = get_range(tin[head_], tin[tail_], 1, 1, n);
		int val[] = {min(S.val[0][0], S.val[0][1]), min(S.val[1][0], S.val[1][1])};
		int upd[] = {-up_par[tin[head_]][0] + min(val[0], val[1] + 1), 
								 -up_par[tin[head_]][1] + min(val[0] + 1, val[1])};
		// S.debug();
		// fprintf(stderr, "update_val(%d, {%d, %d})\n", tin[par[head_]], upd[0], upd[1]);
		update_val(tin[par[head_]], upd, 1, 1, n);
		dp[tin[par[head_]]][0] += upd[0];
		dp[tin[par[head_]]][1] += upd[1];
		up_par[tin[head_]][0] = min(val[0], val[1] + 1);
		up_par[tin[head_]][1] = min(val[0] + 1, val[1]);
		xchain = chain[par[head_]];
		tail_ = tail[xchain];
		head_ = head[xchain];
	}
	// cerr << "hehehehe " << tin[head_] << ' ' << tin[tail_] << '\n';
	return get_range(tin[head_], tin[tail_], 1, 1, n).res();
}

int solve(int s, int c) {
	// cerr << "solve s = " << tin[s] << ", c = " << (c == 1 ? 1 : (c == 2 ? 0 : -1)) << '\n';
	// fprintf(stderr, "invalid_color(%d, %d)\n", tin[s], c);
	invalid_color(tin[s], c, 1, 1, n);
	return get(s);
}

int cat(int v) {
  return solve(v, 2);
}

int dog(int v) {
  return solve(v, 1);
}

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

#ifdef LOCAL
int readInt(){
	int i;
	if(scanf("%d",&i)!=1){
		fprintf(stderr,"Error while reading input\n");
		exit(1);
	}
	return i;
}

int main(){
	int N=readInt();
	
	std::vector<int> A(N-1),B(N-1);
	for(int i=0;i<N-1;i++)
	{
		A[i]=readInt();
		B[i]=readInt();
	}
	int Q;
	assert(scanf("%d",&Q)==1);
	std::vector <int> T(Q),V(Q);
	for(int i=0;i<Q;i++)
	{
		T[i]=readInt();
		V[i]=readInt();
	}
	
	initialize(N,A,B);
	
	// int db[] = {1, 0};
	// update_val(2, db, 1, 1, n);
	// invalid_color(1, 2, 1, 1, n);
	// get_range(1, 2, 1, 1, n).res();
// 	
	// return 0;
	
	// for (int i = 1; i <= tchain; i++)
		// cerr << "chain " << i << " = [" << tin[head[i]] << ", " << tin[tail[i]] << "]\n";
	
	std::vector<int> res(Q);
	for(int j=0;j<Q;j++)
	{
		if(T[j]==1) res[j]=cat(V[j]);
		else if(T[j]==2) res[j]=dog(V[j]);
		else res[j]=neighbor(V[j]);
	}
	// get_range(2, 2, 1, 1, n).debug();
	for(int j=0;j<Q;j++)
		printf("%d\n",res[j]);
	// for (int x = 1; x <= N; x++)
		// cerr << x << ' ' << chain[x] << ' ' << head[chain[x]] << ' ' << tail[chain[x]] << '\n';
	// get_range(2, 2, 1, 1, n).debug();
	return 0;
}
#endif
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 1 ms 8536 KB Output is correct
5 Correct 1 ms 8536 KB Output is correct
6 Correct 1 ms 8536 KB Output is correct
7 Correct 1 ms 8536 KB Output is correct
8 Correct 1 ms 8548 KB Output is correct
9 Correct 2 ms 8536 KB Output is correct
10 Correct 1 ms 8536 KB Output is correct
11 Correct 2 ms 8536 KB Output is correct
12 Correct 1 ms 8536 KB Output is correct
13 Correct 1 ms 8536 KB Output is correct
14 Correct 1 ms 8536 KB Output is correct
15 Correct 1 ms 8536 KB Output is correct
16 Correct 1 ms 8536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 1 ms 8536 KB Output is correct
5 Correct 1 ms 8536 KB Output is correct
6 Correct 1 ms 8536 KB Output is correct
7 Correct 1 ms 8536 KB Output is correct
8 Correct 1 ms 8548 KB Output is correct
9 Correct 2 ms 8536 KB Output is correct
10 Correct 1 ms 8536 KB Output is correct
11 Correct 2 ms 8536 KB Output is correct
12 Correct 1 ms 8536 KB Output is correct
13 Correct 1 ms 8536 KB Output is correct
14 Correct 1 ms 8536 KB Output is correct
15 Correct 1 ms 8536 KB Output is correct
16 Correct 1 ms 8536 KB Output is correct
17 Correct 2 ms 8536 KB Output is correct
18 Correct 2 ms 8536 KB Output is correct
19 Correct 2 ms 8536 KB Output is correct
20 Correct 2 ms 8536 KB Output is correct
21 Correct 2 ms 8536 KB Output is correct
22 Correct 2 ms 8536 KB Output is correct
23 Correct 3 ms 8680 KB Output is correct
24 Correct 2 ms 8536 KB Output is correct
25 Correct 2 ms 8536 KB Output is correct
26 Correct 2 ms 8536 KB Output is correct
27 Correct 1 ms 8536 KB Output is correct
28 Correct 2 ms 8540 KB Output is correct
29 Correct 2 ms 8792 KB Output is correct
30 Correct 2 ms 8536 KB Output is correct
31 Correct 2 ms 8792 KB Output is correct
32 Correct 2 ms 8536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 1 ms 8536 KB Output is correct
5 Correct 1 ms 8536 KB Output is correct
6 Correct 1 ms 8536 KB Output is correct
7 Correct 1 ms 8536 KB Output is correct
8 Correct 1 ms 8548 KB Output is correct
9 Correct 2 ms 8536 KB Output is correct
10 Correct 1 ms 8536 KB Output is correct
11 Correct 2 ms 8536 KB Output is correct
12 Correct 1 ms 8536 KB Output is correct
13 Correct 1 ms 8536 KB Output is correct
14 Correct 1 ms 8536 KB Output is correct
15 Correct 1 ms 8536 KB Output is correct
16 Correct 1 ms 8536 KB Output is correct
17 Correct 2 ms 8536 KB Output is correct
18 Correct 2 ms 8536 KB Output is correct
19 Correct 2 ms 8536 KB Output is correct
20 Correct 2 ms 8536 KB Output is correct
21 Correct 2 ms 8536 KB Output is correct
22 Correct 2 ms 8536 KB Output is correct
23 Correct 3 ms 8680 KB Output is correct
24 Correct 2 ms 8536 KB Output is correct
25 Correct 2 ms 8536 KB Output is correct
26 Correct 2 ms 8536 KB Output is correct
27 Correct 1 ms 8536 KB Output is correct
28 Correct 2 ms 8540 KB Output is correct
29 Correct 2 ms 8792 KB Output is correct
30 Correct 2 ms 8536 KB Output is correct
31 Correct 2 ms 8792 KB Output is correct
32 Correct 2 ms 8536 KB Output is correct
33 Correct 189 ms 15040 KB Output is correct
34 Correct 62 ms 14684 KB Output is correct
35 Correct 172 ms 14436 KB Output is correct
36 Correct 280 ms 19680 KB Output is correct
37 Correct 13 ms 12496 KB Output is correct
38 Correct 317 ms 20288 KB Output is correct
39 Correct 308 ms 20404 KB Output is correct
40 Correct 298 ms 20288 KB Output is correct
41 Correct 324 ms 20416 KB Output is correct
42 Correct 284 ms 20420 KB Output is correct
43 Correct 294 ms 20544 KB Output is correct
44 Correct 292 ms 20288 KB Output is correct
45 Correct 288 ms 20420 KB Output is correct
46 Correct 319 ms 20556 KB Output is correct
47 Correct 311 ms 20280 KB Output is correct
48 Correct 90 ms 18596 KB Output is correct
49 Correct 107 ms 19484 KB Output is correct
50 Correct 44 ms 12480 KB Output is correct
51 Correct 41 ms 13492 KB Output is correct
52 Correct 17 ms 12120 KB Output is correct
53 Correct 130 ms 19116 KB Output is correct
54 Correct 95 ms 13732 KB Output is correct
55 Correct 250 ms 18756 KB Output is correct
56 Correct 155 ms 14588 KB Output is correct
57 Correct 209 ms 19020 KB Output is correct
58 Correct 17 ms 13268 KB Output is correct
59 Correct 39 ms 13392 KB Output is correct
60 Correct 82 ms 19024 KB Output is correct
61 Correct 112 ms 19360 KB Output is correct
62 Correct 54 ms 17608 KB Output is correct
63 Correct 31 ms 16468 KB Output is correct
64 Correct 39 ms 18256 KB Output is correct
65 Correct 45 ms 24144 KB Output is correct
66 Correct 50 ms 13960 KB Output is correct
67 Correct 48 ms 20816 KB Output is correct
68 Correct 106 ms 24400 KB Output is correct
69 Correct 23 ms 10012 KB Output is correct
70 Correct 6 ms 8792 KB Output is correct
71 Correct 41 ms 16540 KB Output is correct
72 Correct 60 ms 23120 KB Output is correct
73 Correct 146 ms 27732 KB Output is correct
74 Correct 167 ms 24144 KB Output is correct
75 Correct 109 ms 27476 KB Output is correct
76 Correct 104 ms 26192 KB Output is correct
77 Correct 182 ms 24572 KB Output is correct