首页 > 其他分享 >Round Dance

Round Dance

时间:2024-06-06 22:36:48浏览次数:14  
标签:le people Dance number round each test Round

题目信息

题目链接

Luogu CF1833ECodeforces 1833E

题面翻译

有 \(n\) 个人围成若干个圈(也有可能只有一个)跳舞,每个圈至少有 \(2\) 个人。在圈中,每个人都与 \(2\) 个人相邻。特殊地,如果圈里只有 \(2\) 个人,则实际上只与一个人相邻。

现在,每个人都只记得与自己相邻的人之一,你需要给出这些人围成的圈数的最小值和最大值。

题目描述

$ n $ people came to the festival and decided to dance a few round dances. There are at least $ 2 $ people in the round dance and each person has exactly two neighbors. If there are $ 2 $ people in the round dance then they have the same neighbor on each side.

You decided to find out exactly how many dances there were. But each participant of the holiday remembered exactly one neighbor. Your task is to determine what the minimum and maximum number of round dances could be.

For example, if there were $ 6 $ people at the holiday, and the numbers of the neighbors they remembered are equal $ [2, 1, 4, 3, 6, 5] $ , then the minimum number of round dances is $ 1 $ :

  • $ 1 - 2 - 3 - 4 - 5 - 6 - 1 $

and the maximum is $ 3 $ : - $ 1 - 2 - 1 $

  • $ 3 - 4 - 3 $
  • $ 5 - 6 - 5 $

输入格式

The first line contains a positive number $ t $ ( $ 1 \le t \le 10^4 $ ) — the number of test cases. The following is a description of the test cases.

The first line of the description of each test case contains a positive number $ n $ ( $ 2 \le n \le 2 \cdot 10^5 $ ) — the number of people at the holiday.

The second line of the description of each test case contains $ n $ integers $ a_i $ ( $ 1 \le a_i \le n, a_i \neq i $ ) — the number of the neighbor that the $ i $ th person remembered.

It is guaranteed that the test cases are correct and corresponds to at least one division of people into round dances.

It is guaranteed that the sum of $ n $ for all test cases does not exceed $ 2 \cdot 10^5 $ .

输出格式

For each test case, output two integers — the minimum and maximum number of round dances that could be.

样例 #1

样例输入 #1

10
6
2 1 4 3 6 5
6
2 3 1 5 6 4
9
2 3 2 5 6 5 8 9 8
2
2 1
4
4 3 2 1
5
2 3 4 5 1
6
5 3 4 1 1 2
5
3 5 4 1 2
6
6 3 2 5 4 3
6
5 1 4 3 4 2

样例输出 #1

1 3
2 2
1 3
1 1
1 2
1 1
1 1
2 2
1 2
1 1

思路分析

一道很好的带权并查集。

记录:\(circle_i\) 表示 \(i\) 的并查集中是否已经出现了环,\(sz_i\) 表示 \(i\) 的并查集的大小。

最多的肯定是连通块的数量,最少的只需要让不是环的相连即可。

代码

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+10;
//DSU
int T,n,near[MAXN];
bool circle[MAXN];
int fath[MAXN],sz[MAXN];
int get_father(int x){
	if(fath[x]==x) return x;
	return fath[x] = get_father(fath[x]);
}
void clear(){
	for(int i = 1;i<=n;i++){
		fath[i] = i;
		circle[i] = 0;
		sz[i] = 1;
	}
}
signed main(){
	scanf("%d",&T);
	while(T--){
		scanf("%d",&n);
		clear();
		for(int i = 1;i<=n;i++){
			scanf("%d",near+i);
		}
		for(int i = 1;i<=n;i++){
			int fn = get_father(near[i]);
			int f = get_father(i);
			if(f!=fn){
				fath[fn] = f;
				circle[f] = circle[f]|circle[fn];
				sz[f] += sz[fn];
			}else{
				if(near[near[i]]!=i) circle[fn] = 1;
			}
		}
		int sum_circle = 0,sum = 0;
		for(int i = 1;i<=n;i++){
			if(get_father(i)==i){
				sum++;
				if(circle[i]&&sz[i]!=2){
					sum_circle++;
				}
			}
		}
		printf("%d %d\n",min(sum,sum_circle+1),sum);
	}
	return 0;
}

