답안 #335633

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
335633 2020-12-13T11:57:28 Z YJU Cats or Dogs (JOI18_catdog) C++14
100 / 100
386 ms 64068 KB
#include<bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef int ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=1e6+5;
const ll K=350;
const ld pi=acos(-1);
const long long INF=(1LL<<28);
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()

ll sz[N],p[N],vid[N],ip[N],top[N],tseg[N],color[N];
vector<ll> v[N],lis;
queue<ll> q;

ll u[N],a[N],b[N];

struct Segment_Tree{
	vector<ll> u;
	vector<ll> seg[2][2];
	Segment_Tree(vector<ll> tmp):u(tmp){
		REP(i,2)REP(j,2)seg[i][j].resize(4*(SZ(tmp)+1));
		REP(i,SZ(u))ins(1,0,SZ(u),i);
	};
	ll A(){return min(seg[0][0][1],seg[0][1][1]);}
	ll B(){return min(seg[1][0][1],seg[1][1][1]);}
	void ins(ll id,ll l,ll r,ll to){
		if(l==r-1){
			if(color[u[l]]==0){
				seg[0][0][id]=a[u[l]];seg[0][1][id]=seg[1][0][id]=INF;seg[1][1][id]=b[u[l]];
			}else{
				REP(i,2)REP(j,2)seg[i][j][id]=INF;
				seg[color[u[l]]-1][color[u[l]]-1][id]=(color[u[l]]==1?a[u[l]]:b[u[l]]);
			}
			return ;
		}
		ll mid=(l+r)>>1;
		if(to<mid){
			ins(id*2,l,mid,to);
		}else{
			ins(id*2+1,mid,r,to);
		}
		REP(i,2)REP(j,2){
			seg[i][j][id]=INF;
			REP(ii,2)REP(jj,2)seg[i][j][id]=min(seg[i][j][id],seg[i][ii][id*2]+seg[jj][j][id*2+1]+(ii==jj?0:1));
		}
	}
	void upd(ll to){
		ins(1,0,SZ(u),to);
	}
};

vector<Segment_Tree> vseg;
 
/*void DFS(ll nd,ll pa){
	dp[nd][1]=dp[nd][2]=0;
	for(auto i:v[nd]){
		if(i==pa)continue;
		DFS(i,nd);
		dp[nd][1]+=min(dp[i][1],dp[i][2]+1);
		dp[nd][2]+=min(dp[i][2],dp[i][1]+1);
	}
	if(col[nd]){dp[nd][3-col[nd]]=INF;}
}*/

void build(){
	function<void(int,int)> predfs=[&](int nd,int pa){
		sz[nd]=1;p[nd]=pa;vid[nd]=0;
		for(int i:v[nd]){
			if(i==pa)continue;
			predfs(i,nd);
			sz[nd]+=sz[i];
			if(sz[vid[nd]]<sz[i])vid[nd]=i;
		}
	};
	predfs(1,0);
	q.push(1);
	while(SZ(q)){
		ll x=q.front();q.pop();
		lis.clear();
		top[x]=x;lis.pb(x);
		while(vid[x]){
			for(int i:v[x])if(i!=p[x]&&i!=vid[x])q.push(i);
			lis.pb(vid[x]);top[vid[x]]=top[x];
			x=vid[x];
		}
		vseg.pb(Segment_Tree(lis));
		REP(i,SZ(lis))ip[lis[i]]=i,tseg[lis[i]]=SZ(vseg)-1;
	}
}

void update(ll nd){
	while(nd){
		a[p[top[nd]]]-=min(vseg[tseg[nd]].A(),vseg[tseg[nd]].B()+1);
		b[p[top[nd]]]-=min(vseg[tseg[nd]].A()+1,vseg[tseg[nd]].B());
		vseg[tseg[nd]].upd(ip[nd]);
		a[p[top[nd]]]+=min(vseg[tseg[nd]].A(),vseg[tseg[nd]].B()+1);
		b[p[top[nd]]]+=min(vseg[tseg[nd]].A()+1,vseg[tseg[nd]].B());
		nd=p[top[nd]];
	}
} 
 
