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

【题意】

【题解】

我们可以把原图的边都反向一下.
然后以每个休息点作为起点,进行dfs.
每次在扩展节点y的时候,要求这个点y必须只有一个出度,然后就能走多远就走多远就ok了。
会发现每个休息点占据的那些链都是唯一的,所以其他的休息点在进行dfs的时候,不会重复走到其他休息点dfs过的点。
因此这样dfs的复杂度是O(N)的。
随便搞搞,更新一下最大值就ok了。

【代码】

  1. #include <bits/stdc++.h>
  2. #define rep1(i,a,b) for (int i = a;i <= b;i++)
  3. using namespace std;
  4. const int N = 1e5;
  5. int n,m;
  6. int a[N+10],ans = 0,s;
  7. vector<int> g1[N+10],g2[N+10];
  8. void dfs(int x,int dep){
  9. if (dep>ans){
  10. ans = dep;
  11. s = x;
  12. }
  13. for (int y:g2[x]){
  14. if (a[y]==1) continue;
  15. if ((int)g1[y].size()>1) continue;
  16. dfs(y,dep+1);
  17. }
  18. }
  19. int main()
  20. {
  21. ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  22. cin >> n;
  23. rep1(i,1,n) cin >> a[i];
  24. rep1(i,1,n){
  25. int x,y;
  26. cin >> x;
  27. y = i;
  28. if(x==0) continue;
  29. g1[x].push_back(y);
  30. g2[y].push_back(x);
  31. }
  32. rep1(i,1,n)
  33. if(a[i]==1) dfs(i,1);
  34. cout<<ans<<endl;
  35. int x = s;
  36. while(1){
  37. cout<<x<<' ';
  38. if (a[x]==1) break;
  39. x = g1[x][0];
  40. }
  41. return 0;
  42. }

【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. 基于ArcGIS Flex API实现动态标绘(1.1)

    动态标绘API 1.1版本号.相较前一版本号1.0(点击进入).该版本号提供标绘符号的编辑功能. 编辑功能包含两种编辑状态:编辑控制点.对标绘符号进行旋转.八方向拉伸.平移. 编辑控制点例如以下图所看 ...

  2. Spring:验证用户登录

    利用 Spring IOC 技术实现用户登录的验证机制,对用户进行登录验证. 首先利用 Spring 的自动装配模式将 User 对象注入到控制器中,然后将用户输入的用户名和密码与系统中限定的合法用户 ...

  3. word2vec和word embedding有什么区别?

    word2vec和word embedding有什么区别? 我知道这两个都能将词向量化,但有什么区别?这两个术语的中文是什么? from: https://www.zhihu.com/question ...

  4. 假脱机服务(SPOOLing service)

    1. 基本含义 SPOOLing 是 Simultaneous Peripheral(外设) Operation On-Line(联机) 的缩写,是关于慢速字符设备(慢速外设,比如打印机)如何与计算机 ...

  5. Spring配置事务中的 transactionAttributes 各属性含义及XML配置

    转自:https://blog.csdn.net/z69183787/article/details/17161393 transactionAttributes 属性: PROPAGATION 事务 ...

  6. idea导入ssm项目启动tomcat报错404

    用idea写ssm项目,基于之前一直在用spring boot  对于idea如何运行ssm花费了一番功夫 启动Tom act一直在报404 我搜了网上各种解决办法都不行,花费一天多的时间解决不了 就 ...

  7. 「LOJ10150」括号配对

    [题目] Hecy 又接了个新任务:BE 处理.BE 中有一类被称为 GBE. 以下是 GBE 的定义: 空表达式是 GBE 如果表达式 A 是 GBE,则 [A] 与 (A) 都是 GBE 如果 A ...

  8. A - Petya and Strings

    Problem description Little Petya loves presents. His mum bought him two strings of the same size for ...

  9. Spring 的优秀工具类盘点---转

    第 1 部分: 文件资源操作和 Web 相关工具类 http://www.ibm.com/developerworks/cn/java/j-lo-spring-utils1/ 文件资源操作 文件资源的 ...

  10. MTD 门店统计

    DROP TABLE #MTD ' ,@endDate date = cast(getdate() as date) CREATE TABLE #MTD(bydate date) DECLARE @c ...