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 ...
随机推荐
- 给idea设置默认使用的maven配置
一,前言 大家都知道,java开发中最经常使用的开发工具是Maven,最近看新同事在使用idea,我也下载了一个,准备尝试一下. 而maven是非诚方便进行工程管理的,至少管理jar包,是非常方便的, ...
- Scala函数式编程(三) scala集合和函数
前情提要: scala函数式编程(二) scala基础语法介绍 scala函数式编程(二) scala基础语法介绍 前面已经稍微介绍了scala的常用语法以及面向对象的一些简要知识,这次是补充上一章的 ...
- jenkins导致磁盘占满问题
背景 今天登陆jenkins提示磁盘空间不足,且构建发生错误 排查问题 cd到jenkins 安装目录 执行df -h 发现root目录沾满 执行 du -ah --max-depth=1 发现是.j ...
- Spring MVC-从零开始-@ResponseBody(未完待续)
Spring MVC-从零开始-@ResponseBody(未完待续)
- 如何评价一个VR体验设计?
如何评价一个VR系统的体验是好是坏?或者说,哪些因素会破坏一个VR的体验? Kruij和Riecke教授在IEEE VR会议上提到了四个角度:Congnition,Game User Experien ...
- ueditor的初始化赋值
ue.ready(function () {ue.setContent('初始内容'); //赋值给UEditor });
- 01、JDBC连接
① 下载JDBC-MySQL数据库驱动 链接:https://pan.baidu.com/s/1KtSZZ5hXlxu2QrmsXg3lkA 提取码:1pbl ② 加载JDBC-MySQL数据库驱动 ...
- NAT网络下tcp_tw_recycle参数引起的故障
记录一次阿里云服务器故障排查思路 公司网络是nat 环境 问题: 同一个服务有两台服务器 172.19.19.252 172.19.19.187 两台服务器 要连node5 发现172.19.19.2 ...
- Spark 学习笔记之 共享变量
共享变量: 共享变量通常情况下,当向Spark操作(如map,reduce)传递一个函数时,它会在一个远程集群节点上执行,它会使用函数中所有变量的副本.这些变量被复制到所有的机器上,远程机器上并没有被 ...
- ELK 学习笔记之 elasticsearch Mget操作
Mget操作: 查询多个文档: curl -XGET 'http://192.168.1.151:9200/_mget' -d '{"docs": [{"_index&q ...