解题报告 NOIP2015 信息传递
来源:互联网 发布:编程菱形 编辑:程序博客网 时间:2024/05/22 02:24
信息传递 NOIP2015 day1 T2
时间限制: 1 s
空间限制: 128000 KB
题目等级 : 黄金 Gold
题解
题目描述 Description
有个同学(编号为 1 到)正在玩一个信息传递的游戏。在游戏里每人都有一个固定的信息传递对象,其中,编号为的同学的信息传递对象是编号为的同学。游戏开始时,每人都只知道自己的生日。之后每一轮中,所有人会同时将自己当前所知的生日信息告诉各自的信息传递对象(注意:可能有人可以从若干人那里获取信息,但是每人只会把信息告诉一个人,即自己的信息传递对象)。当有人从别人口中得知自己的生日时,游戏结束。请问该游戏一共可以进行几轮?
输入描述 Input Description
输入共 2行。
第 1行包含1个正整数n,表示n个人
第 2 行包含n 个用空格隔开的正整数T1 ,T 2 ,……,Tn , 其中第i个整数Ti表示编号为i
的同学的信息传递对象是编号为 T i 的同学,Ti≤n 且 Ti≠i。
数据保证游戏一定会结束。
输出描述 Output Description
输出共 1行,包含 1个整数,表示游戏一共可以进行多少轮。
样例输入 Sample Input
5
2 4 2 3 1
样例输出 Sample Output
3
数据范围及提示 Data Size & Hint
【输入输出样例 1 说明】
游戏的流程如图所示。当进行完第 3 轮游戏后,4 号玩家会听到 2 号玩家告诉他自己的生日,所以答案为 3。当然,第 3 轮游戏后,2 号玩家、3 号玩家都能从自己的消息来源得知自己的生日,同样符合游戏结束的条件。
对于 30%的数据, ≤ 200;
对于 60%的数据, ≤ 2500;
对于 100%的数据, ≤ 200000。
给定一张图,求最小环
1.dfs做法
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <stack>using namespace std;const int MAXN = 200005;int n,num[MAXN],ans = MAXN;int used[MAXN],times[MAXN];void dms(int x){ int clocks = 0,p = x; while(true){ used[p] = x,times[p] = clocks ++,p = num[p]; if(used[p]){if(used[p] == x) ans = min(ans,clocks - times[p]); break; } }}int main(){ memset(times,0,sizeof(times)); memset(used,0,sizeof(used)); scanf("%d",&n); for(int i = 1; i <= n; i ++) scanf("%d",&num[i]); for(int i = 1; i <= n; i ++) if(!used[i]) dms(i); printf("%d\n",ans); return 0;}
2.Tarjan
缩点时记录大小即可
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <stack>using namespace std;const int MAXN = 200005;int n,f,t,tot = 0,ans = 2333333,cnt = 0;int scc_num,dfs_clock,scc[MAXN];int first[MAXN],nxt[MAXN],low[MAXN],dfn[MAXN];stack < int > s;struct edge{ int f,t;}l[MAXN];void build(int f,int t){ l[++ tot] = (edge){f,t}; nxt[tot] = first[f]; first[f] = tot;}int dfs(int u){ low[u] = dfn[u] = ++ dfs_clock; s.push(u); for(int i = first[u]; i != -1; i = nxt[i]){ int w = l[i].t; if(!dfn[w]) dfs(w),low[u] = min(low[w],low[u]); else if(!scc[w]) low[u] = min(low[u],dfn[w]); } if(low[u] == dfn[u]){ scc_num ++,cnt = 0; while(true){int x = s.top();s.pop(),scc[x] = scc_num,cnt ++; if(x == u) break;} if(cnt > 1) ans = min(ans,cnt);//只有一个点不能当做scc }}int main(){ memset(first,0xff,sizeof(first)); scanf("%d",&n); for(int i = 1; i <= n; i ++) scanf("%d",&t),build(i,t); for(int i = 1; i <= n; i ++){ if(!dfn[i]) dfs(i); } printf("%d\n",ans); return 0;}
- NOIP2015信息传递解题报告
- 解题报告 NOIP2015 信息传递
- COGS 2105. [NOIP2015] 信息传递 法一 解题报告
- NOIP2015 信息传递 解题报告(强连通分量)
- COGS 2105. [NOIP2015] 信息传递 解法二 解题报告
- NOIP2015 解题报告
- NOIP2015解题报告
- NOIP2015 day2 解题报告
- [NOIP2015]信息传递
- [noip2015] 信息传递
- 【NOIP2015】信息传递
- Noip2015 D1T2信息传递
- 【NOIP2015】Day1T2 信息传递
- noip2015 信息传递
- NOIP2015信息传递
- 【NOIP2015】信息传递 dfs
- NOIP2015信息传递
- NOIP2015 信息传递
- web。xml初始化spring
- html里面制作表单。去掉一些不是常见的。这些都是基础中的简化版。可以更快的学习
- 2017ACMICPC亚洲区青岛站 B:Chenchen,Tangtang,ZengZeng【五分钟解决的战斗】
- 扫描包
- ubuntu14.04系统安装以及caffe无GPU安装
- 解题报告 NOIP2015 信息传递
- oracle可视化工具的使用
- 对于html的学习
- TensorFlow实现案例汇集
- VMware虚拟机上配置nginx后,本机无法访问问题
- 关于静态全局变量的一些想法
- Python爬虫(四)——模拟登录imooc实战(利用cookie)
- SpringMVC基础-3-参数绑定
- 自定义View实现进度圆环