Luogu 2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm
基环树森林,然而我比较菜,直接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的更多相关文章
- 洛谷 2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm
[题解] 就是基环外向树森林找环,然后从环向外统计size就可以了. #include<cstdio> #include<cstring> #include<algori ...
- LUOGU P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm
传送门 解题思路 记忆化搜索,如果搜到环,就将环的大小处理出来. 代码 #include<iostream> #include<cstdio> #include<cstr ...
- 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< ...
- P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm(Tarjan+记忆化)
P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题意翻译 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...
- 洛谷——P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm
P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题意翻译 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...
- C++ 洛谷 P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题解
P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 分析: 这棵树上有且仅有一个环 两种情况: 1.讨论一个点在环上,如果在则答案与它指向点相同, 2 ...
- [USACO08DEC]在农场万圣节Trick or Treat on the Farm
题目描述 Every year in Wisconsin the cows celebrate the USA autumn holiday of Halloween by dressing up i ...
- P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 记忆化搜索dfs
题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N<=100,000)个牛棚隔间中留下的糖果,以此来庆祝美国秋天的万圣节. 由于牛棚不太大,FJ通过指定奶牛必须遵 ...
随机推荐
- Git中从远程的分支获取最新的版本到本地方式
Git中从远程的分支获取最新的版本到本地方式如下, 如何更新下载到代码到本地,请参阅ice的博客基于Github参与eoe的开源项目指南 方式一 . 查看远程仓库 $ git remote -v eo ...
- 用Python+Django1.9在Eclipse环境下开发web网站
最近想学习一下python django, 按网上各位大神们的说明,试着做了一下,这里记录下来,做个笔记. 参考 http://www.cnblogs.com/linjiqin/p/3595891.h ...
- ubuntu lts install licode tag pre-v5.4
1. Requirements Ubuntu 14.04 LTS 2. Clone Licode codeYou first need to clone our code from github.Yo ...
- Azure VNet介绍
Azure VNet的介绍 VNet是Azure云中逻辑隔离的虚拟网络.它包含两个含义: Azure的用户可以在VNet中创建自己的各种资源,感觉想自己的数据中心中一样; 在一个VNet中创建的资源和 ...
- SpringMVC的环境搭建
MyBatis框架-->持久层框架-->Object[对象]Relation[关系型数据库]Mapping[在MyBatis的体现是哪个映射文件中国的<resultMap>标签 ...
- Celery-4.1 用户指南: Optimizing (优化)
简介 默认的配置做了很多折中考虑.它不是针对某个情况优化的,但是大多数情况下都工作的非常好. 基于一个特殊的使用场景,有很多优化可以做. 优化可以应用到运行环境的不同属性,可以是任务执行的时间,使用的 ...
- 空中楼阁 ( House )最短路
题目描述: 话说Z4阴差阳错地来到了神秘岛.不久,他们发现,这是一个由n个小岛和一个中心岛组成的群岛,群岛之间有m座桥.令他们感到惊讶的是,这些桥并不是固定不变的,经较长时间的观察,发现它们会随时间作 ...
- SqlServer——游标
参考:http://www.cnblogs.com/94cool/archive/2010/04/20/1715951.html http://www.cnblogs.com/moss_tan_ju ...
- XE Button Color
XE Button Color,FMX Button 颜色 也可以放个rectangle+Glyph控件. http://blogs.embarcadero.com/sarinadupont/2014 ...
- Android Fragment用法详解(2)--动态添加Fragment
在上一篇文章<Android Fragment用法详解(1)--静态使用Fragment>我们讲解了Fragment的最简单的用法.这次我们来说一说Fragment复杂一丢丢的用法.在代码 ...