//种类并查集,设置一个time数组表示次数
//AC代码如下:
//Created Author: just_sort
//Created Time : 1015/1/15 15:51
//File Name : Dragon Balls
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define Read(x) scanf("%d",&x)
#define maxn 10010
int fa[maxn],num[maxn],time[maxn];
void init()
{
for(int i=0;i<maxn;i++)
{
fa[i]=i;
num[i]=1;
time[i]=0;
}
}
int Find(int x)
{
if(x==fa[x])return x;
int fx=Find(fa[x]);
time[x]+=time[fa[x]];
return fa[x]=fx;
}
void union_set(int x,int y)
{
int fx=Find(x);
int fy=Find(y);
if(fx!=fy)
{
fa[fx]=fy;
num[fy]+=num[fx];
time[fx]++;
}
}
int main()
{
char s[10];
int tt,N,Q,x,y,ncase=1;
Read(tt);
while(tt--)
{
init();
Read(N);Read(Q);
printf("Case %d:\n",ncase++);
while(Q--)
{
scanf("%s",s);
getchar();
if(s[0]=='T')
{
Read(x);Read(y);
union_set(x,y);
}
else
{
Read(x);
y=Find(x);
printf("%d %d %d\n",y,num[y],time[x]);
}
}
}
return 0;
}
Hdoj 2635 Dragon Balls
最新推荐文章于 2023-03-06 09:47:22 发布
本文介绍了一种使用并查集解决特定问题的算法实现方法。通过一个具体的编程实例,展示了如何利用并查集来处理元素之间的关系,并实现查找与合并操作。文章提供了完整的AC代码,包括初始化、查找和集合合并等功能。
3万+

被折叠的 条评论
为什么被折叠?



