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;
}

原文地址:https://www.cnblogs.com/ilikeeatfish/p/11760780.html

时间: 2024-08-13 19:12:21

Codeforces Round #595 (Div. 3)B2 简单的dfs的相关文章

Codeforces Round #259 (Div. 2) (简单模拟实现题)

题目链接:http://codeforces.com/problemset/problem/454/A A. Little Pony and Crystal Mine time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Twilight Sparkle once got a crystal from the Crystal Mine

Codeforces Round #590 (Div. 3) B2. Social Network (hard version)

链接: https://codeforces.com/contest/1234/problem/B2 题意: The only difference between easy and hard versions are constraints on n and k. You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most k most

Codeforces Round #310 (Div. 2)--A(简单题)

http://codeforces.com/problemset/problem/556/A 题意:给一个01字符串,把所有相邻的0和1去掉,问还剩下几个0和1. 题解:统计所有的0有多少个,1有多少个,最后答案就是两者不同的个数. #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algo

题解Codeforces Round #595 (Div. 3)(CF1249)

开题1小时(雾)严重影响我的提交以及做题心情..我刚开题就发现有人阿克了.. 实际上这场div3真心简单良心很休闲. A:送分题,先排序,每次枚举一下这个数可以加到哪个集合里,加进去就行. 1 #include<stdio.h> 2 #include<algorithm> 3 #define it register int 4 #define il inline 5 using namespace std; 6 const int N=1000005; 7 int a[N],o[N

Codeforces Round #595 (Div. 3) E. By Elevator or Stairs?

题目地址:http://codeforces.com/contest/1249/problem/E 题意:有n层楼,上楼有楼梯和电梯两种方法,从 i 到 i+1 层有不同花费,用电梯有等电梯门开的时间,但上一次用的电梯就不用等这个时间.问去每层楼的最小花费. 思路:很基础的dp题,dp [ i ] [ 0 ] 表示一楼到 i楼的总时间,其中从 i - 1 楼到 i 楼用的是楼梯,dp [ i ] [ 1 ] 表示从一楼到 i 楼的总时间,其中 i - 1 到 i 楼用的电梯. AC代码: 1 #

E. By Elevator or Stairs?.Codeforces Round #595 (Div. 3)

前言 有一说一,这是我做过最简单的一道E题 题意 告诉你有个大楼,然后让你求出从一楼到每一楼的最短时间.其中,上楼有两种方式1.走楼梯2.坐电梯.楼梯可以直接走,电梯需要一个等待时间.数据给出层与层之间不算等待时间的两种方式上楼所需的时间. 做法 很容易想到dp,而且是最基础的dp(估计div3也就敢这么出个算法题) 状态dp[i][0]表示走楼梯上到i层所需的最短时间.dp[i][1]表示坐电梯时上到i层所需的最短时间. 转移方程 dp[i][0]=min(dp[i-1][0],dp[i-1]

Codeforces Round #595 (Div. 3) 题解

前言 无 A 因为没有重复的数,我们只要将数据排序,比较两两之间有没有\(a_j - a_i == 1 (j > i)\) 的,有则输出 \(2\) , 无则输出 \(1\) 普及T1难度 Code #include<algorithm> #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #define N 107 using namespace st

Codeforces Round #595 (Div. 3)

比赛链接:传送门 Codeforces1249A. Yet Another Dividing into Teams(水题) 代码: #include <bits/stdc++.h> #define N 105 using namespace std; int a[N]; int main() { int q; cin >> q; while (q--) { int n; cin >> n; for (int i = 1; i <= n; i++) { cin &g

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 addition, k≤1000,n≤50k≤1000,n≤50 and it is necessary to print swaps themselves. You can hack this problem if you solve it. But you can hack the previou