码迷,mamicode.com
首页 > Web开发 > 详细

POJ3417Network(LCA+树上查分||树剖+线段树)

时间:2017-12-21 23:10:49      阅读:328      评论:0      收藏:0      [点我收藏+]

标签:present   hacker   cst   cti   gpo   contain   always   ==   destory   

Yixght is a manager of the company called SzqNetwork(SN). Now she‘s very worried because she has just received a bad news which denotes that DxtNetwork(DN), the SN‘s business rival, intents to attack the network of SN. More unfortunately, the original network of SN is so weak that we can just treat it as a tree. Formally, there are N nodes in SN‘s network, N-1 bidirectional channels to connect the nodes, and there always exists a route from any node to another. In order to protect the network from the attack, Yixght builds M new bidirectional channels between some of the nodes.

As the DN‘s best hacker, you can exactly destory two channels, one in the original network and the other among the M new channels. Now your higher-up wants to know how many ways you can divide the network of SN into at least two parts.

Input

The first line of the input file contains two integers: N (1 ≤ N ≤ 100 000), M (1 ≤ M ≤ 100 000) — the number of the nodes and the number of the new channels.

Following N-1 lines represent the channels in the original network of SN, each pair (a,b) denote that there is a channel between node a and node b.

Following M lines represent the new channels in the network, each pair (a,b) denote that a new channel between node a and node b is added to the network of SN.

Output

Output a single integer — the number of ways to divide the network into at least two parts.

Sample Input

4 1
1 2
2 3
1 4
3 4

Sample Output

3

 

题意:

一棵树,后来加了m条新边,形成了一个有环无向图,问删去原树上一条边和m条新边中的一条,使得图不连通。这样的方案有多少。

思路:

以前看到过,完全没有思路,放弃了。今天又看到了,还是没有思路。。。GG

把原图dfs建立成有根树,假设1是根节点,再想,有方向可能就有思路了,oh,还是没有。。。GG

  • 试着手动在两点之间加一条新边,我们看到形成了一个环。
  • 对于环上加的一条新边,两点间每一条原树边都多进入了一个环。
  • 1,      如果一条边进入了两个环,则不用再考虑它。
  • 2,      如果只进入一个环,则它与对应的新边是一组答案。
  • 3,      如果一条表没有进入任何环,那它与任何一条新边是一组答案。

 

  • 对每一条新边e,其端点为u,v,LCA(u,v)=a,则把路径u-a-v上每一条边加1,最后处理即可。
  • 显然可以用线段树+lazy做。数据上感觉可以过。但是学到了树上差分。。。像前缀和一样。感觉很简单,但是没想到。。。

                 :sum[u]+1;sum[v]+1;sum[a]-2; 由叶子向根root累加即可。

注意:对象是边,而不是顶点的sum[]。

          我写的是树剖(因为在练习树剖),注意找LCA的时候注意 if(dpt[u]<dpt[v]) 是刷新。

 

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
#define swap(a,b) {a^=b;b^=a;a^=b;}
using namespace std;
const int maxn=400010;
int Laxt[maxn],Next[maxn],To[maxn],cnt;
int fa[maxn],son[maxn],size[maxn],dpt[maxn],top[maxn];
int n,m,ans,sum[maxn];
void add(int u,int v)
{
    Next[++cnt]=Laxt[u];
    Laxt[u]=cnt; To[cnt]=v;
}
void dfs1(int u,int pre)
{
    fa[u]=pre;dpt[u]=dpt[pre]+1;size[u]=1;son[u]=0;
    for(int i=Laxt[u];i;i=Next[i]){
        int v=To[i]; if(v==pre) continue;
        dfs1(v,u);size[u]+=size[v];
        if(!son[u]||size[v]>size[son[u]]) son[u]=v;
    }
}
void dfs2(int u,int Top)
{
    top[u]=Top; if(!son[u]) return ; dfs2(son[u],Top);
    for(int i=Laxt[u];i;i=Next[i])
        if(To[i]!=fa[u]&&To[i]!=son[u]) dfs2(To[i],To[i]);
}

int dfs3(int u,int pre)
{
    for(int i=Laxt[u];i;i=Next[i]){
        int v=To[i];
        if(v!=pre){
            int tmp=dfs3(v,u);    
            sum[u]+=tmp; 
            if(tmp==0) ans+=m;
            if(tmp==1) ans+=1;
        }
    }
    return sum[u];
}
int find(int u,int v)
{
    int a=top[u],b=top[v];
    while(a!=b){
        if(dpt[a]<dpt[b]) { swap(a,b);swap(u,v);}
        u=fa[a]; a=top[u];
    }
    if(dpt[u]<dpt[v]) return u; return v;
}
int main()
{

    while(~scanf("%d%d",&n,&m)){
        memset(Laxt,0,sizeof(Laxt)); cnt=0;
        memset(sum,0,sizeof(sum)); ans=0;
        for(int i=1;i<n;i++){
            int v,u; scanf("%d%d",&u,&v);
            add(u,v);add(v,u);
        }
        dfs1(1,0);
        dfs2(1,1);
        for(int i=1;i<=m;i++){
            int u,v; scanf("%d%d",&u,&v);
            int anc=find(u,v);
            sum[u]++;sum[v]++;sum[anc]-=2;
        }
        dfs3(1,0);
        printf("%d\n",ans);
    } return 0;
}

 

POJ3417Network(LCA+树上查分||树剖+线段树)

标签:present   hacker   cst   cti   gpo   contain   always   ==   destory   

原文地址:http://www.cnblogs.com/hua-dong/p/8082612.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!