【Codeforces 350B】Resort
【链接】 我是链接,点我呀:)
【题意】
【题解】
我们可以把原图的边都反向一下.
然后以每个休息点作为起点,进行dfs.
每次在扩展节点y的时候,要求这个点y必须只有一个出度,然后就能走多远就走多远就ok了。
会发现每个休息点占据的那些链都是唯一的,所以其他的休息点在进行dfs的时候,不会重复走到其他休息点dfs过的点。
因此这样dfs的复杂度是O(N)的。
随便搞搞,更新一下最大值就ok了。
【代码】
#include <bits/stdc++.h>
#define rep1(i,a,b) for (int i = a;i <= b;i++)
using namespace std;
const int N = 1e5;
int n,m;
int a[N+10],ans = 0,s;
vector<int> g1[N+10],g2[N+10];
void dfs(int x,int dep){
if (dep>ans){
ans = dep;
s = x;
}
for (int y:g2[x]){
if (a[y]==1) continue;
if ((int)g1[y].size()>1) continue;
dfs(y,dep+1);
}
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> n;
rep1(i,1,n) cin >> a[i];
rep1(i,1,n){
int x,y;
cin >> x;
y = i;
if(x==0) continue;
g1[x].push_back(y);
g2[y].push_back(x);
}
rep1(i,1,n)
if(a[i]==1) dfs(i,1);
cout<<ans<<endl;
int x = s;
while(1){
cout<<x<<' ';
if (a[x]==1) break;
x = g1[x][0];
}
return 0;
}
【Codeforces 350B】Resort的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
- 【codeforces 515D】Drazil and Tiles
[题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...
随机推荐
- Getting Installation aborted (Status 7) ApplyParsePerms: lsetfilecon of /syst...【转】
OTA升级失败:原文http://en.miui.com/thread-112197-1-1.html Do you get this "Status 7" error in Re ...
- 【CTSC 2007】 数据备份
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1150 [算法] 首先,有一个很显然的结论 : 如果要使距离和最小,必须选择相邻的办公 ...
- 【POJ 3630】 Phone List
[题目链接] http://poj.org/problem?id=3630 [算法] 字典树 [代码] #include <algorithm> #include <bitset&g ...
- AAC头部格式,RTP打包格式
一共有2种AAC头格式,一种是StreamMuxConfig,另一种是AudioSpecificConfig 1.AudioSpecificConfig 读写header的代码参考 ffmpeg ...
- md5的用处
MD5保存摘要及指纹信息 md5的用处: 1.保存用户密码2.校验数据的完整性
- PCB C# 连接MongoDB 数据库
一.C# MongoDB 驱动下载 1.百度网盘:nuget下载地址(V2.7版本) https://pan.baidu.com/s/1VDsVcH1TMrXqhRCZVewZgA 2.VS 中NUg ...
- 模拟Queue(wait/notify)
BlockingQueue:顾名思义,首先它是一个队列,并且支持阻塞的机制,阻塞的放入和得到数据.我们要实现LinkedBlockingQueue下面的两个方法put和take. put(anObje ...
- Netty引导流程解读
Channel的生命周期状态[状态转换将变为相应的事件,转发给ChannelPipeline中的ChannelHandler进行处理] ChannelUnregistered:Channel已经被创建 ...
- log4net实用配置代码
log4net实用配置代码 <?xml version="1.0" encoding="utf-8" ?> <configuration> ...
- python3和python2共存 django-admin Fatal error in launcher: Unable to create process using ‘"‘
python3和python2共存 django-admin Fatal error in launcher: Unable to create process using ‘"‘ 出现这个 ...