并查集入门题

A - How Many Tables

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<string.h>

int bcj[30005];
int Find(int x)
{
    if(bcj[x] < 0) return x;
    return bcj[x] = Find(bcj[x]);
}

void Union(int x, int y)
{
    x = Find(x), y = Find(y);
    if(x == y) return ;
    bcj[x] += bcj[y];
    bcj[y] = x;
}

int main()
{
	int t,n,m;
	int x,y;
	scanf("%d",&t);
	while(t--)
	{
		memset(bcj,-1,sizeof bcj);
		scanf("%d%d",&n,&m);
		for(int i=0;i<m;i++)
		{
			scanf("%d%d",&x,&y);
			Union(x,y);
		}
		int ans=0;
		for(int i=1;i<=n;i++)
		{
			if(bcj[i]<0)
				ans++;
		}
		printf("%d\n",ans);
	}
	return 0;
}

B - 小希的迷宫

在这里插入图片描述

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

这道题除了基本的判断跟结点的数量是否为1,还要在每次路径输入的时候判断两房间是否已经连通(即两房间的根节点是否相同),如果相同用一个标记变量来记录。
还要进行特判当一组的输入仅有0 0时,表示没有房间,此时也成立。

#include<stdio.h>
#include<string.h>

int bcj[100005];
bool mark[100005];
int Find(int x)
{
    if(bcj[x] < 0) return x;
    return bcj[x] = Find(bcj[x]);
}

void Union(int x, int y)
{
    x = Find(x), y = Find(y);
    if(x == y) return ;
    bcj[x] += bcj[y];
    bcj[y] = x;
}

int main()
{
	int x,y;
	bool flag;
	while(scanf("%d%d",&x,&y)!=EOF)
	{
		flag=0;
		if(x==-1&&y==-1) break;
		if(x==0&&y==0)
		{
			printf("Yes\n");//特判 
			continue;	
		} 
		memset(bcj,-1,sizeof bcj);
		memset(mark,0,sizeof mark);
		Union(x,y);
		mark[x]=mark[y]=1;
		while(true)
		{
			scanf("%d%d",&x,&y);
			if(x==0&&y==0) break;
			if(Find(x)==Find(y))
			{
				flag=1;
			}
			Union(x,y);
			mark[x]=mark[y]=1;
		}
		int cnt=0;
		for(int i=1;i<=100000;i++)
		{
			if(mark[i]&&bcj[i]<0) cnt++;
		}
		if(flag) printf("No\n");
		else if(cnt==1) printf("Yes\n");
		else printf("No\n");
	}
	return 0;
}

C - 畅通工程

某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?

Input

测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。
注意:两个城市之间可以有多条道路相通,也就是说
3 3
1 2
1 2
2 1
这种输入也是合法的
当N为0时,输入结束,该用例不被处理。

Output

对每个测试用例,在1行里输出最少还需要建设的道路数目。

Sample Input

4 2
1 3
4 3
3 3
1 2
1 3
2 3
5 2
1 2
3 5
999 0
0

Sample Output

1
0
2
998

Hint

Huge input, scanf is recommended.

#include<stdio.h>
#include<string.h>

int bcj[30005];
int Find(int x)
{
    if(bcj[x] < 0) return x;
    return bcj[x] = Find(bcj[x]);
}

void Union(int x, int y)
{
    x = Find(x), y = Find(y);
    if(x == y) return ;
    bcj[x] += bcj[y];
    bcj[y] = x;
}

int main()
{
	int n,m;
	int x,y;
	while(scanf("%d",&n)!=EOF)
	{
		memset(bcj,-1,sizeof bcj);
		if(n==0) break;
		scanf("%d",&m);
		for(int i=0;i<m;i++)
		{
			scanf("%d%d",&x,&y);
			Union(x,y);
		}
		int ans=0;
		for(int i=1;i<=n;i++)
			if(bcj[i]<0)
				ans++;
		printf("%d\n",ans-1);
	}
	return 0;
}

D - 畅通工程

