Codeforces--Books Exchange (hard version)
题目链接http://codeforces.com/contest/1249/problem/B2 。并查集思想,将数分成多个集合,每个集合的大小就是一轮的所需天数。
Map[i]存储数据。
flag[i]来表示第i个数是否被访问过。
mm[i]记录第i个集合所对应的集合大小,索引i为第i个集合的根对应的值。
getParent方法利用递归的思想更新每个节点的上继,直接指向当前集合的根。
由于访问过的集合不再进行访问,因此,分析时间复杂度,准确是O(CN),C为一个常数。在这里需要注意的是,mm在每次使用后要清,不然会有问题。
#include<bits/stdc++.h> /*
* 并查集
*/ using namespace std; static const int MAX = ; int Map[MAX];
bool flag[MAX];
map<int, int> mm; int getParent(int x){
if(!flag[x]){
flag[x] = true;
Map[x] = getParent(Map[x]);
}
return Map[x];
} int main(){
int q;
scanf("%d", &q);
while(q--){
int n;
scanf("%d", &n);
for(int i=;i<=n;i++){
flag[i] = false;
} // construct set
int tmp;
for(int i=;i<=n;i++){
scanf("%d", &Map[i]);
} // solve
for(int i=;i<=n;i++){
int parent = getParent(i);
mm[parent]++;
} for(int i=;i<=n;i++){
if(i==){
printf("%d", mm[Map[i]]);
}
else{
printf(" %d", mm[Map[i]]);
}
}
printf("\n");
mm.clear();
}
return ;
}
Codeforces--Books Exchange (hard version)的更多相关文章
- CodeForces - 1214D B2. Books Exchange (hard version)
题目链接:http://codeforces.com/problemset/problem/1249/B2 思路:用并查集模拟链表,把关系串联起来,如果成环,则满足题意.之后再用并查集合并一个链,一个 ...
- Books Exchange (easy version) CodeForces - 1249B2
The only difference between easy and hard versions is constraints. There are nn kids, each of them i ...
- Books Exchange (hard version)
The only difference between easy and hard versions is constraints. There are nn kids, each of them i ...
- codeforces B. Ping-Pong (Easy Version) 解题报告
题目链接:http://codeforces.com/problemset/problem/320/B 题目意思:有两种操作:"1 x y" (x < y) 和 " ...
- codeforces#1251E2. Voting (Hard Version)(贪心)
题目链接: http://codeforces.com/contest/1251/problem/E2 题意: 主角需要获得n个人的投票 有两种方式让某个人投票 1,已经投票的人数大于m 2,花p枚硬 ...
- CodeForces - 320B Ping-Pong (Easy Version)
题目最开始 完全不懂 配合案例也看不懂-_- 总之就是用传递性 问能否从a区间到b区间 dfs(x,y) 走遍与第x区间所有的 联通区间 最后检验 第y区是否被访问过 是一道搜索好题 搜索还需加强 # ...
- CodeForces - 1183E Subsequences (easy version) (字符串bfs)
The only difference between the easy and the hard versions is constraints. A subsequence is a string ...
- CodeForces - 1183H Subsequences (hard version) (DP)
题目:https://vjudge.net/contest/325352#problem/C 题意:输入n,m,给你一个长度为n的串,然后你有一个集合,集合里面都是你的子序列,集合里面不能重复,集合中 ...
- Codeforces Round #595 (Div. 3)
A - Yet Another Dividing into Teams 题意:n个不同数,分尽可能少的组,要求组内没有两个人的差恰为1. 题解:奇偶分组. int a[200005]; void te ...
随机推荐
- MySQL中常用到的关于时间的SQL
-- 今天 SELECT DATE_FORMAT(NOW(),'%Y-%m-%d 00:00:00') AS dayStart;SELECT DATE_FORMAT(NOW(),'%Y-%m-%d 2 ...
- docker部署jenkins
步骤一: 查找jenkins镜像(也可以直接去jenkins官网找镜像docker pull jenkins/jenkins)(官方版本文档https://hub.docker.com/_/jenki ...
- ascii codec can't decode byte 0xe8 in position 0:ordinal not in range(128)---python中文编码问题
解决方案一:将如下部分加在报错的py文件里 import sys reload(sys) sys.setdefaultencoding('utf-8')
- 深入理解perf报告中的swapper进程
一.前言 1.在perf监控进程的系统调用时,会出现大量swapper进程 2.官方描述该进程是当CPU上没有其他任务运行时,就会执行swapper.换句话说swapper意味着CPU啥事也没干,跑去 ...
- 性能测试的基础知识--QPS和TPS
基本概念: QPS:Queries Per Second意思是“每秒查询率” ,是一台服务器每秒能够相应的查询次数,是对一个特定的查询服务器在规定时间内所处理流量多少的衡量标准. TPS:Transa ...
- Springboot2.x整合RabbitMQ
1.RabbitMQ介绍 可参照RabbitMQ笔记 2.接入配置 pom依赖 <!--amqp依赖--> <dependency> <groupId>org.sp ...
- Linux 文件或文件夹重命名命令mv
使用命令mv既可以重命名,又可以移动文件或文件夹.例如: 1.将目录A重命名为B mv A B 2.将/a目录移动到/b下,并重命名为c mv /a /b/c 3.将一个名为abc的文件重命名为123 ...
- 【钢琴伴奏基本形态和伴奏织体】技能 get
开头重复一句话:做编曲就是设计和声+伴奏织体. ---- --------- --------------- 分解和弦: 半分解和弦: 做法:在旋律的下方演奏一些和弦的音.就能让音乐更加的饱满,拒绝空 ...
- js 验证数据类型的4中方法
1.typeof 可以检验基本数据类型 但是引用数据类型(复杂数据类型)无用: 总结 : typeof 无法识别引用数据类型 包括 bull; 2.instanceof是一个二元运算符,左操作数 ...
- CSS动画,2D和3D模块
CSS3提供了丰富的动画类属性,使我们可以不通过flash甚至JavaScript,就能实现很多动态的效果.它们主要分为三大类:transform(变换),transition(过渡),animati ...