Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 5601 | Accepted: 1591 |
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
详解:点击打开链接
#include<iostream>
#include<cmath>
#include<stdio.h>
#include<string.h>
#define maxn 120000
using namespace std;
int n,m;
int a,b;
struct node
{
int e;
int next;
} edge[maxn*2];
int head[maxn];
bool check[maxn];
int ver[maxn*2];
int tot;
int first[maxn];
int depth[maxn*2];
int Min[maxn*2][30];
int dp[maxn];
void add(int s,int e,int &k)
{
edge[k].e=e;
edge[k].next=head[s];
head[s]=k++;
swap(s,e);
edge[k].e=e;
edge[k].next=head[s];
head[s]=k++;
}
void dfs(int u,int dep)
{
check[u]=true;
ver[++tot]=u;
first[u]=tot;
depth[tot]=dep;
for(int k=head[u]; k!=-1; k=edge[k].next)
{
int v=edge[k].e;
if(!check[v])
{
dfs(v,dep+1);
ver[++tot]=u;
depth[tot]=dep;
}
}
}
void ST(int len)
{
for(int i=1; i<=len; i++)
Min[i][0]=i;
for(int i=1; (1<<i)<=len; i++)
{
for(int j=1; j+(1<<i)-1<=len; j++)
{
int one=Min[j][i-1];
int two=Min[j+(1<<(i-1))][i-1];
if(depth[one]<depth[two])
Min[j][i]=one;
else
Min[j][i]=two;
}
}
}
int RMQ(int l,int r)
{
int k=(int)(log((double)(r-l+1))/(log((double)2)));
int one=Min[l][k];
int two=Min[r-(1<<k)+1][k];
if(depth[one]<=depth[two])
return one;
else
return two;
}
int LCA(int l,int r)
{
int x=first[l];
int y=first[r];
if(x>y)
swap(x,y);
if(x==y)
return ver[x];
int temp=RMQ(x,y);
return ver[temp];
}
void dfs1(int u)
{
check[u]=true;
for(int k=head[u];k!=-1;k=edge[k].next)
{
int v=edge[k].e;
if(!check[v])
{
dfs1(v);
dp[u]=dp[u]+dp[v];
}
}
}
int main()
{
tot=0;
memset(check,false,sizeof(check));
memset(head,-1,sizeof(head));
memset(dp,0,sizeof(dp));
int num=0;
scanf("%d%d",&n,&m);
for(int i=1; i<n; i++)
{
scanf("%d%d",&a,&b);
add(a,b,num);
}
dfs(1,1);
ST(2*n-1);
for(int i=1; i<=m; i++)
{
scanf("%d%d",&a,&b);
int lca=LCA(a,b);
dp[lca]=dp[lca]-2;
dp[a]++;
dp[b]++;
}
memset(check,false,sizeof(check));
dfs1(1);
int ans=0;
for(int i=2;i<=n;i++)
{
if(dp[i]==0)
ans=ans+m;
else if(dp[i]==1)
ans++;
}
printf("%d\n",ans);
return 0;
}