题目大意:给你一张有向图,每个点最多一条出边,问从每个开始,走多少步会到一个已经过的点

题解:$tarjan$缩点,然后建反图$DP$

卡点:

C++ Code:

#include <cstdio>
#include <algorithm>
#define maxn 100010
int head[maxn], cnt;
struct Edge {
int to, nxt;
} e[maxn];
inline void addedge(int a, int b) {
e[++cnt] = (Edge) {b, head[a]}; head[a] = cnt;
} int n;
int nxt[maxn], ind[maxn], f[maxn]; int DFN[maxn], low[maxn], idx;
int S[maxn], top, res[maxn], scc;
bool ins[maxn];
void tarjan(int u) {
DFN[u] = low[u] = ++idx;
ins[S[++top] = u] = true;
int v = nxt[u];
if (!DFN[v]) {
tarjan(v);
low[u] = std::min(low[u], low[v]);
} else if (ins[v]) low[u] = std::min(low[u], DFN[v]);
if (low[u] == DFN[u]) {
scc++;
do {
ins[v = S[top--]] = false;
f[res[v] = scc]++;
} while (v != u);
}
} int q[maxn], h, t;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", nxt + i);
for (int i = 1; i <= n; i++) if (!DFN[i]) tarjan(i);
for (int u = 1; u <= n; u++) {
int v = nxt[u];
if (res[u] != res[v]) {
addedge(res[v], res[u]);
ind[res[u]]++;
}
}
t = -1, h = 0;
for (int i = 1; i <= scc; i++) if (!ind[i]) q[++t] = i;
while (h <= t) {
int u = q[h++];
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
f[v] += f[u];
if (!--ind[v]) q[++t] = v;
}
}
for (int i = 1; i <= n; i++) printf("%d\n", f[res[i]]);
return 0;
}

  

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    对于一个牛,它存在两种状态:1.处于联通分量 2.不处于联通分量.对于处于联通分量的牛,求出联通分量的大小:对于不处于联通分量的牛,求出其距离联通分量的路程+联通分量大小. 不同的联通分量,染上不同的 ...

  9. [P2921][USACO08DEC]在农场万圣节Trick or Treat on the Farm (记忆化搜索/DP?,Tarjan?)

    第一看还以为是水题 随便打了一个bfs只有40分…… 然后开始颓废 #include<bits/stdc++.h> #define N 100005 using namespace std ...

随机推荐

  1. 封装一个Automapper单例

    public class DataModule : IModule { public void Configure(IMapperConfigurationExpression cfg) { //cf ...

  2. springboot+websocket+sockjs进行消息推送【基于STOMP协议】

    springboot+websocket+sockjs进行消息推送[基于STOMP协议] WebSocket是在HTML5基础上单个TCP连接上进行全双工通讯的协议,只要浏览器和服务器进行一次握手,就 ...

  3. springboot 读写excel

    添加两个坐标: <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</a ...

  4. Mac安装php和redis扩展

    Mac上有特定的包管理工具homebrew,也叫brew,这里的php安装用的就是brew 1安装php brew install php@7.0. brw安装会自动管理依赖,所以不用你一个个先安装依 ...

  5. 用wireshark查看 tcpdump 抓取的mysql交互数据

    用tcpdump  抓取 mysql客户端与服务器端的交互 1开启tcpdump tcpdump -i eth0 -s 3000 port 3306 -w ~/sql.pcap 先故意输入一个错误的密 ...

  6. memory引擎和innodb引擎速度对比

    ysql> insert into innodb_test (name) select name from innodb_test; Query OK, rows affected ( min ...

  7. unity面试题二

    1.以下哪一个选项不属于Unity引擎所支持的视频格式文件(D) A.后缀为mov的文件 B.后缀为mpg的文件 C.后缀为avi的文件 D.后缀为swf的文件 2.Unity引擎使用的是左手坐标系还 ...

  8. Fiddler使用总结(二)

    在上一篇中介绍了Fiddler的基本使用方法.通过上一篇的操作我们可以直接抓取浏览器的数据包.但在APP测试中,我们需要抓取手机APP上的数据包,应该怎么操作呢? Andriod配置方法: .确保手机 ...

  9. (Python爬虫05)完善的爬虫学习大纲

  10. [Clr via C#读书笔记]Cp17委托

    Cp17委托 简单介绍 delegate回调函数机制,可以理解存储函数地址的变量类型: 类型安全: 引用类型支持逆变和协变: 回调 静态方法,实例方法 委托的本质 所有的委托都派生自System.Mu ...