UVa 11688 Rotate to root

来源:互联网 发布:html5企业网站源码 编辑:程序博客网 时间:2024/06/05 09:39

题意:

模拟二叉树的旋转,不理解先去看看旋转再来看着道题。题目就是问每一个节点旋转到根节点时,树的最大深度。用dp来做。首先,我们来看这个图。

这里写图片描述

图中,hr表示节点x向右旋转过根节点再向右一直到达底部所能达到的最大深度,hl同理。dR,dL分别表示某个节点的左右子节点所能达到的最底部的深度,dr和dl表示该点需要左旋(右旋)多少次才能到达根节点。那么方程就是dp[u]=1+max(max(hl,hr),max(dL+dl,dR+dr));这个方程有点难理解,,看到别人的才会的,所以需要多模拟几遍。然后对于转移我就有一个疑问了:DP(lch[u],hl,max(hr,dr+dR+1),dl,dr+1);DP(rch[u],max(hl,dL+dl+1),hr,dl+1,dr); 比如说第一个,为何不是hr+1?求菊苣解答。

代码:

////  Created by  CQU_CST_WuErli//  Copyright (c) 2015 CQU_CST_WuErli. All rights reserved.//// #include<bits/stdc++.h>#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <cctype>#include <cmath>#include <string>#include <vector>#include <list>#include <map>#include <queue>#include <stack>#include <set>#include <algorithm>#include <sstream>#define CLR(x) memset(x,0,sizeof(x))#define OFF(x) memset(x,-1,sizeof(x))#define MEM(x,a) memset((x),(a),sizeof(x))#define ALL(x) x.begin(),x.end()#define AT(i,v) for (auto &i:v)#define For_UVa if (kase!=1) cout << endl#define BUG cout << "I am here" << endl#define lookln(x) cout << #x << "=" << x << endl#define look(x) cout << #x << "=" << x#define SI(a) scanf("%d",&a)#define SII(a,b) scanf("%d%d",&a,&b)#define SIII(a,b,c) scanf("%d%d%d",&a,&b,&c)#define rep(flag,start,end) for(int flag=start;flag<=end;flag++)#define Rep(flag,start,end) for (int flag=start;flag>=end;flag--)#define Lson l,mid,rt<<1#define Rson mid+1,r,rt<<1|1#define Root 1,n,1#define BigInteger bigntemplate <typename T> T max(T& a,T& b) {return a>b?a:b;}template <typename T> T min(T& a,T& b) {return a<b?a:b;}int gcd(int a,int b) {return b==0?a:gcd(b,a%b);}long long gcd (long long a,long long b) {return b==0LL?a:gcd(b,a%b);}const int MAX_L=2005;// For BigIntegerconst int INF_INT=0x3f3f3f3f;const long long INF_LL=0x7fffffff;const int MOD=1e9+7;const double eps=1e-9;const double pi=acos(-1);typedef long long  ll;using namespace std;const int N=1e5+100;int lch[N],rch[N];int par[N];int n;int h[N];int dep[N];int dp[N];int lshift,rshift;int dfs(int u,int d) {    dep[u]=d;    if (!u) return h[u]=0;    h[u]=max(dfs(lch[u],d+1),dfs(rch[u],d+1))+1;    return h[u];    }void DP(int u,int hl,int hr,int dl,int dr) {    if (!u) return;    int dL=h[lch[u]];    int dR=h[rch[u]];    dp[u]=1+max(max(hl,hr),max(dL+dl,dR+dr));    DP(lch[u],hl,max(hr,dr+dR+1),dl,dr+1);    DP(rch[u],max(hl,dL+dl+1),hr,dl+1,dr); }int main(){#ifdef LOCAL    freopen("C:\\Users\\john\\Desktop\\in.txt","r",stdin);//  freopen("C:\\Users\\john\\Desktop\\out.txt","w",stdout);#endif    while (cin >> n) {        CLR(lch);CLR(rch);CLR(par);        rep(i,1,n) {            int l,r;            SII(l,r);            lch[i]=l;            rch[i]=r;            par[l]=par[r]=1;        }        int root;        rep(i,1,n) if (par[i]==0) {            root=i;break;        }        dfs(root,0);        lshift=rshift=0;        DP(root,0,0,0,0);        rep(i,1,n) cout << dp[i] << endl;    }    return 0;}
0 0
原创粉丝点击