原题链接

显然一个强连通分量里所有草场都可以走到,所以先用\(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. jenkins commande not found

    解决方法: http://www.geekcome.com/content-10-3868-1.html 1.控制台执行 echo $PATH 把输出的这句话复制 2.jenkins->系统管理 ...

  2. 11.14java课堂测试

    源代码: import java.*; import java.util.*; import java.io.File; import java.io.FileInputStream; import ...

  3. Archlinux 遇到的坑

    1.系统更新之后pip炸了,解决方案是用官方的get-pip安装,同时配置文件,避免使用sudo安装 2.grub不如syslinux配置快捷,入了syslinux的坑 3.平铺式桌面搭配快捷键,Hi ...

  4. 2017最新整理移动Web开发遇到的坑

    随着前端的热度不断升温,行业对前端的要求越来越高:精准无误的实现UI设计,已成为前端职业更加精细化的一种表现:随着移动互联网的发展.WebApp似乎一种不可逾越的鸿沟:越来越多的企业开始趋势于轻量级的 ...

  5. 激活 pycharm

    step1: 在本地 hosts 文件增加一行,windows 路径一般为:C:\Windows\System32\drivers\etc step2: 输入激活码 7SPIY8PDT7-eyJsaW ...

  6. 第十章 优先级队列 (c)堆排序

  7. 第十章 优先级队列 (b4)完全二叉堆:批量建堆

  8. Mysql 5.7 弱密码限制,及创建用户无密码用户

    一.介绍 1.haproxy Mysql 需要一个无密码登录的mysql用户. 2.Mysql 5.7 版本默认安装了 validate_password 插件,作用:要求密码的复杂度. 3.创建用户 ...

  9. 6. ZigZag Conversion (字符串的连接)

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  10. Codeforces Beta Round #75 (Div. 2 Only)

    Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...