B - Resort

CodeForces - 350B

B. Resort
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Valera's finally decided to go on holiday! He packed up and headed for a ski resort.

Valera's fancied a ski trip but he soon realized that he could get lost in this new place. Somebody gave him a useful hint: the resort has nobjects (we will consider the objects indexed in some way by integers from 1 to n), each object is either a hotel or a mountain.

Valera has also found out that the ski resort had multiple ski tracks. Specifically, for each object v, the resort has at most one object u, such that there is a ski track built from object u to object v. We also know that no hotel has got a ski track leading from the hotel to some object.

Valera is afraid of getting lost on the resort. So he wants you to come up with a path he would walk along. The path must consist of objects v1, v2, ..., vk (k ≥ 1) and meet the following conditions:

  1. Objects with numbers v1, v2, ..., vk - 1 are mountains and the object with number vk is the hotel.
  2. For any integer i (1 ≤ i < k), there is exactly one ski track leading from object vi. This track goes to object vi + 1.
  3. The path contains as many objects as possible (k is maximal).

Help Valera. Find such path that meets all the criteria of our hero!

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of objects.

The second line contains n space-separated integers type1, type2, ..., typen — the types of the objects. If typei equals zero, then the i-th object is the mountain. If typei equals one, then the i-th object is the hotel. It is guaranteed that at least one object is a hotel.

The third line of the input contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ n) — the description of the ski tracks. If number aiequals zero, then there is no such object v, that has a ski track built from v to i. If number ai doesn't equal zero, that means that there is a track built from object ai to object i.

Output

In the first line print k — the maximum possible path length for Valera. In the second line print k integers v1, v2, ..., vk — the path. If there are multiple solutions, you can print any of them.

Examples
input

Copy
5
0 0 0 0 1
0 1 2 3 4
output

Copy
5
1 2 3 4 5
input

Copy
5
0 0 1 0 1
0 1 2 2 4
output

Copy
2
4 5
input

Copy
4
1 0 0 0
2 3 4 2
output

Copy
1
1       读题就读了好长好长时间.
     题意呢,就是:有n个地方,给出n大小的序列a,0代表ai是山地,1代表ai是旅馆,再给出个n大小的序列v,第i的序列表示ai可以冲vi到达,问怎么走才能使路途最长(终点要是旅馆,而且路中途没有分叉)
    
      拿样例二来说
      0 1 2 2 4    就是2连1,3连2,4-2,5-4,画图就是这个样子:
      交叉点不能算,所以2忽略,根据0 0 1 0 1,    3,5都是旅馆,均为终点,有3和4,5两条,第二条最长所以选第二个。
      因为题意终点是旅馆,但我们可以反向建图,从旅馆出发,走下去直到不能再走,比如遇到分岔点或者尽头,这样问题就很清晰了。由于涉及到连接,可以用并查集来存和查找,也是个dfs的过程。
    
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
using namespace std;
const int maxn=1e5+;
int a[maxn],b[maxn],fa[maxn];
int ac[maxn];
int dfs(int i)
{
int ans=;      //在以i往下走的过程中,记录步数
while(fa[i]!=-)
{
i=fa[i];
ans++;
}
return ans;
}
int main()
{
int n;
while(cin>>n)
{
memset(fa,-,sizeof(fa));  //边界
map<int,int>mm;
for(int i=;i<=n;i++)
{
cin>>a[i];    //存入节点类型,旅馆或山头
}
for(int i=;i<=n;i++)
{
cin>>b[i];    //存入b【i】,同时计数
mm[b[i]]++;
}
for(int i=;i<=n;i++)
{
if(mm[i]<=) //如果不是分岔点,那么可以表明,i的根节点是b[i]
fa[i]=b[i];  //如果,mm[i]>1,则此为分岔点,fa=-1,也为边界
}
int maxx=-;    //记录最长路径
int k;        //记录最长路径的起始点(旅馆)
for(int i=;i<=n;i++)
{
if(a[i]==)
{
int mid=dfs(i);  //如果是旅馆,以此i跑dfs
if(mid>maxx)
{
maxx=mid;  //记录最大步数,并记录i值
k=i;
}
}
}
cout<<maxx<<endl;
int tot=;
while()
{          //开新数组记录过程,以k为起点,不断跑下去,记录。
ac[tot++]=k;
k=fa[k];  
if(fa[k]==-)  //跑到边界就break;
break;
}
for(int i=tot-;i>=;i--)
cout<<ac[i]<<" ";
cout<<endl;
}
}

