POJ 3417 Network (LCA应用,5级)

K - Network
Crawling in process... Crawling failed Time Limit:2000MS    Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Appoint description:

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 buildsM 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 theM 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 nodea and node b.

Following M lines represent the new channels in the network, each pair (a,b) denote that a new channel between nodea 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
 
   
思路:求边被环覆盖的次数,0次是桥答案+m 1次则加1 否则不加。计数用个DP,不过公共祖先得减2
 
   
不明白:为啥先递归后查询就错了呢,无数次的WA啊!求解?
 
   
#include<cstdio>
#include<cstring>
#include<iostream>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
using namespace std;
const int mm=1e5+9;
class Edge
{
  public:int v,next;
}e[mm*4];
int edge,dp[mm],head[mm],qh[mm],rt[mm],ans;
int n,m;
bool vis[mm];
void data()
{
  edge=0;clr(head,-1);clr(qh,-1);
}
void add(int u,int v,int*h)
{
  e[edge].v=v;e[edge].next=h[u];h[u]=edge++;
}
int find(int x)
{
  if(rt[x]^x)
    rt[x]=find(rt[x]);
  return rt[x];
}
void dfs(int u)
{ rt[u]=u;
  int v;vis[u]=1;
  for(int i=qh[u];~i;i=e[i].next)
  {
    v=e[i].v;
    if(vis[v])
    {
      dp[find(v)]-=2;//加边公共祖先之上已经不受此边影响了
    }
  } 
  /****************************************
  上下两个for 交换就错了,为啥?
  *****************************************/
  for(int i=head[u];~i;i=e[i].next)
  {
    v=e[i].v;
    if(!vis[v])
    {
      dfs(v);rt[v]=u;
      dp[u]+=dp[v];//边被环覆盖次数
      if(dp[v]==0)ans+=m;
      if(dp[v]==1)ans++;
    }
  }
}
void find_bcc()
{ ans=0;
  clr(vis,0);
  dfs(1);
}
int main()
{ int a,b;
  while(~scanf("%d%d",&n,&m))
  {
    data();
    FOR(i,2,n)
    {
      scanf("%d%d",&a,&b);
      add(a,b,head);add(b,a,head);
    }
    clr(dp,0);
    FOR(i,1,m)
    {
      scanf("%d%d",&a,&b);
      if(a!=b)
        add(a,b,qh),add(b,a,qh),++dp[a],++dp[b];
    }
    find_bcc();
    printf("%d\n",ans);
  }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值