题目描述

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.

输入:

第一行:草场数n,道路数m。

以下m行,每行x和y表明有x到y的单向边,不会有重复的道路出现。

输出格式:

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.

solution:

  本题Tarjan缩点+分层图最短路。

  一个环上的点显然都是能互相到达的,所以我们先缩点,并记录每个强联通分量的点个数,对于逆行1次的情况,只需要建分层图就好了,由于缩点后的图是DAG,于是我们就可以拓扑排序去解决第二问,当然也可以建边跑最长路咯。

代码:

/*Code by 520 -- 9.5*/
#include<bits/stdc++.h>
#define il inline
#define ll long long
#define RE register
#define For(i,a,b) for(RE int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(RE int (i)=(b);(i)>=(a);(i)--)
using namespace std;
const int N=;
int n,m,bl[N],scc,dfn[N],low[N],tot,stk[N],top;
int to[N],net[N],h[N],cnt,w[N],dis[N],siz[N],f[N];
bool ins[N],vis[N];
struct node{
int u,v;
}e[N]; int gi(){
int a=;char x=getchar();
while(x<''||x>'')x=getchar();
while(x>=''&&x<='')a=(a<<)+(a<<)+(x^),x=getchar();
return a;
} il void add(int u,int v){to[++cnt]=v,net[cnt]=h[u],h[u]=cnt;} void tarjan(int u){
dfn[u]=low[u]=++tot,stk[++top]=u,ins[u]=;
for(RE int i=h[u];i;i=net[i])
if(!dfn[to[i]]) tarjan(to[i]),low[u]=min(low[u],low[to[i]]);
else if(ins[to[i]]) low[u]=min(low[u],dfn[to[i]]);
if(dfn[u]==low[u]){
++scc;
while(stk[top+]!=u)
bl[stk[top]]=scc,siz[scc]++,ins[stk[top--]]=;
}
} queue<int>q;
int main(){
n=gi(),m=gi();
For(i,,m) e[i].u=gi(),e[i].v=gi(),add(e[i].u,e[i].v);
For(i,,n) if(!dfn[i]) tarjan(i);
memset(h,,sizeof(h)),cnt=;
For(i,,m) if(bl[e[i].u]!=bl[e[i].v]) {
add(bl[e[i].u],bl[e[i].v]),w[cnt]=siz[bl[e[i].u]];
add(bl[e[i].u]+scc,bl[e[i].v]+scc),w[cnt]=siz[bl[e[i].u]];
add(bl[e[i].v],bl[e[i].u]+scc),w[cnt]=siz[bl[e[i].v]];
}
memset(dis,,sizeof(dis));
q.push(bl[]),dis[bl[]]=;
while(!q.empty()){
RE int u=q.front();q.pop();vis[u]=;
for(RE int i=h[u];i;i=net[i])
if(dis[to[i]]<dis[u]+w[i]){
dis[to[i]]=dis[u]+w[i];
if(!vis[to[i]])vis[to[i]]=,q.push(to[i]);
}
}
cout<<dis[bl[]+scc];
return ;
}

P3119 [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. luogu P3119 [USACO15JAN]草鉴定Grass Cownoisseur

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. [PLC]ST语言三:OUT/OUT_T/OUT_C/OUT_C-C32

    一:OUT/OUT_T/OUT_C/OUT_C-C32               说明:简单的顺控指令不做其他说明. 控制要求:无 编程梯形图: 结构化编程ST语言: (*OUT(EN,D);*) ...

  2. Linux学习之常用系统工作命令(一)

     由于centos和RHEL互通,两个版本可以相互学习,所以截图有两个界面 Linux系统与win系列是两个几乎完全不同的操作系统,但是就应用范围来说,是win系统更胜一筹,然而,这反而也成为win系 ...

  3. sql server sql查询数据库的表,字段,主键,自增,字段类型等信息

    1.查询数据表的属性(名称.说明.是否主键.数据类型.是否自增) SELECT t1.name columnName,case when t4.id is null then 'false' else ...

  4. Catlike学习笔记(1.2)-使用Unity画函数图像

    『Catlike系列教程』第二篇来了~今天周六,早上(上午11点)醒来去超市买了一周的零食回来以后就玩了一整天游戏非常有负罪感.现在晚上九点天还亮着感觉像下午7点左右的样子好像还不是很晚...所以就写 ...

  5. MD5加密--项目案例

    在项目中最尝使用MD5这种非对称加密的就是用户信息登录了.下面我就以一个简单的登录案例来说明MD5的用法 首先来看几张图: 用户登录页:需要选择要登录的系统,同时输入用户的用户名和密码,验证码才能进入 ...

  6. spring boot 配置全局日期类型转换器

    1. 首先自定义一个类型转换器 import org.springframework.core.convert.converter.Converter; import org.springframew ...

  7. shell解析ini格式文件

    功能 本脚本实现了ini文件中的查询修改指定value 百度云连接地址 链接:https://pan.baidu.com/s/12_T5yST7Y3L1H4_MkVEcvA 密码:fo5p 解压后先看 ...

  8. CentOS 7 Docker基本特性

    Docker是一个开源的应用容器引擎,开发人员可以非常容易地打包已经开发好的应用,同时将应用相关的依赖包也打包到这样一个可移植的容器中,然后发布到任意的Linux主机系统上.Docker是基于Linu ...

  9. 笨办法学Python - 习题5: More Variables and Printing

    1.习题 5: 更多的变量和打印 学习目标:了解用户输入方法,明白pthon2和Python3之间的用户输入的区别.了解格式化字符串(format string)的概念,学会如何创建包含变量内容的字符 ...

  10. Linux上安装设置mysql 5.7.24

    一,准备 1,先查看Linux是32位还是64位 getconf LONG_BIT 如果返回的是32,那么就是32位 如果返回的是64,那么就是64位 2,如果服务器不能联网,就先去官网下载好压缩包, ...