# POJ 3417 Network(dp+tarjian LCA)

189人阅读 评论(0)

Network
 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4929 Accepted: 1413

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

【题意】给了一颗树和一些新的边的关系，要你毁掉一条树边和一条新的边，问你有多少种方法使得树不连通。

【解题方法】

【AC 代码】

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100010;
const int maxm = 200010;
int dp[maxn],n,m;//dp[i]表示i到fa[i]的被覆盖次数
struct Edge{
int v,next;
}E[maxn*2];
void addEdge(int u,int v)
{
}
void init1()
{
tot1=0;
}

struct Query{
int v,next,id;
}Q[maxn<<2];
void addQuery(int u,int v,int i)
{
}
void init2()
{
tot2=0;
}

bool vis[maxn],check[maxn];
int ancestor[maxn];
int Find(int x)
{
if(x==ancestor[x]) return x;
else return ancestor[x]=Find(ancestor[x]);
}
void tarjian(int u)
{
ancestor[u]=u;
vis[u]=true;
for(int i=headE[u]; ~i; i=E[i].next)
{
int v=E[i].v;
if(vis[v]) continue;
tarjian(v);
ancestor[v]=u;
}
for(int i=headQ[u]; ~i; i=Q[i].next)
{
int v=Q[i].v,id=Q[i].id;
if(!vis[v]) continue;
if(check[id]) continue;
check[id]=true;
int lca=Find(v);
dp[lca]-=2;
}
}
void dfs(int u,int fa)
{
for(int i=headE[u]; ~i; i=E[i].next)
{
int v=E[i].v;
if(v==fa) continue;
dfs(v,u);
dp[u]+=dp[v];
}
}

int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int u,v;
init1();
init2();
memset(dp,0,sizeof(dp));
for(int i=1; i<n; i++)
{
scanf("%d%d",&u,&v);
}
for(int i=1; i<=m; i++)
{
scanf("%d%d",&u,&v);
++dp[u],++dp[v];
}
memset(vis,false,sizeof(vis));
memset(check,false,sizeof(check));
tarjian(1);
dfs(1,-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;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：292119次
• 积分：14618
• 等级：
• 排名：第847名
• 原创：1214篇
• 转载：13篇
• 译文：0篇
• 评论：62条
文章分类
友情链接
阅读排行