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通过指定奶牛必须遵 ...
随机推荐
- STL迭代器辅助函数——advance
Advance(i, n) increments the iterator i by the distance n. If n > it it , the call has no effect. ...
- OpenCV-Python在图片上输出中文
OpenCV中在图片上输出中文一般需要借助FreeType库实现.FreeType库是一个完全免费(开源)的.高质量的且可移植的字体引擎,它提供统一的接口来访问多种字体格式文件.但使用FreeType ...
- C++继承细节 -1
为什么基类析构函数最好要使用 virtual 进行修饰? class A { private: ...... public: ~A(); A() {} }; class B : public A { ...
- 关于nginx做代理,uwsgi gunicorn等服务器做后端时
(1) 响应数据过大 被截断的问题 通常看buffers参数的设置(缓冲从后端服务器的应答) uwsgi的参数是 uwsgi_buffers 4 128k gunicorn 设置代理参数 proxy_ ...
- Springboot演示小Demo
模拟数据库演示springboot小测试 1.编写一个实体类:user package com.wisezone.test; import java.io.Serializable; public c ...
- ACM学习历程—HDU5668 Circle(数论)
http://acm.hdu.edu.cn/showproblem.php?pid=5668 这题的话,假设每次报x个,那么可以模拟一遍, 假设第i个出局的是a[i],那么从第i-1个出局的人后,重新 ...
- spark gateway引发:跟踪Cloudera安装服务异常日志跟踪
spark gateway是用于接收cloudera管理的应用:可以上报数据,不影响正常使用.启动gateway失败,我觉得可能是因为配置问题? 这个问题可能比较深,因为我通过查看日志(clouder ...
- oracle+110个常用函数
1.ASCII 返回与指定的字符对应的十进制数; SQL> select ascii(A) A,ascii(a) a,ascii(0) zero,ascii( ) space from du ...
- Oracle查询数据表结构(字段,类型,大小,备注)
作用:想要生成整个Oracle数据库所有表结构WORD文档(数据库设计说明书) Oracle数据库字典介绍 Oracle数据字典是有表和视图组成的,存储有关数据库结构信息的一些数据库对象.数据库 ...
- java中io的详解
注:本文全篇转载于:http://blog.csdn.net/taxueyingmei/article/details/7697042,觉得讲的挺详细,就借过来看看,挺不错的文章. 先贴一张图 Jav ...