poj3417 Network 离线LCA + 树形dp

Language: Default
Network
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3827   Accepted: 1095

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

Source

POJ Monthly--2007.10.06, Yang Mu



#include
#include
#include
#include
using namespace std;
const int MAXN=100000+100;
typedef long long ll;
int n,m,edge_cnt;
int head[MAXN],dp[MAXN],fa[MAXN],vis[MAXN],lca[MAXN];
struct Edge
{
    int v;
    int next;
}edge[2*MAXN];
struct node
{
    int v;
    int id;
};
vectorG[MAXN];
void init()
{
    edge_cnt;
    memset(head,-1,sizeof(head));
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=n;i++)
        fa[i]=i,vis[i]=0,G[i].clear();
}
void addedge(int u,int v)
{
    edge[edge_cnt].v=v;
    edge[edge_cnt].next=head[u];
    head[u]=edge_cnt++;
}
int findset(int x)
{
    return fa[x]==x ? x : fa[x]=findset(fa[x]);
}
void tarjan(int u,int f)
{
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==f)
            continue;
        tarjan(v,u);
    }
    vis[u]=1;
    for(int i=0;i


你可能感兴趣的:(lca,树形dp)