题目链接: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. Mysql高手系列 - 第12篇:子查询详解

    这是Mysql系列第12篇. 环境:mysql5.7.25,cmd命令中进行演示. 本章节非常重要. 子查询 出现在select语句中的select语句,称为子查询或内查询. 外部的select查询语 ...

  2. Vue2.0+ElementUI实现表格翻页的实例

    参考地址: https://www.cnblogs.com/zhouyifeng/p/7706815.html

  3. Flask基础(07)-->正则自定义转换器

    正则自定义转换器 为什么要自定义正则转换器? 因为默认转换器太过于笨重,往往不能满足我们实际业务的需求,这时候我们就需要自定义正则转换器了. 那么我们怎么自定义正则转换器呢? from flask i ...

  4. Spring Cloud Gateway 使用

    简介 Spring Cloud Gateway是Spring Cloud官方推出的网关框架,网关作为流量入口,在微服务系统中有着十分重要的作用,常用功能包括:鉴权.路由转发.熔断.限流等. Sprin ...

  5. C++——数组与字符串

    目录 一.数组 1.1定义与初始化 1.1.1使用 1.1.2存储 1.1.3初始化 1.2作函数参数 1.3对象数组 1.3.1定义与访问 1.3.2初始化 1.3.3数组元素所属类的构造函数 二. ...

  6. spark针对web日志正则匹配

    针对于web日志做分析是学习spark常用练习项目.此文介绍web日志正则匹配相关小提示.  实例测试 从网上找了一个access日志案列 218.19.140.242 - - [10/Dec/201 ...

  7. gorilla/mux类库解析

    golang自带的http.SeverMux路由实现简单,本质是一个map[string]Handler,是请求路径与该路径对应的处理函数的映射关系.实现简单功能也比较单一: 不支持正则路由, 这个是 ...

  8. Timed out after 30000 ms while waiting to connect

    今天使用mongo-java-drive写连接mongo的客户端,着实被上面那个错坑了一把.回顾一下解决过程: 报错: com.mongodb.MongoTimeoutException: Timed ...

  9. springboot结合jpa

    idea中新建springboot项目,引入spring-boot-starter-data-jpa依赖 application.yml中配置数据库连接,示例如下: spring: datasourc ...

  10. JDK8 Optional操作学习

    介绍 Optional是JDK8中提供用于包含未知对象的工具类,即可以利用Optional包装对象来避免繁琐的空指针检查,以及NullPointException的处理,在Optional中,用val ...