20191020 练习:割点和桥+树上差分

割点和桥

总览:

有割点不一定有桥,有桥不一定有割点!
在这里插入图片描述
求割点:
子节点low值大于等于父节点dfn值时,父节点为割点。
特别的,对于根节点,如果只有一颗子树,便不是割点。

int head[A];
struct Road{int next,to;}road[A];
bool cut[A];
int root;
int dfn[A],low[A],tim;

void tarjan(int root,int x){
	dfn[x]=low[x]=++tim;
	int all=0;
	for(int y=head[x];y;y=road[y].next){
		int z=road[y].to;
		if(!dfn[z]){
			tarjan(x,z);
			low[x]=min(low[x],low[z]);
			if(low[z]>=dfn[x]&&x!=root)
				cut[x]=1;
			if(x==root)	all++;
			if(all==2)	cut[x]=1;
		}
		else
			low[x]=min(low[x],dfn[z]);
	}
}

int main(){
	for(int i=1;i<=n;i++)
		if(!dfn[i])	tarjan(i,i);
}

求桥:
子节点low值小于父节点dfn值,父节点连向子节点的边为桥。
此处用子节点的编号表示父节点连向子节点的边的编号。

struct Road{int next,to;}road[N];
int dfn[N],low[N],tim;
bool bridge[N];

void tarjan(int fa,int x){
	dfn[x]=low[x]=++tim;
	for(int y=head[x];y;y=road[y].next){
		int z=road[y].to;
		if(z==fa)	continue;
		if(!dfn[z]){
			tarjan(z);
			low[x]=min(low[x],low[z]);
			if(low[z]<dfn[x])
				bridge[z]=1;
		}
	}
}

T1 Network

POJ3694
Time Limit: 5000MS Memory Limit: 65536K

Description
A network administrator manages a large network. The network consists of N N N computers and M links between pairs of computers. Any pair of computers are connected directly or indirectly by successive links, so data can be transformed between any two computers. The administrator finds that some links are vital to the network, because failure of any one of them can cause that data can’t be transformed between some computers. He call such a link a bridge. He is planning to add some new links one by one to eliminate all bridges.
You are to help the administrator by reporting the number of bridges in the network after each new link is added.

Input
The input consists of multiple test cases. Each test case starts with a line containing two integers N ( 1 ≤ N ≤ 100 , 000 ) N(1 ≤ N ≤ 100,000) N(1N100,000) and M ( N − 1 ≤ M ≤ 200 , 000 ) M(N - 1 ≤ M ≤ 200,000) M(N1M200,000).
Each of the following M lines contains two integers A A A and B ( 1 ≤ A ≠ B ≤ N ) B ( 1≤ A ≠ B ≤ N) B(1A=BN), which indicates a link between computer A A A and B B B. Computers are numbered from 1 1 1 to N N N. It is guaranteed that any two computers are connected in the initial network.
The next line contains a single integer Q ( 1 ≤ Q ≤ 1 , 000 ) Q ( 1 ≤ Q ≤ 1,000) Q(1Q1,000), which is the number of new links the administrator plans to add to the network one by one.
The i-th line of the following Q Q Q lines contains two integer A A A and B ( 1 ≤ A ≠ B ≤ N ) B (1 ≤ A ≠ B ≤ N) B(1A=BN), which is the i-th added new link connecting computer A A A and B B B.
The last test case is followed by a line containing two zeros.

Output
For each test case, print a line containing the test case number( beginning with 1 1 1) and Q Q Q lines, the i-th of which contains a integer indicating the number of bridges in the network after the first i new links are added. Print a blank line after the output for each test case.

Sample Input
3 2
1 2
2 3
2
1 2
1 3
4 4
1 2
2 1
2 3
1 4
2
1 2
3 4
0 0

Sample Output
Case 1:
1
0

Case 2:
2
0

思路:先求桥的总数,之后每一次询问时减去被覆盖的桥的数量(暴力跳LCA)。

代码:

#include<bits/stdc++.h>
using namespace std;

const int A=5e5+5;
int cas,n,m,q;
int a,b;
int head[A],tot;
struct Road{int next,to;}road[A];
void ljb(int x,int y){road[++tot]={head[x],y};head[x]=tot;}
bool is[A];
int num;
int dfn[A],low[A],f[A],dep[A],tim;

void tarjan(int fa,int x){
	dfn[x]=low[x]=++tim,f[x]=fa,dep[x]=dep[fa]+1;
	for(int y=head[x];y;y=road[y].next){
		int z=road[y].to;
		if(z==fa)	continue;
		if(!dfn[z]){
			tarjan(x,z);
			low[x]=min(low[x],low[z]);
			if(low[z]>dfn[x]){
				num++;
				is[z]=1;
			}
		}
		else
			low[x]=min(low[x],dfn[z]);
	}
}

