题目描述

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号草场。她想经过尽可能多的草场,贝西在通一个草场只吃一次草,所以一个草场可以经过多次。因为草场是单行道连接,这给贝西的品鉴工作带来了很大的不便,贝西想偷偷逆向行走一次,但最多只能有一次逆行。问,贝西最多能吃到多少个草场的牧草。

这道题还是很复杂的。

根据去品尝牧草,我们很容易想到,如果一些草场在一个连通分量里面,只要到达这个连通分量,里面所有的草场就能到达,所以Tarjan缩点是无疑的了。

缩完点以后,我们保证不存在环,这样就可以跑一遍最长路,其中边权是每个连通分量的大小。

但是,还有能倒着走一条边的情况。这里就需要用到分层最短路。

在对于缩完点的图中,边u->v,我们将v连向u+n,代表下一层,当然u+n也要连到v+n,这样,我们能从每一个点,倒着走到下一层,但是无法从下一层走回来。

然后再输出起点连通分量+n的最长路dis值就好了。

但是注意,如果整个图缩完点是一个连通分量,那么要输出起点连通分量,不用加n。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <stack>
#include <queue>
#define REP(i,k,n) for(int i=k;i<=n;i++)
#define in(a) a=read()
#define MAXN 200010
using namespace std;
inline int read(){
int x=,f=;
char ch=getchar();
for(;!isdigit(ch);ch=getchar())
if(ch=='-')
f=-;
for(;isdigit(ch);ch=getchar())
x=x*+ch-'';
return x*f;
}
stack <int> S;
queue <int> Q;
int n,m;
int total1,head1[MAXN],to1[MAXN],nxt1[MAXN];
int total2,head2[MAXN<<],to2[MAXN<<],nxt2[MAXN<<],val[MAXN<<];
int num,cnt,dfn[MAXN],low[MAXN],vis[MAXN],bel[MAXN],size[MAXN],dis[MAXN];
inline void adl1(int a,int b){
total1++;
to1[total1]=b;
nxt1[total1]=head1[a];
head1[a]=total1;
return ;
}
inline void adl2(int a,int b,int c){
total2++;
to2[total2]=b;
val[total2]=c;
nxt2[total2]=head2[a];
head2[a]=total2;
return ;
}
inline void tarjan(int u){
dfn[u]=low[u]=++cnt;
S.push(u),vis[u]=;
for(int e=head1[u];e;e=nxt1[e]){
if(!dfn[to1[e]]){
tarjan(to1[e]);
low[u]=min(low[u],low[to1[e]]);
}
else if(vis[to1[e]]) low[u]=min(low[u],dfn[to1[e]]);
}
if(dfn[u]==low[u]){
num++;
while(!S.empty() && S.top()!=u) size[num]++,bel[S.top()]=num,vis[S.top()]=,S.pop();
if(!S.empty()) size[num]++,bel[S.top()]=num,vis[S.top()]=,S.pop();
}
return ;
}
inline void spfa(){
Q.push(bel[]);
dis[bel[]]=;
while(!Q.empty()){
int u=Q.front();
Q.pop(),vis[u]=;
for(int e=head2[u];e;e=nxt2[e])
if(dis[to2[e]]<dis[u]+val[e]){
dis[to2[e]]=dis[u]+val[e];
if(!vis[to2[e]]) vis[to2[e]]=,Q.push(to2[e]);
}
}
return ;
}
int main(){
in(n),in(m);
int a,b;
REP(i,,m) in(a),in(b),adl1(a,b);
REP(i,,n)
if(!dfn[i])
tarjan(i);
REP(u,,n)
for(int e=head1[u];e;e=nxt1[e])
if(bel[u]!=bel[to1[e]]){
adl2(bel[u],bel[to1[e]],size[bel[u]]);
adl2(bel[u]+num,bel[to1[e]]+num,size[bel[u]]);
adl2(bel[to1[e]],bel[u]+num,size[bel[to1[e]]]);
}
adl2(bel[],bel[]+num,size[bel[]]);
memset(vis,,sizeof(vis));
spfa();
cout<<dis[bel[]+num]<<endl;
return ;
}

洛谷P3119 USACO15JAN 草鉴定的更多相关文章

  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

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

  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. 洛谷3119 [USACO15JAN]草鉴定Grass Cownoisseur

    原题链接 显然一个强连通分量里所有草场都可以走到,所以先用\(tarjan\)找强连通并缩点. 对于缩点后的\(DAG\),先复制一张新图出来,然后对于原图中的每条边的终点向新图中该边对应的那条边的起 ...

  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

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

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

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

随机推荐

  1. 【codeforces】【比赛题解】#940 CF Round #466 (Div. 2)

    人生的大起大落莫过如此,下一场我一定要回紫. [A]Points on the line 题意: 一个直线上有\(n\)个点,要求去掉最少的点,使得最远两点距离不超过\(d\). 题解: 暴力两重fo ...

  2. linux下的usb抓包方法【转】

    转自:http://blog.chinaunix.net/uid-11848011-id-4508834.html 1.配置内核使能usb monitor: make menuconfig       ...

  3. Linux下MySQL/MariaDB Galera集群搭建过程【转】

    MariaDB介绍 MariaDB是开源社区维护的一个MySQL分支,由MySQL的创始人Michael Widenius主导开发,采用GPL授权许可证. MariaDB的目的是完全兼容MySQL,包 ...

  4. 洛谷P3119 草鉴定

    这个题调了一天.. 传送门 读完题目之后我们不难想出这个题是个tarjan缩点问题,因为尽量多的经过草场,所以一号点所在的强连通分量里左右的点都是不需要在进行走逆向边,所能到达的. 然后问题就落在怎么 ...

  5. Django基础 - 修改默认SQLite3数据库连接为MySQL

    Django数据库连接默认为SQLite3,打开setting.py可以看到数据库部分的配置如下: DATABASES = { 'default': { 'ENGINE': 'django.db.ba ...

  6. hihoCoder #1183 : 连通性一·割边与割点(求割边与各点模板)

    #1183 : 连通性一·割边与割点 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 还记得上次小Hi和小Ho学校被黑客攻击的事情么,那一次攻击最后造成了学校网络数据的丢 ...

  7. dp入门题目

    本文文旨,如题... 转载请注明出处... HDOJ 1176 免费馅饼 http://acm.hdu.edu.cn/showproblem.php?pid=1176 类似数塔,从底往上推,每次都是从 ...

  8. CCF CSP 201509-2 日期计算

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201509-2 日期计算 问题描述 给定一个年份y和一个整数d,问这一年的第d天是几月几日? ...

  9. 【BZOJ】3674: 可持久化并查集加强版

    题解 感觉全世界都写过只有我没写过 毕竟是板子还是挺简单的,只要用可持久化线段树维护一下数组的形态就好了,每个数组里面维护这个数组的father,和这个点所在树的最长链的深度(如果这个点是根按秩合并要 ...

  10. thinkphp5.0返回插入数据id

    添加数据后如果需要返回新增数据的自增主键,可以使用getLastInsID方法: Db::name('user')->insert($data); $userId = Db::name('use ...