好题!妥妥的好题!!!

      

CodeForces - 350B(反向建图,)的更多相关文章

  1. poj1122 FDNY to the Rescue!(dij+反向建图+输出路径)

    题目链接:poj1122 FDNY to the Rescue! 题意:给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路口之间没有直接路径,再给 ...

  2. HDU4857——逃生(反向建图+拓扑排序)(BestCoder Round #1)

    逃生 Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会 ...

  3. POJ3687——Labeling Balls(反向建图+拓扑排序)

    Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...

  4. POJ-1122 FDNY to the Rescue!---Dijkstra+反向建图

    题目链接: https://vjudge.net/problem/POJ-1122 题目大意: 给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路 ...

  5. HDU 2647 Reward 【拓扑排序反向建图+队列】

    题目 Reward Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to d ...

  6. HUD2647 Reward_反向建图拓扑排序

    HDU2647 Reward 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意:老板要发奖金了,有n个人,给你m对数,类似a b,这样的一对 ...

  7. HDU 3639 Hawk-and-Chicken(强连通缩点+反向建图)

    http://acm.hdu.edu.cn/showproblem.php?pid=3639 题意: 有一群孩子正在玩老鹰抓小鸡,由于想当老鹰的人不少,孩子们通过投票的方式产生,但是投票有这么一条规则 ...

  8. POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】

    Silver Cow Party Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Su ...

  9. HPU 3639--Hawk-and-Chicken【SCC缩点反向建图 &amp;&amp; 求传递的最大值】

    Hawk-and-Chicken Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. VMWare 禁用vmem虚拟内存文件

    使用 VMWare 虚拟机,虚拟机启动后,会在虚拟机目录下建立一个与虚拟内存大小相同的 .vmem文件 这个文件主要是将虚拟机内存的内容映射到磁盘,以支持在虚拟机的暂停等功能 对所有的虚拟机" ...

  2. Django(六)实战2:向数据库添加,删除数据、重定向写法、重定向简写

    一.向数据库添加图书数据 [上接]https://blog.csdn.net/u010132177/article/details/103831173 1)首先开启mysql服务,并运行项目 启动my ...

  3. Java按位运算符之按位取反

    一 数据储存形式 二进制在内存中以补码的形式存在. 补码首位是符号位,0表示该数是正数,1表示该数是负数.   例如:   数值 带符号的二进制原码 (首位表示符号位) 补码 内存中的形式 (*表示无 ...

  4. 2.7 学习总结 之【Android】java To Kotlin 一(初识)

    一.Kotlin 的方便之处 1.Kotlin 可以直接使用id来呼叫操控相应的控件( textView.text = "0" )   java( TextView textVie ...

  5. Laravel框架的学习

    用xampp环境 1.Composer的安装 http://www.phpcomposer.com/ 下载Composer的安装exe php.ini中extension_dir.browscap路径 ...

  6. ExcelPackage导入导出,命名空间一定要是EPPlus

    1.引入EPPlus.dll,旧版的是OfficeOpenXml.dll,最好使用EPPlus2.调用 string path = UploadExecl(batchUpload.BinaryExce ...

  7. SpringBoot+Jpa测试自增时报错Springboot-jpa Table 'sell.hibernate_sequence' doesn't exist

    解决办法: @GeneratedValue(strategy = GenerationType.IDENTITY) 如图所示:

  8. 【WPF学习】第二十四章 基于范围的控件

    WPF提供了三个使用范围概念的控件.这些控件使用在特定最小值和最大值之间的数值.这些控件——ScrollBar.ProgressBar以及Slider——都继承自RangeBase类(该类又继承自Co ...

  9. 十四、SAP中定义自定义变量

    一.利用关键字TYPE定义类型,然后在定义此类型的变量,代码如下: 二.效果如下:

  10. 超级简单 一分钟实现react-native屏幕适配

    今天因为react-native的style只能给width和height设置数字 没有react上的vw和vh 因为之前经常用vh vw 感觉不适应 找到了一个新的方法 使用Demension模块 ...