void change(int x,int y){
	if(dep[x]<dep[y])	swap(x,y);
	while(dep[x]>dep[y]){
		if(is[x])	num--;
		is[x]=0;
		x=f[x];
	}
	while(x!=y){
		if(is[x])	num--;
		if(is[y])	num--;
		is[x]=is[y]=0;
		x=f[x],y=f[y];
	}
}

void clean(){
	memset(head,0,sizeof(head));
	tot=0;
	memset(road,0,sizeof(road));
	memset(is,0,sizeof(is));
	num=0;
	memset(dfn,0,sizeof(dfn));
	memset(low,0,sizeof(low));
	memset(f,0,sizeof(f));
	memset(dep,0,sizeof(dep));
	tim=0;
}

int main(){
	while(scanf("%d%d",&n,&m)&&n!=0&&m!=0){
		clean();
		cas++;
		for(int i=1;i<=m;i++){
			scanf("%d%d",&a,&b);
			ljb(a,b),ljb(b,a);
		}
		dep[0]=0;
		tarjan(0,1);
		scanf("%d",&q);
		printf("Case %d:\n",cas);
		while(q--){
			scanf("%d%d",&a,&b);
			change(a,b);
			printf("%d\n",num);
		}
	}
	return 0;
}

树上差分

总览:

a a a b b b的路径的所有点上加 s s s,在差分数组中即为在 a a a b b b上加s,在 L C A ( a , b ) LCA(a,b) LCA(a,b)上减 2 ∗ s 2*s 2s
点的数值即为差分数组中子树的值之和。

T2 Network

POJ3417
Time Limit: 2000MS Memory Limit: 65536K

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 N N nodes in SN’s network, N − 1 N-1 N1 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 M 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 M 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 ≤ 100000 ) N (1 ≤ N ≤ 100 000) N(1N100000), M ( 1 ≤ M ≤ 100000 ) M (1 ≤ M ≤ 100 000) M(1M100000) — the number of the nodes and the number of the new channels.
Following N − 1 N-1 N1 lines represent the channels in the original network of SN, each pair ( a , b ) (a,b) (a,b) denote that there is a channel between node a a a and node b b b.
Following M M M lines represent the new channels in the network, each pair ( a , b ) (a,b) (a,b) denote that a new channel between node a a a and node b b 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

思路:每次将被新边覆盖的树边的value加1,最后统计,value为0的边可以和所有新边搭配,即ans加M,value为1的边可以和其对应的新边搭配,即ans加1。

代码:

#include<bits/stdc++.h>
using namespace std;

const int A=2e5+5;
int n,m;
int a,b;
int tot,head[A];
struct Road{int next,to;}road[A];
void ljb(int x,int y){road[++tot]={head[x],y};head[x]=tot;}
int v[A],s[A],res;
int dep[A],jump[A][20];

void prepare(int fa,int now){
	dep[now]=dep[fa]+1;
	for(int i=1;i<=17;i++)
		jump[now][i]=jump[jump[now][i-1]][i-1];
	for(int y=head[now];y;y=road[y].next){
		int son=road[y].to;
		if(son==fa)	continue;
		jump[son][0]=now;
		prepare(now,son);
	}
}

int find(int x,int y){
	if(dep[x]<dep[y])	swap(x,y);
	for(int i=17;i>=0;i--){
		if(dep[jump[x][i]]<dep[y])	continue;
		x=jump[x][i];
	}
	if(x==y)	return x;
	for(int i=17;i>=0;i--){
		if(jump[x][i]==jump[y][i])	continue;
		x=jump[x][i],y=jump[y][i];
	}
	return jump[x][0];
}

void dfs(int fa,int x){
	s[x]+=v[x];
	for(int y=head[x];y;y=road[y].next){
		int z=road[y].to;
		if(z==fa)	continue;
		dfs(x,z);
		s[x]+=s[z];
	}
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<n;i++){
		scanf("%d%d",&a,&b);
		ljb(a,b),ljb(b,a);
	}
	dep[0]=0;
	prepare(0,1);
	for(int i=1;i<=m;i++){
		scanf("%d%d",&a,&b);
		int lca=find(a,b);
		v[a]++,v[b]++,v[lca]-=2;
	}
	dfs(0,1);
	for(int i=2;i<=n;i++){
		if(!s[i])	res+=m;
		if(s[i]==1)	res++;
	}
	printf("%d",res);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值