标签:le,people,Dance,number,round,each,test,Round
From: https://www.cnblogs.com/gutongxing/p/18236192

相关文章

  • codeforces 1442 D Codeforces Round 681 (Div. 1, based on VK Cup 2019-2020 - Fina
    链接大意就是给你n组物品,这n组物品里面每组有\(t_i\)个,且他们是按照价值不降的顺序排列的。现在允许取k个物品,每个物品必须取在数组的开头处,每个物品在被取用后就会消失。问你最大能够拿到多少价值的物品。其中\(n,k\leq1500,\sumt_i\leq1e6,a_i\leq1e8\)很背包吧。可......
  • GCD-sequence(Round 950)
    #include<bits/stdc++.h>#defineendl'\n'usingll=longlong;typedefunsignedlonglongull;usingnamespacestd;voidGordenGhost();signedmain(){#ifdefGordenfreopen("in.txt","rt",stdin);freopen......
  • Codeforces Round 949题解(A、B、C)
    A.TurtleandPiggyArePlayingaGame首先\(p\)选\(2\)的话除得最慢,得的分多。考虑二进制表示,如果\(x=(1000000000)_{bin}\),则每次除以\(2\)都是相当于右移一位,除完之后仍然是\(2\)的倍数,变成\(1\)的步数就是把最高位的\(1\)移动到\(0\)位的步数。因为\(2l\ler\),所以\(l......
  • Codeforces Round 950 (Div. 3)个人题解(A~F1)
    CodeforcesRound950(Div.3)个人题解(A~F1)题解火车头#define_CRT_SECURE_NO_WARNINGS1#include<iostream>#include<vector>#include<algorithm>#include<set>#include<unordered_map>#include<cstring>#include<cstdio&......
  • T461430 「Daily OI Round 4」Mine
    T461430「DailyOIRound4」MineT461430「DailyOIRound4」Mine解题思路首先,有个简单的想法就是我们考虑选择的那个采矿点是谁,但是我们发现,如果直接算,会重复,比如采矿点\(A\)和采矿点\(B\)所能采集的线段集合如果有交,显然会方案数会重复。这里学到一个计数的技巧:考......
  • Codeforces Round 949 (Div. 2) 中文题解
    A对于一个特定的\(x\),Piggy总是会选择\(p\)使得\(p\)是质数,所以分数就是\(x\)的质因子个数。容易发现至少有\(t\)个质因子的数是\(2^t\)。最大的满足\(2^t\ler\)的整数\(t\)是\(\left\lfloor\log_2r\right\rfloor\)。又因为\(2l\ler\),所以\(\log_2l+......
  • Codeforces Round 950 (Div. 3)
    https://codeforces.com/contest/1980A.ProblemGenerator题意:Thereisgoingtobemroundsnextmouth,eachofthemonthshouldbeconsistof"ABCDEFG",countthenumebrofalphabetweshouldaddtosatisfythisrequirementunderagivingsequenc......
  • 每日练习——牛客周赛 Round 45
    小紫的总分题目描述登录—专业IT笔试面试备考平台_牛客网运行代码#include<iostream>usingnamespacestd;intmain(){inta,b,c,d,e,sum;cin>>a>>b>>c>>d>>e;sum=a+b+c+d+e;if(sum>100){cout<<"YES";}else......
  • Educational Codeforces Round 166 (Rated for Div. 2)
    A.VerifyPassword题目描述Monocarpisworkingonhisnewsite,andthecurrentchallengeistomaketheuserspickstrongpasswords.Monocarpdecidedthatstrongpasswordsshouldsatisfythefollowingconditions:passwordshouldconsistonlyoflowerc......
  • 牛客周赛 Round 3
    D游游的矩阵权值题目描述游游定义一个矩阵权值为:每一对相邻元素之和的总和。例如,对于矩阵:1234它的权值是(1+2)+(1+3)+(2+4)+(3+4)=3+4+6+7=20。游游希望你构造一个\(n*n\)的矩阵,矩阵中的元素为1到\(n^2\)且每个数恰好出现一次。她希望最终矩阵的权值尽可能大。你能帮帮......