原题链接

显然一个强连通分量里所有草场都可以走到,所以先用\(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. 判断素数(翁凯男神MOOC)

    从2到x-1测试是否可以整除 int isPrime(int x); int main(int argc, char **argv) { int x; scanf("%d",&am ...

  2. Python __init__ 特殊方法

    在Python中有很多以双下划线开头且以双下划线结尾的固定方法.他们会在特定的时机被触发执行. __init__ 就是其中之一,它会在实例化之后自动被调用.以完成实例的初始化. >>> ...

  3. python 多线程操作数据库

    如果使用多线程操作数据库,容易引起多用户操作锁表 OperationalError: (2013, 'Lost connection to MySQL server during query') 使用 ...

  4. 【翻译】 View Frustum Culling --1 View Frustum’s Shape

    这是一些列来自lighthouse3d的视锥体裁剪教程.旨在学习总结,及便于查阅. 1.视锥体的形状 在OpenGL中,透视投影是由两个函数定义的gluPerspective和gluLookAt.我们 ...

  5. modal template

    <div class="modal fade" id="tmp_order_modal" tabindex="-1" role=&qu ...

  6. MySql安装和基本管理&mysql语句

    MySql安装和基本管理   本章内容: mysql的安装.启动 mysql破解密码 统一字符编码 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下 ...

  7. cloudera cdh5.13.0 vmware 快速安装

    1. 从官网上载VMWARE VM快速安装包 https://www.cloudera.com/downloads/quickstart_vms/5-12.html 2. 下载后的安装包,解压之后得到 ...

  8. poj1984(带权并查集)

    题目链接:http://poj.org/problem?id=1984 题意:给定n个farm,m条边连接farm,k组询问,询问根据前t3条边求t1到t2的曼哈顿距离,若不可求则输出-1. 思路:类 ...

  9. 分布式处理框架MapReduce的深入简出

    1).MapReduce的概述 2).MapReduce 编程模型 3).MapReduce架构 4).MapReduce编程 Google MapReduce论文,论文发表于2004年 Hadoop ...

  10. PHP文件上传与下载

    一:上传文件与报错 $_FILES 超全局数组,包含了有关上传文件的所有信息! 而且,这个数组中只包含文件相关信息,其他数据依然在$_POST里面$_FILES是一个二维数组,每上传一个文件,都是数组 ...