void initialize(ll n,vector<ll> ea,vector<ll> eb){
	REP(i,n-1)v[ea[i]].pb(eb[i]),v[eb[i]].pb(ea[i]);
	build();
}
 
ll cat(ll id){
	color[id]=1;
	update(id);
	return min(vseg[0].A(),vseg[0].B());
}
 
ll dog(ll id){
	color[id]=2;
	update(id);
	return min(vseg[0].A(),vseg[0].B());
}
 
ll neighbor(ll id){
	color[id]=0;
	update(id);
	return min(vseg[0].A(),vseg[0].B());
}
/*
int main(){
	ll n;
	cin>>n;
	vector<ll> ea(n-1),eb(n-1);
	REP(i,n-1)cin>>ea[i]>>eb[i];
	initialize(n,ea,eb);
	return 0;
}*/
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 23916 KB Output is correct
2 Correct 17 ms 23916 KB Output is correct
3 Correct 17 ms 23916 KB Output is correct
4 Correct 16 ms 23916 KB Output is correct
5 Correct 16 ms 23916 KB Output is correct
6 Correct 17 ms 23916 KB Output is correct
7 Correct 16 ms 23916 KB Output is correct
8 Correct 18 ms 23916 KB Output is correct
9 Correct 17 ms 23916 KB Output is correct
10 Correct 16 ms 23916 KB Output is correct
11 Correct 16 ms 23916 KB Output is correct
12 Correct 16 ms 23936 KB Output is correct
13 Correct 16 ms 23916 KB Output is correct
14 Correct 16 ms 23916 KB Output is correct
15 Correct 16 ms 23936 KB Output is correct
16 Correct 17 ms 23916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 23916 KB Output is correct
2 Correct 17 ms 23916 KB Output is correct
3 Correct 17 ms 23916 KB Output is correct
4 Correct 16 ms 23916 KB Output is correct
5 Correct 16 ms 23916 KB Output is correct
6 Correct 17 ms 23916 KB Output is correct
7 Correct 16 ms 23916 KB Output is correct
8 Correct 18 ms 23916 KB Output is correct
9 Correct 17 ms 23916 KB Output is correct
10 Correct 16 ms 23916 KB Output is correct
11 Correct 16 ms 23916 KB Output is correct
12 Correct 16 ms 23936 KB Output is correct
13 Correct 16 ms 23916 KB Output is correct
14 Correct 16 ms 23916 KB Output is correct
15 Correct 16 ms 23936 KB Output is correct
16 Correct 17 ms 23916 KB Output is correct
17 Correct 17 ms 24044 KB Output is correct
18 Correct 17 ms 24172 KB Output is correct
19 Correct 18 ms 24044 KB Output is correct
20 Correct 18 ms 23916 KB Output is correct
21 Correct 18 ms 23916 KB Output is correct
22 Correct 17 ms 24044 KB Output is correct
23 Correct 18 ms 24172 KB Output is correct
24 Correct 17 ms 24172 KB Output is correct
25 Correct 17 ms 24044 KB Output is correct
26 Correct 17 ms 24044 KB Output is correct
27 Correct 17 ms 23916 KB Output is correct
28 Correct 17 ms 24044 KB Output is correct
29 Correct 18 ms 24044 KB Output is correct
30 Correct 17 ms 24044 KB Output is correct
31 Correct 17 ms 24172 KB Output is correct
32 Correct 18 ms 24044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 23916 KB Output is correct
2 Correct 17 ms 23916 KB Output is correct
3 Correct 17 ms 23916 KB Output is correct
4 Correct 16 ms 23916 KB Output is correct
5 Correct 16 ms 23916 KB Output is correct
6 Correct 17 ms 23916 KB Output is correct
7 Correct 16 ms 23916 KB Output is correct
8 Correct 18 ms 23916 KB Output is correct
9 Correct 17 ms 23916 KB Output is correct
10 Correct 16 ms 23916 KB Output is correct
11 Correct 16 ms 23916 KB Output is correct
12 Correct 16 ms 23936 KB Output is correct
13 Correct 16 ms 23916 KB Output is correct
14 Correct 16 ms 23916 KB Output is correct
15 Correct 16 ms 23936 KB Output is correct
16 Correct 17 ms 23916 KB Output is correct
17 Correct 17 ms 24044 KB Output is correct
18 Correct 17 ms 24172 KB Output is correct
19 Correct 18 ms 24044 KB Output is correct
20 Correct 18 ms 23916 KB Output is correct
21 Correct 18 ms 23916 KB Output is correct
22 Correct 17 ms 24044 KB Output is correct
23 Correct 18 ms 24172 KB Output is correct
24 Correct 17 ms 24172 KB Output is correct
25 Correct 17 ms 24044 KB Output is correct
26 Correct 17 ms 24044 KB Output is correct
27 Correct 17 ms 23916 KB Output is correct
28 Correct 17 ms 24044 KB Output is correct
29 Correct 18 ms 24044 KB Output is correct
30 Correct 17 ms 24044 KB Output is correct
31 Correct 17 ms 24172 KB Output is correct
32 Correct 18 ms 24044 KB Output is correct
33 Correct 166 ms 38840 KB Output is correct
34 Correct 116 ms 41292 KB Output is correct
35 Correct 137 ms 34632 KB Output is correct
36 Correct 262 ms 50892 KB Output is correct
37 Correct 45 ms 32080 KB Output is correct
38 Correct 295 ms 52652 KB Output is correct
39 Correct 289 ms 52652 KB Output is correct
40 Correct 319 ms 52668 KB Output is correct
41 Correct 302 ms 52652 KB Output is correct
42 Correct 284 ms 52652 KB Output is correct
43 Correct 294 ms 52652 KB Output is correct
44 Correct 290 ms 52640 KB Output is correct
45 Correct 284 ms 52780 KB Output is correct
46 Correct 289 ms 52604 KB Output is correct
47 Correct 290 ms 52588 KB Output is correct
48 Correct 143 ms 60848 KB Output is correct
49 Correct 165 ms 64068 KB Output is correct
50 Correct 53 ms 33360 KB Output is correct
51 Correct 73 ms 42312 KB Output is correct
52 Correct 42 ms 33084 KB Output is correct
53 Correct 185 ms 51068 KB Output is correct
54 Correct 100 ms 36944 KB Output is correct
55 Correct 216 ms 43712 KB Output is correct
56 Correct 133 ms 37604 KB Output is correct
57 Correct 201 ms 50648 KB Output is correct
58 Correct 65 ms 42440 KB Output is correct
59 Correct 71 ms 37964 KB Output is correct
60 Correct 149 ms 62000 KB Output is correct
61 Correct 160 ms 62248 KB Output is correct
62 Correct 116 ms 60336 KB Output is correct
63 Correct 84 ms 36076 KB Output is correct
64 Correct 88 ms 38568 KB Output is correct
65 Correct 125 ms 46932 KB Output is correct
66 Correct 83 ms 29548 KB Output is correct
67 Correct 109 ms 40100 KB Output is correct
68 Correct 194 ms 47088 KB Output is correct
69 Correct 43 ms 25964 KB Output is correct
70 Correct 22 ms 24172 KB Output is correct
71 Correct 86 ms 34644 KB Output is correct
72 Correct 135 ms 43600 KB Output is correct
73 Correct 311 ms 51564 KB Output is correct
74 Correct 381 ms 46496 KB Output is correct
75 Correct 295 ms 51308 KB Output is correct
76 Correct 285 ms 49580 KB Output is correct
77 Correct 386 ms 46988 KB Output is correct