初入并查集

嘻嘻哈哈并查集

刚入手并查集的时候,学校大佬给推荐的是这篇博客,可以说这博主也是堪称神人。至少我是没有看过那么多武狭小说

安静微笑微笑

傻子都能看懂的并查集

做题前必看上面链接的内容哦(大佬忽略)委屈

G - Ubiquitous Religions

There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding out how many different religions students in your university believe in.

You know that there are n students in your university (0 < n <= 50000). It is infeasible for you to ask every student their religious beliefs. Furthermore, many students are not comfortable expressing their beliefs. One way to avoid these problems is to ask m (0 <= m <= n(n-1)/2) pairs of students and ask them whether they believe in the same religion (e.g. they may know if they both attend the same church). From this data, you may not know what each person believes in, but you can get an idea of the upper bound of how many different religions can be possibly represented on campus. You may assume that each student subscribes to at most one religion.

Input
The input consists of a number of cases. Each case starts with a line specifying the integers n and m. The next m lines each consists of two integers i and j, specifying that students i and j believe in the same religion. The students are numbered 1 to n. The end of input is specified by a line in which n = m = 0.
Output
For each test case, print on a single line the case number (starting with 1) followed by the maximum number of different religions that the students in the university believe in.
Sample Input
10 9
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10 4
2 3
4 5
4 8
5 8
0 0

Sample Output





Case 1: 1
Case 2: 7

HintHuge input, scanf is recommended.

题目来源

#include <stdio.h>
int count;
int pre[100000];
int find(int x)//用来查找 x 的祖先
{
	 return pre[x] == x ? x : (pre[x] = find(pre[x]));//此三元表达式用递归来实现将有共同信仰的人连在一起 
}
void join(int x, int y)
{
   int fx = find(x);
   int fy = find(y);
   if(fx != fy)
   {
       	pre[fx] = fy;//也可以是pre[fy] = fx;只需要将两个根 
   }	
} 
int main()
{
	int peo;
	int book = 0;
	int num;
	while(scanf("%d %d", &peo, &num))
	{
	   ++book;
	   count = 0;
	   if(peo == 0 && num == 0)
	      break;
	   for(int i = 1; i <= peo; i++)
	       pre[i] = i;
	   for(int i = 0; i < num; i++)
	   {
	   	  int p1, p2;
	   	  scanf("%d %d", &p1, &p2);
	      join(p1, p2);	  
	   }
	   for(int i = 1; i <= peo; i++)
	      if(pre[i] == i)
		     ++count;
		printf("Case %d: %d\n", book, count);	
	}
	return 0; 
}

这个题坑的地方在于pre数组少一个0就报Runtime error。抓狂抓狂





上一典型题

A - 畅通工程

本题来源

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

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

Huge input, scanf is recommended.

解决思路和上题几乎一样。

#include <iostream>
using namespace std;

int pre[100000];
int count;
int find(int x)
{
	return pre[x] == x ? x : pre[x] = find(pre[x]); 
}

void join(int x, int y)
{
	int fx = find(x);
	int fy = find(y);
	
	if(fx != fy)
	   pre[fx] = fy;
}

int main()
{
	int n;
	int m;
	while(scanf("%d", &n))
	{
		count = 0;
		if(n == 0)
		   break;
		scanf("%d", &m);
		if(m == 0)
		{
		   printf("%d\n", n - 1);
		   continue;
	    }     
		for(int i = 1; i <= n; i++)
		    pre[i] = i;
		for(int i = 0; i < m; i++)
		{
			int a, b;
			scanf("%d %d", &a, &b);
			join(a, b);
		}
		for(int i = 1; i <= n; i++)
		   if(pre[i] == i)
		     ++count;
	    printf("%d\n", count - 1);//此处稍微有所改动
	}
	return 0;
}

这两个题都只能算是并查集的入门题,所以要想有更深的了解,大家还是需要多找题来练习。

谢谢观赏。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值