bzoj1095[ZJOI2007]Hide 捉迷藏

http://www.lydsy.com/JudgeOnline/problem.php?id=1095

好像有2种做法:线段树维护括号编码&动态树分治。

线段树维护括号编码:

《数据结构的提炼与压缩》

这篇论文是讲得极好的。

#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 fill(a,l,r,v) fill(a+l,a+r+1,v)
#define re(i,a,b)  for(i=(a);i<=(b);i++)
#define red(i,a,b) for(i=(a);i>=(b);i--)
#define ire(i,x) for(typedef(x.begin()) i=x.begin();i!=x.end();i++)
#define fi first
#define se second
#define m_p(a,b) make_pair(a,b)
#define p_b(a) push_back(a)
#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;}

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

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; 
    }
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;
int now,info[maxn+100];
struct Tedge{int v,next;}edge[2*maxn+100];
int status[maxn+100],ge;

void addedge(int u,int v){now++;edge[now].v=v;edge[now].next=info[u];info[u]=now;}

int cnt,ty[3*maxn+100],pos[maxn+100];
void dfs(int u)
  {
      ty[++cnt]=-1;
      ty[++cnt]=u;
      pos[u]=cnt;
      int i,v;
      for(i=info[u],v=edge[i].v;i!=-1;i=edge[i].next,v=edge[i].v)if(!pos[v])dfs(v);
      ty[++cnt]=-2;
  }

struct Tdata
  {
      int a,b;
      Tdata(int _a=0,int _b=0){a=_a;b=_b;}
      friend Tdata operator +(Tdata l,Tdata r){return (l.b<=r.a)?Tdata(l.a+r.a-l.b,r.b):Tdata(l.a,l.b-r.a+r.b);}
      friend bool operator ==(Tdata x,Tdata y){return x.a==y.a && x.b==y.b;}
      friend bool operator !=(Tdata x,Tdata y){return !(x==y);}
  };
Tdata null=Tdata(-1,-1);

/*
dist 子区间的最大值 
whole 区间[l,r]的二元组(a,b) 
lp    右端点为黑点的a+b最大的前缀的二元组(a,b)
rp    左端点为黑点的a+b最大的后缀的二元组(a,b)
lm    右端点为黑点的b-a最大的前缀的二元组(a,b)
rm    左端点为黑点的a-b最大的后缀的二元组(a,b)
null表示不存在 
*/  
struct Tnode
  {
      int dist;
      Tdata whole,lp,rp,lm,rm;
      void init(int x);
  };

void Tnode::init(int x)
  {
      int lcnt=(ty[x]==-2),rcnt=(ty[x]==-1);
      whole=Tdata(lcnt,rcnt);
      dist=-1;
      if(ty[x]>=1 && status[ty[x]]) lp=rp=lm=rm=whole; else lp=rp=lm=rm=null;
  }

Tnode make_up(Tnode x,Tnode y)
  {
      Tnode res;Tdata p;
      
      res.whole=x.whole+y.whole;
      
      res.lp=x.lp;
      if(y.lp!=null)
        {
            p=x.whole+y.lp;
            if(res.lp==null || res.lp.a+res.lp.b<p.a+p.b)res.lp=p;
        }
      if(y.lm!=null)
        {
            p=x.whole+y.lm;
            if(res.lp==null || res.lp.a+res.lp.b<p.a+p.b)res.lp=p;
        }
      
      res.rp=y.rp;
      if(x.rp!=null)
        {
            p=x.rp+y.whole;
            if(res.rp==null || res.rp.a+res.rp.b<p.a+p.b)res.rp=p;
        }
      if(x.rm!=null)
        {
            p=x.rm+y.whole;
            if(res.rp==null || res.rp.a+res.rp.b<p.a+p.b)res.rp=p;
        }
      
      res.lm=x.lm;
      if(y.lm!=null)
        {
            p=x.whole+y.lm;
            if(res.lm==null || res.lm.b-res.lm.a<p.b-p.a)res.lm=p;
        }
      
      res.rm=y.rm;
      if(x.rm!=null)
        {
            p=x.rm+y.whole;
            if(res.rm==null || res.rm.a-res.rm.b<p.a-p.b)res.rm=p;
        }
      
      res.dist=max(x.dist,y.dist);
      if(x.rp!=null && y.lm!=null)
          {
              p=x.rp+y.lm;
                upmax(res.dist,p.a+p.b);
            }
      if(x.rm!=null && y.lp!=null)
          {
              p=x.rm+y.lp;
                upmax(res.dist,p.a+p.b);
            }
      
      return res;
      
  }

