poj3417Network【LCA】【树形DP】

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

感觉LCA真的是高深莫测

还是看的题解

原来的图是一棵树,新增的m条边

每增加一条,一定会构成一个u-lca(u,v)-v-u的一个环

每两个节点之间的边一定都会被一些环给覆盖 称为覆盖数

计算原图中每条边的覆盖数

如果覆盖数为0 那么这条边只要被删除,新图中任意删去一条边 整个network都会被分割

如果覆盖数为1 那么删除这条边和新图中对应的边 也会被分割

只有覆盖数为2 这两个点才不会被分割

用dp[u]表示节点u和他父亲相连的边的覆盖数

每增加一个新边(u, v)就使dp[u]++,dp[v]++ 而dp[lca(u, v)] -= 2

因为最后从根开始遍历,累加dp

dp[u]++后,路径u-lca(u,v)上的覆盖数都加上了,lca(u,v)到根节点的也被加上了,而实际上是不需要加的因为这条路径没有在环里

所以-2就行了


#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <stdio.h>
#include <queue>
#include <stack>
#define inf 0x3f3f3f3f
using namespace std;

int n, m, ecnt;
const int maxn = 100005;
int dep[maxn], dp[maxn], parent[maxn][22];
struct node{
    int to, next;
}edge[2 * maxn];
int head[maxn];
bool vis[maxn];

void dfs(int u)
{
    vis[u] = true;
    for(int i = head[u]; ~i; i = edge[i].next){
        int v = edge[i].to;
        if(!vis[v]){
            parent[v][0] = u;
            dep[v] = dep[u] + 1;
            dfs(v);
        }
    }
}

void init()
{
    ecnt = 0;
    memset(head, -1, sizeof(head));
    memset(vis, 0, sizeof(vis));
    memset(dep, 0, sizeof(dep));
    memset(parent, -1, sizeof(parent));
    memset(dp, 0, sizeof(dp));
}

void add(int u, int v)
{
    edge[ecnt].to = v;
    edge[ecnt].next = head[u];
    head[u] = ecnt++;
}

void rmq()
{
    for(int j = 1; (1 << j) <= n; j++){
        for(int i = 1; i <= n; i++){
            if(~parent[i][j - 1]){
                parent[i][j] = parent[parent[i][j - 1]][j - 1];
            }
        }
    }
}

int LCA(int a, int b)
{
    if(dep[a] < dep[b]) swap(a, b);
    int i;
    for(i = 0; (1 << i) <= dep[a]; i++);
    i--;
    for(int j = i; j >= 0; j--){
        if(dep[a] - (1 << j) >= dep[b]){
            a = parent[a][j];
        }
    }
    if(a == b){
        return a;
    }
    for(int j = i; j >= 0; j--){
        if(parent[a][j] != -1 && parent[a][j] != parent[b][j]){
            a = parent[a][j];
            b = parent[b][j];
        }
    }
    return parent[a][0];
}

void goto_dp(int u)
{
    vis[u] = true;
    for(int i = head[u]; ~i; i = edge[i].next){
        int v = edge[i].to;
        if(!vis[v]){
            goto_dp(v);
            dp[u] += dp[v];
        }
    }
}

int main()
{
    while(scanf("%d%d", &n, &m) != EOF){
        init();
        for(int i = 1; i < n; i++){
            int u, v;
            scanf("%d%d", &u, &v);
            add(u, v);
            add(v, u);
        }
        dfs(1);
        rmq();
        for(int i = 1; i <= m; i++){
            int u, v;
            scanf("%d%d", &u, &v);
            dp[u]++;
            dp[v]++;
            dp[LCA(u, v)] -= 2;
        }
        memset(vis, 0, sizeof(vis));
        goto_dp(1);
        int ans = 0;
        for(int i = 2; i <= n; i++){
            if(dp[i] == 0) ans += m;
            else if(dp[i] == 1) ans++;
        }
        printf("%d\n", ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wybooooooooo/article/details/80793751