BZOJ 3319 黑白樹 並查集+線段樹,bzoj3319

來源:互聯網
上載者:User

BZOJ 3319 黑白樹 並查集+線段樹,bzoj3319

題目大意:給定一棵樹,有兩種操作:

1.詢問某個點到根的路徑上遇到的第一個黑色邊的編號

2.將某條路徑塗黑


首先將每條邊歸到它下面的點上

記錄每個點到根路徑上深度最大的黑點

那麼將一個點塗黑就相當於把子樹中所有的點的最深黑點取個max

這個用線段樹就可以維護

由於一個點最多隻會被塗黑一次 因此時間複雜度是O(nlogn)的

 

現在問題就是如何在每次修改時找到路徑上所有白點

這個用並查集就可以搞了

如果一個點被塗黑 那麼就把這個點在並查集中向父節點連一條邊

然後按照樹鏈剖分那種做法就可以了


總時間複雜度O(nlogn)

加了讀入最佳化之後跑了9888ms

正解難道是線性做法?不明- -


#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define M 1001001using namespace std;struct abcd{int to,next;}table[M<<1];int head[M],tot=1;int n,m;int fa[M],dpt[M],num[M],st[M],ed[M];void Add(int x,int y){table[++tot].to=y;table[tot].next=head[x];head[x]=tot;}void DFS(int x){static int T;int i;st[x]=++T;dpt[x]=dpt[fa[x]]+1;for(i=head[x];i;i=table[i].next)if(table[i].to!=fa[x]){fa[table[i].to]=x;num[table[i].to]=i>>1;DFS(table[i].to);}ed[x]=T;}bool Compare(int x,int y){return dpt[x]<dpt[y];}struct Segtree{Segtree *ls,*rs;int max_dpt_point;Segtree(){ls=rs=0x0;max_dpt_point=0;}void Build_Tree(int x,int y){int mid=x+y>>1;if(x==y) return ;(ls=new Segtree)->Build_Tree(x,mid);(rs=new Segtree)->Build_Tree(mid+1,y);}void Modify(int x,int y,int l,int r,int val){int mid=x+y>>1;if(x==l&&y==r){max_dpt_point=max(max_dpt_point,val,Compare);return ;}if(r<=mid) ls->Modify(x,mid,l,r,val);else if(l>mid) rs->Modify(mid+1,y,l,r,val);else ls->Modify(x,mid,l,mid,val) , rs->Modify(mid+1,y,mid+1,r,val);}int Get_Point(int x,int y,int pos){int mid=x+y>>1;if(x==y) return max_dpt_point;if(pos<=mid)return max(ls->Get_Point(x,mid,pos),max_dpt_point,Compare);elsereturn max(rs->Get_Point(mid+1,y,pos),max_dpt_point,Compare);}}*tree=new Segtree;namespace Union_Find_Set{int belong[M];int Find(int x){if(!belong[x]||belong[x]==x)return belong[x]=x;return belong[x]=Find(belong[x]);}}void Modify(int x,int y){using namespace Union_Find_Set;x=Find(x);y=Find(y);while(x!=y){if(dpt[Find(fa[x])]<dpt[Find(fa[y])])swap(x,y);tree->Modify(1,n,st[x],ed[x],x);belong[x]=fa[x];x=Find(x);}}namespace IStream{#define L (1<<15)char Get_Char(){static char buffer[M],*S,*T;if(S==T){T=(S=buffer)+fread(buffer,1,L,stdin);if(S==T) return EOF;}return *S++;}int Get_Int(){int re=0;char c=0;while(c<'0'||c>'9')c=Get_Char();while(c>='0'&&c<='9')re=(re<<1)+(re<<3)+(c-'0'),c=Get_Char();return re;}}int main(){using namespace Union_Find_Set;using namespace IStream;int i,p,x,y;cin>>n>>m;for(i=1;i<n;i++){x=Get_Int();y=Get_Int();Add(x,y);Add(y,x);}DFS(1);tree->Build_Tree(1,n);for(i=1;i<=m;i++){p=Get_Int();if(p==1)x=Get_Int(),printf("%d\n",num[tree->Get_Point(1,n,st[x])]);elsex=Get_Int(),y=Get_Int(),Modify(x,y);}return 0;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.