有几天没做了,hhhh~~~
Five hundred years later, the number of dragon balls will increase unexpectedly, so it's too difficult for Monkey King(WuKong) to gather all of the dragon balls together.
His country has N cities and there are exactly N dragon balls in the world. At first, for the ith dragon ball, the sacred dragon will puts it in the ith city. Through long years, some cities' dragon ball(s) would be transported to other cities. To save physical strength WuKong plans to take Flying Nimbus Cloud, a magical flying cloud to gather dragon balls.
Every time WuKong will collect the information of one dragon ball, he will ask you the information of that ball. You must tell him which city the ball is located and how many dragon balls are there in that city, you also need to tell him how many times the ball has been transported so far.
Input
The first line of the input is a single positive integer T(0 < T <= 100).
For each case, the first line contains two integers: N and Q (2 < N <= 10000 , 2 < Q <= 10000).
Each of the following Q lines contains either a fact or a question as the follow format:
T A B : All the dragon balls which are in the same city with A have been transported to the city the Bth ball in. You can assume that the two cities are different.
Q A : WuKong want to know X (the id of the city Ath ball is in), Y (the count of balls in Xth city) and Z (the tranporting times of the Ath ball). (1 <= A, B <= N)
Output
For each test case, output the test case number formated as sample output. Then for each query, output a line with three integers X Y Z saparated by a blank space.
Sample Input
2
3 3
T 1 2
T 3 2
Q 2
3 4
T 1 2
Q 1
T 1 3
Q 1
Sample Output
Case 1:
2 3 0
Case 2:
2 2 1
3 3 2
思路:
- T a b 代表将a所在的集合挂在b上,
- Q a总共询问3个信息:
- ①a所在的集合
- ②a所在的集合中元素的个数;
- ③a移动的次数:
- 设一个Move[]数组,那么需要在路径压缩的时候维护它,比如说 T 1 2, T 1 3
- 当1和3合并的时候,实际上是f[2]=3;Move[2]++;此时Move[1]仍然等于1,
- 所以当我们查找1时,首先会找的1的祖先是2,
- 这时令Move[1]+=Move[2];即向上回溯祖先是,
- 要将转移次数加给子孙。然后会找到2的祖先是3.
#include <iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int maxn=1e5+10;
int f[maxn];//记录父节点
int sum[maxn];//记录所在城市共有多少龙珠
int Move[maxn];//记录移动的次数
int n,m;
void init_set()
{
for(int i=1; i<=n; i++)
{
f[i]=i;
sum[i]=1;
Move[i]=0;
}
}
int find_set(int x)
{
if(x!=f[x])
{
int t=f[x];
f[x]=find_set(f[x]);//压缩路径 ,都指向根节点
Move[x]+=Move[t];//每个球移动的次数等于本身移动的个数加上父节点移动的次数
return f[x];
}
return x;
}
void unit_set(int x,int y)
{
x=find_set(x);
y=find_set(y);
if(x==y)return;
f[x]=y;//移动一个根节点到另一个根节点
sum[y]+=sum[x];//fy为根节点的总个数等于两个根节点拥有的个数相加
Move[x]++;//被移动的根节点第一次移动
}
int main()
{
int t;
int l=0;
scanf("%d",&t);
while(t--)
{
l++;
printf("Case %d:\n",l);
scanf("%d%d",&n,&m);
init_set();
for(int i=0; i<m; i++)
{
char s[6];
scanf("%s",s);
if(s[0]=='T')
{
int x,y;
scanf("%d%d",&x,&y);
unit_set(x,y);
}
else if(s[0]=='Q')
{
int x;
scanf("%d",&x);
int ans=find_set(x);//要输出根节点的所有的个数
printf("%d %d %d\n",ans,sum[ans],Move[x]);
}
}
}
return 0;
}
本文介绍了一个基于并查集数据结构的算法,用于解决孙悟空在众多城市中高效收集龙珠的问题。通过路径压缩和集合合并策略,算法能快速响应龙珠位置变化及数量统计,适用于动态变化的场景。
11万+

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



