基环树森林,然而我比较菜,直接tarjan找环。

发现缩点之后变成了DAG,每一个点往下走一定会走到一个环,缩点之后搜一遍看看会走到哪个环以及那个环的编号是多少,答案就是环的$siz$$ + $要走的路程。

比较垃圾的我忘记了判重边WA了好多发……

时间复杂度$O(n)$。

Code:

#include <cstdio>
#include <cstring>
using namespace std; const int N = 1e5 + ;
const int inf = << ; int n, to[N], dfsc = , dfn[N], low[N], cur[N];
int scc = , siz[N], top = , sta[N], bel[N], dis[N];
int tot = , head[N];
bool vis[N], isCur[N]; struct Edge {
int to, nxt;
} e[N]; inline void add(int from, int ver) {
e[++tot].to = ver;
e[tot].nxt = head[from];
head[from] = tot;
} inline void read(int &X) {
X = ; char ch = ; int op = ;
for(; ch > ''|| ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline int min(int x, int y) {
return x > y ? y : x;
} inline int max(int x, int y) {
return x > y ? x : y;
} void tarjan(int x) {
dfn[x] = low[x] = ++dfsc;
sta[++top] = x, vis[x] = ; int y = to[x];
if(!dfn[y]) {
tarjan(y);
low[x] = min(low[x], low[y]);
} else if(vis[y]) low[x] = min(low[x], dfn[y]); if(low[x] == dfn[x]) {
++scc;
for(; sta[top + ] != x; --top) {
bel[sta[top]] = scc;
siz[scc]++;
vis[sta[top]] = ;
}
}
} void dfs(int x) {
vis[x] = ;
if(isCur[x]) {
dis[x] = , cur[x] = x;
return;
}
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(!vis[y]) dfs(y);
dis[x] = dis[y] + , cur[x] = cur[y];
}
} int main() {
// freopen("testdata.in", "r", stdin);
// freopen("mine.out", "w", stdout); read(n);
for(int i = ; i <= n; i++) read(to[i]); for(int i = ; i <= n; i++)
if(!dfn[i]) tarjan(i); /* for(int i = 1; i <= n; i++)
printf("%d ", bel[i]);
printf("\n"); */
for(int i = ; i <= n; i++)
if(to[i] == i) isCur[bel[i]] = ;
for(int i = ; i <= scc; i++)
if(siz[i] >= ) isCur[i] = ; for(int i = ; i <= n; i++) {
if(bel[i] == bel[to[i]]) continue;
add(bel[i], bel[to[i]]);
} for(int i = ; i <= scc; i++)
if(!vis[i]) dfs(i); /* for(int i = 1; i <= scc; i++)
printf("%d ", cur[i]);
printf("\n"); */ for(int i = ; i <= n; i++) {
if(isCur[bel[i]]) printf("%d\n", siz[bel[i]]);
else printf("%d\n", dis[bel[i]] + siz[cur[bel[i]]]);
} return ;
}

Luogu 2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm的更多相关文章

  1. 洛谷 2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm

    [题解] 就是基环外向树森林找环,然后从环向外统计size就可以了. #include<cstdio> #include<cstring> #include<algori ...

  2. LUOGU P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm

    传送门 解题思路 记忆化搜索,如果搜到环,就将环的大小处理出来. 代码 #include<iostream> #include<cstdio> #include<cstr ...

  3. LGOJ P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm

    今天我来给大家带来一片蒟蒻题解 ~~真香 LGOJ P2921  [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题目描述 每年,在威斯康星州,奶牛们都会穿上 ...

  4. 缩点【洛谷P2921】 [USACO08DEC]在农场万圣节Trick or Treat on the Farm

    [洛谷P2921] [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...

  5. P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm(Tarjan+记忆化)

    P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题意翻译 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...

  6. 洛谷——P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm

    P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题意翻译 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...

  7. C++ 洛谷 P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题解

    P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 分析: 这棵树上有且仅有一个环 两种情况: 1.讨论一个点在环上,如果在则答案与它指向点相同, 2 ...

  8. [USACO08DEC]在农场万圣节Trick or Treat on the Farm

    题目描述 Every year in Wisconsin the cows celebrate the USA autumn holiday of Halloween by dressing up i ...

  9. P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 记忆化搜索dfs

    题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N<=100,000)个牛棚隔间中留下的糖果,以此来庆祝美国秋天的万圣节. 由于牛棚不太大,FJ通过指定奶牛必须遵 ...

随机推荐

  1. vector的内存分配机制分析

    该程序初步演示了我对vector在分配内存的时候的理解.可能有误差,随着理解的改变,改代码可以被修改. /* 功能说明: vector的内存分配机制分析. 代码说明: vector所管理的内存地址是连 ...

  2. Linux命令学习(18):route命令

    版权声明更新:2017-05-20博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下面的route命令. ...

  3. oracle+110个常用函数

    1.ASCII  返回与指定的字符对应的十进制数;  SQL> select ascii(A) A,ascii(a) a,ascii(0) zero,ascii( ) space from du ...

  4. 第十篇 javascript基础语法

    1)基本概念 a) 语句(statement)是为了完成某种任务而进行的操作,语句以分号结尾,一个分号就表示一个语句结束.:如:var a = 1; b) 表达式(expression),指一个为了得 ...

  5. 蓝桥杯 算法训练 ALGO-142 P1103

    算法训练 P1103   时间限制:1.0s   内存限制:256.0MB 编程实现两个复数的运算.设有两个复数 和 ,则他们的运算公式为: 要求:(1)定义一个结构体类型来描述复数. (2)复数之间 ...

  6. Linux python安装

    Linux 安装 Python3.5   1. 准备 操作系统:Red Hat Enterprise Linux Server release 6.5 (Santiago) Python 安装包:Py ...

  7. 网络监控之一:ss(Socket Statistics)

    ss是Socket Statistics的缩写. 顾名思义,ss命令可以用来获取socket统计信息,它可以显示和netstat类似的内容.但ss的优势在于它能够显示更多更详细的有关TCP和连接状态的 ...

  8. Oracle 高可用作业测试

    1.RAC 是一种: A 分布式数据库B 内存共享多节点数据库C 能够保护数据安全的架构 2.RAC 和CRS 的区别是: A 前者表示一种架构,后者是一套软件B 前者表示一套软件,后者是一种架构 C ...

  9. C语言生成程序问题

    问题: 我用VS2013写好C语言程序调试运行后就在debug文件夹下生成了EXE文件,可以在本机运行.但是这个EXE文件在别的没装过VS2013的电脑上就不能直接运行,说丢失MSVCR120D.dl ...

  10. mongodb用mongoose得到的对象不能增加属性解决

    一,先定义了一个goods(商品)的models var mongoose = require('mongoose'); var Schema = mongoose.Schema; var produ ...