Submission #118413

# Submission time Handle Problem Language Result Execution time Memory
118413 2019-06-19T01:15:34 Z kig9981 Cats or Dogs (JOI18_catdog) C++17
100 / 100
743 ms 21880 KB
#include <bits/stdc++.h>
#include "catdog.h"

using namespace std;

const int SZ=1<<17;
vector<int> adj[100000];
int N, parent[100000], W[100000], num[100000], R[100000], hld[100000], V[100000], tree[2*SZ][4];

void add_tree(int n, int v1, int v2)
{
	n+=SZ;
	tree[n][0]+=v1; tree[n][3]+=v2;
	while(n>>=1) {
		for(int i=0;i<4;i++) tree[n][i]=0x1fffffff;
		for(int i=0;i<4;i++) for(int j=0;j<4;j++) tree[n][(i&2)|(j&1)]=min(tree[n][(i&2)|(j&1)],tree[2*n][i]+tree[2*n+1][j]+((i&1)^(j>>1)));
	}
}

void get_ans(int *ret, int s, int e)
{
	int r1[4]={0,0x1fffffff,0x1fffffff,0}, r2[4]={0,0x1fffffff,0x1fffffff,0};
	for(s+=SZ,e+=SZ;s<=e;s>>=1,e>>=1) {
		if(s&1) {
			for(int i=0;i<4;i++) ret[i]=0x1fffffff;
			for(int i=0;i<4;i++) for(int j=0;j<4;j++) ret[(i&2)|(j&1)]=min(ret[(i&2)|(j&1)],r1[i]+tree[s][j]+((i&1)^(j>>1)));
			for(int i=0;i<4;i++) r1[i]=ret[i];
			s++;
		}
		if(~e&1) {
			for(int i=0;i<4;i++) ret[i]=0x1fffffff;
			for(int i=0;i<4;i++) for(int j=0;j<4;j++) ret[(i&2)|(j&1)]=min(ret[(i&2)|(j&1)],tree[e][i]+r2[j]+((i&1)^(j>>1)));
			for(int i=0;i<4;i++) r2[i]=ret[i];
			e--;
		}
	}
	for(int i=0;i<4;i++) ret[i]=0x1fffffff;
	for(int i=0;i<4;i++) for(int j=0;j<4;j++) ret[(i&2)|(j&1)]=min(ret[(i&2)|(j&1)],r1[i]+r2[j]+((i&1)^(j>>1)));
}

int dfs(int c)
{
	W[c]=1;
	for(auto n: adj[c]) if(W[n]==0) {
		parent[n]=c;
		W[c]+=dfs(n);
	}
	return W[c];
}

int dfs2(int c)
{
	num[c]=R[c]=N++;
	for(auto n: adj[c]) if(parent[n]==c && 2*W[n]>=W[c]) {
		hld[n]=hld[c];
		R[c]=dfs2(n);
	}
	for(auto n: adj[c]) if(parent[n]==c && 2*W[n]<W[c]) {
		hld[n]=n;
		dfs2(n);
	}
	return R[c];
}

void initialize(int N, vector<int> A, vector<int> B)
{
	for(int i=0;i<N-1;i++) {
		adj[--A[i]].push_back(--B[i]);
		adj[B[i]].push_back(A[i]);
		tree[SZ+i][1]=tree[SZ+i][2]=N;
	}
	tree[SZ+N-1][1]=tree[SZ+N-1][2]=N;
	dfs(0); dfs2(0);
	for(int n=SZ-1;n;n--) {
		for(int i=0;i<4;i++) tree[n][i]=0x1fffffff;
		for(int i=0;i<4;i++) for(int j=0;j<4;j++) tree[n][(i&2)|(j&1)]=min(tree[n][(i&2)|(j&1)],tree[2*n][i]+tree[2*n+1][j]+((i&1)^(j>>1)));
	}
}

int cat(int c)
{
	int t1[4], t2[4], v1=0, v2=N;
	V[--c]=1;
	for(;;) {
		get_ans(t1,num[hld[c]],R[c]);
		add_tree(num[c],v1,v2);
		get_ans(t2,num[hld[c]],R[c]);
		if(hld[c]==0) break;
		t1[0]=min(t1[0],t1[1]); t1[1]=min(t1[2],t1[3]);
		t2[0]=min(t2[0],t2[1]); t2[1]=min(t2[2],t2[3]);
		v1=min(t2[0],t2[1]+1)-min(t1[0],t1[1]+1);
		v2=min(t2[0]+1,t2[1])-min(t1[0]+1,t1[1]);
		c=parent[hld[c]];
		if(v1==0 && v2==0) {
			get_ans(t2,num[0],R[0]);
			break;
		}
	}
	return min({t2[0],t2[1],t2[2],t2[3]});
}