[NWUACM]
给出无向图中M个节点间N条边的权值。
求一个使得所有点连通的子图,要求图内的边权和最小

Input

测试输入包含若干测试用例
每个测试用例的第1行包含N、M ( <100 )
随后的 N 行每行包含3个数,边的起点,终点,权值
节点从1到M编号
当N为0时,全部测试结束.

Output

对每个测试用例,在1行里输出最小生成树权值。
若不存在符合要求的子图,则输出“?”。

Sample Input

3 3
1 2 1
1 3 2
2 3 4
1 3
2 3 2
0 100

Sample Output

3
?

这道题用到了最小生成树,单纯的套模板就可以。
注意这道题中的n,m与模板中常用的相反,所以在输入时,将n,m交换位置即可。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
using namespace std;
const int N = 2e5;
typedef pair<int, int> P;
int bcj[5005];
int n, m, cnt, ans;
struct node {
    int u, v, w;
    inline bool operator < (const node &x) const {
        return w < x.w;
    }
} edge[200005];

int Find(int x)
{
    if(bcj[x] < 0) return x;
    return bcj[x] = Find(bcj[x]);
}
void Union(int x, int y)
{
    x = Find(x), y = Find(y);
    if(x == y) return ;
    bcj[x] += bcj[y];
    bcj[y] = x;
}

void kruskal()
{
    cnt = ans = 0;
    sort(edge, edge + m);
    for(int i = 0; i < m; ++i) {
        int u = Find(edge[i].u), v = Find(edge[i].v);
        if(u == v) continue;
        ans += edge[i].w;
        bcj[v] = u;
        cnt++;
        if(cnt == n - 1) break;   
    }
}



int main()
{
    while(scanf("%d %d", &m, &n)!=EOF)
    {
    	if(m==0) break;
		memset(bcj, -1, sizeof bcj);
		for(int i = 0; i < m; ++i) {
		    scanf("%d %d %d", &edge[i].u, &edge[i].v, &edge[i].w);
		}
		kruskal();
		if(cnt==n-1)
		    printf("%d\n", ans);
		else
		   	printf("?\n");
	}
    return 0;
}

E - 还是畅通工程

某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。

Input

测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( < 100 );随后的N(N-1)/2行对应村庄间的距离,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间的距离。为简单起见,村庄从1到N编号。
当N为0时,输入结束,该用例不被处理。

Output

对每个测试用例,在1行里输出最小的公路总长度。

Sample Input

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

Sample Output

3
5

Hint

Huge input, scanf is recommended.

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
using namespace std;
const int N = 2e5;
typedef pair<int, int> P;
int bcj[5005];
int n, m, cnt, ans;
struct node {
    int u, v, w;
    inline bool operator < (const node &x) const {
        return w < x.w;
    }
} edge[200005];

int Find(int x)
{
    if(bcj[x] < 0) return x;
    return bcj[x] = Find(bcj[x]);
}

void kruskal()
{
    cnt = ans = 0;
    sort(edge, edge + m);
    for(int i = 0; i < m; ++i) {
        int u = Find(edge[i].u), v = Find(edge[i].v);
        if(u == v) continue;
        ans += edge[i].w;
        bcj[v] = u;
        cnt++;
        if(cnt == n - 1) break;   
    }
}



int main()
{
    while(scanf("%d", &n)!=EOF)
	{
		if(n==0) break;
	    m=n*(n-1)/2;
	    memset(bcj, -1, sizeof bcj);
	    for(int i = 0; i < m; ++i) {
	        scanf("%d %d %d", &edge[i].u, &edge[i].v, &edge[i].w);
	    }
	    kruskal();
	    printf("%d\n", ans);
	}
    return 0;
}

F - 畅通工程续

某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。

现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。

Input

本题目包含多组数据,请处理到文件结束。
每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。
接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。
再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。

Output

对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1.

Sample Input

3 3
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2

Sample Output

2
-1

这也是一道最短路板子题,但是坑有点多,城镇的标号是从0——n-1的,还有记得要保存每次输入的两城镇之间最小的路径

#include<stdio.h>
#include<string.h>

