BZOJ 1589 采集糖果
23333怎么调了一晚上。。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxv 100500
#define maxe 100500
using namespace std;
struct edge
{
int v,nxt;
}e[maxe];
int n,xx[maxv],g[maxv],nume=,dfn[maxv],low[maxv],times=,stack[maxv],top=,dp[maxv],ret=,hash[maxv];
bool vis[maxv];
void addedge(int u,int v)
{
e[++nume].v=v;
e[nume].nxt=g[u];
g[u]=nume;
}
void tarjan(int x)
{
vis[x]=true;dfn[x]=low[x]=++times;stack[++top]=x;
for (int i=g[x];i;i=e[i].nxt)
{
int v=e[i].v;
if (!vis[v])
{
tarjan(v);
low[x]=min(low[x],low[v]);
}
else low[x]=min(low[x],dfn[v]);
}
if (dfn[x]==low[x])
{
int p=top,cnt=;ret++;
while (dfn[stack[p]]!=low[stack[p]])
{
cnt++;
p--;
}
cnt++;
while (dfn[stack[top]]!=low[stack[top]])
{
hash[stack[top]]=ret;
if (cnt!=) dp[stack[top]]=cnt;
top--;
}
hash[stack[top]]=ret;
if (cnt!=) dp[stack[top]]=cnt;
top--;
}
}
int find(int x)
{
if (dp[x]) return dp[x];
else if (xx[x]==x) {dp[x]=;return ;}
else return dp[x]=find(xx[x])+;
}
int main()
{
scanf("%d",&n);
for (int i=;i<=n;i++)
{
scanf("%d",&xx[i]);
if (i!=xx[i])
addedge(i,xx[i]);
}
for (int i=;i<=n;i++)
{
if (!vis[i])
tarjan(i);
}
for (int i=;i<=n;i++)
{
if (i==xx[i])
{
dp[i]=;
printf("1\n");
}
else printf("%d\n",find(i));
}
return ;
}
BZOJ 1589 采集糖果的更多相关文章
- BZOJ 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
Description 每年万圣节,威斯康星的奶牛们都要打扮一番,出门在农场的N(1≤N≤100000)个牛棚里转悠,来采集糖果.她们每走到一个未曾经过的牛棚,就会采集这个棚里的1颗糖果. 农场不大, ...
- 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 ...
- 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 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 采集糖果【tarjan+记忆化搜索】
对这个奇形怪状的图tarjan,然后重新连边把图变成DAG,然后记忆化搜索即可 #include<iostream> #include<cstdio> using namesp ...
- 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 ...
- [BZOJ 1045] [HAOI2008] 糖果传递
题目链接:BZOJ 1045 Attention:数据范围中 n <= 10^5 ,实际数据范围比这要大,将数组开到 10^6 就没有问题了. 我们先来看一下下面的这个问题. 若 n 个人坐成一 ...
- BZOJ 2330: [SCOI2011]糖果 [差分约束系统] 【学习笔记】
2330: [SCOI2011]糖果 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5395 Solved: 1750[Submit][Status ...
- BZOJ 1045: [HAOI2008] 糖果传递 数学
1045: [HAOI2008] 糖果传递 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1045 Description 有n个小朋友坐 ...
随机推荐
- LA 2038
Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solu ...
- POJ 1989
#include <iostream> #define MAXN 10005 using namespace std; bool mark[MAXN]; int main() { //fr ...
- mysql 存储过程事务
DECLARE t_error INTEGER DEFAULT ; DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=; START TRAN ...
- 用C#读取,写入ini文件
[DllImport("kernel32.dll")] private static extern bool WritePrivateProfileString(string se ...
- ExtJs之Ext.util.TextMetrics
<!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...
- JAVA类型信息——反射机制
JAVA类型信息——反射机制 一.反射机制概述 1.反射机制:就是java语言在运行时拥有的一项自我观察的能力,java通过这种能力彻底了解程序自身的情况,并为下一步的动作做准备. 2.反射机制的功能 ...
- Codeforces Round #335 (Div. 2) D. Lazy Student 贪心
D. Lazy Student Student Vladislav came to his programming exam completely unprepared as usual. He ...
- Hadoop InputFormat OutputFormat
InputFormat有两个抽象方法: getSplits createRecordReader InputSplits 将数据按照Split进行切分,一个Split分给一个task执行. ...
- spring 注入失败
最近发现autowired注入总是失败. 总结下: 一个bean 要么都通过getter setter加上配置文件配置注入. <bean id="temResetService&quo ...
- hdu 1848 Fibonacci again and again (初写SG函数,详解)
思路: SG函数的应用,可取的值为不连续的固定值,可用GetSG求出SG,然后三堆数异或. SG函数相关注释见代码: 相关详细说明请结合前一篇博客: #include<stdio.h> # ...