信息传递

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://www.luogu.org/problem/show?pid=2661

Description

有n个同学(编号为1到n)正在玩一个信息传递的游戏。在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学。
  游戏开始时,每人都只知道自己的生日。之后每一轮中,所有人会同时将自己当前所知的生日信息告诉各自的信息传递对象(注意:可能有人可以从若干人那里
获取信息,但是每人只会把信息告诉一个人,即自己的信息传递对象)。当有人从别人口中得知自己的生日时,游戏结束。请问该游戏一共可以进行几轮?

Under two situations the player could score one point.

⋅1. If you touch a buoy before your opponent, you will get one point. For example if your opponent touch the buoy #2 before you after start, he will score one point. So when you touch the buoy #2, you won't get any point. Meanwhile, you cannot touch buoy #3 or any other buoys before touching the buoy #2.

⋅2. Ignoring the buoys and relying on dogfighting to get point.
If you and your opponent meet in the same position, you can try to
fight with your opponent to score one point. For the proposal of game
balance, two players are not allowed to fight before buoy #2 is touched by anybody.

There are three types of players.

Speeder:
As a player specializing in high speed movement, he/she tries to avoid
dogfighting while attempting to gain points by touching buoys.
Fighter:
As a player specializing in dogfighting, he/she always tries to fight
with the opponent to score points. Since a fighter is slower than a
speeder, it's difficult for him/her to score points by touching buoys
when the opponent is a speeder.
All-Rounder: A balanced player between Fighter and Speeder.

There will be a training match between Asuka (All-Rounder) and Shion (Speeder).
Since the match is only a training match, the rules are simplified: the game will end after the buoy #1 is touched by anybody. Shion is a speed lover, and his strategy is very simple: touch buoy #2,#3,#4,#1 along the shortest path.

Asuka is good at dogfighting, so she will always score one point by dogfighting with Shion, and the opponent will be stunned for T seconds after dogfighting.
Since Asuka is slower than Shion, she decides to fight with Shion for
only one time during the match. It is also assumed that if Asuka and
Shion touch the buoy in the same time, the point will be given to Asuka
and Asuka could also fight with Shion at the buoy. We assume that in
such scenario, the dogfighting must happen after the buoy is touched by
Asuka or Shion.

The speed of Asuka is V1 m/s. The speed of Shion is V2 m/s. Is there any possibility for Asuka to win the match (to have higher score)?

Input

输入共2行。
第1行包含1个正整数n表示n个人。
第2行包含n个用空格隔开的正整数T1,T2,……,Tn其中第i个整数Ti示编号为i
的同学的信息传递对象是编号为Ti的同学,Ti≤n且Ti≠i
数据保证游戏一定会结束。

Output

输出共 1 行,包含 1 个整数,表示游戏一共可以进行多少轮。

Sample Input

5
2 4 2 3 1
 

Sample Output

3

HINT

题意

题解:

tarjan找SCC,然后找到最小的SCC就好了

两次 dfs也可以啦~

但是TM的会爆栈……

代码

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<stdio.h>
#include<vector>
#include<cstring>
#include<stack>
using namespace std;
#define maxn 300005 vector<int> G[maxn];
int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt,num[maxn];
stack<int> S;
void dfs(int u)
{
pre[u]=lowlink[u]=++dfs_clock;
S.push(u);
for(int i=;i<G[u].size();i++)
{
int v = G[u][i];
if(!pre[v])
{
dfs(v);
lowlink[u]=min(lowlink[u],lowlink[v]);
}
else if(!sccno[v])
{
lowlink[u]=min(lowlink[u],pre[v]);
}
}
if(lowlink[u]==pre[u])
{
scc_cnt++;
for(;;)
{
int x = S.top();S.pop();
sccno[x]=scc_cnt;
num[scc_cnt]++;
if(x==u)break;
}
}
}
void find_scc(int n)
{
dfs_clock = scc_cnt = ;
memset(sccno,,sizeof(sccno));
memset(pre,,sizeof(pre));
for(int i=;i<n;i++)
if(!pre[i])dfs(i);
}
int main()
{
int n;
scanf("%d",&n);
int flag = ;
for(int i=;i<n;i++)
{
int x;scanf("%d",&x);
x--;
if(i==x)
flag = ;
G[i].push_back(x);
}
if(flag)
{
printf("1\n");
return ;
}
find_scc(n);
int ans = ;
for(int i=;i<scc_cnt;i++)
if(num[i]>)
ans = min(ans,num[i]);
printf("%d\n",ans);
}

