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 采集糖果的更多相关文章

  1. BZOJ 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果

    Description 每年万圣节,威斯康星的奶牛们都要打扮一番,出门在农场的N(1≤N≤100000)个牛棚里转悠,来采集糖果.她们每走到一个未曾经过的牛棚,就会采集这个棚里的1颗糖果. 农场不大, ...

  2. 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 ...

  3. 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 ...

  4. 【BZOJ】1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果

    [算法]基环树DP [题意]给定若干有向基环树,每个点能走的最远路径长度. [题解] 参考:[BZOJ1589]Trick or Treat on the Farm 基环树裸DP by 空灰冰魂 考虑 ...

  5. bzoj 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果【tarjan+记忆化搜索】

    对这个奇形怪状的图tarjan,然后重新连边把图变成DAG,然后记忆化搜索即可 #include<iostream> #include<cstdio> using namesp ...

  6. 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 ...

  7. [BZOJ 1045] [HAOI2008] 糖果传递

    题目链接:BZOJ 1045 Attention:数据范围中 n <= 10^5 ,实际数据范围比这要大,将数组开到 10^6 就没有问题了. 我们先来看一下下面的这个问题. 若 n 个人坐成一 ...

  8. BZOJ 2330: [SCOI2011]糖果 [差分约束系统] 【学习笔记】

    2330: [SCOI2011]糖果 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 5395  Solved: 1750[Submit][Status ...

  9. BZOJ 1045: [HAOI2008] 糖果传递 数学

    1045: [HAOI2008] 糖果传递 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1045 Description 有n个小朋友坐 ...

随机推荐

  1. 翻译 - 元编程动态方法之public_send

    李哲 - MAY 20, 2015 原文地址:Metaprogramming Dynamic Methods: Using Public_send 作者:Friends of The Web的开发者V ...

  2. poj 2749

    Building roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6091   Accepted: 2046 De ...

  3. .NET Framework 框架简述01

    NET技术可以以规范和实现两部分来划分.   规范:   公共语言架构(Common Language Infrastructure, CLI),主要包括 1.通用类型系统(Common Type S ...

  4. tomcat下context.xml中JNDI数据源配置

    jndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务将名称和对象联系起来,使得我们可以用 ...

  5. C Primer Plus之指针

    c之精髓——指针(pointer)——用来存储地址的变量.一般来讲,指针是一个其数值为地址的变量(或更一般地说是一个数据对象). 一元运算符&可以取得变量的存储地址,一个变量的地址可以被看作是 ...

  6. Spark基础与Java Api介绍

    原创文章,转载请注明: 转载自http://www.cnblogs.com/tovin/p/3832405.html  一.Spark简介 1.什么是Spark 发源于AMPLab实验室的分布式内存计 ...

  7. Project Euler 108:Diophantine reciprocals I 丢番图倒数I

    Diophantine reciprocals I In the following equation x, y, and n are positive integers. For n = 4 the ...

  8. hive-学习笔记

    1.hive模糊搜索表  show tables like '*name*'; 2.查看表结构信息  desc formatted table_name;  desc table_name; 3.查看 ...

  9. java开发--反射技术

    学习目标: 1.什么是反射:即反射的定义, 2.反射有什么作用,能解决什么问题, 3.反射的知识点是什么, 4.反射的利弊 5.反射的例子 1.什么是反射:反射的定义: a) 能够分析类能力的程序被称 ...

  10. Java科普之算法剖析

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 从小白晋升,一路走来:从helloworld,到JFrame,再到Android:从城外小子,到内城 ...