int dog(int c)
{
	int t1[4], t2[4], v1=N, v2=0;
	V[--c]=2;
	for(;;) {
		get_ans(t1,num[hld[c]],R[c]);
		add_tree(num[c],v1,v2);
		get_ans(t2,num[hld[c]],R[c]);
		if(hld[c]==0) break;
		t1[0]=min(t1[0],t1[1]); t1[1]=min(t1[2],t1[3]);
		t2[0]=min(t2[0],t2[1]); t2[1]=min(t2[2],t2[3]);
		v1=min(t2[0],t2[1]+1)-min(t1[0],t1[1]+1);
		v2=min(t2[0]+1,t2[1])-min(t1[0]+1,t1[1]);
		c=parent[hld[c]];
		if(v1==0 && v2==0) {
			get_ans(t2,num[0],R[0]);
			break;
		}
	}
	return min({t2[0],t2[1],t2[2],t2[3]});
}

int neighbor(int c)
{
	c--;
	int t1[4], t2[4], v1=-N*(V[c]==2), v2=-N*(V[c]==1);
	V[c]=0;
	for(;;) {
		get_ans(t1,num[hld[c]],R[c]);
		add_tree(num[c],v1,v2);
		get_ans(t2,num[hld[c]],R[c]);
		if(hld[c]==0) break;
		t1[0]=min(t1[0],t1[1]); t1[1]=min(t1[2],t1[3]);
		t2[0]=min(t2[0],t2[1]); t2[1]=min(t2[2],t2[3]);
		v1=min(t2[0],t2[1]+1)-min(t1[0],t1[1]+1);
		v2=min(t2[0]+1,t2[1])-min(t1[0]+1,t1[1]);
		c=parent[hld[c]];
		if(v1==0 && v2==0) {
			get_ans(t2,num[0],R[0]);
			break;
		}
	}
	return min({t2[0],t2[1],t2[2],t2[3]});
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 4736 KB Output is correct
2 Correct 11 ms 4736 KB Output is correct
3 Correct 11 ms 4736 KB Output is correct
4 Correct 11 ms 4736 KB Output is correct
5 Correct 11 ms 4736 KB Output is correct
6 Correct 10 ms 4736 KB Output is correct
7 Correct 11 ms 4608 KB Output is correct
8 Correct 11 ms 4736 KB Output is correct
9 Correct 12 ms 4736 KB Output is correct
10 Correct 11 ms 4736 KB Output is correct
11 Correct 12 ms 4736 KB Output is correct
12 Correct 11 ms 4736 KB Output is correct
13 Correct 11 ms 4736 KB Output is correct
14 Correct 10 ms 4736 KB Output is correct
15 Correct 11 ms 4736 KB Output is correct
16 Correct 10 ms 4864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 4736 KB Output is correct
2 Correct 11 ms 4736 KB Output is correct
3 Correct 11 ms 4736 KB Output is correct
4 Correct 11 ms 4736 KB Output is correct
5 Correct 11 ms 4736 KB Output is correct
6 Correct 10 ms 4736 KB Output is correct
7 Correct 11 ms 4608 KB Output is correct
8 Correct 11 ms 4736 KB Output is correct
9 Correct 12 ms 4736 KB Output is correct
10 Correct 11 ms 4736 KB Output is correct
11 Correct 12 ms 4736 KB Output is correct
12 Correct 11 ms 4736 KB Output is correct
13 Correct 11 ms 4736 KB Output is correct
14 Correct 10 ms 4736 KB Output is correct
15 Correct 11 ms 4736 KB Output is correct
16 Correct 10 ms 4864 KB Output is correct
17 Correct 12 ms 4864 KB Output is correct
18 Correct 13 ms 4836 KB Output is correct
19 Correct 13 ms 4864 KB Output is correct
20 Correct 11 ms 4912 KB Output is correct
21 Correct 12 ms 4736 KB Output is correct
22 Correct 13 ms 4864 KB Output is correct
23 Correct 14 ms 4864 KB Output is correct
24 Correct 15 ms 4864 KB Output is correct
25 Correct 13 ms 4864 KB Output is correct
26 Correct 11 ms 4736 KB Output is correct
27 Correct 11 ms 4864 KB Output is correct
28 Correct 11 ms 4864 KB Output is correct
29 Correct 13 ms 4864 KB Output is correct
30 Correct 12 ms 4856 KB Output is correct
31 Correct 12 ms 4864 KB Output is correct
32 Correct 12 ms 4864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 4736 KB Output is correct
2 Correct 11 ms 4736 KB Output is correct
3 Correct 11 ms 4736 KB Output is correct
4 Correct 11 ms 4736 KB Output is correct
5 Correct 11 ms 4736 KB Output is correct
6 Correct 10 ms 4736 KB Output is correct
7 Correct 11 ms 4608 KB Output is correct
8 Correct 11 ms 4736 KB Output is correct
9 Correct 12 ms 4736 KB Output is correct
10 Correct 11 ms 4736 KB Output is correct
11 Correct 12 ms 4736 KB Output is correct
12 Correct 11 ms 4736 KB Output is correct
13 Correct 11 ms 4736 KB Output is correct
14 Correct 10 ms 4736 KB Output is correct
15 Correct 11 ms 4736 KB Output is correct
16 Correct 10 ms 4864 KB Output is correct
17 Correct 12 ms 4864 KB Output is correct
18 Correct 13 ms 4836 KB Output is correct
19 Correct 13 ms 4864 KB Output is correct
20 Correct 11 ms 4912 KB Output is correct
21 Correct 12 ms 4736 KB Output is correct
22 Correct 13 ms 4864 KB Output is correct
23 Correct 14 ms 4864 KB Output is correct
24 Correct 15 ms 4864 KB Output is correct
25 Correct 13 ms 4864 KB Output is correct
26 Correct 11 ms 4736 KB Output is correct
27 Correct 11 ms 4864 KB Output is correct
28 Correct 11 ms 4864 KB Output is correct
29 Correct 13 ms 4864 KB Output is correct
30 Correct 12 ms 4856 KB Output is correct
31 Correct 12 ms 4864 KB Output is correct
32 Correct 12 ms 4864 KB Output is correct
33 Correct 332 ms 11236 KB Output is correct
34 Correct 131 ms 11128 KB Output is correct
35 Correct 324 ms 9776 KB Output is correct
36 Correct 552 ms 15336 KB Output is correct
37 Correct 30 ms 7756 KB Output is correct
38 Correct 563 ms 16400 KB Output is correct
39 Correct 572 ms 16356 KB Output is correct
40 Correct 587 ms 16360 KB Output is correct
41 Correct 529 ms 16360 KB Output is correct
42 Correct 549 ms 16392 KB Output is correct
43 Correct 564 ms 16356 KB Output is correct
44 Correct 493 ms 16332 KB Output is correct
45 Correct 588 ms 16484 KB Output is correct
46 Correct 503 ms 16356 KB Output is correct
47 Correct 743 ms 16356 KB Output is correct
48 Correct 206 ms 13380 KB Output is correct
49 Correct 216 ms 15008 KB Output is correct
50 Correct 86 ms 7292 KB Output is correct
51 Correct 94 ms 8924 KB Output is correct
52 Correct 45 ms 6912 KB Output is correct
53 Correct 253 ms 14712 KB Output is correct
54 Correct 185 ms 9520 KB Output is correct
55 Correct 456 ms 13296 KB Output is correct
56 Correct 303 ms 10272 KB Output is correct
57 Correct 454 ms 14480 KB Output is correct
58 Correct 42 ms 8948 KB Output is correct
59 Correct 88 ms 8616 KB Output is correct
60 Correct 189 ms 14072 KB Output is correct
61 Correct 206 ms 14404 KB Output is correct
62 Correct 125 ms 12272 KB Output is correct
63 Correct 81 ms 12152 KB Output is correct
64 Correct 93 ms 13648 KB Output is correct
65 Correct 102 ms 18680 KB Output is correct
66 Correct 145 ms 8616 KB Output is correct
67 Correct 119 ms 14716 KB Output is correct
68 Correct 260 ms 19192 KB Output is correct
69 Correct 73 ms 6364 KB Output is correct
70 Correct 23 ms 5112 KB Output is correct
71 Correct 97 ms 11456 KB Output is correct
72 Correct 144 ms 16832 KB Output is correct
73 Correct 390 ms 21880 KB Output is correct
74 Correct 414 ms 18936 KB Output is correct
75 Correct 284 ms 21752 KB Output is correct
76 Correct 272 ms 20744 KB Output is correct
77 Correct 402 ms 19320 KB Output is correct