原题链接

显然一个强连通分量里所有草场都可以走到,所以先用\(tarjan\)找强连通并缩点。

对于缩点后的\(DAG\),先复制一张新图出来,然后对于原图中的每条边的终点向新图中该边对应的那条边的起点连一条边,表示逆向走一次,且之后不会再逆向了。

最后在该图上跑\(SPFA\)求单源最长路即可。

#include<cstdio>
using namespace std;
const int N = 1e5 + 10;
struct eg {
int x, y;
};
eg a[N];
int fi[N], di[N], ne[N], cfi[N << 1], cdi[N << 2], cda[N << 2], cne[N << 2], dfn[N], low[N], bl[N], si[N], sta[N], q[N << 2], dis[N << 1], l, lc, ti, tp, SCC;
bool v[N << 1];
inline int re()
{
int x = 0;
char c = getchar();
bool p = 0;
for (; c < '0' || c > '9'; c = getchar())
p |= c == '-';
for (; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - '0';
return p ? -x : x;
}
inline void add(int x, int y)
{
di[++l] = y;
ne[l] = fi[x];
fi[x] = l;
}
inline void add_c(int x, int y, int z)
{
cdi[++lc] = y;
cda[lc] = z;
cne[lc] = cfi[x];
cfi[x] = lc;
}
inline int minn(int x, int y)
{
return x < y ? x : y;
}
void tarjan(int x)
{
int i, y;
dfn[x] = low[x] = ++ti;
sta[++tp] = x;
v[x] = 1;
for (i = fi[x]; i; i = ne[i])
if (!dfn[y = di[i]])
{
tarjan(y);
low[x] = minn(low[x], low[y]);
}
else
if (v[y])
low[x] = minn(low[x], dfn[y]);
if (!(dfn[x] ^ low[x]))
{
SCC++;
do
{
y = sta[tp--];
bl[y] = SCC;
si[SCC]++;
v[y] = 0;
} while (x ^ y);
}
}
void spfa()
{
int head = 0, tail = 1, i, x, y;
q[1] = bl[1];
while (head ^ tail)
{
x = q[++head];
v[x] = 0;
for (i = cfi[x]; i; i = cne[i])
if (dis[y = cdi[i]] < dis[x] + cda[i])
{
dis[y] = dis[x] + cda[i];
if (!v[y])
{
q[++tail] = y;
v[y] = 1;
}
}
}
}
int main()
{
int i, m, x, y, n;
n = re();
m = re();
for (i = 1; i <= m; i++)
{
a[i].x = re();
a[i].y = re();
add(a[i].x, a[i].y);
}
for (i = 1; i <= n; i++)
if (!dfn[i])
tarjan(i);
for (i = 1; i <= m; i++)
{
x = bl[a[i].x];
y = bl[a[i].y];
if (x ^ y)
{
add_c(x, y, si[x]);
add_c(y, x + SCC, si[y]);
add_c(x + SCC, y + SCC, si[x]);
}
}
add_c(bl[1], bl[1] + SCC, si[bl[1]]);
spfa();
printf("%d", dis[bl[1] + SCC]);
return 0;
}

洛谷3119 [USACO15JAN]草鉴定Grass Cownoisseur的更多相关文章

  1. 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur 解题报告

    P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 约翰有\(n\)块草场,编号1到\(n\),这些草场由若干条单行道相连.奶牛贝西是美味牧草的鉴赏家,她想到达尽可 ...

  2. 洛谷——P3119 [USACO15JAN]草鉴定Grass Cownoisseur

    P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 In an effort to better manage the grazing patterns of hi ...

  3. 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur (SCC缩点,SPFA最长路,枚举反边)

    P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 In an effort to better manage the grazing patterns of hi ...

  4. 洛谷—— P3119 [USACO15JAN]草鉴定Grass Cownoisseur || BZOJ——T 3887: [Usaco2015 Jan]Grass Cownoisseur

    http://www.lydsy.com/JudgeOnline/problem.php?id=3887|| https://www.luogu.org/problem/show?pid=3119 D ...

  5. 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur

    屠龙宝刀点击就送 Tarjan缩点+拓扑排序 以后缩点后建图看n范围用vector ,或者直接用map+vector 结构体里数据要清空 代码: #include <cstring> #i ...

  6. Luogu 3119 [USACO15JAN]草鉴定Grass Cownoisseur

    思路很乱,写个博客理一理. 缩点 + dp. 首先发现把一个环上的边反向是意义不大的,这样子不但不好算,而且相当于浪费了一次反向的机会.反正一个强连通分量里的点绕一遍都可以走到,所以我们缩点之后把一个 ...

  7. [USACO15JAN]草鉴定Grass Cownoisseur(分层图+tarjan)

    [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 In an effort to better manage the grazing patterns of his cows ...

  8. 【洛谷P3119】[USACO15JAN]草鉴定Grass Cownoisseur

    草鉴定Grass Cownoisseur 题目链接 约翰有n块草场,编号1到n,这些草场由若干条单行道相连.奶牛贝西是美味牧草的鉴赏家,她想到达尽可能多的草场去品尝牧草. 贝西总是从1号草场出发,最后 ...

  9. P3119 [USACO15JAN]草鉴定Grass Cownoisseur

    题目描述 In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-w ...

随机推荐

  1. 配置linux的ip、网络等

      之前配过ubuntu的..以为centos的也是这么配置,结果照抄下来,启动报错哈哈...网上搜下资料发现centos配置需要不少文件.忘了以后再参考下- ubuntu的,这样配置 vim /et ...

  2. spring boot 2 内嵌Tomcat Stopping service [Tomcat]

    我在使用springboot时,当代码有问题时,发现控制台打印下面信息: Connected to the target VM, address: '127.0.0.1:42091', transpo ...

  3. HttpClient之EntityUtils对象

    最近在学习安卓并用thinkphp做后台,为了抵抗自己的烂记性,就在这里记录一下当我从tp后台获取到json串传到安卓客户端所用到的一个方法函数. EntityUtils对象是org.apache.h ...

  4. Flex Basis与Width的区别

    [Flex Basis与Width的区别] Flex Items的应用准则 content –> width –> flex-basis (limted by max|min-width) ...

  5. 数据库类型空间效率探索(三)-char

    测试环境 表信息 表数据量22.23万,占用空间44.494M 用到的sql语句 增加列:alter table t_type add column new_column char(1) defaul ...

  6. zookeeper报错: org.I0Itec.zkclient.exception.ZkMarshallingError: java.io.EOFException

    zookeeper报错: org.I0Itec.zkclient.exception.ZkMarshallingError: java.io.EOFException 主要因为是没有序列化. 可以使用 ...

  7. dedecms list 添加自定义字段方法

    在内容模型管理中,添加字段时需这样:

  8. CentOS 6 UNEXPECTED INCONSISTENCY RUN fsck MANUALLY

    1:按Control-D,系统自动重启: 2:直接输入root的密码进入命令行 3:看网上的介绍需要输入mount |grep “on/” 找到root的分区,我试过后无效 4:直接输入fsck -y ...

  9. i2c初步理解

    引用自:http://www.cnblogs.com/zym0805/archive/2011/07/31/2122890.html I2C是由Philips公司发明的一种串行数据通信协议,仅使用两根 ...

  10. python 学习笔记---Locust 测试服务端性能

    由于人工智能的热度, python目前已经成为最受欢迎的编程语言,一度已经超越Java . 本文将介绍开源的python 测试工具: locust 使用步骤: 1. 安装python 3.0以上版本 ...