const int inf=0x3f3f3f3f;

int ma[1005][1005];
int dis[1005];
bool vis[1005];
int n,m;
void Dijk()
{
	for(int i=0;i<n-1;i++)//遍历除了n本身以外的n-1个城市
	{
		int min=inf,k;//用来存储最小的且没被松弛过的城市
		for(int j=0;j<n;j++)
		{
			if(min>dis[j]&&!vis[j]) //依次比较
			{
				min=dis[j];
				k=j;//记录最小且没被松弛过的城市的下标
			}
		}
		vis[k]=1;//标记被松弛过的城市
		for(int h=0;h<n;h++)
		{
			if(dis[h]>dis[k]+ma[k][h])
				dis[h]=dis[k]+ma[k][h];
		}
	}	
}

int main()
{
	int a,b,x;
	int s,t;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		memset(vis,0,sizeof vis);
		memset(ma,inf,sizeof ma);
		for(int i=0;i<n;i++)
			ma[i][i]=0;
		for(int i=0;i<m;i++)
		{
			scanf("%d%d%d",&a,&b,&x);
			if(x<ma[a][b])
				ma[b][a]=ma[a][b]=x;
		}
		 scanf("%d%d",&s,&t);
		 vis[s]=1;
		 for(int i=0;i<n;i++)
		 	dis[i]=ma[s][i];
		Dijk();
		if(dis[t]==inf)
			printf("-1\n");
		else
			printf("%d\n",dis[t]);
	}	
	return 0;
}

G - 畅通工程再续

相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现。现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全畅通!经过考察小组RPRush对百岛湖的情况充分了解后,决定在符合条件的小岛间建上桥,所谓符合条件,就是2个小岛之间的距离不能小于10米,也不能大于1000米。当然,为了节省资金,只要求实现任意2个小岛之间有路通即可。其中桥的价格为 100元/米。

Input

输入包括多组数据。输入首先包括一个整数T(T <= 200),代表有T组数据。
每组数据首先是一个整数C(C <= 100),代表小岛的个数,接下来是C组坐标,代表每个小岛的坐标,这些坐标都是 0 <= x, y <= 1000的整数。

Output

每组输入数据输出一行,代表建桥的最小花费,结果保留一位小数。如果无法实现工程以达到全部畅通,输出”oh!”.

Sample Input

2
2
10 10
20 20
3
1 1
2 2
1000 1000

Sample Output

1414.2
oh!

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
using namespace std;
const int N = 2e5;

typedef pair<int, int> P;
int bcj[5005];
int n, m, cnt;
double x[105],y[105],ans;

struct node {
    double u, v, w;
    inline bool operator < (const node &x) const {
        return w < x.w;
    }
} edge[200005];

int Find(int x)
{
    if(bcj[x] < 0) return x;
    return bcj[x] = Find(bcj[x]);
}

void kruskal()
{
    cnt = ans = 0;
    sort(edge, edge + m);
    for(int i = 0; i < m; ++i) {
        int u = Find(edge[i].u), v = Find(edge[i].v);
        if(u == v) continue;
        ans += edge[i].w;
        bcj[v] = u;
        cnt++;
        if(cnt == n - 1) break;   
    }
}

int main()
{
	int t;
	double dis;
	scanf("%d",&t);
	while(t--) 
	{
		m=0;
		scanf("%d",&n);
	    for(int i=0;i<n;i++)
	    	scanf("%lf%lf",&x[i],&y[i]);
	    for(int i=0;i<n;i++)
	    	for(int j=i+1;j<n;j++)
	    	{
	    		dis=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
				if(dis>=10&&dis<=1000)
				{
					edge[m].u=i;edge[m].v=j;
					edge[m].w=dis;
					m++;
				}
	    	}
	    memset(bcj, -1, sizeof bcj);
	    kruskal();
	    if(cnt==n-1)
			printf("%.1lf\n", ans*100);
		else
			printf("oh!\n");
	}
    return 0;
}

H - 食物链

在另一篇博客:https://blog.csdn.net/weixin_43772166/article/details/89716198

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值