Network POJ - 3417 LCA+树上差分

本文描述了一个网络攻防场景,公司经理Yixght面对竞争对手DxtNetwork的威胁,通过增加新通道来保护公司网络。作为黑客,目标是破坏两个通道,使网络至少分裂成两部分。文章提供了输入输出示例及解决方案,涉及网络结构、节点和通道的概念。

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

lca + 差分即可;
其中 fg 表示该边被覆盖多少次;
那么显然我们只能选 0 或 1 次的边;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 200005
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const int mod = 10000007;
#define Mod 20100403
#define sq(x) (x)*(x)
#define eps 1e-10


inline int rd() {
	int x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

int head[maxn], nxt[maxn], ver[maxn];
int cnt;
int n, m, N;


void addedge(int x, int y) {
	ver[++cnt] = y; nxt[cnt] = head[x]; head[x] = cnt;
}

int grand[maxn][20];
int depth[maxn];
int fg[maxn];

void init() {
	memset(head, 0, sizeof(head));
	memset(fg, 0, sizeof(fg));
	N = (int)(log(n) / log(2)) + 1;
	depth[1] = 1; cnt = 0;
}
void dfs(int rt) {
	for (int i = 1; i <= N; i++) {
		grand[rt][i] = grand[grand[rt][i - 1]][i - 1];
	}
	for (int i = head[rt]; i; i = nxt[i]) {
		int v = ver[i];
		if (v == grand[rt][0])continue;
		grand[v][0] = rt;
		depth[v] = depth[rt] + 1;
		dfs(v);
	}
}

int lca(int a, int b) {
	if (depth[a] > depth[b])swap(a, b);
	for (int i = N; i >= 0; i--) {
		if (depth[a] <= depth[grand[b][i]])
			b = grand[b][i];
	}
	if (a == b)return a;
	for (int i = N; i >= 0; i--) {
		if (grand[a][i] != grand[b][i]) {
			a = grand[a][i]; b = grand[b][i];
		}
	}
	return grand[a][0];
}

void DFS(int rt) {
	for (int i = head[rt]; i; i = nxt[i]) {
		int v = ver[i];
		if (v == grand[rt][0])continue;
		DFS(v);
		fg[rt] += fg[v];
	}
}



int main()
{
	//ios::sync_with_stdio(false);
	while (cin >> n >> m) {
		init();
		for (int i = 1; i < n; i++) {
			int x, y;
			scanf("%d%d", &x, &y);
			addedge(x, y); addedge(y, x);
		}
		dfs(1);
		for (int i = 0; i < m; i++) {
			int x, y;
			scanf("%d%d", &x, &y);
			fg[x]++; fg[y]++;
			fg[lca(x, y)] -= 2;
		}
		DFS(1);
		long long ans = 0;
		for (int i = 2; i <= n; i++) {
			if (fg[i] == 0)ans += m;
			else if (fg[i] == 1)ans++;
		}
		cout << ans << endl;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值