hdu 5029 Relief grain(樹鏈剖分好題),hdurelief

來源:互聯網
上載者:User

hdu 5029 Relief grain(樹鏈剖分好題),hdurelief
Relief grainTime Limit: 10000/5000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)
Total Submission(s): 295    Accepted Submission(s): 66


Problem DescriptionThe soil is cracking up because of the drought and the rabbit kingdom is facing a serious famine. The RRC(Rabbit Red Cross) organizes the distribution of relief grain in the disaster area.

We can regard the kingdom as a tree with n nodes and each node stands for a village. The distribution of the relief grain is divided into m phases. For each phases, the RRC will choose a path of the tree and distribute some relief grain of a certain type for every village located in the path.

There are many types of grains. The RRC wants to figure out which type of grain is distributed the most times in every village. 
InputThe input consists of at most 25 test cases.

For each test case, the first line contains two integer n and m indicating the number of villages and the number of phases.

The following n-1 lines describe the tree. Each of the lines contains two integer x and y indicating that there is an edge between the x-th village and the y-th village.
  
The following m lines describe the phases. Each line contains three integer x, y and z indicating that there is a distribution in the path from x-th village to y-th village with grain of type z. (1 <= n <= 100000, 0 <= m <= 100000, 1 <= x <= n, 1 <= y <= n, 1 <= z <= 100000)

The input ends by n = 0 and m = 0. 
OutputFor each test case, output n integers. The i-th integer denotes the type that is distributed the most times in the i-th village. If there are multiple types which have the same times of distribution, output the minimal one. If there is no relief grain in a village, just output 0. 
Sample Input

2 41 21 1 11 2 22 2 22 2 15 31 23 13 45 32 3 31 5 23 3 30 0
 
Sample Output
1223302Hint  For the first test case, the relief grain in the 1st village is {1, 2}, and the relief grain in the 2nd village is {1, 2, 2}. 
 
Source2014 ACM/ICPC Asia Regional Guangzhou Online 
Recommendhujie   |   We have carefully selected several similar problems for you:  5041 5040 5039 5038 5037  

題意:

給你一顆節點數最多為1e5的樹。然後是m(1e5)條操作

x y z。

就是把x到y路徑上的所有點包括x,y圖上x顏色。

最後要你輸出,每個點被圖的次數最多的顏色是那個。如果有多個輸出z最小的那個z(1-1e5)。

思路:

先說下我比賽時的思路吧。雖然跟我下面要講的方法比起來有點蠢。但是是自己想出來的。且能A掉這題。所以還是記錄下吧。開始想這題時一頭霧水。這題很明顯是樹鏈剖分加線段樹。樹鏈剖分那部分肯定不用怎麼動了。關鍵就是線段樹維護什麼呀。維護什麼顏色最多。由於操作圖的顏色可能不規律。給每個結點開個數組明顯不現實。糾結半天

注意到題目是先操作完再詢問。所以可以考慮離線處理。先存下所有操作。然後按顏色排序。然後再用線段樹維護。加的lazy是自己對應的區間col[rt]顏色被圖了num[rt]次。比較麻煩的是如果一個區間如果有顏色了切和現在要標記的顏色不一樣怎麼辦。最蠢的辦法就是先把現在的標記PushDown下去然後再標記。但是這樣到一定時候跟暴力沒什麼區別。注意到我們對顏色排序了。所以可以知道顏色標記線上段樹裡是一層一層分布的。也就是可以確定的是兩個不同顏色標記之間的樹只可能一種顏色。所以我們PushDown的時候有些標記是沒必要PushDown的。比如現在在圖5顏色。而現在要下傳的標記是3顏色。而子樹卻是2顏色。如果3顏色標記數目小於2顏色標記的話就不用下傳了。因為所有3顏色肯定全部集中在3這個標記了(因為顏色是分層的。3顏色標記已經全部被5這個顏色標記推到2這裡了)。所以3一定成不了答案。由於比賽時代碼寫殘了。到最後還是wa。比賽後各種找bug也沒找到。於是用admin帳號copy了幾個AC代碼對拍。結果發現AC代碼居然有好幾個是錯的。。(因為我也只找了幾個)可見資料也是蠻水的。。。

後來我程式還是成功debug了。用時2s多一點。

詳細見代碼:

