BZOJ 3732 Network Link-Cut-Tree (我是認真的!!

來源:互聯網
上載者:User

標籤:include   splay   log   mil   路徑   roo   push   動態   ace   

題目大意:給定一個n個點m條邊的無向連通圖。k次詢問兩點之間全部路徑中最長邊的最小值

LCT的裸題!

首先維護一個動態最小產生樹,然後每次增加邊時刪除兩點間路徑上權值最大的邊。最後詢問時直接求x到y鏈上的最大權值就可以。水爆了!

!

。

。。

好吧開玩笑的 真正的題解見http://blog.csdn.net/popoqqq/article/details/39755703

我僅僅是閑得無聊水一發LCT罷了0.0

TLE了好久。。。

由於有邊權為0的邊我沒更新。。。

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define M 15010#define INF 2147483647using namespace std;struct edge{    int x,y,z;}e[M<<1];struct abcd{    abcd *fa,*ls,*rs;    int num,maxedge;    bool rev_mark;    abcd(int x);    void Reverse();    void Push_Up();    void Push_Down();}*null=new abcd(0),*tree[M],*edges[M<<1];abcd :: abcd(int x){    fa=ls=rs=null;    num=maxedge=x;    rev_mark=0;}void abcd :: Reverse(){    rev_mark^=1;    swap(ls,rs);}void abcd :: Push_Up(){    int maxnum=-1;    if(e[ls->maxedge].z>maxnum)        maxnum=e[ls->maxedge].z,maxedge=ls->maxedge;    if(e[rs->maxedge].z>maxnum)        maxnum=e[rs->maxedge].z,maxedge=rs->maxedge;    if(e[num].z>maxnum)        maxedge=num;     }void abcd :: Push_Down(){    if(fa->ls==this||fa->rs==this)        fa->Push_Down();    if(rev_mark)    {        ls->Reverse();        rs->Reverse();        rev_mark=0;    }}void Zig(abcd *x){    abcd *y=x->fa;    y->ls=x->rs;    x->rs->fa=y;    x->rs=y;    x->fa=y->fa;    if(y==y->fa->ls)        y->fa->ls=x;    else if(y==y->fa->rs)        y->fa->rs=x;    y->fa=x;    y->Push_Up();}void Zag(abcd *x){    abcd *y=x->fa;    y->rs=x->ls;    x->ls->fa=y;    x->ls=y;    x->fa=y->fa;    if(y==y->fa->ls)        y->fa->ls=x;    else if(y==y->fa->rs)        y->fa->rs=x;    y->fa=x;    y->Push_Up();}void Splay(abcd *x){    x->Push_Down();    while(x->fa->ls==x||x->fa->rs==x)    {        abcd *y=x->fa,*z=y->fa;        if(x==y->ls)        {            if(y==z->ls)                Zig(y);            Zig(x);        }        else        {            if(y==z->rs)                Zag(y);            Zag(x);        }    }    x->Push_Up();}void Access(abcd *x){    abcd *y=null;    while(x!=null)    {        Splay(x);        x->rs=y;        x->Push_Up();        y=x;        x=x->fa;    }}abcd* Find_Root(abcd *x){    while(x->fa!=null)        x=x->fa;    return x;}void Move_To_Root(abcd *x){    Access(x);    Splay(x);    x->Reverse();}void Link(abcd *x,abcd *y){    Move_To_Root(x);    x->fa=y;}void Cut(abcd *x,abcd *y){    Move_To_Root(x);    Access(y);    Splay(y);    x->fa=null;    y->ls=null;    y->Push_Up();}int Query(abcd *x,abcd *y){    Move_To_Root(x);    Access(y);    Splay(y);    return y->maxedge;}int n,m,k;void Insert(abcd *x,abcd *y,int pos){    if(e[pos].x==e[pos].y)        return ;    if( Find_Root(x)==Find_Root(y) )    {        int temp=Query(x,y);        if(e[temp].z<=e[pos].z)            return;        Cut(edges[temp],tree[e[temp].x]);        Cut(edges[temp],tree[e[temp].y]);    }    //if( Find_Root(x)==Find_Root(y) )    //printf("%d\n",1/0);    Link(x,edges[pos]);    Link(y,edges[pos]);}int main(){    int i,x,y;    cin>>n>>m>>k;    for(i=1;i<=n;i++)        tree[i]=new abcd(0);    for(i=1;i<=m;i++)        edges[i]=new abcd(i);    for(i=1;i<=m;i++)    {        scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].z);        Insert(tree[e[i].x],tree[e[i].y],i);    }    for(i=1;i<=k;i++)    {        scanf("%d%d",&x,&y);        printf("%d\n", e[Query(tree[x],tree[y])].z );    }}


BZOJ 3732 Network Link-Cut-Tree (我是認真的!!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.