B1. Books Exchange (easy version)1000 / 并查集

Posted 幽殇默

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了B1. Books Exchange (easy version)1000 / 并查集相关的知识,希望对你有一定的参考价值。


https://codeforces.com/problemset/problem/1249/B1

#include<bits/stdc++.h> 
using namespace std;
const int N=1e5+10;
int a[N],p[N],cnt[N],n,t;
int find(int x)
{
	if(x!=p[x]) p[x]=find(p[x]);
	return p[x];
}
int main(void)
{
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i=1;i<=n;i++) p[i]=i,cnt[i]=1;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
			if(find(i)!=find(a[i])) cnt[find(i)]+=cnt[find(a[i])],p[find(a[i])]=find(i);
		}
		for(int i=1;i<=n;i++) cout<<cnt[find(i)]<<" ";
		cout<<endl;
	}
	return 0;
}

以上是关于B1. Books Exchange (easy version)1000 / 并查集的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces--Books Exchange (hard version)

B1. Social Network (easy version)1000 / 队列模拟

Codeforces Round #599 (Div. 2) B1. Character Swap (Easy Version)

博弈论B1. Palindrome Game (easy version)—— Codeforces Round #721 (Div. 2)

Codeforces Round #659 (Div. 2) B1. Koa and the Beach (Easy Version)

Codeforces Round #721 (Div. 2) - B1. Palindrome Game (easy version) - 对称博弈