Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 4284 | Accepted: 1235 |
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
题解:http://blog.csdn.net/qq574857122/article/details/15718147
不想自己写了。。
题意:
n个点m条无向边
下面n-1行给定原树
m行给定新边
问删一条老边和新边使得图不连通的方法
首先,对于一条新边(u,v),加入后 成环 u, v, LCA(u,v)
所以删除新边(a,b)以及这个环上的没有被其他环覆盖的边
即可分成两部分。所以问题转化为求每条边被环覆盖的次数
设dp[x]表示x所在的父边被 新边覆盖的次数
引进一条新边(a,b)后,dp[a]++,dp[b]++
而这个环上的其他边的统计可以用treeDP解决,即for(v)
dp[u]+=dp[v]
注意到LCA(a,b)的父边是不在环上的,所以每次引进新边(a,b),dp[LCA[a,b]]-=2
最后,if(dp[i]==1)ans++ 删除该边及覆盖它的那个环
if(dp[i]==0)ans+=M 表明这条树边是桥,删除它及任意一条新边都可以
#include<stdio.h>
#include<string.h>
#include<math.h>
#define N 300100
struct s
{
int u,v,next;
}edge[100100<<1];
int num[100100],head[100100],cnt;
int first[N*2],node[N*2],deep[N*2],minv[N<<1][25],hah[N],pre[N],vis[N];
void add(int u,int v)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
int tot;
void dfs(int u,int dep)
{
tot++;
// fa[u]=pre;
node[tot]=u;
deep[tot]=dep;
vis[u]=1;
first[u]=tot;
int i;
for(i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(!vis[v])
{
dfs(v,dep+1);
tot++;
node[tot]=u;
deep[tot]=dep;
}
}
}
void init_RMQ(int n)
{
int i,j,k;
for(i=1;i<=n;i++)
{
minv[i][0]=i;
}
int kk = (int) (log((double) n) / log(2.0));
for(j=1;j<=kk;j++)
{
for(k=1;k+(1<<j)-1<=n;k++)
{
if(deep[minv[k][j-1]]>deep[minv[k+(1<<(j-1))][j-1]])
minv[k][j]=minv[k+(1<<(j-1))][j-1];
else
minv[k][j]=minv[k][j-1];
}
}
}
int q_min(int l,int r)
{
int k=(int)(log((double)(r-l+1))/(log(2.0)));
if(deep[minv[l][k]]>deep[minv[r-(1<<k)+1][k]])
return minv[r-(1<<k)+1][k];
else
return minv[l][k];
}
int lca(int a,int b)
{
int x=first[a];
int y=first[b];
int k;
if(x<y)
{
k=q_min(x,y);
}
else
k=q_min(y,x);
return node[k];
}
void Tree_DP(int u,int pre)
{
int i;
for(i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(v==pre)
continue;
Tree_DP(v,u);
num[u]+=num[v];
}
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int i;
memset(head,-1,sizeof(head));
cnt=0;
for(i=1;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
tot=0;
memset(vis,0,sizeof(vis));
memset(first,0,sizeof(first));
dfs(1,0);
init_RMQ(tot-1);
memset(num,0,sizeof(num));
for(i=0;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
num[u]++;
num[v]++;
num[lca(u,v)]-=2;
}
Tree_DP(1,-1);
int ans=0;
for(i=2;i<=n;i++)/
{
if(num[i]==0)
ans+=m;
else
if(num[i]==1)
ans++;
}
printf("%d\n",ans);
}
}