题目链接: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)的更多相关文章

  1. Books Exchange (easy version)   CodeForces - 1249B2

    The only difference between easy and hard versions is constraints. There are nn kids, each of them i ...

  2. Books Exchange (hard version)

    The only difference between easy and hard versions is constraints. There are nn kids, each of them i ...

  3. 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 ...

  4. [Codeforces 1214A]Optimal Currency Exchange(贪心)

    [Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...

  5. [Codeforces 1214D]Treasure Island(dfs)

    [Codeforces 1214D]Treasure Island(dfs) 题面 给出一个n*m的字符矩阵,'.'表示能通过,'#'表示不能通过.每步可以往下或往右走.问至少把多少个'.'变成'#' ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力

    Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...

随机推荐

  1. Java String 对象,你真的了解了吗?

    String 对象的实现 String对象是 Java 中使用最频繁的对象之一,所以 Java 公司也在不断的对String对象的实现进行优化,以便提升String对象的性能,看下面这张图,一起了解一 ...

  2. mysql8.0版本忘记root密码

    1.先关掉系统服务 net stop mysql 2.进入mysql安装目录的bin文件中,以管理员的方式运行cmd,然后输入如下命令,实现无密码登陆 mysqld --console --skip- ...

  3. 使用Fedora8 iso开发环境开发gtk3跨Linux多版本桌面应用

    原文: https://bbs.otherhill.com/index.php/topic/show/82 gtk3 demo在/usr/local/gtk3demo 目录下 cd /usr/loca ...

  4. [python]OS文件系统

    1.getcwdd() 获得应用程序当前的工作目录 #getcwd() 获取应用程序当前的工作目录 import os print(os.getcwd()) 2.chdir(path) 改变当前工作目 ...

  5. 基于Docker搭建大数据集群(三)Hadoop部署

    主要内容 Hadoop安装 前提 zookeeper正常使用 JAVA_HOME环境变量 安装包 微云下载 | tar包目录下 Hadoop 2.7.7 角色划分 角色分配 NN DN SNN clu ...

  6. webservice传输文件的三种方式

    1, 在接口中不定义,直接以附件形式传输. 2, 在接口参数中定义byte[]类型,文件在xml中以base64编码传输. 3, 在接口参数中定义DataHandler类型,然后使用MTOM形式来进行 ...

  7. mysql5.7初始密码及设置问题

    为了加强安全性,MySQL5.7为root用户随机生成了一个密码,如果安装的是RPM包,则默认是在/var/log/mysqld.log中. 可通过# grep "password" ...

  8. Mysql的表级锁和行级锁

    表级锁 MySQL表级锁分为读锁和写锁. 读锁 用法:LOCK TABLE table_name [ AS alias_name ] READ 释放锁使用UNLOCK tables.可以为表使用别名, ...

  9. 节点操作--JavaScript

    1 - 概念 网页中的所有内容都是节点(标签.属性.文本.注释),在DOM中,节点使用node来表示. HTML DOM树中的所有节点均可通过JS进行访问,所有HTML元素(节点)均可被修改,也可以创 ...

  10. 运算符 字符串 for循环

    1. 运算符 1.1赋值运算符 = += -= *= /= //= %= **= 1.2比较运算符 < > = <= == != 1.3成员运算符 in not in 1.4逻辑运算 ...