题目描述

In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-way cow paths all over his farm. The farm consists of N fields, conveniently numbered 1..N, with each one-way cow path connecting a pair of fields. For example, if a path connects from field X to field Y, then cows are allowed to travel from X to Y but not from Y to X.

Bessie the cow, as we all know, enjoys eating grass from as many fields as possible. She always starts in field 1 at the beginning of the day and visits a sequence of fields, returning to field 1 at the end of the day. She tries to maximize the number of distinct fields along her route, since she gets to eat the grass in each one (if she visits a field multiple times, she only eats the grass there once).

As one might imagine, Bessie is not particularly happy about the one-way restriction on FJ's paths, since this will likely reduce the number of distinct fields she can possibly visit along her daily route. She wonders how much grass she will be able to eat if she breaks the rules and follows up to one path in the wrong direction. Please compute the maximum number of distinct fields she can visit along a route starting and ending at field 1, where she can follow up to one path along the route in the wrong direction. Bessie can only travel backwards at most once in her journey. In particular, she cannot even take the same path backwards twice.

约翰有n块草场,编号1到n,这些草场由若干条单行道相连。奶牛贝西是美味牧草的鉴赏家,她想到达尽可能多的草场去品尝牧草。

贝西总是从1号草场出发,最后回到1号草场。她想经过尽可能多的草场,贝西在通一个草场只吃一次草,所以一个草场可以经过多次。因为草场是单行道连接,这给贝西的品鉴工作带来了很大的不便,贝西想偷偷逆向行走一次,但最多只能有一次逆行。问,贝西最多能吃到多少个草场的牧草。

输入输出格式

输入格式:

INPUT: (file grass.in)

The first line of input contains N and M, giving the number of fields and the number of one-way paths (1 <= N, M <= 100,000).

The following M lines each describe a one-way cow path. Each line contains two distinct field numbers X and Y, corresponding to a cow path from X to Y. The same cow path will never appear more than once.

输出格式:

OUTPUT: (file grass.out)

A single line indicating the maximum number of distinct fields Bessie

can visit along a route starting and ending at field 1, given that she can

follow at most one path along this route in the wrong direction.

输入输出样例

输入样例#1: 复制

7 10
1 2
3 1
2 5
2 4
3 7
3 5
3 6
6 5
7 2
4 7
输出样例#1: 复制

6

说明

SOLUTION NOTES:

Here is an ASCII drawing of the sample input:

v---3-->6

7 |\ |

^\ v \ |

| \ 1 | | | v | v 5

4<--2---^

Bessie can visit pastures 1, 2, 4, 7, 2, 5, 3, 1 by traveling

backwards on the path between 5 and 3. When she arrives at 3 she

cannot reach 6 without following another backwards path.

1A爽,缩点后变成一棵树,spfa处理出树上每个点到1所在点的最长路,然后枚举连向点1和点1连向的点之间的边,答案就是这两个点最长路的和

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = ;
inline int read() {
int x=,f=;
char c=getchar();
while(c<''||c>'') {
if(c=='-')f=-;
c=getchar();
}
while(c<=''&&c>='') {
x=x*+c-'';
c=getchar();
}
return x*f;
}
int n,m;
struct node{
int v,next;
}edge[maxn],E1[maxn],E2[maxn];
int head[maxn],H1[maxn],H2[maxn],num,N1,N2;
void add_edge(int x,int y) {
edge[++num].v=y;edge[num].next=head[x];head[x]=num;
}
void A1(int x,int y) {
E1[++N1].v=y;E1[N1].next=H1[x];H1[x]=N1;
}
void A2(int x,int y){
E2[++N2].v=y;E2[N2].next=H2[x];H2[x]=N2;
}
int dfn[maxn],low[maxn],tn=,stack[maxn],color[maxn],color_cnt;
int top=,cnt[maxn];bool vis[maxn];
void tarjan(int x) {
dfn[x]=low[x]=++tn;stack[++top]=x,vis[x]=;
for(int i=head[x];i;i=edge[i].next) {
int v=edge[i].v;
if(!dfn[v]) {
tarjan(v);
low[x]=min(low[x],low[v]);
}
else if(vis[v]) low[x]=min(low[x],dfn[v]);
}
if(dfn[x]==low[x]) {
++color_cnt;
while(stack[top]!=x) {
vis[stack[top]]=;
color[stack[top]]=color_cnt;
top--;
cnt[color_cnt]++;
}
cnt[color_cnt]++;
color[x]=color_cnt;vis[x]=;top--;
}
}
int dis1[maxn],dis2[maxn];
int que[maxn*];
bool can[maxn],can2[maxn];
void topo1(int S) {
int h=,t=;
que[]=S;
dis1[S]=cnt[S];
memset(vis,,sizeof vis);
vis[S]=;can[S]=;
while(h<=t) {
int u=que[h++];
for(int i=H1[u];i;i=E1[i].next) {
int v=E1[i].v;can[v]=;
if(dis1[v]<dis1[u]+cnt[v]) {
dis1[v]=max(dis1[v],dis1[u]+cnt[v]);
if(!vis[v])que[++t]=v,vis[v]=;
}
}
vis[u]=;
}
}
void topo2(int S) {
int h=,t=;
que[]=S;
dis2[S]=cnt[S];
memset(vis,,sizeof vis);
vis[S]=;can2[S]=;
while(h<=t) {
int u=que[h++];
for(int i=H2[u];i;i=E2[i].next) {
int v=E2[i].v;can2[v]=;
if(dis2[v]<dis2[u]+cnt[v]) {
dis2[v]=max(dis2[v],dis2[u]+cnt[v]);
if(!vis[v])que[++t]=v,vis[v]=;
}
}
vis[u]=;
}
}
int main() {
n=read(),m=read();
for(int a,b,i=;i<=m;++i) {
a=read(),b=read();
add_edge(a,b);
}
for(int i=;i<=n;++i)
if(!dfn[i])tarjan(i);
int S=color[];
for(int i=;i<=n;++i) {
for(int j=head[i];j;j=edge[j].next) {
int v=edge[j].v;
if(color[v]!=color[i]){
A1(color[i],color[v]);
A2(color[v],color[i]);
}
}
}
topo1(color[]);
topo2(color[]);
int ans=;
for(int i=;i<=color_cnt;++i) {
for(int j=H1[i];j;j=E1[j].next) {
int v=E1[j].v;
if(can2[i]&&can[v]) {
ans=max(ans,dis2[i]+dis1[v]-cnt[color[]]);
}
}
}
printf("%d\n",ans);
return ;
}

