kaungbing 专题5并查集


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");
        }
    }
}




资源下载链接为: https://pan.quark.cn/s/3d8e22c21839 随着 Web UI 框架(如 EasyUI、JqueryUI、Ext、DWZ 等)的不断发展与成熟,系统界面的统一化设计逐渐成为可能,同时代码生成器也能够生成符合统一规范的界面。在这种背景下,“代码生成 + 手工合并”的半智能开发模式正逐渐成为新的开发趋势。通过代码生成器,单表数据模型以及一对多数据模型的增删改查功能可以被直接生成并投入使用,这能够有效节省大约 80% 的开发工作量,从而显著提升开发效率。 JEECG(J2EE Code Generation)是一款基于代码生成器的智能开发平台。它引领了一种全新的开发模式,即从在线编码(Online Coding)到代码生成器生成代码,再到手工合并(Merge)的智能开发流程。该平台能够帮助开发者解决 Java 项目中大约 90% 的重复性工作,让开发者可以将更多的精力集中在业务逻辑的实现上。它不仅能够快速提高开发效率,帮助公司节省大量的人力成本,同时也保持了开发的灵活性。 JEECG 的核心宗旨是:对于简单的功能,可以通过在线编码配置来实现;对于复杂的功能,则利用代码生成器生成代码后,再进行手工合并;对于复杂的流程业务,采用表单自定义的方式进行处理,而业务流程则通过工作流来实现,并且可以扩展出任务接口,供开发者编写具体的业务逻辑。通过这种方式,JEECG 实现了流程任务节点和任务接口的灵活配置,既保证了开发的高效性,又兼顾了项目的灵活性和可扩展性。
资源下载链接为: https://pan.quark.cn/s/502b0f9d0e26 “vue后台管理前后端代码.zip”项目是一个完整的后台管理系统实现,包含前端、后端和数据库部分,适合新手学习。前端方面,Vue.js作为核心视图层框架,凭借响应式数据绑定和组件化功能,让界面构建与用户交互处理更高效。Element UI作为基于Vue的开源组件库,提供了丰富的企业级UI组件,如表格、按钮、表单等,助力快速搭建后台管理界面。项目还可能集成了Quill、TinyMCE等富文本编辑器,方便用户进行内容编辑。 后端采用前后端分离架构,前端负责数据展示和交互,后端专注于业务逻辑和数据处理,提升了代码的模块化程度、维护可性和可扩展性。后端部分可能涉及使用Node.js(如Express或Koa框架)或其他后端语言(如Java、Python)编写服务器端API接口,用于接收前端请求、处理数据并返回响应。 数据库使用MySQL存储数据,如用户信息、商品信息、订单等,开发者通过SQL语句进行数据的增删改查操作。 通过学习该项目,初学者可以掌握以下要点:Vue.js的基础知识,包括基本语法、组件化开发、指令、计算属性、监听器等;Element UI的引入、配置及组件使用方法;前后端通信技术,如AJAX或Fetch API,用于前端请求后端数据;RESTful API的设计原则,确保后端接口清晰易用;数据库表结构设计及SQL查询语句编写;基本的认证与授权机制(如JWT或OAuth),保障系统安全;以及前端和后端错误处理与调试技巧。 这个项目为初学者提供了一个全面了解后台管理系统运作的实践平台,覆盖从前端交互到后端处理再到数据存储的全过程。在实践中,学习者不仅能巩固理论知识,还能锻炼解决实际问题的能力。
资源下载链接为: https://pan.quark.cn/s/d3128e15f681 该旅游网站是一个综合性的在线服务平台,整合了前端用户界面、后端服务器处理以及数据库管理,为用户提供了全方位的旅游服务体验。以下是该网站涉及的关键技术及其详细解释: 1. Java SSM框架:SSM框架由Spring、Spring MVC和MyBatis组成,是Java Web开发中常用的三层架构模式。其中,Spring主要负责依赖注入和事务管理;Spring MVC用于处理HTTP请求和响应;MyBatis作为持久层框架,实现了SQL与Java代码的解耦,简化了数据库操作流程。 2. 网站设计:在前端设计方面,通常采用HTML、CSS和JavaScript来构建用户交互界面。Ajax技术的应用使得页面可以在不刷新整个页面的情况下更新部分内容,从而为用户提供流畅的体验。 3. 数据库管理:网站背后的数据库一般采用MySQL或其他关系型数据库管理系统,用于存储用户信息、旅游产品数据、订单等关键信息。数据库设计需要遵循一定的规范,以确保数据的一致性和完整性。 4. 短信通知:通过集成第三方短信服务提供商(如阿里云短信服务),网站能够实现用户注册验证、订单提醒等实时通知功能。这涉及到API调用、回调处理以及错误处理机制。 5. 微信支付:微信支付接口的集成使得用户可以通过微信账号进行在线支付。开发者需要获取微信支付的商户ID、API密钥等,并遵循微信支付的SDK规范,处理支付请求、订单状态查询、退款等功能。 6. 安全措施:鉴于网站涉及用户敏感信息(如支付和个人信息),必须实施安全性措施,例如采用HTTPS加密通信、防止SQL注入、XSS攻击防护等。 7. 服务器部署:网站通常部署在Apache或Nginx等Web服务器上,并通过Tomcat等应用服务器运行Java应用程序。同时,需要考虑负
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值