[USACO15JAN]草鉴定Grass Cownoisseur

题目描述

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.

题解

这道题。。emmmm

两个板子合在一起吧,注意一点点细节就好了。

先tarjan缩一下点。(这个应该很显然吧)

然后反着走一次其实就是分层跑一次啦。

就愉快的水过去了。

代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
const int N=1e6+5;
struct node{
int to,nex;
}e[N<<1],e2[N<<1];
int vis[N],line[N],top,idx;
int low[N],dfn[N],size[N];
int n,m,cnt,dis[N],bl[N];
int num,num2,head[N],head2[N];
queue<int>q;
int read(){
int x=0,w=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*w;
} void add(int from,int to){
num++;
e[num].to=to;
e[num].nex=head[from];
head[from]=num;
} void add2(int from,int to){
num2++;
e2[num2].to=to;
e2[num2].nex=head2[from];
head2[from]=num2;
} void tarjan(int x){
dfn[x]=low[x]=++idx;line[++top]=x;vis[x]=1;
for(int i=head[x];i;i=e[i].nex){
int v=e[i].to;
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]){
cnt++;
while(line[top+1]!=x){
bl[line[top]]=cnt;
size[cnt]++;
vis[line[top]]=0;
top--;
}
}
} int main(){
n=read();m=read();
for(int i=1;i<=m;i++){
int x=read(),y=read();
add(x,y);
}
for(int i=1;i<=n;i++)if(!dfn[i])tarjan(i);
for(int i=1;i<=n;i++)size[cnt+i]=size[i];
for(int i=1;i<=n;i++){
for(int j=head[i];j;j=e[j].nex){
int v=e[j].to;
if(bl[i]!=bl[v]){
add2(bl[i],bl[v]);
add2(bl[v],bl[i]+cnt);
add2(bl[i]+cnt,bl[v]+cnt);
}
}
}
memset(vis,0,sizeof(vis));
q.push(bl[1]);vis[bl[1]]=1;
while(!q.empty()){
int u=q.front();q.pop();vis[u]=0;
for(int i=head2[u];i;i=e2[i].nex){
int v=e2[i].to;
if(dis[u]+size[u]>dis[v]){
dis[v]=dis[u]+size[u];
if(!vis[v])vis[v]=1,q.push(v);
}
}
}
printf("%d\n",dis[bl[1]+cnt]);
return 0;
}

[USACO15JAN]草鉴定Grass Cownoisseur(分层图+tarjan)的更多相关文章

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

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

  2. [USACO15JAN]草鉴定Grass Cownoisseur (分层图,最长路,$Tarjan$)

    题目链接 Solution 水水的套路题. 可以考虑到一个环内的点是可以都到达的,所以 \(tajan\) 求出一个 \(DAG\) . 然后 \(DAG\) 上的点权值就是 \(scc\) 的大小. ...

  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 解题报告

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

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

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

  7. P3119 [USACO15JAN]草鉴定Grass Cownoisseur

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

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

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

  9. luogu P3119 [USACO15JAN]草鉴定Grass Cownoisseur

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

随机推荐

  1. 乌班图 之 apt命令 及 VMware共享文件夹

    apt是Advanced Packaging Tool ,是Ubuntu下的一个安装包管理工具 大部分软件的安装.更新.卸载 都是利用apt命令来实现 直接在终端输入apt即可查阅命令的帮助信息 常用 ...

  2. HDU 3342 Legal or Not【拓扑排序】

    题意:给出n,m,人的编号为 0到n-1,再给出m个关系,问能不能够进行拓扑排序 #include<iostream> #include<cstdio> #include< ...

  3. HDU 1010 Tempter of the Bone【DFS】

    学习剪枝的第一篇@_@学习别人的剪枝,一剪就是两天@_@---- 参看的这篇--http://blog.csdn.net/libin56842/article/details/8962512自己的小体 ...

  4. PHP SOAP模块的使用方法:NON-WSDL模式

    PHP SOAP扩展可以帮助我们很轻松的实现web service服务,在PHP的SOAP扩展中主要有两种操作模式:WSDL模式和NON-WSDL模式,前者通过使用WSDL文件名作为参数,并从 WSD ...

  5. luogu P3674 小清新人渣的本愿(莫队+bitset)

    这题是莫队维护bitset. 然而我并不会bitset以前讲过认为不考就没学 我真的太菜了. 首先维护一个权值的bitset--s. 操作3比较简单,我们可以\(\sqrt{x}\)枚举约数然后判断就 ...

  6. 获取mapper

    static UpdateLogMapper updateLogMapper = (UpdateLogMapper)SpringContextUtil.getBean(UpdateLogMapper. ...

  7. Jtester使用

    1.在Jtester中使用DataMap 为什么要使用DataMap? 早先的jTester中提供了dbFit方式来准备和验证数据库数据,应该来说,这个工具解决了很多问题.实际使用过程中,开发同学反映 ...

  8. Github README.md中添加图片

    1.先把图片上传到你的项目中:然后在github网站上按路径打开图片,如下打开的图片链接: 2.复制图片的地址 3.然后在README.md写上: ![这里随便写文字](你刚复制的图片路径) 注意  ...

  9. codevs1281 矩阵乘法 快速幂 !!!手写乘法取模!!! 练习struct的构造函数和成员函数

    对于这道题目以及我的快速幂以及我的一节半晚自习我表示无力吐槽,, 首先矩阵乘法和快速幂没必要太多说吧,,嗯没必要,,我相信没必要,,实在做不出来写两个矩阵手推一下也就能理解矩阵的顺序了,要格外注意一些 ...

  10. code vs 1216 跳马问题

    1216 跳马问题  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 题目 输入描述 Input Descri ...