20150128并查集
来源:互联网 发布:淘宝达人入口网址 编辑:程序博客网 时间:2024/05/29 16:19
第一篇。下面直接来记录什么是并查集,它是一种树型数据结构,通过一个数组中的值来记录它的father,若其中值为自身,则它为树的顶端。
上例题
Memory Limit: 65536KTotal Submissions: 25461
Accepted: 12576
Description
You know that there are n students in your university (0 < n <= 50000). It is infeasible for you to ask every student their religious beliefs. Furthermore, many students are not comfortable expressing their beliefs. One way to avoid these problems is to ask m (0 <= m <= n(n-1)/2) pairs of students and ask them whether they believe in the same religion (e.g. they may know if they both attend the same church). From this data, you may not know what each person believes in, but you can get an idea of the upper bound of how many different religions can be possibly represented on campus. You may assume that each student subscribes to at most one religion.
Input
Output
Sample Input
<span style="font-size: 14px;">10 91 21 31 41 51 61 71 81 91 1010 42 34 54 85 80 0</span>
Sample Output
<span style="font-size: 14px;">Case 1: 1Case 2: 7</span>
Hint
Source
题目链接:http://poj.org/problem?id=2524
题目大意:有n个地方,m组关系,每组关系表示两个地方的信仰相同,问一共有多少种不同的信仰
#include<stdio.h>
int array[50010];
int findfather(int x)
{
return (x == array[x]) ? x : (array[x] = findfather(array[x]));
}
int main()
{
int n,m,a,fa,b,fb,i;
int t=0;
int count=0;
while (scanf("%d %d", &n, &m) != EOF && (m + n))
{
for ( i = 1; i <= n; i++)
{
array[i] = i;
}
for (i = 1; i <= m; i++)
{
scanf("%d %d", &a, &b);
fa = findfather(a);
fb = findfather(b);
array[fa] = fb;
}
for ( i = 1; i <= n; i++)
if (i == findfather(i))
count++;
printf("Case %d: %d\n", ++t, count);
}
}
第一步重置数组
第二步写一个find函数寻找顶端的father
第三步将有关系的联结起来
第四步统计顶端的father数量
第一个模板
下面是封装成一个一个小函数
#include<stdio.h>
int array[50010];
int reset(int n)
{
for (int i = 1; i <= n; i++)
{
array[i] = i;
}
}
int findfather(int x)
{
return (x == array[x]) ? x : (array[x] = findfather(array[x]));
}
void father(int a,int b)
{
int fa = findfather(a);
int fb = findfather(b);
array[fa] = fb;
}
int main()
{
int n, m, a, b, i;
int t = 0;
int count = 0;
while (scanf_s("%d %d", &n, &m) != EOF && (m + n))
{
reset(n);
for (i = 1; i <= m; i++)
{
scanf_s("%d %d", &a, &b);
father(a, b);
}
for (i = 1; i <= n; i++)
if (i == findfather(i))
count++;
printf("Case %d: %d\n", ++t, count);
}
}
Description
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).
Once a member in a group is a suspect, all members in the group are suspects.
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.
Input
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
Output
Sample Input
100 42 1 25 10 13 11 12 142 0 12 99 2200 21 55 1 2 3 4 51 00 0
Sample Output
411
Source
题目链接:http://poj.org/problem?id=1611
题目大意:n个人,m组关系,每组关系中有k个人,与0相关的均为嫌疑人,求有多少嫌疑人
int array[50010];
int reset(int n)
{
for (int i = 0; i <= n; i++)
{
array[i] = i;
}
}
int findfather(int x)
{
return (x == array[x]) ? x : (array[x] = findfather(array[x]));
}
void father(int a,int b)
{
int fa = findfather(a);
int fb = findfather(b);
if (fa == 0)array[fb] = fa;
else array[fa] = fb;
}
int main()
{
int n, m, a, b, i,p,f,x;
int count;
while (scanf("%d %d", &n, &m) != EOF && (m + n))
{
count = 1;
reset(n);
for (i = 1; i <= m; i++)
{
scanf("%d %d", &p, &f);
while (--p)
{
scanf("%d", &x);
father(f, x);
}
}
for (i = 1; i <= n; i++)
if (!findfather(i))
count++;
printf("%d\n", count);
}
- 20150128并查集
- HDU3938 并查集 并查集
- 并查集(集并查)
- HDU1232 并查集<并>
- 并查集
- 数据结构-并查集
- 并查集
- 并查集!
- 并查集
- 并查集
- 并查集
- 并查集
- 并查集总结
- 并查集学习
- 并查集
- 并查集
- 并查集
- 所谓并查集
- AngularJS开发者常犯的10个错误
- 2015年手机六大技术趋势预测
- 在eclipse中android NDK开发环境的搭建
- Android中Handler的意义和用法
- Mysql与Oracle区别
- 20150128并查集
- keil编译的CODE.RW.RO.ZI的大小说明
- processAnnotations
- Ocaml 中的module system
- 使用Android studio导入项目
- Android EditText的设置
- iOS拿到推送从而跳转到指定页面
- 必应输入法不争气,自力更生
- 8 Tips To Become A Better Front End Developer