poj 3417 Network (LCA,路徑上有值)

來源:互聯網
上載者:User

標籤:style   blog   color   ar   for   sp   div   c   on   

題意:

N個點,構成一棵樹。給出這棵樹的結構。

M條邊,(a1,b1)...(am,bm),代表給樹的這些點對連上邊。這樣就形成了有很多環的一個新”樹“。

現在要求你在原樹中斷一條邊,在M條邊中斷一條邊,使得新”樹“被分成兩個部分。

問有多少種方案。

 

思路:

連上某條新邊(a,b),則必定形成一個環。環的路徑是a->...->lca(a,b)->...->b,給這些邊的值都加1 。

分情況:

在原樹中,若一條邊的值>=2,去掉這條邊,再去掉M條邊中的一條,不影響原樹的連通性。方案數為0。

       若一條邊的值==1,去掉這條邊,必須去掉這條邊所在環的那條新邊(M條邊之一)。方案數為1。

     若一條邊的值==0,去掉這條邊,再隨意去掉M條邊中的一條,都會破壞原樹的連通性。方案數為M。

在計算邊的值時,採用DP的方式,若要將路徑a->...->lca(a,b)->...->b上的邊值都加1,則dp[a]++,dp[b]++,dp[lca(a,b)]-=2。

直接看代碼。

 

代碼:

int const maxn = 100005;struct node{    int to,next,lca;};int fa[maxn];bool vis[maxn];int head[2*maxn];int qhead[2*maxn];node edge[maxn*2];node qedge[maxn*2];int n,m,cnt1,cnt2,cn;int dp[maxn], edgeCount[maxn];inline void Addedge(int u,int v){    edge[++cnt1].to=v;    edge[cnt1].next=head[u];    head[u]=cnt1;    edge[++cnt1].to=u;    edge[cnt1].next=head[v];    head[v]=cnt1;}inline void Addqedge(int u,int v){    qedge[++cnt2].to=v;    qedge[cnt2].next=qhead[u];    qhead[u]=cnt2;    qedge[++cnt2].to=u;    qedge[cnt2].next=qhead[v];    qhead[v]=cnt2;}void init(){    mem(head,-1);  mem(qhead,-1);  mem(vis,false);    rep(i,1,n) fa[i]=i;    cnt1=cnt2=0;}int findFa(int x){    return fa[x]==x?x:fa[x]=findFa(fa[x]);}void Tarjan_LCA(int u){    fa[u]=u;    vis[u]=true;    for(int i=head[u];i!=-1;i=edge[i].next){        if(!vis[edge[i].to]){            Tarjan_LCA(edge[i].to);            fa[edge[i].to]=u;        }    }    for(int i=qhead[u];i!=-1;i=qedge[i].next){        if(vis[qedge[i].to])            qedge[i].lca=findFa(qedge[i].to);    }}int dfsDp(int u,int fa){    for(int i=head[u];i!=-1;i=edge[i].next){        int v=edge[i].to;  if(v==fa) continue;        int t1=dfsDp(v,u);        edgeCount[++cn]=t1;        dp[u]+=t1;    }    return dp[u];}int a,b;int main(){    while(scanf("%d%d",&n,&m)!=EOF){        init();        rep(i,1,n-1){            scanf("%d%d",&a,&b);            Addedge(a,b);        }        mem(dp,0);        rep(i,1,m){            scanf("%d%d",&a,&b);            Addqedge(a,b);            dp[a]++;  dp[b]++;        }        Tarjan_LCA(1);        for(int i=1;i<=cnt2;i+=2){            dp[qedge[i].lca]-=2;        }        mem(edgeCount,0);        cn=0;        dfsDp(1,-1);        int ans=0;        rep(i,1,cn){            if(edgeCount[i]==1)                ans++;            else if(edgeCount[i]==0)                ans+=m;        }        printf("%d\n",ans);    }}

 

poj 3417 Network (LCA,路徑上有值)

聯繫我們

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