【链接】 我是链接,点我呀:)

【题意】

【题解】

我们可以把原图的边都反向一下.
然后以每个休息点作为起点,进行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的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  3. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  4. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  5. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  6. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  7. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  8. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

  9. 【codeforces 515D】Drazil and Tiles

    [题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...

随机推荐

  1. 一个jeecg整合activiti的学习样例,源代码下载

    社区成员:刘京华採用技术:jeecg+ activiti源代码下载地址:http://pan.baidu.com/s/1dDxOHrV 截图演示:  2.jpg (71.81 KB, 下载次数: 0) ...

  2. 20170322Linux

  3. Android源码编译全过程记录(基于最新安卓5.1.0)【转】

    本文转载自:http://blog.csdn.net/drg1612/article/details/44802533 我的编译条件: 1 Ubuntu Kylin 14.04 长期支持版 下载地址 ...

  4. SQL 优化记录

    1. 对Where 语句的法则 1.1 避免在WHERE子句中使用in,not  in,or 或者having. 可以使用 exist 和not exist代替 in和not in. 可以使用表链接代 ...

  5. hihocoder 1677 翻转字符串 splay

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个字符串S,小Hi希望对S进行K次翻转操作. 每次翻转小Hi会指定两个整数Li和Ri,表示要将S[Li..Ri]进行 ...

  6. sublime如何汉化

    1.将sublime安装文件夹里面的defavlut.sublime-package这个文件zip解压. 2.然后查找到sublime-menu文件. 3.打开文件将json里面的caption里面的 ...

  7. C - Domino piling

    Problem description You are given a rectangular board of M × N squares. Also you are given an unlimi ...

  8. 【MFC】在MFC中PreTranslateMessage()的使用方法

    BOOL CSearchuserDlg::PreTranslateMessage(MSG* pMsg) { if (pMsg->message==WM_KEYDOWN) // 判断是否有按键按下 ...

  9. 查看 Android App 的 versionCode

    有 App 源码时,可以直接查看 AndroidManifest.xml 文件. <manifest android:versionName="1.4" android:ve ...

  10. py2exe打包遇到的问题

    py2exe打包python成.exe文件 打包过程和结果 1.创建setup脚本打包文件,其中设置打包的属性和方法.注意:尽量将被打包文件和此打包脚本放在同目录下(因为在尝试非同目录下时,出现了非可 ...