luogu P3119 [USACO15JAN]草鉴定Grass Cownoisseur的更多相关文章

  1. [Luogu P3119] [USACO15JAN]草鉴定Grass Cownoisseur (缩点+图上DP)

    题面 传送门:https://www.luogu.org/problemnew/show/P3119 Solution 这题显然要先把缩点做了. 然后我们就可以考虑如何处理走反向边的问题. 像我这样的 ...

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

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

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

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

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

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

  5. P3119 [USACO15JAN]草鉴定Grass Cownoisseur

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

  6. 洛谷—— 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 ...

  7. P3119 [USACO15JAN]草鉴定Grass Cownoisseur 分层图或者跑两次最长路

    https://www.luogu.org/problemnew/show/P3119 题意 有一个有向图,允许最多走一次逆向的路,问从1再走回1,最多能经过几个点. 思路 (一)首先先缩点.自己在缩 ...

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

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

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

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

随机推荐

  1. Linux 关于SELinux的命令及使用

    1. 模式的设置 : 修改/etc/selinux/config文件中的SELINUX=”" 的值 ,然后重启.enforcing:强制模式,只要selinux不允许,就无法执行 permi ...

  2. Linux学习-Linux 主机上的用户讯息传递

    查询使用者: w, who, last, lastlog 如果你想要知道目前已登入在系统上面的用户呢?可以透过 w 或 who 来查询喔!如下范例所示: [root@study ~]# w 01:49 ...

  3. Alpha版(内部测试版)发布

    首先通过微信扫吗下载我们的软件校园服务,首先进去登录界面没账号点击注册,注册完就可以登录了,进去界面我们在二手交易这项功能里我们即可以事卖家又可以是买家如果我们卖东西点击商品出售,填写商品信息,商品图 ...

  4. Python动态属性和特性(一)

    在Python中,数据的属性和处理数据的方法统称为属性.其实,方式只是可调用的属性.除了这二者之外,我们还可以创建特性(property),在不改变类接口的前提下,使用存取方法(即读取值和设置值方法) ...

  5. day01_08.三大控制结构

    编程三要素:变量,运算,控制 控制: 有选择性的控制让你某部分代码执行,某部分不执行,或者来回反复执行某段代码 控制的三种基本机构:顺序,选择,循环 1.顺序 程序从上到下,顺序执行 <?php ...

  6. IO Streams:字节流

    简介 程序使用字节流来执行8位字节的输入和输出.所有字节流类都继承于InputStream和OutputStream. 有很多字节流类:为了说明字节流如何工作,我们将重点关注文件I / O字节流Fil ...

  7. 九度oj 1003

    前几天开刷九度oj,准备把做的题都放上,先放1003 题目1003:A+B             时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:16923 解决:7029 题目描述: 给 ...

  8. python相关——如何安装pip

    今天在新的一台电脑上安装了pip.流程有点忘记了,在这里再次记录下来. 本教程基于python3.4,并且需要连接互联网,总共需要两步. 1.要安装pip,首先要安装setuptools,链接:htt ...

  9. CAReplicatorLayer 详解

    CAReplicatorLayer可以将自己的子图层复制指定的次数,并且复制体会保持被复制图层的各种基础属性以及动画 基本属性 instanceCountvar instanceCount: Int拷 ...

  10. 【bzoj1406】 AHOI2007密码箱 数论

    在一次偶然的情况下,小可可得到了一个密码箱,听说里面藏着一份古代流传下来的藏宝图,只要能破解密码就能打开箱子,而箱子背面刻着的古代图标,就是对密码的提示.经过艰苦的破译,小可可发现,这些图标表示一个数 ...