CodeForces - 1214D B2. Books Exchange (hard version)
题目链接:http://codeforces.com/problemset/problem/1249/B2
思路:用并查集模拟链表,把关系串联起来,如果成环,则满足题意。之后再用并查集合并一个链,一个链代表
一个集合,一个集合有共同的祖先,一个集合答案相同,则输出答案时候只需要输出该元素属于哪一个集合,那个集合有几个元素
就可以了。
#include <stdio.h>
#include <iostream>
using namespace std; const int N = (int)1e5+;
int fa[N];
int ans[N];
int root[N]; int search(int x,int& deep){ ++deep;
if(fa[x] == x) return root[x] = x;
else{
return root[x] = search(fa[x],deep);
}
} int main(){ int q;
scanf("%d",&q); int n,in;
while(q--){
scanf("%d",&n); for(int i = ; i <= n; i++){
fa[i] = root[i] = i;
ans[i] = ;
} int deep = ;
for(int i = ; i <= n; i++){
scanf("%d",&in);
deep = ;
if(search(in,deep) == i){
ans[i] = deep;
}
else fa[i] = in;
} printf("%d",ans[root[]]);
for(int i = ; i <= n; i++) printf(" %d",ans[root[i]]);
printf("\n");
} return ;
}
CodeForces - 1214D B2. Books Exchange (hard version)的更多相关文章
- 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 Round #599 (Div. 2) B2. Character Swap (Hard Version) 构造
B2. Character Swap (Hard Version) This problem is different from the easy version. In this version U ...
- [Codeforces 1214A]Optimal Currency Exchange(贪心)
[Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...
- [Codeforces 1214D]Treasure Island(dfs)
[Codeforces 1214D]Treasure Island(dfs) 题面 给出一个n*m的字符矩阵,'.'表示能通过,'#'表示不能通过.每步可以往下或往右走.问至少把多少个'.'变成'#' ...
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) B2. TV Subscriptions (Hard Version)
链接: https://codeforces.com/contest/1247/problem/B2 题意: The only difference between easy and hard ver ...
- Codeforces Round #590 (Div. 3) B2. Social Network (hard version)
链接: https://codeforces.com/contest/1234/problem/B2 题意: The only difference between easy and hard ver ...
- Codeforces Round #599 (Div. 2) B2. Character Swap (Hard Version)
This problem is different from the easy version. In this version Ujan makes at most 2n2n swaps. In a ...
- Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力
Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...
随机推荐
- SpringBoot区块链之以太坊开发(整合Web3j)
最近公司需要ETH兑换功能,ETH转账需要区块打包,这个时候就需要区块检测,目前只是简单整合,后面会将区块自动检测代码上传致QQ群 对于区块链开发不太熟悉的童鞋,可以看看:[区块链开发(零)如何开始学 ...
- SpringBoot区块链之以太坊区块高度扫描(简洁版)
继续昨天的demo往下写写:[SpringBoot区块链之以太坊开发(整合Web3j)](https://juejin.im/post/5d88e6c1518825094f69e887),将复杂的逻辑 ...
- 使用回车键执行input框事件
html: <input type="text" class="search-data-input" placeholder="请输入关键词&q ...
- IDEA加密算法(含所需jar包(commons-codec-1.11.jar ,bcprov-jdk15on-160.jar))
软件设计上机实验IDEA算法: import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.Se ...
- CocosCreator实现动物同化
获取源码 关注微信公众号『一枚小工 』,发送『动物同化 』获取完整游戏源码. 游戏玩法 游戏目标是将游戏区域的动物全部同化成同一种动物.游戏从左上角开始,从右边点击需要变成的目标动物头像,如果被同化动 ...
- 环境搭建-ELK单节点环境搭建(02)
写在前面 常说:"工欲善其事必先利其器",这话想想也是一点毛病也没有,在开始学习任何技术之前,我们总得有一个实际可供操作的实验环境.有人说,"看十遍不如用一遍" ...
- hadoop入门之海量Web日志分析 用Hadoop提取KPI统计指标
转载自:http://blog.fens.me/hadoop-mapreduce-log-kpi/ 今天学习了这一篇博客,写得十分好,照着这篇博客敲了一遍. 发现几个问题, 一是这篇博客中采用的had ...
- grep、正则表达式
1.grep :文本搜索工具 -i:忽略大小写--color:匹配到的内容高亮显示-v:显示没有被模式匹配到的行-o:只显示被模式匹配到的字符串-n:显示匹配到行的行号-E:使用扩展正则表达式-A:后 ...
- python import cv2异常(dll load fail / windows server 2008)
最近服务器迁移,从得win7系统迁移到云服务器器上的windows server2008系统,迁移过程中安装Python的opencv一直出错,不管是用whl安装或者是在线pip安装都报错,尝试打包成 ...
- JavaScript系列:高级函数篇
前言: 本篇主要是介绍 JavaScript使用函数的高级方法,函数是JavaSCript中最有趣的部分,利用Function特性可以编写出很多非常有意思的代码,本篇主要包括:函数回调,高阶函数以及函 ...