POJ3417 Network(LCA+树上差分)

传送门:http://poj.org/problem?id=3417

Network

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6047 Accepted: 1724

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

题意:给n个点,给m条附加边,接下来是n-1对点,组成树,接下来m条是附加边。现在想要把树搞成不连通的两部分,一开始选一条主要边弄断,然后在选择切一个附加边。问有几种方案。

思路:加一条附加边到树上会成环,比如在a,b上加了附加边,那么假如切的是ab路上的某个边,那么第二步肯定切断ab这条附加边才能变成不连通两部分。也就是说ab路上每个边都被ab这个附加边覆盖了一次。如果第一步我们把覆盖0次的树上主要边去掉,那么第二步干掉任意附加边都行,如果干掉的是覆盖次数为1的,那么只有一个方案,覆盖2次及以上的没有办法了。ps:标记覆盖次数可以使用树上差分算法。设个权值数组val[],让附加边两端的ab的权值+1,lca(a,b)的权值-2。最后跑一边dfs求出ff[i]表示以i为根的树各节点权值之和,这个ff[i]就是i和他父亲之间这条边被覆盖的次数。

代码:

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
#include<map>
#include<vector>
#include<set>
#include<queue>
using namespace std;
typedef long long ll;
const ll maxv=1e5+5;

ll head[maxv],vis[maxv],d[maxv],fa[maxv][20],val[maxv],ff[maxv];
ll n,m,edgenum,ans;

struct node
{
	ll to,next;
}edge[maxv<<1];

void init()
{
	fill(d,d+maxv,0);
	fill(head,head+maxv,-1);
	fill(ff,ff+maxv,0);
	fill(val,val+maxv,0);
	edgenum=0;
}

void addedge(ll u,ll v)
{
	edge[edgenum].to=v;
	edge[edgenum].next=head[u];
	head[u]=edgenum++;
}

void bfs()
{
	queue<ll>q;
	q.push(1);
	d[1]=1;
	while(!q.empty())
	{
		ll now=q.front();
		q.pop();
		for(ll i=head[now];i!=-1;i=edge[i].next)
		{
			ll y=edge[i].to;
			if(d[y])
				continue;
			d[y]=d[now]+1;
			fa[y][0]=now;
			for(ll j=1;j<20;j++)
			{
				fa[y][j]=fa[fa[y][j-1]][j-1];
			}
			q.push(y);
		}
	}
}

ll lca(ll x,ll y)
{
	if(d[x]>d[y])
		swap(x,y);
	for(ll i=19;i>=0;i--)
	{
		if(d[fa[y][i]]>=d[x])
			y=fa[y][i];
	}
	if(x==y)
		return x;
	for(ll i=19;i>=0;i--)
	{
		if(fa[x][i]!=fa[y][i])
			x=fa[x][i],y=fa[y][i];
	}
	return fa[x][0];
}

ll dfs(ll p,ll father)
{
	ll sum=val[p];
	for(ll i=head[p];i!=-1;i=edge[i].next)
	{
		ll now=edge[i].to;
		if(now==father)
			continue;
		ll cnt=dfs(now,p);
		sum+=cnt;
	}
	ff[p]=sum;
	return sum;
}

int main()
{
	while(scanf("%lld%lld",&n,&m)!=EOF)
	{
		init();
		ll u,v;
		for(ll i=1;i<=n-1;i++)
		{
			scanf("%lld%lld",&u,&v);
			addedge(u,v);
			addedge(v,u);
		}
		bfs();
		for(int i=1;i<=m;i++)
		{
			ll a,b;
			scanf("%lld%lld",&a,&b);
			val[a]++;
			val[b]++;
			val[lca(a,b)]-=2;
		}
		dfs(1,-1);
		ans=0;
		for(ll i=2;i<=n;i++)
		{
			if(ff[i]==1)
			{
				ans++;
			}
			else if(ff[i]==0)
			{
				ans+=m;
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值