#include<algorithm>#include<iostream>#include<string.h>#include<stdio.h>using namespace std;const int INF=0x3f3f3f3f;const int maxn=100010;typedef long long ll;#pragma comment(linker,"/STACK:1024000000,1024000000")#define lson L,mid,ls#define rson mid+1,R,rsint sz[maxn],dep[maxn],fa[maxn],son[maxn],id[maxn],top[maxn],rd[maxn];int col[maxn<<2],num[maxn<<2],cnum[maxn],ccol[maxn],ans[maxn],cnt,ptr,ccl;struct node{    int v;    node *next;} ed[maxn<<1],*head[maxn];struct qnode{    int u,v,cc;} qq[maxn];bool cmp(qnode a,qnode b){    return a.cc<b.cc;}void adde(int u,int v){    ed[cnt].v=v;    ed[cnt].next=head[u];    head[u]=&ed[cnt++];}void build(int L,int R,int rt){    col[rt]=num[rt]=0;    if(L==R)    {        ccol[L]=cnum[L]=0;        return;    }    int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;    build(lson);    build(rson);}void PushDown(int L,int R,int rt){    int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;    if(mid==L)    {        if(col[ls]==col[rt]||col[ls]==0)        {            col[ls]=col[rt];            num[ls]+=num[rt];        }        else        {            if(ccol[L]!=col[rt])                ccol[L]=col[rt],cnum[L]=num[rt];            else                cnum[L]+=num[rt];            if(cnum[L]>num[ls])            {                num[ls]=cnum[L];                col[ls]=col[rt];            }        }    }    else if(col[ls]==col[rt]||col[ls]==0)    {        col[ls]=col[rt];        num[ls]+=num[rt];    }    else    {        if(num[ls]<num[rt]||ccl==col[rt])        {            PushDown(L,mid,ls);            col[ls]=col[rt];            num[ls]=num[rt];        }    }    if(mid+1==R)    {        if(col[rs]==col[rt]||col[rs]==0)        {            col[rs]=col[rt];            num[rs]+=num[rt];        }        else        {            if(ccol[R]!=col[rt])                ccol[R]=col[rt],cnum[R]=num[rt];            else                cnum[R]+=num[rt];            if(cnum[R]>num[rs])            {                num[rs]=cnum[R];                col[rs]=col[rt];            }        }    }    else if(col[rs]==col[rt]||col[rs]==0)    {        col[rs]=col[rt];        num[rs]+=num[rt];    }    else    {        if(num[rs]<num[rt]||ccl==col[rt])        {            PushDown(mid+1,R,rs);            col[rs]=col[rt];            num[rs]=num[rt];        }    }    //printf("push %d->%d col %d num %d\n",L,mid,col[ls],num[ls]);    //printf("push %d->%d col %d num %d\n",mid+1,R,col[rs],num[rs]);    col[rt]=num[rt]=0;}void update(int L,int R,int rt,int l,int r,int c){    if(l<=L&&R<=r)    {        if(c==col[rt]||col[rt]==0)        {            col[rt]=c;            num[rt]++;            //printf("X %d->%d col %d num %d\n",L,R,col[rt],num[rt]);            return;        }        if(L!=R)        {            PushDown(L,R,rt);            col[rt]=c;            num[rt]=1;        }        else        {            if(ccol[L]!=c)                ccol[L]=c,cnum[L]=1;            else                cnum[L]++;            if(cnum[L]>num[rt])            {                num[rt]=cnum[L];                col[rt]=c;            }        }        //printf("X %d->%d col %d num %d\n",L,R,col[rt],num[rt]);        return;    }    int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;    if(col[rt]!=0)        PushDown(L,R,rt);    if(l<=mid)        update(lson,l,r,c);    if(r>mid)        update(rson,l,r,c);    //printf("X %d->%d col %d num %d\n",L,R,col[rt],num[rt]);}void qu(int L,int R,int rt){    if(L==R)    {        if(cnum[L]>num[rt])            col[rt]=ccol[L];        ans[rd[L]]=col[rt];        return;    }    int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;    if(col[rt]!=0)        PushDown(L,R,rt);    qu(lson);    qu(rson);}void dfs(int u){    int v,ms=0;    sz[u]=1,son[u]=-1;    for(node *p=head[u];p!=NULL;p=p->next)    {        v=p->v;        if(v==fa[u])            continue;        dep[v]=dep[u]+1;        fa[v]=u;        dfs(v);        if(sz[v]>ms)            ms=sz[v],son[u]=v;        sz[u]+=sz[v];    }}void getid(int u,int ft){    id[u]=++ptr,top[u]=ft,rd[ptr]=u;    if(son[u]!=-1)        getid(son[u],top[u]);    for(node *p=head[u];p!=NULL;p=p->next)    {        if(p->v==son[u]||p->v==fa[u])            continue;        getid(p->v,p->v);    }}void init(int rt){    fa[rt]=-1;    dep[rt]=ptr=0;    dfs(rt);    getid(rt,rt);}void uppath(int u,int v,int d){    //printf("u %d v %d c %d\n",u,v,d);    int f1=top[u],f2=top[v];    while(f1!=f2)    {        if(dep[f1]<dep[f2])            swap(f1,f2),swap(u,v);        //printf("%d->%d\n",id[f1],id[u]);        update(1,ptr,1,id[f1],id[u],d);        u=fa[f1],f1=top[u];    }    if(dep[u]>dep[v])        swap(u,v);    //printf("%d->%d\n",id[u],id[v]);    update(1,ptr,1,id[u],id[v],d);}int main(){    int n,m,i,rt,u,v;    //freopen("in.txt","r",stdin);    //freopen("out1.txt","w",stdout);    while(scanf("%d%d",&n,&m),n||m)    {        cnt=0;        memset(head,0,sizeof head);        for(i=1;i<n;i++)        {            scanf("%d%d",&u,&v);            adde(u,v);            adde(v,u);        }        rt=(n+1)/2;        init(rt);        build(1,ptr,1);        for(i=0;i<m;i++)            scanf("%d%d%d",&qq[i].u,&qq[i].v,&qq[i].cc);        sort(qq,qq+m,cmp);        //for(i=1;i<=n;i++)            //printf("i %d id %d\n",i,id[i]);        for(i=0;i<m;i++)        {            ccl=qq[i].cc;            uppath(qq[i].u,qq[i].v,qq[i].cc);        }        ccl=-1;        qu(1,ptr,1);        for(i=1;i<=n;i++)            printf("%d\n",ans[i]);    }    return 0;}
現在講一下我覺得是正解的方法吧。

