# POJ 3417 Network(dp+tarjian LCA)

359人阅读 评论(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 init1()
{
tot1=0;
}

struct Query{
int v,next,id;
}Q[maxn<<2];
{
}
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;
{
int v=E[i].v;
if(vis[v]) continue;
tarjian(v);
ancestor[v]=u;
}
{
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)
{
{
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

个人资料
• 访问：415855次
• 积分：16535
• 等级：
• 排名：第744名
• 原创：1274篇
• 转载：21篇
• 译文：0篇
• 评论：61条
博客专栏
 计算机视觉-人体姿态识别 文章：16篇 阅读：1038
文章分类
友情链接
阅读排行