2015 NOIP day2 t2 信息传递 tarjan的更多相关文章

  1. 2015 提高组 信息传递--tarjan找最小环

    P2661 信息传递 题目描述 有 n 个同学(编号为 1 到 n )正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为 i 的同学的信息传递对象是编号为 Ti​ 的同学. ...

  2. 【 NOIP2015 DAY1 T2 信息传递】带权并查集

    题目描述 有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学. 游戏开始时,每人都只知道自己的生日.之后每一 ...

  3. 【NOIP2015提高组】 Day1 T2 信息传递

    题目描述 有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学. 游戏开始时,每人都只知道自己的生日.之后每一 ...

  4. 【NOIP 2015 D1 T2】信息传递(图论--带权并查集/bfs)

    题目:有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学.游戏开始时,每人都只知道自己的生日.之后每一轮中, ...

  5. NOIP 2015 信息传递

    kawayi 题目描述 有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学. 游戏开始时,每人都只知道自己的 ...

  6. NOIP 2015:信息传递

    题目描述 有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学. 游戏开始时,每人都只知道自己的生日.之后每一 ...

  7. codevs 4511 信息传递(NOIP2015 day1 T2)

    4511 信息传递 NOIP2015 day1 T2 时间限制: 1 s 空间限制: 128000 KB 传送门 题目描述 Description 有个同学(编号为 1 到)正在玩一个信息传递的游戏. ...

  8. 信息传递(tarjan)

    信息传递 http://uoj.ac/problem/146 有 n 个同学(编号为 1 到n )正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为 i的同学的信息传递对象 ...

  9. 洛谷P2661 信息传递==coedevs4511 信息传递 NOIP2015 day1 T2

    P2661 信息传递 题目描述 有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学. 游戏开始时,每人都只知 ...

随机推荐

  1. OracleBulkCopy的批量数据导入

    private void button1_Click(object sender, EventArgs e) { OpenFileDialog afd = new OpenFileDialog(); ...

  2. 【转】让apache支持中文路径或者中文文件

    本帖最后由 狂人阿川 于 2013-4-12 19:13 编辑 今天在给一美国VPS客户调试他的程序的时候.发现他的网站有中文名称.貌似apache无法认识中文路径,火狐下面能下载他的文件,IE下面不 ...

  3. HDU 5749 Colmerauer 单调队列+暴力贡献

    BestCoder Round #84   1003 分析:(先奉上zimpha巨官方题解) 感悟:看到题解单调队列,秒懂如何处理每个点的范围,但是题解的一句算贡献让我纠结半天 已知一个点的up,do ...

  4. cocos2d CCLayer 触摸相关

    要让一个  CCLayer 能够接受触摸输入  需要进行如下设置: [selfsetTouchEnabled:YES]; cocos2d-x提供了两种触摸事件处理机制, 分别是CCStandardTo ...

  5. Linux环境上,Oracle常用命令

    1.启动oracle数据库: //切换至Oracle用户: [root@server36 ~]# su - oracle //进入sqlplus环境,nolog参数表示不登录: [oracle@ser ...

  6. Android自带CalendarView类实现日历视图

    文章由来:学习一下日历控件的实现,Android3.0以下的版本请查阅其他文章. 本文主要是介绍Android日历视图控件CalendarView相关的内容,然后在后面会给出一个简单的Demo. Ca ...

  7. Windows7 64位系统下无法安装网络打印机的解决方法

    背景: 公司一台HP LaserJet 1010 打印机连在一台Windows XP的电脑上,而我的是windows7 64位系统,无法安装驱动解决办法:1:去惠普官网上下载对应的64位驱动(什么Vi ...

  8. ThinkPHP中U方法与url的四种访问模式

     ThinkPHP中U方法的用处主要是完成对url地址的组装,在模板中使用U方法而不是固定写死URL地址的好处在于,一旦你的环境变化或者参数设置改变,你不需要更改模板中的任何代码.在模板中的调用格式需 ...

  9. 使用DBCC SHOW_STATISTICS展示索引的统计信息

    在开始之前搭建演示环境: USE master GO SET NOCOUNT ON --创建表结构 IF OBJECT_ID(N'ClassA', N'U') IS NOT NULL DROP TAB ...

  10. centos5 vim升级到7.4

    vim在centos中的版本为7.0,导致很多插件都无法使用,所以想到升级一下. wget ftp://ftp.vim.org/pub/vim/unix/vim-7.4.tar.bz2 tar jvz ...