Luo Gu [explanations] P1551 relatives

The first hair problem solution

Original title Portal

This problem is probably intended:
to give three numbers n, m, p, a total of 1 ... n individual, to the relationship between (a, b) relative to a given set of m, for example 1 and 2 relatives, 1 and 3 is relatives, then 2 and 3 are also relatives. Finally, p people, asked them not relatives, but also relatives and relatives of relatives, so this question is to use disjoint-set, merge title known relatives.

The following code is attached to

#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<algorithm>
 //头文件
using namespace std;
int n,m,p,a,b;                     
int c,d,f[500001];
void init()
{
	for(int i=1;i<=n;i++)
	{
		f[i]=i;
	}
}
//赋值函数,先假定每个人的亲戚都是自己
int getf(int k)
{
	if(f[k]==k)
	{
		return k;
	}
	else
	{
		return getf(f[k]);
	}
}
//寻找函数,如果给定的a,b相同或者a,b有亲戚关系,那么返回出b的亲戚就是a,把a赋值给b,方便后来判断;
void merge(int x,int y)
{
	int t1=getf(x);
	int t2=getf(y);
	if(t1!=t2)
	{
		f[t2]=t1;
	}
	return;
}
//合并函数,将两个亲戚合并,相当于把b的亲戚标上了a;
int main()
{
	cin>>n>>m>>p;
	//输入
	init();
	//调用函数
	for(int i=1;i<=m;i++)
	{
		cin>>a>>b;
		merge(a,b);
	}
	//输入亲戚关系,合并亲戚关系
	for(int i=1;i<=p;i++)
	{
		cin>>c>>d;
		//输入询问亲戚
		c=getf(c);
		d=getf(d);
		依旧是调用寻找函数;
		if(c==d)
		{
			cout<<"Yes"<<endl;
		}
		//寻找函数如果两个是亲戚,数字就相同,如果相同,就输出"Yes";
		else
		{
			cout<<"No"<<endl;
		}
		//不一样就输出"No";
	}
	return 0;
}

Now it piece by piece

#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<algorithm>

Header files, learn C ++ and C knows ............

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

This is a function of the assignment, assuming that everyone is only the beginning of a relative, that is their own ......

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

Function to find relatives, if given the two people are the same, then they are their own relatives, it returns a value, if not the same, do not know is not relative, then continue to identify a given number of relatives until he finally found relatives, and in the process, all to be looked for if the final number of relatives and relatives, then put the final number assigned to a number of relatives of the course to be found, so it will be very final judgment Convenience……

void merge(int x,int y)
{
	int t1=getf(x);
	int t2=getf(y);
	if(t1!=t2)
	{
		f[t2]=t1;
	}
	return;
}

This should be best understood ...... given two numbers, ultimately relatives find each number, if not the same, then assign the value of a b, this is also easy to determine if the same, then do not merge a direct exit ......

int main()
{
	cin>>n>>m>>p;
	init();
	for(int i=1;i<=m;i++)
	{
		cin>>a>>b;
		merge(a,b);
	}
	for(int i=1;i<=p;i++)
	{
		cin>>c>>d;
		c=getf(c);
		d=getf(d);
		if(c==d)
		{
			cout<<"Yes"<<endl;
		}
		else
		{
			cout<<"No"<<endl;
		}
	}
	return 0;
}

The main function, do not say it %%% ...... %%%
enter the number, relative number of relations, to determine the number of relatives, and then call the function assigned to the input relatives, merge, and then enter the judgment, if two the final number of relatives is the same, then that relatives, otherwise it is not.

The perfect ending

The perfect ending

The perfect ending

Guess you like

Origin blog.csdn.net/tidongCrazy/article/details/90244884