BZOJ1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 400 Solved: 220
[Submit][Status]
Description
Input
Output
Sample Input
1 //1有一条边指向1
3 //2有一条边指向3
2 //3有一条边指向2
3
INPUT DETAILS:
Four stalls.
* Stall 1 directs the cow back to stall 1.
* Stall 2 directs the cow to stall 3
* Stall 3 directs the cow to stall 2
* Stall 4 directs the cow to stall 3
Sample Output
2
2
3
HINT
Cow 1: Start at 1, next is 1. Total stalls visited: 1. Cow 2: Start at 2, next is 3, next is 2. Total stalls visited: 2. Cow 3: Start at 3, next is 2, next is 3. Total stalls visited: 2. Cow 4: Start at 4, next is 3, next is 2, next is 3. Total stalls visited: 3.
Source
题解:
先tarjan,然后如果在一个>1的环内答案肯定就是这个环的大小,否则就是1+它的后继的ans
代码:
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 500+100
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int n,ti,tot,top,cnt;
int go[],sta[],dfn[],low[];
int head[],s[],scc[],ans[];
struct data{int go,next;}e[];
inline void insert(int x,int y)
{
e[++tot].go=y;e[tot].next=head[x];head[x]=tot;
}
inline void dfs(int x)
{
dfn[x]=low[x]=++ti;sta[++top]=x;
if(!dfn[go[x]]){dfs(go[x]);low[x]=min(low[x],low[go[x]]);}
else if(!scc[go[x]])low[x]=min(low[x],dfn[go[x]]);
if(low[x]==dfn[x])
{
cnt++;int now=;
while(now!=x)
{
now=sta[top--];
scc[now]=cnt;
s[cnt]++;
}
}
}
inline int solve(int x)
{
if(ans[x])return ans[x];
ans[x]=s[x];
if(s[x]==)ans[x]+=solve(e[head[x]].go);
return ans[x];
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();
for1(i,n)go[i]=read();
for1(i,n)if(!dfn[i])dfs(i);
for1(i,n)if(scc[i]!=scc[go[i]])insert(scc[i],scc[go[i]]);
for1(i,n)printf("%d\n",solve(scc[i]));
return ;
}
BZOJ1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果的更多相关文章
- bzoj千题计划161:bzoj1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
http://www.lydsy.com/JudgeOnline/problem.php?id=1589 tarjan缩环后拓扑排序上DP #include<cstdio> #includ ...
- 【强连通分量缩点】【记忆化搜索】bzoj1589 [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
缩成DAG f(i)表示以i为起点的最长路 #include<cstdio> #include<cstring> #include<algorithm> #incl ...
- [BZOJ1589] [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果(tarjan缩点 + 记忆化搜索)
传送门 先用tarjan缩点,再记忆话搜索一下 #include <stack> #include <cstdio> #include <cstring> #inc ...
- 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 4 ...
- 【BZOJ】1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
[算法]基环树DP [题意]给定若干有向基环树,每个点能走的最远路径长度. [题解] 参考:[BZOJ1589]Trick or Treat on the Farm 基环树裸DP by 空灰冰魂 考虑 ...
- BZOJ 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
Description 每年万圣节,威斯康星的奶牛们都要打扮一番,出门在农场的N(1≤N≤100000)个牛棚里转悠,来采集糖果.她们每走到一个未曾经过的牛棚,就会采集这个棚里的1颗糖果. 农场不大, ...
- bzoj 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果【tarjan+记忆化搜索】
对这个奇形怪状的图tarjan,然后重新连边把图变成DAG,然后记忆化搜索即可 #include<iostream> #include<cstdio> using namesp ...
- LGOJ P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm
今天我来给大家带来一片蒟蒻题解 ~~真香 LGOJ P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题目描述 每年,在威斯康星州,奶牛们都会穿上 ...
- 缩点【洛谷P2921】 [USACO08DEC]在农场万圣节Trick or Treat on the Farm
[洛谷P2921] [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...
随机推荐
- 外观模式-Facade
外观模式是为了解决类与类之间依赖关系的,外观模式将类间关系放在一个Facade类中,降低了类类之间的耦合度,该模式中不涉及接口 举一个经典的例子: CPU类: public class CPU { p ...
- Axiom3D学习日记 0.Axiom基础知识
Axiom 3D Engine An open-source, cross-platform, managed 3D rendering engine for DirectX, XNA and Ope ...
- ftp不能上传解决办法
自己的服务器,然后我分好ftp,有一天,有个客户要传东西,发现怎么也传不上,但是可以下载,,,,弄了半天,目录权限也是完全访问,但还是不行,原来是serv-u分配的空间小了啊,所以只能下载不能上传.. ...
- TFS 服务器更换后工作区无法绑定
需要删除工作区,删除命令如下 tf workspace /delete 工作区名;创建的用户 /server:TFS服务器 例 tf workspace /delete WHQ-PC;whq /ser ...
- StringHelper类,内容截取,特别适合资讯展示列表
public class StringHelper { /// <summary> /// 截字符串 /// </summary> ...
- Log4j简单配置
Log4j是一组强大的日志组件,在项目中时常需要用它提供一些信息,这两天学习了一下它的简单配置. 第一步,我们需要导入log4j-1.2.14.jar到lib目录下 第二步,在src下建立log4j. ...
- AspNetPage 使用案例
.首先在DBHelper创建一个方法,用于执行存储过程 public static DataTable ExecuteProc(string sql,params SqlParameter[] par ...
- 网络编程(学习整理)---2--(Udp)实现简单的控制台聊天室
1.UDP协议: 总结一下,今天学习的一点知识点! UDP也是一种通信协议,常被用来与TCP协议作比较!我们知道,在发送数据包的时候使用TCP协议比UDP协议安全,那么到底安全在哪里呢?怎么理解呢! ...
- phpcms V9 内容模型管理(转)
转自:http://www.cnblogs.com/Braveliu/p/5102627.html [1]理解模型 模型,系统知识的抽象表示.既然抽象了,那就得脑补一下.大家都是面向对象设计的专业人员 ...
- php实现的太平洋时间和北京时间互转的自定义函数
date_default_timezone_set('Asia/Shanghai'); $time = time(); } date_default_timezone_set('Pacific/Api ...