Submission #210436

# Submission time Handle Problem Language Result Execution time Memory
210436 2020-03-17T10:58:20 Z dennisstar Cats or Dogs (JOI18_catdog) C++17
100 / 100
1101 ms 21240 KB
#include "catdog.h"
#include <bits/stdc++.h>
#define eb emplace_back
using namespace std;

const int MX = 100005, INF = 1<<28;

struct node {
	int v[2][2];
	node() { for (int i=0; i<2; i++) v[i][i]=0, v[i][i^1]=INF; }
	node(int c, int d) { v[0][0]=c, v[0][1]=v[1][0]=INF, v[1][1]=d; }
};

node operator + (const node &n1, const node &n2) {
	node r;
	for (int i=0; i<2; i++) for (int j=0; j<2; j++) {
		r.v[i][j]=INF;
		for (int k=0; k<2; k++) for (int l=0; l<2; l++)
			r.v[i][j]=min(r.v[i][j], n1.v[i][k]+n2.v[l][j]+(k^l));
	}
	return r;
}

int N, Q, sz[MX];
int P[MX], C[MX], D[MX], T[MX];
int in[MX], out[MX], hld[MX], hla[MX];
vector<int> adj[MX];

node st[1<<18];
void upd(int i, int s, int e, int t, int c, int d) {
	if (s==e) { st[i]=node(c, d); return ; }
	int md=(s+e)/2;
	if (t<=md) upd(i*2, s, md, t, c, d);
	else upd(i*2+1, md+1, e, t, c, d);
	st[i]=st[i*2]+st[i*2+1];
}
node get(int i, int s, int e, int ts, int te) {
	if (e<ts||te<s) return node();
	if (ts<=s&&e<=te) return st[i];
	int md=(s+e)/2;
	return get(i*2, s, md, ts, te)+get(i*2+1, md+1, e, ts, te);
}

void dfs(int n, int p) {
	sz[n]=1; P[n]=p;
	for (auto &i:adj[n]) if (i!=p) dfs(i, n), sz[n]+=sz[i];
}
int t1, t2;
void bhld(int n, int p) {
	in[n]=++t1;
	if (!hld[n]) hld[n]=++t2, hla[n]=n;
	out[hld[n]]=max(out[hld[n]], in[n]);
	for (auto &i:adj[n]) if (i!=p) {
		if (sz[n]<sz[i]*2) hld[i]=hld[n], hla[i]=hla[n];
		bhld(i, n);
	}
}

void upd(int v, int c, int d) {
	if (!v) return ;

	int hla=::hla[v], hlp=P[hla];
	node nd; int cc, dd;

	nd=get(1, 1, N, in[hla], out[hld[v]]);
	cc=min(nd.v[0][0], nd.v[0][1]), dd=min(nd.v[1][0], nd.v[1][1]);
	C[hlp]-=min(cc, dd+1), D[hlp]-=min(cc+1, dd);

	upd(1, 1, N, in[v], c, d);

	nd=get(1, 1, N, in[hla], out[hld[v]]);
	cc=min(nd.v[0][0], nd.v[0][1]), dd=min(nd.v[1][0], nd.v[1][1]);
	C[hlp]+=min(cc, dd+1), D[hlp]+=min(cc+1, dd);

	upd(hlp, T[hlp]&1?C[hlp]:INF, T[hlp]&2?D[hlp]:INF);
}

void initialize(int N, vector<int> A, vector<int> B) {
	::N=N;
	for (int i=0; i<N-1; i++) adj[A[i]].eb(B[i]), adj[B[i]].eb(A[i]);
	dfs(1, 0);
	for (int i=1; i<=N; i++) sort(adj[i].begin(), adj[i].end(), [&](int n1, int n2){ return sz[n1]>sz[n2]; });
	bhld(1, 0); fill(T+1, T+N+1, 3);
}

int getans() {
	node nd=get(1, 1, N, 1, out[1]);
	int c=min(nd.v[0][0], nd.v[0][1]), d=min(nd.v[1][0], nd.v[1][1]);
	return min(T[1]&1?c:INF, T[1]&2?d:INF);
}

int cat(int v) {
	T[v]=1;
	upd(v, C[v], INF);
	return getans();
}

int dog(int v) {
	T[v]=2;
	upd(v, INF, D[v]);
	return getans();
}

