Round #203 (Div. 2)B------Resort
来源:互联网 发布:p2p网络借贷监管政策 编辑:程序博客网 时间:2024/06/01 12:22
思路:把出度大于1的点去掉,以每一个hotel为起点搜索找最长路径。
链接:点击打开链接
#include<cstdio>#include<iostream>#include<cstring>using namespace std;int len,fa[100010],sum[100010],maxpath,hotel[100010];int dfs(int x){ if(fa[x]==0) return len; len++; return dfs(fa[x]);}void print(int x){ if(fa[x]==0) { printf("%d\n",maxpath); printf("%d",x); maxpath--; if(maxpath) printf(" "); else printf("\n"); } else { print(fa[x]); printf("%d",x); maxpath--; if(maxpath) printf(" "); else printf("\n"); } return;}int main(){ int n; while(scanf("%d",&n)!=EOF) { int cnt=0,tmp,maxindex; memset(sum,0,sizeof(sum)); memset(fa,0,sizeof(fa)); for(int i=1;i<=n;i++) { scanf("%d",&tmp); if(tmp==1) hotel[cnt++]=i; } for(int i=1;i<=n;i++) { scanf("%d",&fa[i]); sum[fa[i]]++; } for(int i=1;i<=n;i++) { if(sum[fa[i]]>1) fa[i]=0; } maxpath=-1; for(int i=0;i<cnt;i++) { len=1; int h=dfs(hotel[i]); if(h>maxpath) { maxpath=h; maxindex=hotel[i]; } } print(maxindex); } return 0;}
- Codeforces Round #203 (Div. 2) B. Resort
- Round #203 (Div. 2)B------Resort
- Codeforces Round #203(Div. 2)B. Resort
- Codeforces Round #203 (Div. 2)B
- Codeforces 350B Resort
- Codeforces Round #131 (Div. 2) A B
- Codeforces Round #134 (Div. 2)B. Airport
- Codeforces Round #170 (Div. 2) problem B
- Codeforces Round #173 (Div. 2) Problem B
- Codeforces Round #181 (Div. 2) B. Coach
- Codeforces Round #185 (Div. 2)--A,B
- Codeforces Round #171 (Div. 2) B
- Codeforces Round #169 (Div. 2) B题
- Codeforces Round #188 (Div. 2) B题
- Codeforces Round #192 (Div. 2) B
- Codeforces Round #206 (Div. 2) - b
- Codeforces Round #202 (Div. 2)B-贪心
- Codeforces Round #206 (Div. 2)B
- CentOS 防火墙配置 80端口的实例详解
- 学会拒绝别人
- Codeforces Round #203 (Div. 2) 水果3题
- android应用程序如何设置样式
- UVa 10303 How Many Trees? (卡特兰数&高精度)
- Round #203 (Div. 2)B------Resort
- 分享一例iptables 防火墙配置
- Linux的五个查找命令
- 第二章--选择器
- pku3461 Oulipo (KMP)
- C++内存分配方式详解——堆、栈、自由存储区、全局/静态存储区和常量存储区
- 第三章--可视化格式模型
- md5算法的C程序
- 第四章--背景图像效果