Tnode tr[4*3*maxn+100];
void build(int rt,int l,int r)
  {
      if(l==r){tr[rt].init(l);return;}
      int mid=(l+r)>>1;
      build(rt<<1,l,mid);
      build(rt<<1|1,mid+1,r);
      tr[rt]=make_up(tr[rt<<1],tr[rt<<1|1]);
  }
void update(int rt,int l,int r,int x)
  {
      if(l==r){tr[rt].init(l);return;}
      int mid=(l+r)/2;
      if(x<=mid)update(rt<<1,l,mid,x);else update(rt<<1|1,mid+1,r,x);
      tr[rt]=make_up(tr[rt<<1],tr[rt<<1|1]);
  }

int main()
  {
      freopen("bzoj1095.in","r",stdin);
      freopen("bzoj1095.out","w",stdout);
      int i;
      n=gint();
      now=-1;mmst(info,-1);
      re(i,1,n-1){int u=gint(),v=gint();addedge(u,v);addedge(v,u);}
      dfs(1);
        re(i,1,n)status[i]=1;ge=n;
      build(1,1,cnt);
      int Q;SF("%d\n",&Q);
      while(Q--)
        {
            char t;SF("%c",&t);
            if(t=='G')
              {
                  if(ge==0)PF("-1\n");else if(ge==1) PF("0\n"); else PF("%d\n",tr[1].dist);
              }
            else
              {
                      int x;SF("%d",&x);
                        status[x]^=1;
                        if(status[x])ge++;else ge--;
                        update(1,1,cnt,pos[x]);
                    }
                SF("\n");
        }
      return 0;
  }
View Code

动态树分治:

首先,我们想平常树分治那样,找到一个重心。

然后将这个重心作为新的根,将它的所有儿子及其子树划分成大小相等的两个整体。(普通的树分治是一个儿子作为一个整体,但是这样合并的时候有点麻烦,如果只分成两个整体,合并就比较简单)

分别用一个堆维护这两个整体的黑点到重心的距离。

那么经过重心的最长的路径就是这两个堆的最大值之和。

递归处理这两个整体。

每个点最多属于$logn$个重心。

看程序比较好理解。

#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 fill(a,l,r,v) fill(a+l,a+r+1,v)
#define re(i,a,b)  for(i=(a);i<=(b);i++)
#define red(i,a,b) for(i=(a);i>=(b);i--)
#define ire(i,x) for(typedef(x.begin()) i=x.begin();i!=x.end();i++)
#define fi first
#define se second
#define m_p(a,b) make_pair(a,b)
#define p_b(a) push_back(a)
#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;}

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

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; 
    }
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;
const int inf=0x3f3f3f3f;

struct Priority_Queue
  {
      priority_queue<int>heap,delmark;
      void push(int x){heap.push(x);}
      void erase(int x){delmark.push(x);}
      void clear(){while(!delmark.empty() && !heap.empty() && delmark.top()==heap.top())heap.pop(),delmark.pop();}
      int top(){clear();return heap.empty()?-inf:heap.top();}
      void pop(){clear();heap.pop();}
      bool empty(){return heap.size()==delmark.size();}
  };

int n;
int now[2],info[2][maxn+100];
struct Tedge{int v,next,kk,dis;}edge[2][3000000];//注意边数是nlogn 
int status[maxn+100];