方法就是打標記。線段樹維護的是顏色。也就是維護的是[a,b]就是維護a顏色到b顏色某種顏色出現的最多次數。

假設我們處理的是序列而不是樹吧。比如我們要把區間[x,y]圖成a顏色.那麼我們就在x出加個標記a。在y+1就標記-a。

多個標記用鄰接表連起來就行了。然後從序列的最左端處理到最右端先把所有標記更新到線段樹裡。a則a顏色+1。

-a則線上段樹將a顏色-1.然後再詢問線段樹裡出現最多的次數就是序列該位置的次數最多的顏色了。相當於遞推的思想吧。知道了x位置的顏色線段樹.x+1位置的顏色線段樹無非是多了一些顏色或者少了某些顏色。多了減掉。少了的加上就是自己這個位置上的了。這樣做之所以高效的原因是標記的是區間的端點而不是區間類的每一個元素。總的時間複雜度m*log(n)*log(c)。m為詢問數。n為結點數。c為顏色種數。應該是極限了吧。

詳細見代碼:

#include<algorithm>#include<iostream>#include<string.h>#include<stdio.h>using namespace std;const int INF=0x3f3f3f3f;const int maxn=100010;typedef long long ll;#pragma comment(linker,"/STACK:1024000000,1024000000")#define lson L,mid,ls#define rson mid+1,R,rsint sz[maxn],dep[maxn],fa[maxn],son[maxn],id[maxn],top[maxn];int mv[maxn<<2],ans[maxn],leaf[maxn],cnt,ptr,col;struct node{    int v;    node *next;} ed[(maxn<<1)+40*maxn],*head[maxn],*oh[maxn];void adde(node *h[],int u,int v){    ed[cnt].v=v;    ed[cnt].next=h[u];    h[u]=&ed[cnt++];}void build(int L,int R,int rt){    mv[rt]=0;    if(L==R)    {        leaf[L]=rt;        return;    }    int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;    build(lson);    build(rson);}void update(int rt,int c){    int ls,rs;    mv[rt]+=c;    while(rt>1)    {        rt>>=1;        ls=rt<<1,rs=ls|1;        mv[rt]=max(mv[ls],mv[rs]);    }}int qu(int L,int R,int rt){    int ls,rs,mid;    if(mv[rt]==0)        return 0;    while(L<R)    {        ls=rt<<1,rs=ls|1,mid=(L+R)>>1;        if(mv[rs]>mv[ls])            L=mid+1,rt=rs;        else            R=mid,rt=ls;    }    return L;}void dfs(int u){    int v,ms=0;    sz[u]=1,son[u]=-1;    for(node *p=head[u];p!=NULL;p=p->next)    {        v=p->v;        if(v==fa[u])            continue;        dep[v]=dep[u]+1;        fa[v]=u;        dfs(v);        if(sz[v]>ms)            ms=sz[v],son[u]=v;        sz[u]+=sz[v];    }}void getid(int u,int ft){    id[u]=++ptr,top[u]=ft;    if(son[u]!=-1)        getid(son[u],top[u]);    for(node *p=head[u];p!=NULL;p=p->next)    {        if(p->v==son[u]||p->v==fa[u])            continue;        getid(p->v,p->v);    }}void init(int rt){    fa[rt]=-1;    dep[rt]=ptr=0;    dfs(rt);    getid(rt,rt);}void uppath(int u,int v,int d){    int f1=top[u],f2=top[v];    while(f1!=f2)    {        if(dep[f1]<dep[f2])            swap(f1,f2),swap(u,v);        adde(oh,id[f1],d);        adde(oh,id[u]+1,-d);        u=fa[f1],f1=top[u];    }    if(dep[u]>dep[v])        swap(u,v);    adde(oh,id[u],d);    adde(oh,id[v]+1,-d);}int main(){    int n,m,i,rt,u,v,z;    col=100005;    while(scanf("%d%d",&n,&m),n||m)    {        cnt=0;        for(i=1;i<=n;i++)            head[i]=oh[i]=NULL;        for(i=1;i<n;i++)        {            scanf("%d%d",&u,&v);            adde(head,u,v);            adde(head,v,u);        }        rt=(n+1)/2;        init(rt);        build(1,col,1);        for(i=0;i<m;i++)        {            scanf("%d%d%d",&u,&v,&z);            uppath(u,v,z);        }        for(i=1;i<=n;i++)        {           for(node *p=oh[i];p!=NULL;p=p->next)                if(p->v>0)                    update(leaf[p->v],1);                else                    update(leaf[-p->v],-1);            ans[i]=qu(1,col,1);        }        for(i=1;i<=n;i++)            printf("%d\n",ans[id[i]]);    }    return 0;}/*5 103 45 25 15 45 5 171 4 184 4 133 3 74 5 53 2 123 1 133 5 101 2 124 3 55 51 43 44 54 25 2 24 3 85 4 154 1 83 4 20 0*/



