Submission #964160

# Submission time Handle Problem Language Result Execution time Memory
964160 2024-04-16T11:39:34 Z Trisanu_Das Cats or Dogs (JOI18_catdog) C++17
100 / 100
151 ms 23268 KB
#include "catdog.h"
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
#define mp make_pair
#define pb push_back
const int inf=1e9+7;
int min(int a, int b){ return a>b?b:a;}
const int N=100050;
const int M=2*N;
struct Matrix
{
	int a[2][2];
	void init(){ a[0][0]=a[1][1]=0;a[1][0]=a[0][1]=inf;}
	Matrix(){ init();}
	Matrix operator * (Matrix b) const
	{
		Matrix c;
		int i,j,k,l;
		for(i=0;i<2;i++) for(j=0;j<2;j++)
		{
			c.a[i][j]=inf;
			for(k=0;k<2;k++) for(l=0;l<2;l++) c.a[i][j]=min(c.a[i][j],a[i][k]+b.a[l][j]+(k^l));
		}
		return c;
	}
} node[M];
int ls[M],rs[M],root[N],tsz;
void Set(int &c, int ss, int se, int qi, int a, int b)
{
	if(!c) c=++tsz;
	if(ss==se){ node[c].a[0][0]+=a;node[c].a[1][1]+=b;return;}
	int mid=ss+se>>1;
	if(qi<=mid) Set(ls[c],ss,mid,qi,a,b);
	else Set(rs[c],mid+1,se,qi,a,b);
	node[c]=node[ls[c]]*node[rs[c]];
}
pair<int,int> Get(int c)
{
	int a=min(node[c].a[0][0],node[c].a[0][1]);
	int b=min(node[c].a[1][0],node[c].a[1][1]);
	a=min(a,b+1);
	b=min(b,a+1);
	return mp(a,b);
}
int head[N],sz[N],dep[N],par[N],csz[N];
vector<int> E[N];
void DFS(int u, int p)
{
	par[u]=p;dep[u]=dep[p]+1;sz[u]=1;
	for(int i=0;i<E[u].size();i++) if(E[u][i]!=p) DFS(E[u][i],u),sz[u]+=sz[E[u][i]];
}
void HLD(int u, int p)
{
	if(!head[u]) head[u]=u;
	csz[head[u]]++;
	int HC=0,i;
	for(i=0;i<E[u].size();i++) if(E[u][i]!=p && sz[HC]<sz[E[u][i]]) HC=E[u][i];
	if(HC) head[HC]=head[u];
	for(i=0;i<E[u].size();i++) if(E[u][i]!=p) HLD(E[u][i],u);
}
int t[N];
void initialize(int n, vector<int> a, vector<int> b)
{
	for(int i=0;i<n-1;i++) E[a[i]].pb(b[i]),E[b[i]].pb(a[i]);
	DFS(1,0);HLD(1,0);
}
int Solve(){ pair<int,int> sol=Get(root[1]);return min(sol.first,sol.second);}
void Set(int u, int a, int b)
{
	while(u)
	{
		int la,lb,na,nb;
		pair<int,int> tmp=Get(root[head[u]]);
		la=tmp.first;lb=tmp.second;
		Set(root[head[u]],1,csz[head[u]],dep[u]-dep[head[u]]+1,a,b);
		tmp=Get(root[head[u]]);
		na=tmp.first;nb=tmp.second;
		a=na-la;b=nb-lb;
		u=par[head[u]];
	}
}
int cat(int u){ t[u]=1;Set(u,N,0);return Solve();}
int dog(int u){ t[u]=2;Set(u,0,N);return Solve();}
int neighbor(int u)
{
	if(t[u]==1) Set(u,-N,0);
	if(t[u]==2) Set(u,0,-N);
	return Solve();
}

Compilation message

catdog.cpp: In function 'void Set(int&, int, int, int, int, int)':
catdog.cpp:34:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   34 |  int mid=ss+se>>1;
      |          ~~^~~
catdog.cpp: In function 'void DFS(int, int)':
catdog.cpp:52:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |  for(int i=0;i<E[u].size();i++) if(E[u][i]!=p) DFS(E[u][i],u),sz[u]+=sz[E[u][i]];
      |              ~^~~~~~~~~~~~
catdog.cpp: In function 'void HLD(int, int)':
catdog.cpp:59:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |  for(i=0;i<E[u].size();i++) if(E[u][i]!=p && sz[HC]<sz[E[u][i]]) HC=E[u][i];
      |          ~^~~~~~~~~~~~
catdog.cpp:61:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |  for(i=0;i<E[u].size();i++) if(E[u][i]!=p) HLD(E[u][i],u);
      |          ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9820 KB Output is correct
