传送门:无间道之并查集

并查集的分析可以看上面的传送门,写的挺好的了。
其实在我看来并查集就是一种方便的维护集合的一种技巧,提出了代表元素这一概念。

My AC Code

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=1e5+5;

int represent[maxn];

int find_represent(int x)
{
	if(x == represent[x])
	{
		return x;
	}
	else
	{
		represent[x]=find_represent(represent[x]);
		return represent[x];
	}
	
}
int main()
{
	int n;
	scanf("%d",&n);
	int temp;
	int index=1;
	map<string,int> map_str;
	for(int i=1;i<maxn;i++)
	{
		represent[i]=i;
	}
	for(int i=0;i<n;i++)
	{
		scanf("tmep");
		string sa,sb;
		scanf("%d",&temp);
		cin>>sa>>sb;
		if(map_str[sa]==0)
			map_str[sa]=index++;
		if(map_str[sb]==0)
			map_str[sb]=index++;
		int ia=map_str[sa];
		int ib=map_str[sb];
		
		int ra=find_represent(ia);
		int rb=find_represent(ib);
		if(temp)
		{
			if(ra==rb)
			{
				printf("yes\n");
			}
			else
			{
				printf("no\n");
			}
		}
		else
		{		
			represent[ra]=rb;
		}
		
	}
    return 0;
}

相关文章:

  • 2021-07-31
  • 2021-10-08
  • 2021-07-31
  • 2021-06-15
  • 2021-07-08
  • 2021-09-10
  • 2022-12-23
  • 2021-06-01
猜你喜欢
  • 2021-08-01
  • 2022-12-23
  • 2022-02-20
  • 2021-12-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案