對於樹鏈剖分等等一系列樹套樹的問題

關於樹鏈剖分等等一系列樹套樹的問題
提原本就沒有樹,明亮的鏡子也並非是台,本來就虛無一物,那裡會染上什麼塵埃?

首先說一下這首詩的出處

在南北朝的時候,佛教禪宗傳到了第五祖弘忍大師,弘忍大師當時在湖北的黃梅開壇講學,手下有弟子五百餘人,其中翹楚者當屬大弟子神秀大師。神秀也是大家公認的禪宗衣缽的繼承人。弘忍漸漸的老去,於是他要在弟子中尋找一個繼承人,所以他就對徒弟們說,大家都做一首畿子(有禪意的詩),看誰做得好就傳衣缽給誰。這時神秀很想繼承衣缽,但又怕因為出於繼承衣缽的目的而去做這個畿子,違法了佛家的無為而作意境。所以他就在半夜起來,在院牆上寫了一首畿子身是菩提樹,心為明鏡台。時時勤拂拭,勿使惹塵埃。這首畿子的意思是,要時時刻刻的去照顧自己的心靈和心境,通過不斷的修行來抗拒外面的誘惑,和種種邪魔。是一種入世的心態,強調修行的作用。而這種理解與禪宗大乘教派的頓悟是不太吻合的,所以當第二天早上大家看到這個畿子的時候,都說好,而且都猜到是神秀作的而很佩服的時候,弘忍看到了以後沒有做任何的評價。因為他知道神秀還沒有頓悟。

而這時,當廟裡的和尚們都在談論這首畿子的時候,被廚房裡的一個火頭僧—慧能禪師聽到了。慧能當時就叫別人帶他去看這個畿子,這裡需要說明的一點是,慧能是個文盲,他不識字。他聽別人說了這個畿子,當時就說這個人還沒有領悟到真諦啊。於是他自己又做了一個畿子,央求別人寫在了神秀的畿子的旁邊,菩提本無樹,明鏡亦非台,本來無一物,何處惹塵埃。有這首畿子可以看出慧能是個有大智慧的人(後世有人說他是十世比丘轉世),他這個畿子很契合禪宗的頓悟的理念。是一種出世的態度,主要意思是,世上本來就是空的,看世間萬物無不是一個空字,心本來就是空的話,就無所謂抗拒外面的誘惑,任何事物從心而過,不留痕迹。這是禪宗的一種很高的境界,領略到這層境界的人,就是所謂的開悟了。

弘忍看到這個畿子以後,問身邊的人是誰寫的,邊上的人說是慧能寫的,於是他叫來了慧能,當著他和其他僧人的面說:寫得亂七八糟,胡言亂語,並親自擦掉了這個畿子。然後在慧能的頭上打了三下就走了。這時只有慧能理解了五祖的意思,於是他在晚上三更的時候去了弘忍的禪房,在那裡弘忍向他講解了《金剛經》這部佛教最重要的經典之一,並傳了衣缽給他。然後為了防止神秀的人傷害慧能,讓慧能連夜逃走。於是慧能連夜遠走南方,隱居10年之後在莆田少林寺創立了禪宗的南宗。而神秀在第二天知道了這件事以後,曾派人去追慧能,但沒有追到。後來神秀成為梁朝的護國法師,創立了禪宗的北宗。
參考資料:
 

聯繫我們

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