基环树森林,然而我比较菜,直接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. BEC listen and translation exercise 8

    The double-decker plane that can carry over 550 passengers dwarfs all other commercial jets. In just ...

  2. New Concept English three (53)

    30w/m 56errors The Scandinavian countries are much admired all over the world for their enlightened ...

  3. 15_游戏编程模式EventQueue

    #### 两个例子 .GUI event loop ``` while (running) { // 从事件队列里获取一个事件 Event event = getNextEvent(); // Han ...

  4. windows下mysql提示Can't connect to MySQL server on 'localhost'

    1.开cmd输入mysqld --defaults-file="c:\Program Files (x86)\MySQL\MySQL Server 5.6\my-default.ini&qu ...

  5. [HDU5324]Boring Class

    vjudge sol 字典序最小可以通过倒着\(dp\)解决.对每个\(i\)记录它可以转移到的\(dp\)值最大且字典序最小的\(nxt_i\). 尝试着写一下\(dp\)式子. \[dp_i=ma ...

  6. CH5E01[NOIP2010] 乌龟棋[暴力]

    众所周知,由于这个人太菜了,所以她又来切上古水题了. 显然最多$40^4$种状态,暴力跑出可以拼出多少种状态,然后按序号从小到大对应的状态瞎转移即可. 我知道我想繁了,但是不想改了,因为思路一出来,没 ...

  7. QT:QString、QByteArray和char *的转换 【转载】

    原文网址:http://blog.csdn.net/light1028/article/details/7899541 第一种,数据流的方式,这里只说从QByteArray转向QString. QBy ...

  8. 基于JDK1.7.0_80与JDK1.8.0_66做的分析

    JDK1.7中 使用一个Entry数组来存储数据,用key的hashcode取模来决定key会被放到数组里的位置,如果hashcode相同,或者hashcode取模后的结果相同(hash collis ...

  9. DropShadowEffect导致下拉框控件抖动

    <!--<Border.Effect> <DropShadowEffect Direction="180" BlurRadius="1" ...

  10. laravel 对于ajax请求返回的数据

    ajax在调试器中的位置  XHR 代表 XMlHTTPREQUET 一般ajax请求php的时候我们需要给返回什么数据呢? 一般我都是直接renturn 数组的 其实也没啥问题 但是还是感觉第三种写 ...