CodeForces1249B1/B2-Books Exchange-dfs-一般搜索+记忆化搜索
一般搜索
注意:一般定义成void
Books Exchange (easy version) CodeForces - 1249B2
The only difference between easy and hard versions is constraints.
There are nn kids, each of them is reading a unique book. At the end of any day, the ii-th kid will give his book to the pipi-th kid (in case of i=pii=pi the kid will give his book to himself). It is guaranteed that all values of pipi are distinct integers from 11 to nn (i.e. pp is a permutation). The sequence pp doesn't change from day to day, it is fixed.
For example, if n=6n=6 and p=[4,6,1,3,5,2]p=[4,6,1,3,5,2] then at the end of the first day the book of the 11-st kid will belong to the 44-th kid, the 22-nd kid will belong to the 66-th kid and so on. At the end of the second day the book of the 11-st kid will belong to the 33-th kid, the 22-nd kid will belong to the 22-th kid and so on.
Your task is to determine the number of the day the book of the ii-th child is returned back to him for the first time for every ii from 11 to nn.
Consider the following example: p=[5,1,2,4,3]p=[5,1,2,4,3]. The book of the 11-st kid will be passed to the following kids:
- after the 11-st day it will belong to the 55-th kid,
- after the 22-nd day it will belong to the 33-rd kid,
- after the 33-rd day it will belong to the 22-nd kid,
- after the 44-th day it will belong to the 11-st kid.
So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.
You have to answer qq independent queries.
Input
The first line of the input contains one integer qq (1≤q≤10001≤q≤1000) — the number of queries. Then qq queries follow.
The first line of the query contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of kids in the query. The second line of the query contains nn integers p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n, all pipi are distinct, i.e. pp is a permutation), where pipi is the kid which will get the book of the ii-th kid.
It is guaranteed that ∑n≤2⋅105∑n≤2⋅105 (sum of nn over all queries does not exceed 2⋅1052⋅105).
Output
For each query, print the answer on it: nn integers a1,a2,…,ana1,a2,…,an, where aiai is the number of the day the book of the ii-th child is returned back to him for the first time in this query.
Example
6
5
1 2 3 4 5
3
2 3 1
6
4 6 2 1 5 3
1
1
4
3 4 1 2
5
5 1 2 4 3
1 1 1 1 1
3 3 3
2 3 3 2 1 3
1
2 2 2 2
4 4 4 1 4 题意:
给出t组数,每组给出n,表示接下来有n本书,a[i]=k,表示第i个人需要把手上的书给第k个人。
问:编号为i的书回到第i个人手里需要多少次。 看第二组样例输入2 3 1,输出3 3 3。
- sum1=0,
第1个人把手上的书给第2个人,sum++,第二个人把书给第3个人,sum++,第三个人把书给第1个人,sum++,所以sum1=3;
- sum2=0,
第2个人把手上的书给第3个人,sum++,第3个人把书给第1个人,sum++,第1个人把书给第2个人,sum++,所以sum2=3;
- sum3=0,
第3个人把手上的书给第1个人,sum++,第1个人把书给第2个人,sum++,第2个人把书给第3个人,sum++,所以sum3=3;
思路: 开一个for循环,然后dfs(i,i,1)
i:第i个人
i:第i个人所对应下次把书传到a[i]手上去
1:代表书转手次数=1,第一次书在自己手上,所以次数为1,以后每次搜索次数++ 开始进行搜索
void dfs(int x,int k,int t)//传入下标x,下标k,转手次数t
{
if(w)
return;//剪枝
if(x==a[k])//此时表示第x个人找到了对应a[k]=x的书
{
w=t;//定义w为全局变量,w=t,记录转手次数
return ;
}
dfs(x,a[k],t+);//否则还未找到,继续进行搜索
}
代码如下:
#include<stdio.h>
int a[],w; void dfs(int x,int k,int t)
{
if(w)
return;
if(x==a[k])
{
w=t;
return ;
}
dfs(x,a[k],t+);
} int main()
{
int t,m;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
{
w=;
dfs(i,i,);
printf("%d ",w);
}
printf("\n");
}
return ;
}
记忆化搜索
注意:一般定义成int,需要有返回值,表示之前已经搜到过的值,无需再搜
Books Exchange (hard version) CodeForces - 1249B2
The only difference between easy and hard versions is constraints.
There are nn kids, each of them is reading a unique book. At the end of any day, the ii-th kid will give his book to the pipi-th kid (in case of i=pii=pi the kid will give his book to himself). It is guaranteed that all values of pipi are distinct integers from 11 to nn (i.e. pp is a permutation). The sequence pp doesn't change from day to day, it is fixed.
For example, if n=6n=6 and p=[4,6,1,3,5,2]p=[4,6,1,3,5,2] then at the end of the first day the book of the 11-st kid will belong to the 44-th kid, the 22-nd kid will belong to the 66-th kid and so on. At the end of the second day the book of the 11-st kid will belong to the 33-th kid, the 22-nd kid will belong to the 22-th kid and so on.
Your task is to determine the number of the day the book of the ii-th child is returned back to him for the first time for every ii from 11 to nn.
Consider the following example: p=[5,1,2,4,3]p=[5,1,2,4,3]. The book of the 11-st kid will be passed to the following kids:
- after the 11-st day it will belong to the 55-th kid,
- after the 22-nd day it will belong to the 33-rd kid,
- after the 33-rd day it will belong to the 22-nd kid,
- after the 44-th day it will belong to the 11-st kid.
So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.
You have to answer qq independent queries.
Input
The first line of the input contains one integer qq (1≤q≤10001≤q≤1000) — the number of queries. Then qq queries follow.
The first line of the query contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of kids in the query. The second line of the query contains nn integers p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n, all pipi are distinct, i.e. pp is a permutation), where pipi is the kid which will get the book of the ii-th kid.
It is guaranteed that ∑n≤2⋅105∑n≤2⋅105 (sum of nn over all queries does not exceed 2⋅1052⋅105).
Output
For each query, print the answer on it: nn integers a1,a2,…,ana1,a2,…,an, where aiai is the number of the day the book of the ii-th child is returned back to him for the first time in this query.
Example
6
5
1 2 3 4 5
3
2 3 1
6
4 6 2 1 5 3
1
1
4
3 4 1 2
5
5 1 2 4 3
1 1 1 1 1
3 3 3
2 3 3 2 1 3
1
2 2 2 2
4 4 4 1 4 思路:
由于数据变大了,所以按照上面的方法进行深搜会超时,所以需要用到记忆化搜素。
开一个book数组,book[x]=w,表示书回到第x个人手上需要转手w次。
假设输入2 3 1,a[i]=k的书回到第k个人手中需要t次的话,那么在转手过程中所经过的所有点k回到第k个人手中也需要t次,
所以开一个b数组,把所经过的所有k全部保存下来,for循环往下走走到该点就不需要再搜,直接输出即可
利用book数组,把b数组里面存的所有数(有p个)通过循环赋值为k,即book[ b[i] ]=w。
代码如下:
#include<stdio.h>
#include<string.h>
const int N=2e5+;
int a[N],w,b[N],p,book[N]; void dfs(int x,int k,int t)
{
if(x==a[k])
{
w=t;
book[x]=w;
return;
// return book[x]=t;
}
b[p++]=a[k];//把走过的点全部存起来,下次碰到不用再走就不会超时
dfs(x,a[k],t+);
} int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
memset(book,,sizeof(book));
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
{
p=;
if(i!=)
printf(" ");
if(book[i])
{
printf("%d",book[i]);
continue;
}
// w=0;
dfs(i,i,);
for(int j=;j<p;j++)
book[b[j]]=w;
printf("%d",w);
}
printf("\n");
}
return ;
}
CodeForces1249B1/B2-Books Exchange-dfs-一般搜索+记忆化搜索的更多相关文章
- skiing(搜索+记忆化搜索)
skiing 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...
- 【蓝桥杯真题】地宫取宝(搜索->记忆化搜索详解)
链接 [蓝桥杯][2014年第五届真题]地宫取宝 题目描述 X 国王有一个地宫宝库.是 n x m 个格子的矩阵.每个格子放一件宝贝.每个宝贝贴着价值标签. 地宫的入口在左上角,出口在右下角. 小明被 ...
- P4363 [九省联考2018]一双木棋chess(对抗搜索+记忆化搜索)
传送门 这对抗搜索是个啥玩意儿…… 首先可以发现每一行的棋子数都不小于下一行,且局面可由每一行的棋子数唯一表示,那么用一个m+1进制数来表示当前局面,用longlong存,开map记忆化搜索 然后时间 ...
- hdu1078 dfs+dp(记忆化搜索)搜索一条递增路径,路径和最大,起点是(0,0)
#include<bits/stdc++.h> using namespace std; typedef unsigned int ui; typedef long long ll; ty ...
- 数位dp/记忆化搜索
一.引例 #1033 : 交错和 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个数 x,设它十进制展从高位到低位上的数位依次是 a0, a1, ..., an ...
- 蓝桥杯历届试题 地宫取宝 dp or 记忆化搜索
问题描述 X 国王有一个地宫宝库.是 n x m 个格子的矩阵.每个格子放一件宝贝.每个宝贝贴着价值标签. 地宫的入口在左上角,出口在右下角. 小明被带到地宫的入口,国王要求他只能向右或向下行走. 走 ...
- 【noip 2009】 乌龟棋 记忆化搜索&动规
题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩家控制一个乌龟棋子从起 ...
- hdu 1428(很好的一道题,最短路+记忆化搜索)
漫步校园 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- POJ1088滑雪(记忆化搜索)
就是用DP,DP[i][j]是在这个(i,j)位置作为起点的最长长度. 因为可能会超时,DP的话每次就是记录,然后就不用回溯了. 很简单的DFS里面的记忆化搜索. #include <stdio ...
随机推荐
- Dubbo 如何成为连接异构微服务体系的最佳服务开发框架
从编程开发的角度来说,Apache Dubbo (以下简称 Dubbo)首先是一款 RPC 服务框架,它最大的优势在于提供了面向接口代理的服务编程模型,对开发者屏蔽了底层的远程通信细节.同时 Dubb ...
- Android_开发片段(Part 2)
1.List和Map知识: 1)如何定义 List<Map<String,Object>> list=new ArrayList<Map<String,Object ...
- nteract 使用教程
安装 直接去官网下载 一路回车 官网 建立python虚拟环境 和我们平时一样 不同的是在建立完之后 要安装一个kernel Using Python3 with pip and a virtual ...
- ()centos7 安装mysql8.0
一.下载mysql 1 .下载 https://dev.mysql.com/downloads/repo/yum/ wget http://repo.mysql.com/mysql80-communi ...
- mvc 前台传入后台
转自:http://blog.csdn.net/huangyezi/article/details/45274553 一个很简单的分部视图,Model使用的是列表,再来看看调用该分部视图的action ...
- 安卓apk反编译、修改、重新打包、签名全过程
首先明确,反编译别人apk是一件不厚道的事情.代码是程序员辛苦工作的成果,想通过这种手段不劳而获,是不对的.这也说明,代码混淆是非常重要的.本文抱着学习的态度,研究在一些特殊的情况下如果有需要,该怎么 ...
- 截取url中的某个字符串后面的值
获取到当前网址 var url = window.location.href; http://localhost:8080/exam_questions?type=3 //获取url中的参数 func ...
- PAT_A1080#Graduate Admission
Source: PAT A1080 Graduate Admission (30 分) Description: It is said that in 2011, there are about 10 ...
- Python中两大神器&exec() &eval()
一.神器1 -- 内置函数eval eval是python中的内置函数,它的作用是将字符串变为所对应的表达式,也相当于一个功能代码加双引号变为字符串,而eval又将字符串转为相应的功能,它在使用过程中 ...
- Vuex 源码解析
先来看一下这张Vuex的数据流程图,熟悉Vuex使用的同学应该已经有所了解. Vuex实现了一个单向数据流,在全局拥有一个State存放数据,所有修改State的操作必须通过Mutation进行,Mu ...