2 Correct 2 ms 9820 KB Output is correct
3 Correct 2 ms 9820 KB Output is correct
4 Correct 3 ms 9976 KB Output is correct
5 Correct 2 ms 9820 KB Output is correct
6 Correct 2 ms 9820 KB Output is correct
7 Correct 2 ms 9820 KB Output is correct
8 Correct 2 ms 9820 KB Output is correct
9 Correct 2 ms 9820 KB Output is correct
10 Correct 3 ms 9820 KB Output is correct
11 Correct 2 ms 9816 KB Output is correct
12 Correct 3 ms 9820 KB Output is correct
13 Correct 2 ms 9820 KB Output is correct
14 Correct 2 ms 9820 KB Output is correct
15 Correct 3 ms 9820 KB Output is correct
16 Correct 2 ms 9820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9820 KB Output is correct
2 Correct 2 ms 9820 KB Output is correct
3 Correct 2 ms 9820 KB Output is correct
4 Correct 3 ms 9976 KB Output is correct
5 Correct 2 ms 9820 KB Output is correct
6 Correct 2 ms 9820 KB Output is correct
7 Correct 2 ms 9820 KB Output is correct
8 Correct 2 ms 9820 KB Output is correct
9 Correct 2 ms 9820 KB Output is correct
10 Correct 3 ms 9820 KB Output is correct
11 Correct 2 ms 9816 KB Output is correct
12 Correct 3 ms 9820 KB Output is correct
13 Correct 2 ms 9820 KB Output is correct
14 Correct 2 ms 9820 KB Output is correct
15 Correct 3 ms 9820 KB Output is correct
16 Correct 2 ms 9820 KB Output is correct
17 Correct 4 ms 9820 KB Output is correct
18 Correct 4 ms 9816 KB Output is correct
19 Correct 2 ms 9820 KB Output is correct
20 Correct 2 ms 9828 KB Output is correct
21 Correct 2 ms 9820 KB Output is correct
22 Correct 2 ms 9824 KB Output is correct
23 Correct 2 ms 9820 KB Output is correct
24 Correct 3 ms 9836 KB Output is correct
25 Correct 3 ms 9816 KB Output is correct
26 Correct 2 ms 9812 KB Output is correct
27 Correct 3 ms 9832 KB Output is correct
28 Correct 2 ms 9816 KB Output is correct
29 Correct 3 ms 9820 KB Output is correct
30 Correct 2 ms 9820 KB Output is correct
31 Correct 2 ms 9816 KB Output is correct
32 Correct 2 ms 9820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9820 KB Output is correct
2 Correct 2 ms 9820 KB Output is correct
3 Correct 2 ms 9820 KB Output is correct
4 Correct 3 ms 9976 KB Output is correct
5 Correct 2 ms 9820 KB Output is correct
6 Correct 2 ms 9820 KB Output is correct
7 Correct 2 ms 9820 KB Output is correct
8 Correct 2 ms 9820 KB Output is correct
9 Correct 2 ms 9820 KB Output is correct
10 Correct 3 ms 9820 KB Output is correct
11 Correct 2 ms 9816 KB Output is correct
12 Correct 3 ms 9820 KB Output is correct
13 Correct 2 ms 9820 KB Output is correct
14 Correct 2 ms 9820 KB Output is correct
15 Correct 3 ms 9820 KB Output is correct
16 Correct 2 ms 9820 KB Output is correct
17 Correct 4 ms 9820 KB Output is correct
18 Correct 4 ms 9816 KB Output is correct
19 Correct 2 ms 9820 KB Output is correct
20 Correct 2 ms 9828 KB Output is correct
21 Correct 2 ms 9820 KB Output is correct
22 Correct 2 ms 9824 KB Output is correct
23 Correct 2 ms 9820 KB Output is correct
24 Correct 3 ms 9836 KB Output is correct
25 Correct 3 ms 9816 KB Output is correct
26 Correct 2 ms 9812 KB Output is correct
27 Correct 3 ms 9832 KB Output is correct
28 Correct 2 ms 9816 KB Output is correct
29 Correct 3 ms 9820 KB Output is correct
30 Correct 2 ms 9820 KB Output is correct
31 Correct 2 ms 9816 KB Output is correct
32 Correct 2 ms 9820 KB Output is correct
33 Correct 91 ms 14244 KB Output is correct
34 Correct 39 ms 13912 KB Output is correct
35 Correct 89 ms 13484 KB Output is correct
36 Correct 139 ms 17124 KB Output is correct
37 Correct 16 ms 11612 KB Output is correct
38 Correct 130 ms 17768 KB Output is correct
39 Correct 134 ms 17620 KB Output is correct
40 Correct 137 ms 17784 KB Output is correct
41 Correct 139 ms 17724 KB Output is correct
42 Correct 128 ms 17728 KB Output is correct
43 Correct 138 ms 17792 KB Output is correct
44 Correct 122 ms 17624 KB Output is correct
45 Correct 130 ms 17808 KB Output is correct
46 Correct 151 ms 17788 KB Output is correct
47 Correct 125 ms 17728 KB Output is correct
48 Correct 42 ms 15432 KB Output is correct
49 Correct 43 ms 16720 KB Output is correct
50 Correct 19 ms 11608 KB Output is correct
51 Correct 18 ms 12632 KB Output is correct
52 Correct 11 ms 11096 KB Output is correct
53 Correct 63 ms 16132 KB Output is correct
54 Correct 47 ms 12808 KB Output is correct
55 Correct 109 ms 15692 KB Output is correct
56 Correct 78 ms 13732 KB Output is correct
57 Correct 83 ms 16208 KB Output is correct
58 Correct 17 ms 12500 KB Output is correct
59 Correct 23 ms 12468 KB Output is correct
60 Correct 42 ms 16040 KB Output is correct
61 Correct 48 ms 16392 KB Output is correct
62 Correct 40 ms 14856 KB Output is correct
63 Correct 27 ms 15196 KB Output is correct
64 Correct 32 ms 16476 KB Output is correct
65 Correct 42 ms 20052 KB Output is correct
66 Correct 37 ms 12868 KB Output is correct
67 Correct 38 ms 17112 KB Output is correct
68 Correct 82 ms 20472 KB Output is correct
69 Correct 19 ms 10956 KB Output is correct
70 Correct 6 ms 9820 KB Output is correct
71 Correct 34 ms 14936 KB Output is correct
72 Correct 52 ms 19036 KB Output is correct
73 Correct 101 ms 23268 KB Output is correct
74 Correct 110 ms 20568 KB Output is correct
75 Correct 84 ms 23148 KB Output is correct
76 Correct 95 ms 22100 KB Output is correct
77 Correct 117 ms 20692 KB Output is correct