void addedge(int k,int u,int v,int kk=0,int dis=0)
  {
      now[k]++;
      edge[k][now[k]].v=v;
      edge[k][now[k]].next=info[k][u];
      if(k){edge[k][now[k]].kk=kk;edge[k][now[k]].dis=dis;}
      info[k][u]=now[k];
  }

int num;
PII cd[maxn+100];
Priority_Queue Q[maxn+100][2];
int ans[maxn+100];

int sz[maxn+100],mx[maxn+100];
void getroot(int u,int fa,int all,int &rt)
  {
      sz[u]=1;mx[u]=0;
      int i,v;
      for(i=info[0][u],v=edge[0][i].v;i!=-1;i=edge[0][i].next,v=edge[0][i].v)if(v!=fa)
        {
            getroot(v,u,all,rt);
            sz[u]+=sz[v];
            upmax(mx[u],sz[v]);
        }
      upmax(mx[u],all-sz[u]);
      if(!rt || mx[u]<mx[rt])rt=u;
  }

void dfs(int u,int fa,int dis,int cur,int kind)
  {
      addedge(1,u,cur,kind,dis);
      Q[cur][kind].push(dis);
      int i,v;
      for(i=info[0][u],v=edge[0][i].v;i!=-1;i=edge[0][i].next,v=edge[0][i].v)if(v!=fa)dfs(v,u,dis+1,cur,kind);
  }

void calc(int cur)
  {
      ans[cur]=max(ans[cd[cur].fi],ans[cd[cur].se]);
      if(!Q[cur][0].empty() && !Q[cur][1].empty())upmax(ans[cur],Q[cur][0].top()+Q[cur][1].top());
  }

int prep(int u,int all)
  {
      if(all<=2)return 0;
      int i,rt=0,tmp=0;
      getroot(u,-1,all,rt);
      getroot(rt,-1,all,tmp);
      int cur=++num,sum=0,v;
      for(i=info[0][rt],v=edge[0][i].v;i!=-1;i=edge[0][i].next,v=edge[0][i].v)
          {
              sum+=sz[v];
                if(sum>=(all-1)/2 || edge[0][i].next==-1)break;
            }
      int st1=info[0][rt],ed1=i,st2=edge[0][ed1].next;
      edge[0][ed1].next=-1;
      dfs(rt,-1,0,cur,0);
      cd[cur].fi=prep(rt,sum+1);
      edge[0][ed1].next=info[0][rt]=st2;
      dfs(rt,-1,0,cur,1);
      cd[cur].se=prep(rt,all-sum);
      info[0][rt]=st1;
      calc(cur);
      return cur;
      
  }

void update(int x)
  {
      int i,cur,dis,kind;
      for(i=info[1][x],cur=edge[1][i].v,dis=edge[1][i].dis,kind=edge[1][i].kk;i!=-1;i=edge[1][i].next,cur=edge[1][i].v,dis=edge[1][i].dis,kind=edge[1][i].kk)
        {
            if(status[x])Q[cur][kind].push(dis);else Q[cur][kind].erase(dis);
            calc(cur);
        }
  }

int main()
  {
      freopen("bzoj1095.in","r",stdin);
      freopen("bzoj1095.out","w",stdout);
      int i;
      n=gint();
      mmst(now,-1);mmst(info,-1);
      re(i,1,n-1){int u=gint(),v=gint();addedge(0,u,v);addedge(0,v,u);}
      re(i,1,n)status[i]=1;
      ans[0]=-inf;
      prep(1,n);
      int ge=n,T;SF("%d\n",&T);
      while(T--)
        {
            char z=getchar();
            if(z=='G')
              {
                  if(ge==0)PF("-1\n");else if(ge==1) PF("0\n");else PF("%d\n",ans[1]);
              }
            else
              {
                  int x;SF("%d",&x);
                  status[x]^=1;
                  if(status[x])ge++;else ge--;
                  update(x);
              }
            SF("\n");
        }
      return 0;
  }
View Code

 

posted @ 2015-11-23 21:16  maijing  阅读(223)  评论(0编辑  收藏