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 ...
随机推荐
- Matlab 模拟退火算法模型代码
function [best_solution,best_fit,iter] = mySa(solution,a,t0,tf,Markov) % 模拟退化算法 % ===== 输入 ======% % ...
- linux下安装node.js时npm无法使用
安装node.js 10.15.1版本时,安装完在node的安装目录下执行./node -v查看node版本,成功则表示node已安装成功 [root@localhost bin]# ./node - ...
- 基于Spark的电影推荐系统(实战简介)
写在前面 一直不知道这个专栏该如何开始写,思来想去,还是暂时把自己对这个项目的一些想法 和大家分享 的形式来展现.有什么问题,欢迎大家一起留言讨论. 这个项目的源代码是在https://github. ...
- Android adb shell am 命令学习(1)
am:activity manager 启动Activity,打开或关闭进程,发送广播等操作 为什么学习: 主要应用部分,后台启动对应的package的Activity adb shell am st ...
- linux centos安装zabbix 4.0服务端
1.服务器安装docker sudo yum install -y yum-utils device-mapper-persistent-data lvm2 sudo yum-config-manag ...
- mysql 版本引起的 utf8mb4 问题(linux centos6.9下升级mysql)
文字输入时候存在火星文或者表情,insert到低版本的mysql中一般会报错,所以我们需要将mysql版本升级到5.5.3及以上,高版本的mysql为我们提供了utf8mb4的编码,解决了这些复杂数据 ...
- linux下mysql数据库操作命令
1:启动服务 service mysqld start (5.0版本是mysqld) service mysql start (5.5.7版本是mysql) 2:停止服务 service mysqld ...
- .Net Core快速创建Windows服务
1.新建.Net Core控制台程序,添加新建项Windows服务: NuGet引用 System.ServiceProcess.ServiceController,然后修改Progran.cs: c ...
- mysql创建用户和权限管理
一 权限管理 我们知道我们的最高权限管理者是root用户,它拥有着最高的权限操作.包括select.update.delete.update.grant等操作.那么一般情况在公司之后DBA工程师会创建 ...
- 夯实Java基础系列21:Java8新特性终极指南
本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial 喜欢的话麻烦点下 ...