【 题集 】 【kuangbin带你飞】专题五 并查集 更新ing...

    几天没搞了,脑子也不好使,又开始烦躁起来了、、、

    现在只做出几题,有几题看了题解还是不会写,有些题目有别的思路可以解的,我也没有想到,想想自己的水平,真是差劲、、

    chrome 的这种格式 ,真的好嘛、、

A - Wireless Network

Description

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

    简单的模版题、

<span style="font-weight: normal;">#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;

#define N 10010

int n, m;
int vis[N];
int cnt;

struct node
{
    int x, y;
    int parent;
}po[N];

int find(int x)
{
    if (x != po[x].parent)
    {
        po[x].parent = find(po[x].parent);
    }
    return po[x].parent;
}


void merge(node a, node b)
{
    int xx = find(a.parent);
    int yy = find(b.parent);
    if(xx != yy)
    {
        if( (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y) <= m*m )
            po[yy].parent = xx;
        cnt --;
    }
}

void init(int n)
{
    memset(vis, 0, sizeof(vis));
    for(int i = 1; i <= n; i ++)
    {
        po[i].parent = i;
    }
}

int main()
{
    int ct, ca, cb;
    char c;
    while(~scanf("%d%d",&n, &m))
    {
        init(n);
        for(int i = 1; i <= n; i ++)
        {
            scanf("%d%d",&po[i].x,&po[i].y);
        }
        getchar();
        while(~scanf("%c",&c))
        {
            if(c == 'O')
            {
                scanf("%d",&ct);
                vis[ct] = 1;
                for(int i = 1; i <= n; i ++)
                {
                    if(vis[i] && i != ct)
                    {
                        merge(po[i], po[ct]);
                    }
                }
            }

            else if(c == 'S')
            {
                scanf("%d%d",&ca,&cb);
                if(find(ca) == find(cb))
                {
                    printf("SUCCESS\n");
                }
                else
                {
                    printf("FAIL\n");
                }
            }
            getchar();
        }
    }

}</span>

B - The Suspects

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 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

    题意比较简单 采用num[]存储该集合中元素个数

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;

#define N 30010

int parent[N];
int rank[N];
int num[N];
int n, m;

int find(int x)
{
    if (x != parent[x])
    {
        parent[x] = find(parent[x]);
    }
    return parent[x];
}

void merge(int a, int b)
{
    int xx = find(a);
    int yy = find(b);
    if(xx == yy)
        return;
    if(rank[xx] > rank[yy])
    {
        parent[yy] = xx;
        num[xx] += num[yy];
    }
    else
    {
        parent[xx] = yy;
        if(rank[xx] == rank[yy])
            rank[yy] ++;
        num[yy] += num[xx];
    }
}

void init(int n)
{
    for(int i = 0; i <= n; i ++)
    {
        parent[i] = i;
        num[i] = 1;
        rank[i] = 0;
    }
}

int main()
{
    int x, t, tt;
    while(~scanf("%d%d",&n, &m))
    {
        if(n == 0 && m == 0)
            break;
        if(m == 0)
        {
            printf("1\n");
            continue;
        }

        init(n);
        while(m --)
        {
            scanf("%d",&t);
            scanf("%d",&x);
            for(int i = 1; i < t; i ++)
            {
                scanf("%d",&tt);
                merge(x, tt);
            }
        }
        int ans = find(0);
        printf("%d\n", num[ans]);
    }
}

C - How Many Tables

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

      
      
2 5 3 1 2 2 3 4 5 5 1 2 5
 

Sample Output

      
      
2 4


   最简单的并查集了,求出最后有几个集合就可以了。我也以为并查集也只有这些的应用,做这套习题的时候,其实也学到蛮多的,也更觉得自己的不够。
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;

#define N 30010

int parent[N];
int rank[N];
int num[N];
int n, m;

int find(int x)
{
    if (x != parent[x])
    {
        parent[x] = find(parent[x]);
    }
    return parent[x];
}

void merge(int a, int b)
{
    int xx = find(a);
    int yy = find(b);
    if(xx == yy)
        return;
    if(rank[xx] > rank[yy])
    {
        parent[yy] = xx;
        num[xx] += num[yy];
    }
    else
    {
        parent[xx] = yy;
        if(rank[xx] == rank[yy])
            rank[yy] ++;
        num[yy] += num[xx];
    }
}

void init(int n)
{
    for(int i = 0; i <= n; i ++)
    {
        parent[i] = i;
        num[i] = 1;
        rank[i] = 0;
    }
}

int main()
{
    int t, a, b;
    while(~scanf("%d",&t))
    {
        while(t --)
        {
            int amt = 0;
            scanf("%d%d",&n, &m);
            init(n);
            for(int i = 0; i < m; i ++)
            {
                scanf("%d%d",&a,&b);
                merge(a, b);
            }

            for(int i = 1; i <= n; i ++)
            {
                if(parent[i] != i)
                    amt ++;
            }
            printf("%d\n",n - amt);
        }

    }
}

M - 小希的迷宫

Description

上次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
 


     一开始,我没有用vis标记,也没有判断 0 0 的情况,查完题解才知道,我觉得 ,以后真的 得少看题解!!- -#
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;

#define N 100010

int parent[N];
int rank[N];
int num[N];
int vis[N];

int find(int x)
{
    if (x != parent[x])
    {
        parent[x] = find(parent[x]);
    }
    return parent[x];
}

void merge(int a, int b)
{
    int xx = find(a);
    int yy = find(b);
    if(xx == yy)
        return;
    if(rank[xx] > rank[yy])
    {
        parent[yy] = xx;
        num[xx] += num[yy];
    }
    else
    {
        parent[xx] = yy;
        if(rank[xx] == rank[yy])
            rank[yy] ++;
        num[yy] += num[xx];
    }
}

void init(int n)
{
    for(int i = 0; i <= n; i ++)
    {
        parent[i] = i;
        num[i] = 1;
        rank[i] = 0;
        vis[i] = 0;
    }
}


int main()
{
    int n, m;
    int a, b;
    while(~scanf("%d%d",&n,&m))
    {
        if(n == -1 && m == - 1)
            break;
        if(n == 0 && m == 0)
        {
            printf("Yes\n");
            continue;
        }
        init(100010);
        bool flag = true;
        int cnt = 0;
        merge(n, m);
        vis[n] = vis[m] = 1;

        while(~scanf("%d%d",&a,&b))
        {
            if(a == 0 && b == 0)
                break;
            vis[a] = 1;
            vis[b] = 1;
            if(find(a) == find(b))
            {
                flag = false;
                continue;
            }
            merge(a, b);
        }
        if(!flag)
            printf("No\n");
        else
        {
            for(int i = 1; i <= N; i ++)
            {
                if(vis[i] && parent[i] == i)
                    cnt ++;
            }
            if(cnt == 1)
                printf("Yes\n");
            else
                printf("No\n");
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值