int neighbor(int v) {
	T[v]=3;
	upd(v, C[v], D[v]);
	return getans();
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 6776 KB Output is correct
2 Correct 9 ms 6776 KB Output is correct
3 Correct 9 ms 6776 KB Output is correct
4 Correct 9 ms 6776 KB Output is correct
5 Correct 9 ms 6776 KB Output is correct
6 Correct 8 ms 6776 KB Output is correct
7 Correct 9 ms 6776 KB Output is correct
8 Correct 9 ms 6776 KB Output is correct
9 Correct 9 ms 6776 KB Output is correct
10 Correct 8 ms 6776 KB Output is correct
11 Correct 8 ms 6776 KB Output is correct
12 Correct 8 ms 6776 KB Output is correct
13 Correct 10 ms 6776 KB Output is correct
14 Correct 9 ms 6776 KB Output is correct
15 Correct 8 ms 6776 KB Output is correct
16 Correct 9 ms 6776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 6776 KB Output is correct
2 Correct 9 ms 6776 KB Output is correct
3 Correct 9 ms 6776 KB Output is correct
4 Correct 9 ms 6776 KB Output is correct
5 Correct 9 ms 6776 KB Output is correct
6 Correct 8 ms 6776 KB Output is correct
7 Correct 9 ms 6776 KB Output is correct
8 Correct 9 ms 6776 KB Output is correct
9 Correct 9 ms 6776 KB Output is correct
10 Correct 8 ms 6776 KB Output is correct
11 Correct 8 ms 6776 KB Output is correct
12 Correct 8 ms 6776 KB Output is correct
13 Correct 10 ms 6776 KB Output is correct
14 Correct 9 ms 6776 KB Output is correct
15 Correct 8 ms 6776 KB Output is correct
16 Correct 9 ms 6776 KB Output is correct
17 Correct 12 ms 6904 KB Output is correct
18 Correct 12 ms 6904 KB Output is correct
19 Correct 10 ms 6904 KB Output is correct
20 Correct 9 ms 6904 KB Output is correct
21 Correct 10 ms 6776 KB Output is correct
22 Correct 10 ms 6904 KB Output is correct
23 Correct 12 ms 6904 KB Output is correct
24 Correct 11 ms 6904 KB Output is correct
25 Correct 11 ms 6904 KB Output is correct
26 Correct 10 ms 6904 KB Output is correct
27 Correct 9 ms 6776 KB Output is correct
28 Correct 9 ms 6908 KB Output is correct
29 Correct 11 ms 6904 KB Output is correct
30 Correct 10 ms 6776 KB Output is correct
31 Correct 9 ms 6904 KB Output is correct
32 Correct 11 ms 6904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 6776 KB Output is correct
2 Correct 9 ms 6776 KB Output is correct
3 Correct 9 ms 6776 KB Output is correct
4 Correct 9 ms 6776 KB Output is correct
5 Correct 9 ms 6776 KB Output is correct
6 Correct 8 ms 6776 KB Output is correct
7 Correct 9 ms 6776 KB Output is correct
8 Correct 9 ms 6776 KB Output is correct
9 Correct 9 ms 6776 KB Output is correct
10 Correct 8 ms 6776 KB Output is correct
11 Correct 8 ms 6776 KB Output is correct
12 Correct 8 ms 6776 KB Output is correct
13 Correct 10 ms 6776 KB Output is correct
14 Correct 9 ms 6776 KB Output is correct
15 Correct 8 ms 6776 KB Output is correct
16 Correct 9 ms 6776 KB Output is correct
17 Correct 12 ms 6904 KB Output is correct
18 Correct 12 ms 6904 KB Output is correct
19 Correct 10 ms 6904 KB Output is correct
20 Correct 9 ms 6904 KB Output is correct
21 Correct 10 ms 6776 KB Output is correct
22 Correct 10 ms 6904 KB Output is correct
23 Correct 12 ms 6904 KB Output is correct
24 Correct 11 ms 6904 KB Output is correct
25 Correct 11 ms 6904 KB Output is correct
26 Correct 10 ms 6904 KB Output is correct
27 Correct 9 ms 6776 KB Output is correct
28 Correct 9 ms 6908 KB Output is correct
29 Correct 11 ms 6904 KB Output is correct
30 Correct 10 ms 6776 KB Output is correct
31 Correct 9 ms 6904 KB Output is correct
32 Correct 11 ms 6904 KB Output is correct
33 Correct 624 ms 12928 KB Output is correct
34 Correct 191 ms 12664 KB Output is correct
35 Correct 611 ms 11760 KB Output is correct
36 Correct 993 ms 16828 KB Output is correct
37 Correct 30 ms 9592 KB Output is correct
38 Correct 1071 ms 17768 KB Output is correct
39 Correct 1038 ms 17764 KB Output is correct
40 Correct 1048 ms 17864 KB Output is correct
41 Correct 1024 ms 17800 KB Output is correct
42 Correct 979 ms 17900 KB Output is correct
43 Correct 1031 ms 17768 KB Output is correct
44 Correct 988 ms 17768 KB Output is correct
45 Correct 966 ms 17768 KB Output is correct
46 Correct 1022 ms 17772 KB Output is correct
47 Correct 1101 ms 17764 KB Output is correct
48 Correct 287 ms 14400 KB Output is correct
49 Correct 301 ms 16112 KB Output is correct
50 Correct 120 ms 9080 KB Output is correct
51 Correct 126 ms 10420 KB Output is correct
52 Correct 55 ms 8696 KB Output is correct
53 Correct 424 ms 16108 KB Output is correct
54 Correct 341 ms 11184 KB Output is correct
55 Correct 846 ms 14952 KB Output is correct
56 Correct 566 ms 12064 KB Output is correct
57 Correct 690 ms 16032 KB Output is correct
58 Correct 49 ms 10612 KB Output is correct
59 Correct 126 ms 10360 KB Output is correct
60 Correct 279 ms 15220 KB Output is correct
61 Correct 302 ms 15600 KB Output is correct
62 Correct 172 ms 13680 KB Output is correct
63 Correct 89 ms 13048 KB Output is correct
64 Correct 85 ms 14200 KB Output is correct
65 Correct 102 ms 18424 KB Output is correct
66 Correct 146 ms 10232 KB Output is correct
67 Correct 113 ms 15096 KB Output is correct
68 Correct 257 ms 19064 KB Output is correct
69 Correct 69 ms 8184 KB Output is correct
70 Correct 22 ms 7032 KB Output is correct
71 Correct 120 ms 12408 KB Output is correct
72 Correct 155 ms 16892 KB Output is correct
73 Correct 466 ms 21240 KB Output is correct
74 Correct 483 ms 19064 KB Output is correct
75 Correct 282 ms 21112 KB Output is correct
76 Correct 281 ms 20344 KB Output is correct
77 Correct 475 ms 19192 KB Output is correct