20150128并查集

来源:互联网 发布:淘宝达人入口网址 编辑:程序博客网 时间:2024/05/29 16:19

第一篇。下面直接来记录什么是并查集,它是一种树型数据结构,通过一个数组中的值来记录它的father,若其中值为自身,则它为树的顶端。

上例题

A题 Ubiquitous Religions
Time Limit: 5000MS
Memory Limit: 65536KTotal Submissions: 25461
Accepted: 12576

Description

There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding out how many different religions students in your university believe in.

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

The input consists of a number of cases. Each case starts with a line specifying the integers n and m. The next m lines each consists of two integers i and j, specifying that students i and j believe in the same religion. The students are numbered 1 to n. The end of input is specified by a line in which n = m = 0.

Output

For each test case, print on a single line the case number (starting with 1) followed by the maximum number of different religions that the students in the university believe in.

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

Huge input, scanf is recommended.

Source

Alberta Collegiate Programming Contest 2003.10.18

题目链接: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);

 }
}


B题 The Suspects

Time Limit: 1000MS Memory Limit: 20000KTotal Submissions: 23748 Accepted: 11584

Description

Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others.
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

The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.

Output

For each case, output the number of suspects in one line.

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

Asia Kaohsiung 2003

题目链接:http://poj.org/problem?id=1611

题目大意:n个人,m组关系,每组关系中有k个人,与0相关的均为嫌疑人,求有多少嫌疑人
注意将模板中重置时放入0;并在函数中特判0为树的顶端;并可以将每一行的首数设为关系第一个
#include<stdio.h>
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);
 }
}



0 0
原创粉丝点击