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 ...
随机推荐
- js/jquery去掉空格,回车,换行
Jquery:$("#accuracy").val($("#accuracy").val().replace(/\ +/g,""));//去 ...
- 解决微信小程序开发中wxss中不能用本地图片
微信小程序开发中wxss中不能用本地图片,我们可以用将我们的图片传到服务器上,然后直接引用在线地址.但是当我们没有服务器时,我们可以用"图床",这个具体可以百度.这里我们用第二种方 ...
- 2018年蓝桥杯ava b组第一题
第一题.标题:第几天 2000年的1月1日,是那一年的第1天.那么,2000年的5月4日,是那一年的第几天? 注意:需要提交的是一个整数,不要填写任何多余内容 如果问我怎么做的,我就是看日历做的,看了 ...
- FastDfs之TrackerServer的详细配置介绍
# is this config file disabled # false for enabled # true for disabled disabled=false #当前配置是否不可用fals ...
- 认识MongoDB复制集
从这一篇开始,我们要踏上MongoDB进阶之路啦,想想还有点小开心呢.一筐猪镇楼. 引入复制集 我们先来想一个场景,如果本地项目使用MongoDB,都是下载,安装,连接一条龙服务.这实际也就是 ...
- 第三方日志库logrus使用
日志是程序中必不可少的一个环节,由于Go语言内置的日志库功能比较简洁,我们在实际开发中通常会选择使用第三方的日志库来进行开发.本文介绍了logrus这个日志库的基本使用. logrus介绍 Logru ...
- hadoop之yarn详解(框架进阶篇)
前面在hadoop之yarn详解(基础架构篇)这篇文章提到了yarn的重要组件有ResourceManager,NodeManager,ApplicationMaster等,以及yarn调度作业的运行 ...
- Js获取宽高度的归纳集锦总结
首先,先吓唬一下我们的小白们!在js中的描述宽高的可以细分有22种.属性根据不同的兼容性也分为五种 window.innerWidth //除去菜单栏的窗口宽度,与浏览器相关 window.inner ...
- 初识Django,了解一下大概流程
学习Django一个礼拜了,对其有了一个大概的了解,自己画了一个简单的图,虽然有点丑,但是基本上已经把自己所想已经表达 写完这篇随笔之后发现自己逻辑表述的有点不太清晰,有点乱,哪里不对,希望各位指正 ...
- Airport Express UVA - 11374
In a small city called Iokh, a train service, Airport-Express, takes residents to the airport more q ...