bzoj1149

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1149

水题。。。。。
直接BFS。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional>
#include<deque>
#include<cctype>
#include<climits>
#include<complex>
//#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj
 
using namespace std;

typedef long long LL;
typedef double DB;
typedef pair<int,int> PII;
typedef complex<DB> CP;

#define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define re(i,a,b)  for(i=a;i<=b;i++)
#define red(i,a,b) for(i=a;i>=b;i--)
#define fi first
#define se second
#define m_p(a,b) make_pair(a,b)
#define SF scanf
#define PF printf
#define two(k) (1<<(k))

template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;}

const DB EPS=1e-9;
inline int sgn(DB x){if(abs(x)<EPS)return 0;return(x>0)?1:-1;}
const DB Pi=acos(-1.0);

inline int gint()
  {
        int res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z=='-'){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
        return (neg)?-res:res; 
    }
inline LL gll()
  {
      LL res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z=='-'){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
        return (neg)?-res:res; 
  }

const int maxN=100000;

int N;
PII a[maxN+100];
int maxdep,dep[maxN+100];
int ans;
int head,tail,que[maxN+100];
int cnt,val[2*maxN+100],sum[2*maxN+100];

int main()
  {
      freopen("bzoj1149.in","r",stdin);
      freopen("bzoj1149.out","w",stdout);
      int i;
      N=gint();
      dep[1]=1;
      re(i,1,N)
        {
            a[i].fi=gint(),a[i].se=gint();
            if(a[i].fi!=-1)dep[a[i].fi]=dep[i]+1;
            if(a[i].se!=-1)dep[a[i].se]=dep[i]+1;
        }
        ans=0;
        re(i,1,N)upmax(maxdep,dep[i]);
      re(i,1,N)if(a[i].fi==-1 || a[i].se==-1)if(maxdep-dep[i]>1){ans=-1;break;}
      if(ans==-1){cout<<ans<<endl;return 0;}
      que[head=tail=1]=1;
      re(i,2,maxdep-1)
        {
            int temp=tail;
            while(head<=temp)
              {
                  int u=que[head++];
                  que[++tail]=a[u].fi;
                  que[++tail]=a[u].se;
              }
        }
      re(i,head,tail)
        {
            int u=que[i];
            val[++cnt]=(a[u].fi==-1)?0:1;
            val[++cnt]=(a[u].se==-1)?0:1;
        }
      re(i,1,N)sum[i]=sum[i-1]+val[i];
      int l=1,r=cnt;
      while(l<r)
        {
            int mid=(l+r)/2;
            int lf,rf;
            if(sum[mid]-sum[l-1]==mid-l+1) lf=1; else if(sum[mid]-sum[l-1]==0) lf=0; else lf=2;
            if(sum[r]-sum[mid]==r-mid) rf=1; else if(sum[r]-sum[mid]==0) rf=0; else rf=2;
            if(lf==2 && rf==2) {ans=-1;break;}
            if(lf==1 && rf==0) break;
            if(lf==0 && rf==1) {ans++;break;}
            if(lf==0 && rf==0) break;
            if(lf==1 && rf==1) break;
            if(lf==0 && rf==2) {ans++;l=mid+1;continue;}
            if(lf==1 && rf==2) {l=mid+1;continue;}
            if(lf==2 && rf==0) {r=mid;continue;}
            if(lf==2 && rf==1) {ans++;r=mid;continue;}
        }
      cout<<ans<<endl;
      return 0;
  }
View Code

 

posted @ 2015-07-15 21:06  maijing  阅读(167)  评论(0编辑  收藏  举报