kaungbing 专题5并查集

本文介绍了并查集在解决特定问题中的应用,如网络修复、疾病传播预防、迷宫设计验证及社交群体分析等场景,并提供了详细的算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


poj2236并查集
An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broken. The computers are repaired one by one, and the network gradually began to work again. Because of the hardware restricts, each computer can only directly communicate with the computers that are not farther than d meters from it. But every computer can be regarded as the intermediary of the communication between two other computers, that is to say computer A and computer B can communicate if computer A and computer B can communicate directly or there is a computer C that can communicate with both A and B.

In the process of repairing the network, workers can take two kinds of operations at every moment, repairing a computer, or testing if two computers can communicate. Your job is to answer all the testing operations.
Input
The first line contains two integers N and d (1 <= N <= 1001, 0 <= d <= 20000). Here N is the number of computers, which are numbered from 1 to N, and D is the maximum distance two computers can communicate directly. In the next N lines, each contains two integers xi, yi (0 <= xi, yi <= 10000), which is the coordinate of N computers. From the (N+1)-th line to the end of input, there are operations, which are carried out one by one. Each line contains an operation in one of following two formats:
1. "O p" (1 <= p <= N), which means repairing computer p.
2. "S p q" (1 <= p, q <= N), which means testing whether computer p and q can communicate.

The input will not exceed 300000 lines.
Output
For each Testing operation, print "SUCCESS" if the two computers can communicate, or "FAIL" if not.
Sample Input
4 1
0 1
0 2
0 3
0 4
O 1
O 2
O 4
S 1 4
O 3
S 1 4
Sample Output
FAIL
SUCCESS 
这题目比较明显的并查集,用vis标记是否开了电脑,然后从1-n扫出开过的电脑是否与该电脑联通,如果联通,那么将另外一台电脑的父节点设为该电脑,也就是形成一个集合,判断是否能够沟通,就判断他们是否在同一集合就可以了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;
struct node
{
    int x;
    int y;
}a[10009];
int d;
int father[10009];
int vis[10009];
int Find(int x)
{
    if(x!=father[x]) father[x]=Find(father[x]);
    return father[x];
}
int pd(int i,int j)
{
    int m;
    m=(a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y);
    if(m<=d*d) return 1;
    else return 0;
}
void Union(int i,int j)
{
    int x,y;
    x=Find(i);
    y=Find(j);
    if(x!=y) father[x]=y;
}
int main()
{
    int b,c,n,i,j;
    char ch[2];
   scanf("%d%d",&n,&d);
   for(i=1;i<=n;i++)
   {
       father[i]=i;
       vis[i]=0;
   }
   for(i=1;i<=n;i++)
   {
       scanf("%d%d",&a[i].x,&a[i].y);
   }
   while(scanf("%s",ch)!=EOF)
   {
       if(ch[0]=='O')
       {
           scanf("%d",&b);
           vis[b]=1;
           for(i=1;i<=n;i++)
           {
               if(vis[i]&&pd(i,b)&&i!=b)
               {
                   Union(i,b);
               }
           }
       }
       else
       {
           scanf("%d%d",&b,&c);
           if(Find(b)==Find(c)) printf("SUCCESS\n");
           else printf("FAIL\n");
       }

   }
}
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 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
200 2
1 5
5 1 2 3 4 5
1 0
0 0
Sample Output
4
1
1
判断这些人是否得病,那么我们就判断他们是否和0号元素在同一集合,如果这个社团里面,有一个节点它的根节点为0,那么这个社团全部都要把父节点设为0,因为他们全部得病,如果这个社团没有根节点为0的节点,那么把整个社团做成一个集合就好了,最后把路径压缩一下,父节点是0的节点就是得病的,算出sum就好了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;
int father[30009];
int a[30009];
int Find(int x)
{
    if(x!=father[x]) father[x]=Find(father[x]);
    return father[x];
}
void Union(int i,int j)
{
    int x,y;
    x=Find(i);
    y=Find(j);
    father[x]=y;
}
int main()
{
    int sum=0,i,j,k,n,m,flag,Count;
    while(scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0) break;
        sum=0;
        for(i=0; i<=n-1; i++)
        {
            father[i]=i;
        }
        while(m--)
        {
            flag=0;
            scanf("%d",&k);
            for(i=1; i<=k; i++)
            {
                scanf("%d",&a[i]);
                if(Find(a[i])==0)
                {
                    flag=1;
                }
            }
            if(flag)
            {
                for(i=1; i<=k; i++)
                {
                    j=Find(a[i]);
                    father[j]=0;
                }
            }
            else
            {
                for(i=1; i<=k-1; i++)
                {
                    Union(a[i],a[i+1]);
                }
            }
        }
        for(i=0; i<=n-1; i++) if(Find(i)==0) sum++;
        printf("%d\n",sum);
    }
}
/*
10 4
2 1 2
5 3 4 5 6 7
2 0 1
2 9 2
*/
hdu1272
上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走。但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了房间A和B,那么既可以通过它从房间A走到房间B,也可以通过它从房间B走到房间A,为了提高难度,小希希望任意两个房间有且仅有一条路径可以相通(除非走了回头路)。小希现在把她的设计图给你,让你帮忙判断她的设计图是否符合她的设计思路。比如下面的例子,前两个是符合条件的,但是最后一个却有两种方法从5到达8。


