Codeforces Round #595 (Div. 3)B2 简单的dfs
原题
https://codeforces.com/contest/1249/problem/B2
这道题一开始给的数组相当于地图的路标,我们只需对每个没走过的点进行dfs即可
#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+20;
int a[maxn],b[maxn],c[maxn];
int dfs(int pos,int step){//传递坐标与步数
if(b[pos]==1){//再次遇到b[pos],返回步数+1
return step+1;
}
else{//若没b[pos]==0,则说明没回原位
b[pos]=1;//标记b[pos]已走过
c[pos]=dfs(a[pos],step+1);
return c[pos];
}
}
int main()
{
int n,m;
cin>>n;
while(n--){
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
scanf("%d",&m);
for(int i=1;i<=m;i++){
cin>>a[i];
}
for(int i=1;i<=m;i++){
if(!b[i]){//若走过b[i],则跳过
b[i]=1;
c[i]=dfs(a[i],0);
}
}
for(int i=1;i<=m;i++){
cout<<c[i]<<" ";
}
cout<<endl;
}
return 0;
}
Codeforces Round #595 (Div. 3)B2 简单的dfs的更多相关文章
- 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 #309 (Div. 1) C. Love Triangles dfs
C. Love Triangles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/553/pro ...
- Codeforces Round #599 (Div. 2)的简单题题解
难题不会啊…… 我感觉写这个的原因就是因为……无聊要给大家翻译题面 A. Maximum Square 简单题意: 有$n$条长为$a_i$,宽为1的木板,现在你可以随便抽几个拼在一起,然后你要从这一 ...
- Codeforces Round #310 (Div. 2)--A(简单题)
http://codeforces.com/problemset/problem/556/A 题意:给一个01字符串,把所有相邻的0和1去掉,问还剩下几个0和1. 题解:统计所有的0有多少个,1有多少 ...
- Codeforces Round #281 (Div. 2) D(简单博弈)
题目:http://codeforces.com/problemset/problem/493/D 题意:一个n*n的地图,有两个人在比赛,第一个人是白皇后开始在(1,1)位置,第二个人是黑皇后开始在 ...
- Codeforces Round #595 (Div. 3)D1D2 贪心 STL
一道用STL的贪心,正好可以用来学习使用STL库 题目大意:给出n条可以内含,相交,分离的线段,如果重叠条数超过k次则为坏点,n,k<2e5 所以我们贪心的想我们从左往右遍历,如果重合部分条数超 ...
- 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 Round #595 (Div. 3)
A - Yet Another Dividing into Teams 题意:n个不同数,分尽可能少的组,要求组内没有两个人的差恰为1. 题解:奇偶分组. int a[200005]; void te ...
- Codeforces Round #595 (Div. 3) A,B,C,D
A题:n个学生,分成任意组,要求每组中任意两名学生的技能值相差不等于1,问最小分组. #include<bits/stdc++.h> using namespace std; #defin ...
随机推荐
- springboot jpa使用
1.添加pom依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId ...
- MongoDB的复制源oplog
之前有说过MongoDB的复制是异步复制的,其实也就是通过oplog来实现的,他存放在local数据库中,我们来查询一下主节点的日志大小. 除了主节点有oplog之外,其他节点也就有oplog ...
- VR应用评测 - Apollo 11 阿波罗11号
Apollo 11 VR http://store.steampowered.com/app/457860/Apollo_11_VR/ Steam VR 2016年发布 好评率 50% 基于美国航空航 ...
- Spark 学习笔记之 优雅地关闭Spark Streaming
优雅地关闭Spark Streaming: 当touch stop文件后,程序仍然会再执行一次,执行完成后退出.
- ELK 学习笔记之 Logstash之filter配置
Logstash之filter: json filter: input{ stdin{ } } filter{ json{ source => "message" } } o ...
- 分享一个移动端rem布局的适配mixin
/*================================================================ 以下为基于ip5 宽度320做的适配,标准html{font-si ...
- python编程基础之十六
for in 循环,与其说是循环不如说精确点交遍历 for 变量名 in + 迭代对象 语句A else: 语句B 作用:一次访问迭代对象中的元素并赋值给变量 循环终止时,执行else语句块,如果br ...
- 安装、卸载 cocoapods
卸载cocoapods: localhost:~ je$ sudo gem uninstall cocoapods Remove executables: pod, sandbox-pod in ad ...
- Ubuntu安装时卡死在启动界面
上下选中Install Ubuntu后,按'e'进入编辑页面(不要按回车),删除'quiet splash'之后的"---",输入"$vt_handoff acpi_os ...
- cobalt strike笔记-常用beacon扫盲
最近还是重新补一下cs的东西 0x01 Beacon命令 Beacon Commands =============== Command Description ------- ----------- ...