P3621 [[APIO2007] wind chimes]

This story tells us, everything Jieke violence (du) force (liu) dp.
First of all, the title of the effect is to make us given him some way that transforms into a complete binary tree. We all complete binary tree leaf nodes connected together, there should be only two cases, one is a chain, and the other is the two strands, the depth of which points on a chain 1 greater than the depth of the other strand.
Order dp (i, j) represents the i-node status is the minimum number of movements at j.
Status nothing three types:
chains 1 a deeper.
2. a shallow chain.
3. The two chains.
We note that for a point, no matter how mobile, depth is always the same, so for State 1 and State 2, no recursive down, just to see if all the depth of the leaf nodes within the tree subtree corresponding value on it.

The third case is the transfer of the following six cases:
1.1 2 +
2.1 3 +
3.3 + 2
4.2 + 1 (1)
5.3 + 1 (1)
6.2 + 3 (1) refers to the plus a left and right subtrees spent exchanging

the code below:

#pragma GCC optimize("Ofast,fast-math")
#pragma GCC target("avx,avx2")
#pragma GCC optimize("O2")
#include<bits/stdc++.h>
#define gc pa==pb&&(pb=(pa=buf)+fread(buf,1,1<<17,stdin),pa==pb)?EOF:*pa++
static char buf[1<<17],*pa(buf),*pb(buf);
inline int read() {
	register int s=0,f=1;
	register char ch(gc);
	while(ch<'0'||ch>'9') ch=='-'?f=-1,ch=gc:ch=gc;
	while(ch>='0'&&ch<='9') s=s*10+ch-48,ch=gc;
	return s*f;
}
using namespace std;
struct yuansu {
	int x,y;
} bian[200005];
int minn(10000000);
int b[100005],dui[100005],deepth[100005];
int lef[100005],rig[100005];
int f(1);
int n,x,y;
int possible[100005][2];
int cmp(yuansu a,yuansu b) {
	return a.x<b.x;
}
int min(int a,int b) {
	return a<b?a:b;
}
int bfs() {
	int head(1),tail(2);
	b[0]=b[1]=1;
	dui[1]=1;
	while(head!=tail) {
		int i(dui[head]);
		if(!lef[i]) {
			if(minn!=10000000) {
				if((minn>deepth[dui[head]]+1)||(minn<deepth[dui[head]]-1))return 1;
			}
			if(minn!=deepth[dui[head]]&&minn!=10000000)f=0;
			minn=min(minn,deepth[dui[head]]);
		}
		if(!rig[i]) {
			if(minn!=10000000) {
				if((minn>deepth[dui[head]]+1)||(minn<deepth[dui[head]]-1))return 1;
			} 
			IF (!! Minn = deepth [DUI [head]] && Minn = 10000000) F = 0; 
	IF {(RIG [Dian]!)
			Minn = min (Minn, deepth [DUI [head]]); 
		} 
		IF {(B [LEF [I]]!) 
			DUI [tail] = LEF [I]; 
			deepth [DUI [tail]] = deepth [DUI [ head]] +. 1; 
			tail ++; 
		} 
		IF {(B [RIG [I]]!) 
			DUI [tail] = RIG [I]; 
			deepth [DUI [tail]] = deepth [DUI [head]] +. 1; 
			tail ++ ; 
		} 
		head ++; 
	} 
	return 0; 
} 
void DFS (int Dian) { 
	IF {(LEF [Dian]!) 
		IF (deepth [Dian] == Minn) 
			Possible [Dian] [0] =. 1; // If the point as a light point, the point of the subtree comprising impossible penetration state (i.e. state 0) 
		the else 
			possible [Dian] [. 1] =. 1; 
	} 
		IF (deepth [Dian] == Minn) 
			possible [Dian] [0 ] =. 1; 
		the else 
			Possible [Dian] [. 1] =. 1;
	}
	if(lef[dian]) {
		dfs(lef[dian]);
		possible[dian][0]|=possible[lef[dian]][0];
		possible[dian][1]|=possible[lef[dian]][1];
	}
	if(rig[dian]) {
		dfs(rig[dian]);
		possible[dian][0]|=possible[rig[dian]][0];
		possible[dian][1]|=possible[rig[dian]][1];
	}
	return;
}
void chushihua() {
	dfs(1);
	return;
}
int violence_dp(int dian,int zt) {
	if(zt==0||zt==1) {
		if(!possible[dian][zt])return 0;
		else return 100000000;
	} else {
		int ans(100000000);
		if(lef[dian]==0&&rig[dian]==0)return 100000000;
		else if(!lef[dian]&&rig[dian]) {
			if (deepth [dian] == minn) { 
				years = min (years violence_dp (rig [dian], 0) +1); // 1 + 0 (+1) 
				ans = min (years violence_dp (rig [dian ], 2) +1); // 1 + 2 (1) 
			} else { 
				years = min (years violence_dp (rig [dian], 1)); // 0 1 + 
				years = min (years violence_dp ( rig [dian] 2)); // 0 + 2 
			} 
		} else if (lef [dian] && rig [dian]) {? 
			if (deepth [dian] == minn) { 
				years = min (years violence_dp ( lef [dian], 0)); // 0 1 + 
				years = min (years violence_dp (lef [dian] 2)); // 2 + 1 
			} else { 
				years = min (years violence_dp (lef [dian ], 1) +1); // 1 + 0 (1) 
				year = min (years violence_dp (lef [dian] 2) +1) // 2 + 0 (1) 
			} 
		} else {
			years = min ( years violence_dp (lef [dian], 0) + violence_dp (rig [dian], 1)); // 0 + 1
			ans=min(ans,violence_dp(lef[dian],2)+violence_dp(rig[dian],1));//2+1
			ans=min(ans,violence_dp(lef[dian],0)+violence_dp(rig[dian],2));//0+2
			ans=min(ans,violence_dp(lef[dian],1)+violence_dp(rig[dian],0)+1);//1+0 (+1)
			ans=min(ans,violence_dp(lef[dian],1)+violence_dp(rig[dian],2)+1);//1+2 (+1)
			ans=min(ans,violence_dp(lef[dian],2)+violence_dp(rig[dian],0)+1);//2+0 (+1)
		}
		return ans;
	}
}
int main() {
	freopen("mobiles.in","r",stdin);
	freopen("mobiles.out","w",stdout);
	n=read();
	for(int i=1; i<=n; i++) {
		x=read();
		y=read();
		IF (X> 0) { 
			LEF [I] = X; 
		}
		if(y>0) {
			rig[i]=y;
		} 
	} 
	IF (BFS ()) { 
		COUT << -. 1; 
		return 0; 
	} 
	chushihua (); 
	IF (F) {// judgment is not a full binary 
		COUT << 0; 
		return 0; 
	} 
	IF (violence_dp ( 1,2)> 10000000) COUT << -. 1; 
	the else COUT << violence_dp (1,2); 
	return 0; 
}

 


    }
    Chushihua ();
    IF (F) {// judgment is not a full binary
        COUT << 0;
        return 0;
    }
    IF (violence_dp (1,2)> 10000000) COUT << -. 1;
    the else COUT << violence_dp (1,2);
    return 0;
}

Guess you like

Origin www.cnblogs.com/thedreammaker/p/10986448.html