HDU-畅通工程-1232(并查集)How Many Tables(1213)

来源:互联网 发布:代码优化的目的和意义 编辑:程序博客网 时间:2024/06/06 01:51


 


并查集--学习详解

文章作者:yx_th000 文章来源:Cherish_yimi (http://www.cnblogs.com/cherish_yimi/) 转载请注明,谢谢合作。

    [本文新址: http://www.ahathinking.com/archives/10.html ] 

    昨天和今天学习了并查集和trie树,并练习了三道入门题目,理解更为深刻,觉得有必要总结一下,这其中的内容定义之类的是取自网络,操作的说明解释及程序的注释部分为个人理解。并查集学习:

l        并查集:(union-find sets)

一种简单的用途广泛的集合. 并查集是若干个不相交集合,能够实现较快的合并和判断元素所在集合的操作,应用很多,如其求无向图的连通分量个数等。最完美的应用当属:实现Kruskar算法求最小生成树。

l        并查集的精髓(即它的三种操作,结合实现代码模板进行理解):

1、Make_Set(x) 把每一个元素初始化为一个集合

初始化后每一个元素的父亲节点是它本身,每一个元素的祖先节点也是它本身(也可以根据情况而变)。

2、Find_Set(x) 查找一个元素所在的集合

查找一个元素所在的集合,其精髓是找到这个元素所在集合的祖先!这个才是并查集判断和合并的最终依据。
判断两个元素是否属于同一集合,只要看他们所在集合的祖先是否相同即可。
合并两个集合,也是使一个集合的祖先成为另一个集合的祖先,具体见示意图

3、Union(x,y) 合并x,y所在的两个集合

合并两个不相交集合操作很简单:
利用Find_Set找到其中两个集合的祖先,将一个集合的祖先指向另一个集合的祖先。如图



l        并查集的优化

1、Find_Set(x)时 路径压缩
寻找祖先时我们一般采用递归查找,但是当元素很多亦或是整棵树变为一条链时,每次Find_Set(x)都是O(n)的复杂度,有没有办法减小这个复杂度呢?
答案是肯定的,这就是路径压缩,即当我们经过"递推"找到祖先节点后,"回溯"的时候顺便将它的子孙节点都直接指向祖先,这样以后再次Find_Set(x)时复杂度就变成O(1)了,如下图所示;可见,路径压缩方便了以后的查找。

2、Union(x,y)时 按秩合并
即合并的时候将元素少的集合合并到元素多的集合中,这样合并之后树的高度会相对较小。



 

 

 

 

 

 

 

畅通工程

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22606    Accepted Submission(s): 11804


Problem Description
某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?
 

Input
测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。
注意:两个城市之间可以有多条道路相通,也就是说
3 3
1 2
1 2
2 1
这种输入也是合法的
当N为0时,输入结束,该用例不被处理。
 

Output
对每个测试用例,在1行里输出最少还需要建设的道路数目。
 

Sample Input
4 2
1 3
4 3
3 3
1 2
1 3
2 3
5 2
1 2
3 5
999 0
0
 

Sample Output
1
0
2
998

Hint
Hint
 
Huge input, scanf is recommended.
 

Source
浙大计算机研究生复试上机考试-2005年
*/

#include<stdio.h>int a[1001],count=0;int find(int c){        while(a[c]!=c)        c=a[c];//找总的根节点 return c;       }void merger(int x,int y){        int x1=find(x);    int y1=find(y);    if(x1!=y1)//有关系而其根节点不相同时,赋值y的根为x的根       a[y1]=x1;    }int main(){    int n,i,m,x,y;    while(scanf("%d",&n),n){        for(i=1;i<=n;i++){            a[i]=i;        }        scanf("%d",&m);        while(m--){            scanf("%d%d",&x,&y);            merger(x,y);        }        for(i=1,count=0;i<=n;i++)            if(a[i]==i) ++count;//看有多少孤立的节点,即需要count-1个才可以畅通!            printf("%d\n",count-1);            }    return 0;}



 

 

How Many Tables

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9845    Accepted Submission(s): 4884


Problem Description
Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.

One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.

For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.
 


 

Input
The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.
 


 

Output
For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
 


 

Sample Input
25 31 22 34 55 12 5
 


 

Sample Output
24
 


 

Author
Ignatius.L
 


 

Source
杭电ACM省赛集训队选拔赛之热身赛

 

这个是最简单的并查集原型题目

 

 

import java.util.Scanner;public class hdu1213How_Many_Tables {  static int[]f;  //static boolean[]used;public static void main(String[] args) {Scanner sc= new Scanner(System.in);int N=sc.nextInt();while(N-->0){int n=sc.nextInt();int m=sc.nextInt();f=new int[n+10]; for(int i=1;i<=n;i++) f[i]=i; while(m-->0){ int a=sc.nextInt(); int b=sc.nextInt();  Union(a,b); } int i,count; for( i=1,count=0;i<=n;i++) if(f[i]==i) count++;  System.out.println(count); }}private static void Union(int a, int b) { int x=find(a); int y=find(b);   if(x>y)   f[x]=y;   else   f[y]=x;}private static int find(int x) {int r=x;while(f[r]!=r)  r=f[r];int i=x;while(i!=f[i]){//优化,将其各个节点指向同一个根节点int j=f[i]; f[i]=r; i=j;}return r;}}


 

 

 

 

 

 

 

 


原创粉丝点击