基环树森林,然而我比较菜,直接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. tar 多文件解压压缩

    tar 多文件解压:因为tar -zxvf一次值能解压一个文件,所以用xargs -n1 .先查找 ls *gz | xargs -n1 tar -zxvf .要解压的文件在list中 cat lis ...

  2. Soldier and Badges (set的检索简单运用)

    Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge has a coolne ...

  3. Android UI--提高Android UI体验

    1,自定义虚拟键盘 当一个用户被要求在一个文本框输入时希望又怎样的体验?  从用户需求来看,虚拟键盘应该改变以帮助用户输入的数据.这里是一些例子: 如果一个视图是一个电子邮件地址,一个键盘的“@”符号 ...

  4. HIVE-分区表详解以及实例

    HIVE中的分区表是什么,我们先看操作,然后再来体会. 创建一个分区表,分区的单位时dt和国家名 hive> create table logs(ts bigint,line string) & ...

  5. centos7下搭建ceph luminous(12.2.1)--无网或网络较差

    本博客的主要内容是在centos7下搭建luminous,配置dashboard,搭建客户端使用rbd,源码安装ceph,最后给出一些较为常用的命令.本博客针对初次接触ceph的人群. 搭建环境: 主 ...

  6. sysfs: cannot create duplicate filename '/class/spi_master/spi1'

    在编写SPI驱动程序的时候,遇到如下问题 s3c2410-spi s3c2410-spi.0: master is unqueued, this is deprecated ------------[ ...

  7. Ubuntu登录异常: 输入正确的密码, 但是却无法进入系统, 总是返回到登录界面, 但是用ctrl+alt+F1-F文字界面登录都可以进入。

    今天打开电脑的时候, 在输入密码之后, 未进入ubuntu的桌面, 而是显示了几行英文之后有返回到了登录界面.显示的英文如下: could not write bytes: Broken pipe   ...

  8. 2016.8.17服务器端数据库用户导入导出方法 expdp和impdp

    EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用. EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用,不能在客户端使用. IMP只适用于EXP导出的 ...

  9. spring特点与好处

    Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development a ...

  10. leetcode874

    这道题直接按照题意来解,建立坐标系和移动方案,思路是比较简单的.只是需要注意需要使用set来判断是否遇到障碍,否则会超时. int robotSim(vector<int>& co ...