Submission #776718

# Submission time Handle Problem Language Result Execution time Memory
776718 2023-07-08T07:47:57 Z ymm Cats or Dogs (JOI18_catdog) C++17
38 / 100
3000 ms 12232 KB
#include "catdog.h"

#include <bits/stdc++.h>
#define Loop(x, l, r) for (ll x = (l); x < (r); ++x)
typedef long long ll;
typedef std::pair<int,int> pii;
using namespace std;

const int N = 100'010;
int col[N], cntcol[N][3];
vector<int> A[N];
int cpar[N];
int cnt[N];
bool vis[N];

void dfs(int v, int p) {
	cnt[v] = 1;
	for (int u : A[v]) {
		if (u == p || vis[u])
			continue;
		dfs(u, v);
		cnt[v] += cnt[u];
	}
}

int centroid(int v, int n)
{
	for (int u : A[v]) {
		if (vis[u] || cnt[u] > cnt[v])
			continue;
		if (cnt[u]*2 > n)
			return centroid(u, n);
	}
	return v;
}

int decomp(int v)
{
	dfs(v, -1);
	v = centroid(v, cnt[v]);
	vis[v] = 1;
	for (int u : A[v]) {
		if (vis[u])
			continue;
		u = decomp(u);
		cpar[u] = v;
	}
	return v;
}

void dfsdard(int v, int p)
{
	cpar[v] = p;
	for (int u : A[v])
		if (u != p)
			dfsdard(u, v);
}

void initialize(int N, std::vector<int> V, std::vector<int> U) {
	Loop (i,0,N-1) {
		V[i]--; U[i]--;
		A[V[i]].push_back(U[i]);
		A[U[i]].push_back(V[i]);
	}
	dfsdard(0, -1);
	//int rt = decomp(0);
	//cpar[rt] = -1;
}

int calc(int v) { return col[v] == 0? min(cntcol[v][1], cntcol[v][2]): cntcol[v][3-col[v]]; }
int calccol(int v) {
	if (col[v])
		return col[v];
	if (cntcol[v][1] < cntcol[v][2])
		return 2;
	if (cntcol[v][1] == cntcol[v][2])
		return 0;
	if (cntcol[v][1] > cntcol[v][2])
		return 1;
	assert(false);
}


int Ans = 0;

void ch(int v, int from, int to)
{
	int c0 = calccol(v);
	Ans -= calc(v);
	cntcol[v][from]--;
	cntcol[v][to]++;
	Ans += calc(v);
	int c1 = calccol(v);
	if (cpar[v] != -1 && c0 != c1)
		ch(cpar[v], c0, c1);
}

int cat(int v) {
	--v;
	int c0 = calccol(v);
	Ans -= calc(v);
	col[v] = 1;
	Ans += calc(v);
	int c1 = calccol(v);
	if (cpar[v] != -1 && c0 != c1)
		ch(cpar[v], c0, c1);
	return Ans;
}

int dog(int v) {
	--v;
	int c0 = calccol(v);
	Ans -= calc(v);
	col[v] = 2;
	Ans += calc(v);
	int c1 = calccol(v);
	if (cpar[v] != -1 && c0 != c1)
		ch(cpar[v], c0, c1);
	return Ans;
}

int neighbor(int v) {
	--v;
	int c0 = calccol(v);
	Ans -= calc(v);
	col[v] = 0;
	Ans += calc(v);
	int c1 = calccol(v);
	if (cpar[v] != -1 && c0 != c1)
		ch(cpar[v], c0, c1);
	return Ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 2 ms 2672 KB Output is correct
8 Correct 2 ms 2672 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 1 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 2 ms 2672 KB Output is correct
8 Correct 2 ms 2672 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 1 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 2 ms 2676 KB Output is correct
22 Correct 2 ms 2680 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 1 ms 2684 KB Output is correct
29 Correct 2 ms 2684 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 2 ms 2676 KB Output is correct
32 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 2 ms 2672 KB Output is correct
8 Correct 2 ms 2672 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 1 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 2 ms 2676 KB Output is correct
22 Correct 2 ms 2680 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 1 ms 2684 KB Output is correct
29 Correct 2 ms 2684 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 2 ms 2676 KB Output is correct
32 Correct 2 ms 2644 KB Output is correct
33 Correct 37 ms 7988 KB Output is correct
34 Correct 28 ms 7636 KB Output is correct
35 Correct 42 ms 7004 KB Output is correct
36 Correct 56 ms 11312 KB Output is correct
37 Correct 10 ms 4944 KB Output is correct
38 Correct 61 ms 12136 KB Output is correct
39 Correct 63 ms 12188 KB Output is correct
40 Correct 64 ms 12192 KB Output is correct
41 Correct 67 ms 12176 KB Output is correct
42 Correct 65 ms 12204 KB Output is correct
43 Correct 69 ms 12156 KB Output is correct
44 Correct 74 ms 12232 KB Output is correct
45 Correct 75 ms 12124 KB Output is correct
46 Correct 64 ms 12124 KB Output is correct
47 Correct 67 ms 12132 KB Output is correct
48 Correct 35 ms 8752 KB Output is correct
49 Correct 39 ms 10020 KB Output is correct
50 Correct 13 ms 4544 KB Output is correct
51 Correct 17 ms 5624 KB Output is correct
52 Correct 8 ms 4180 KB Output is correct
53 Correct 46 ms 10464 KB Output is correct
54 Correct 26 ms 6392 KB Output is correct
55 Correct 51 ms 9708 KB Output is correct
56 Correct 34 ms 7244 KB Output is correct
57 Correct 68 ms 10588 KB Output is correct
58 Correct 13 ms 5424 KB Output is correct
59 Correct 17 ms 5504 KB Output is correct
60 Correct 41 ms 9340 KB Output is correct
61 Correct 51 ms 9752 KB Output is correct
62 Correct 47 ms 7932 KB Output is correct
63 Correct 1548 ms 7440 KB Output is correct
64 Correct 2802 ms 8068 KB Output is correct
65 Correct 2499 ms 11068 KB Output is correct
66 Correct 1606 ms 5556 KB Output is correct
67 Execution timed out 3053 ms 8772 KB Time limit exceeded
68 Halted 0 ms 0 KB -