2021-1-21并查集

并查集

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

#include<iostream>
using namespace std;
#define endl "\n"
int n,m,f[50010],sum;
void init(){
    int i;
    for(i=1;i<=n;i++){
        f[i]=i;
    }
}
int getf(int v){
    if(f[v]==v) {
        return v;
    }
    else{
        f[v]=getf(f[v]);
        return f[v];
    }
}
void Union(int v,int u){
    int t1,t2;
    t1=getf(v);
    t2=getf(u);
    if(t1!=t2)
    {
        f[t2]=t1;
    }
}
int main(){
    int x,y,o=1;
    while(cin>>n>>m&&(n||m)){
    	sum=0;
    	init();
    	for(int i=1;i<=m;i++){
    		cin>>x>>y;
    		Union(x,y);
		}
		for(int i=1;i<=n;i++){
			if(f[i]==i) sum++;
		}
		cout<<"Case "<<o<<": "<<sum<<endl;
		o++;
	}
	return 0;
}

F - How Many Answers Are Wrong

TT and FF are … friends. Uh… very very good friends -________-b

FF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. To begin with, TT should write down a sequence of integers-_-!!(bored).

Then, FF can choose a continuous subsequence from it(for example the subsequence from the third to the fifth integer inclusively). After that, FF will ask TT what the sum of the subsequence he chose is. The next, TT will answer FF’s question. Then, FF can redo this process. In the end, FF must work out the entire sequence of integers.

BoringBoringa very very boring game!!! TT doesn’t want to play with FF at all. To punish FF, she often tells FF the wrong answers on purpose.

The bad boy is not a fool man. FF detects some answers are incompatible. Of course, these contradictions make it difficult to calculate the sequence.

However, TT is a nice and lovely girl. She doesn’t have the heart to be hard on FF. To save time, she guarantees that the answers are all right if there is no logical mistakes indeed.

What’s more, if FF finds an answer to be wrong, he will ignore it when judging next answers.

But there will be so many questions that poor FF can’t make sure whether the current answer is right or wrong in a moment. So he decides to write a program to help him with this matter. The program will receive a series of questions from FF together with the answers FF has received from TT. The aim of this program is to find how many answers are wrong. Only by ignoring the wrong answers can FF work out the entire sequence of integers. Poor FF has no time to do this job. And now he is asking for your help~(Why asking trouble for himself~~Bad boy)
Input
Line 1: Two integers, N and M (1 <= N <= 200000, 1 <= M <= 40000). Means TT wrote N integers and FF asked her M questions.

Line 2…M+1: Line i+1 contains three integer: Ai, Bi and Si. Means TT answered FF that the sum from Ai to Bi is Si. It’s guaranteed that 0 < Ai <= Bi <= N.

You can assume that any sum of subsequence is fit in 32-bit integer.
Output
A single line with a integer denotes how many answers are wrong.
Sample Input
10 5
1 10 100
7 10 28
1 3 32
4 6 41
6 6 1
Sample Output
1

#include<stdio.h>
#define endl "\n"
long long f[200010], r[200010];
void init(int i){
    f[i] = i;
    r[i] = 0;
}
long long getf(long long v){
	long long t;
    if(f[v] == v) {
        return v;
    }
    else{
        t = getf(f[v]);
        r[v] += r[f[v]];
        f[v] = t;
        return f[v];
    }
}
int main(){
    long long x, y, o, ra, rb, sum;
    int n, m;
   	while(~scanf("%d%d",&n,&m)){
   		sum = 0;
	   	for(int i = 1; i<=n+1; i++){
		   init(i);
		}
	   	while(m--){
	   	 	scanf("%lld%lld%lld",&x,&y,&o);
	   	 	y++;
	   	 	ra = getf(x); rb = getf(y);
	   	 	if(ra == rb) {
	   	 		if(r[y] - r[x] != o) sum++;
			} 
			else {
				f[rb] = ra;
				r[rb] = r[x] - r[y] +o;
			}
	    }
		printf("%lld\n",sum);
	}
   	return 0;
}

初始化

每个刚开始都是爹

void init(){
    int i;
    for(i=1;i<=n;i++){
        f[i]=i;
    }
}

找爹(直接到根部)

int getf(int v){
    if(f[v]==v) {
        return v;
    }
    else{
        f[v]=getf(f[v]);
        return f[v];
    }
}

合并

就如果爹不同 就认大爹

void Union(int v,int u){
    int t1,t2;
    t1=getf(v);
    t2=getf(u);
    if(t1!=t2)
    {
        f[t2]=t1;
    }
}

感觉这篇文章讲的很通俗
https://blog.csdn.net/weixin_30855761/article/details/97604505

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值