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
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.
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
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.
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 4Sample 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
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.
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
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
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 0Sample Output
4 1 1
#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 -1Sample Output
Yes Yes No
#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.
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 5Sample Output
2 4
#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");
}
}
}