Input 输入包含多组数据,每组数据是一个以0 0结尾的整数对列表,表示了一条通道连接的两个房间的编号。房间的编号至少为1,且不超过100000。每两组数据之间有一个空行。
整个文件以两个-1结尾。
Output 对于输入的每一组数据,输出仅包括一行。如果该迷宫符合小希的思路,那么输出"Yes",否则输出"No"。
Sample Input
6 8  5 3  5 2  6 4
5 6  0 0

8 1  7 3  6 2  8 9  7 5
7 4  7 8  7 6  0 0

3 8  6 8  6 4
5 3  5 6  5 2  0 0

-1 -1
Sample Output
Yes
Yes
No
这道题目要仔细去思考一下,题目要求了只能走一次,那么就是说,这类似于树的结构由上到下,不存在回路,所以在这个树的集合里面,输入两个节点a,b,如果这两个Find(a)==Find(b)那么就是说,他们已经在同一集合里面了,还要相连,所以他们就形成回路,还有一种情况就是,如果形成两个或者多个集合,同样也是不符合题目的,我们要把所有的节点都遍历一遍,看是否只存在一个根节点(多个根节点意味着多个集合)
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
int father[100009];
struct node
{
    int x;
    int y;
} c[100009];
int vis[100009];
int Find(int x)
{
    if(x!=father[x]) father[x]=Find(father[x]);
    return father[x];
}
int main()
{
    int a,b,flag,i,j,k,x,y;
    while(scanf("%d%d",&a,&b))
    {
        flag=0;
        if(a==-1&&b==-1) break;
        if(a==0&&b==0)
        {
            printf("Yes\n");
            continue;
        }
        for(i=0; i<=100000; i++) father[i]=i;
        memset(vis,0,sizeof(vis));
        vis[a]=1;
        vis[b]=1;
        x=Find(a);
        y=Find(b);
        if(x!=y) father[x]=y;
        else
        {
            flag=1;
        }
        while(1)
        {
            scanf("%d%d",&a,&b);
            if(a==0&&b==0) break;
            vis[a]=1;
            vis[b]=1;
            x=Find(a);
            y=Find(b);
            if(x!=y) father[x]=y;
            else
            {
                flag=1;
            }
        }
        int Count=0;
        if(flag) printf("No\n");
        else
        {
            for(i=0; i<=100000; i++)
            {
                if(vis[i]&&father[i]==i) Count++;
            }
            if(Count==1) printf("Yes\n");
            else printf("No\n");
        }
    }
}




hdu1213
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
2
5 3
1 2
2 3
4 5

5 1
2 5
Sample Output
2
4
题目就是求有多少个不同集合,我们一开始有n个人,假设有n个集合,如果a,b联通且不在同一集合,就加入集合,n--,最后的出来的n就是答案了
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
int father[100009];
struct node
{
    int x;
    int y;
} c[100009];
int vis[100009];
int Find(int x)
{
    if(x!=father[x]) father[x]=Find(father[x]);
    return father[x];
}
int main()
{
    int a,b,flag,i,j,k,x,y;
    while(scanf("%d%d",&a,&b))
    {
        flag=0;
        if(a==-1&&b==-1) break;
        if(a==0&&b==0)
        {
            printf("Yes\n");
            continue;
        }
        for(i=0; i<=100000; i++) father[i]=i;
        memset(vis,0,sizeof(vis));
        vis[a]=1;
        vis[b]=1;
        x=Find(a);
        y=Find(b);
        if(x!=y) father[x]=y;
        else
        {
            flag=1;
        }
        while(1)
        {
            scanf("%d%d",&a,&b);
            if(a==0&&b==0) break;
            vis[a]=1;
            vis[b]=1;
            x=Find(a);
            y=Find(b);
            if(x!=y) father[x]=y;
            else
            {
                flag=1;
            }
        }
        int Count=0;
        if(flag) printf("No\n");
        else
        {
            for(i=0; i<=100000; i++)
            {
                if(vis[i]&&father[i]==i) Count++;
            }
            if(Count==1) printf("Yes\n");
            else printf("No\n");
        }
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值