poj3417 LCA + 树形dp

Network
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 4478   Accepted: 1292

Description

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
 
题意:
有n个点的树,另外添加m - 1条边。现在要你删除一条树上原有的边,和一条添加的边,使图分成2部分,有多少种方法。
 
思路:
在树上添加边之后,一定会构成一个环,那么对于当前的这个环,就是删除这条添加的边和原有的边后,可以分成2部分。所以题目可以转化为每天边被环包围了几次。
如果只有0次,说明这条树边删除后,直接分成了2部分,方案书就是m种。如果当前被被包围1次,那么说明这个环中方案树为环中树边的个数。包围次数大于1的,
方案为0,因为至少删除2条添加的边和1条树边后才能分成2部分。
对于添加的边x,y,我们可以找到lca(x,y),然后用点表示边,s[x] += 1,s[y] += 1,表示x和y所在的边被包围了一次,对于x->lca(x,y)和y->lca(x,y)上的边可以用树形dp来处理,
由于这里用点表示边,lca(x,y)这个点肯定被加了2次,由于lca(x,y)代表的边不在这个环中,所以s[lca(x,y)] -= 2; 剩下的就是判断一下答案即可。
 
 
/*
 * Author:  sweat123
 * Created Time:  2016/7/14 8:29:55
 * File Name: main.cpp
 */
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define key_value ch[ch[root][1]][0]
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = 100010;
struct node{
    int to;
    int next;
}edge[MAXN*2];
int pre[MAXN],ind,vis[MAXN],dfn[MAXN*2],dp[MAXN*2][20],rev[MAXN*2],tot,first[MAXN],n,m;
int s[MAXN];
void add(int x,int y){
    edge[ind].to = y;
    edge[ind].next = pre[x];
    pre[x] = ind ++;
}
void dfs(int rt,int dep){
    vis[rt] = 1;
    rev[++tot] = rt;
    first[rt] = tot;
    dfn[tot] = dep;
    for(int i = pre[rt]; i != -1; i = edge[i].next){
        int t = edge[i].to;
        if(!vis[t]){
            dfs(t,dep+1);
            rev[++tot] = rt;
            dfn[tot] = dep;
        }
    }
}
void rmq(){
    for(int i = 1; i <= tot; i++){
        dp[i][0] = i;
    }
    for(int i = 1; i < 20; i++){
        for(int j = 1; j + (1 << i) - 1 <= tot; j++){
            int l = dp[j][i-1];
            int r = dp[j+(1<<(i-1))][i-1];
            if(dfn[l] > dfn[r]){
                dp[j][i] = r;
            } else{
                dp[j][i] = l;
            }
        }
    }
}
int lca(int x,int y){
    x = first[x];
    y = first[y];
    if(x > y)swap(x,y);
    int k = (int)(log(y - x + 1) * 1.0 / log(2.0));
    int l = dp[x][k];
    int r = dp[y - (1 << k) + 1][k];
    if(dfn[l] > dfn[r])return r;
    return l;
}
void tree_dfs(int rt){
    vis[rt] = 1;
    for(int i = pre[rt]; i != -1; i = edge[i].next){
        int t = edge[i].to;
        if(!vis[t]){
            tree_dfs(t);
            s[rt] += s[t];
        }
    }
}
void init(){
    ind = tot = 0;
    memset(pre,-1,sizeof(pre));
    memset(s,0,sizeof(s));
}
int main(){
    while(~scanf("%d%d",&n,&m)){
        init();
        for(int i = 1; i < n; i++){
            int x,y;
            scanf("%d%d",&x,&y);
            add(x,y);
            add(y,x);
        }
        memset(vis,0,sizeof(vis));
        dfs(1,1);
        rmq();
        for(int i = 1; i <= m; i++){
            int x,y;
            scanf("%d%d",&x,&y);
            int tp = rev[lca(x,y)];
            s[x] ++;
            s[y] ++;
            s[tp] -= 2;// the ponint of lca(x,y) has no birdge and I hava count two way
        }
        memset(vis,0,sizeof(vis));
        tree_dfs(1);
        ll ans = 0;
        for(int i = 2; i <= n; i++){
            if(s[i] <= 0){
                ans += m;
            } else if(s[i] == 1){
                ans += 1;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

 
posted @ 2016-07-14 09:30  sweat123  阅读(645)  评论(0编辑  收藏  举报