在很多时候,并查集并不是一个完整的解题方法,而是一种思路。
通过以下题目来体会并查集逆向运用的思想。
Description
Farmer John and his cows are planning to leave town for a long vacation, and so FJ wants to temporarily close down his farm to save money in the meantime.The farm consists of NN barns connected with MM bidirectional paths between some pairs of barns (1≤N,M≤200,000). To shut the farm down, FJ plans to close one barn at a time. When a barn closes, all paths adjacent to that barn also close, and can no longer be used.FJ is interested in knowing at each point in time (initially, and after each closing) whether his farm is "fully connected" -- meaning that it is possible to travel from any open barn to any other open barn along an appropriate series of paths. Since FJ's farm is initially in somewhat in a state of disrepair, it may not even start out fully connected.
Input
The first line of input contains N and M. The next M lines each describe a path in terms of the pair
of barns it connects (barns are conveniently numbered 1…N). The final N lines give a permutation o
f 1…N describing the order in which the barns will be closed.
Output
The output consists of N lines, each containing "YES" or "NO". The first line indicates whether the initial farm is fully connected, and line i+1 indicates whether the farm is fully connected after the iith closing.
Sample Input
4 3
1 2
2 3
3 4
3
4
1
2
Sample Output
YES
NO
YES
YES
显然,按照正向逻辑,每次删去一条边都必须检查整幅图的连通性,做法过于冗杂,时间复杂度高。换一种思维,我们将一个一个点加进图中,通过并查集来维护图的连通性,则可以再Om的时间之内完成。程序如下。
#include<iostream>
#include<cstdio>
using namespace std;
struct line{
int to;
int next;
}; line a[];
int head[];
int n,m,be[],ans[],fa[],que[];
int getf(int k){ //并查集常规+路径压缩
if(fa[k]!=k)fa[k]=getf(fa[k]);
return fa[k];
}
int main(){
cin>>n>>m;
for(int i=;i<=m;++i){
int x,y;
scanf("%d%d",&x,&y);
a[*i-].next=head[x]; //每条边看做两条单向边处理,运用链式前向星保证空间充足
a[*i-].to=y;
head[x]=*i-;
a[*i].next=head[y];
a[*i].to=x;
head[y]=*i;
}
for(int i=n;i>=;--i)scanf("%d",&que[i]); for(int i=;i<=n;++i)fa[i]=i; int num=;
be[que[]]=;
ans[]=; for(int i=;i<=n;++i){
num++; //加入一个新的点,num记录当前图中的集合个数,只有一个集合时说明图连通
be[que[i]]=; //bei表示这个点已经加入图中
int now=head[que[i]];
while(now!=){
if(be[a[now].to]==){
int fx=getf(a[now].to);
if(fx!=que[i]){
num--;
fa[fx]=que[i];
}
}
now=a[now].next;
}
if(num==)ans[i]=;
else ans[i]=;
} for(int i=n;i>=;--i){ //逆向输出
if(ans[i]==)printf("YES\n");
else printf("NO\n");
}
return ;
}
To be continue......