http://www.lydsy.com/JudgeOnline/problem.php?id=3887||

https://www.luogu.org/problem/show?pid=3119

Description

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.
给一个有向图,然后选一条路径起点终点都为1的路径出来,有一次机会可以沿某条边逆方向走,问最多有多少个点可以被经过?(一个点在路径中无论出现多少正整数次对答案的贡献均为1)

Input

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

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.
 

Sample Input

7 10
1 2
3 1
2 5
2 4
3 7
3 5
3 6
6 5
7 2
4 7

Sample Output

6

HINT

 

Source

Gold&鸣谢18357

先把原图缩点,跑出从1到n和从n到1的最多可以遍历的牧场数,

枚举每个边做无向边的情况更新答案

SPFA跑最多牧场数

 #include <cstdio>
#include <queue>
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b) const int INF(0x3f3f3f3f);
const int N(1e5+);
int n,head[N],sumedge;
struct Edge
{
int v,next;
Edge(int v=,int next=):v(v),next(next){}
}edge[N];
inline void ins(int u,int v)
{
edge[++sumedge]=Edge(v,head[u]);
head[u]=sumedge;
} int tim,dfn[N],low[N];
int top,Stack[N],instack[N];
int sumcol,col[N],point[N];
void DFS(int u)
{
low[u]=dfn[u]=++tim;
Stack[++top]=u; instack[u]=;
for(int v,i=head[u];i;i=edge[i].next)
{
v=edge[i].v;
if(!dfn[v]) DFS(v),low[u]=min(low[u],low[v]);
else if(instack[v]) low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
col[u]=++sumcol;
point[sumcol]++;
for(;Stack[top]!=u;top--)
{
point[sumcol]++;
col[Stack[top]]=sumcol;
instack[Stack[top]]=;
}
instack[u]=; top--;
}
} int hed[N],had[N],sum;
struct E
{
int v,next,w;
E(int v=,int next=,int w=):v(v),next(next),w(w){}
}e[N][];
inline void insert(int u,int v)
{
e[++sum][]=E(v,hed[u],point[v]);
hed[u]=sum;
e[sum][]=E(u,had[v],point[u]);
had[v]=sum;
} bool inq[N];
int v1[N],v2[N];
void SPFA(int op,int s,int *val,int *head)
{
for(int i=;i<=sumcol;i++)
inq[i]=,val[i]=-INF;
val[s]=point[s];
std::queue<int>que;
que.push(s);
for(int u,v;!que.empty();)
{
u=que.front(); que.pop(); inq[u]=;
for(int i=head[u];i;i=e[i][op].next)
{
v=e[i][op].v;
if(val[v]<val[u]+e[i][op].w)
{
val[v]=val[u]+e[i][op].w;
if(!inq[v]) inq[v]++,que.push(v);
}
}
}
} inline void read(int &x)
{
x=; register char ch=getchar();
for(;ch>''||ch<'';) ch=getchar();
for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-'';
} int AC()
{
int m; read(n),read(m);
for(int u,v;m--;)
read(u),read(v),ins(u,v);
for(int i=;i<=n;i++)
if(!dfn[i]) DFS(i);
for(int v,u=;u<=n;u++)
for(int i=head[u];i;i=edge[i].next)
{
v=edge[i].v;
if(col[u]!=col[v]) insert(col[u],col[v]);
}
int ans=-INF;
SPFA(,col[],v1,hed);
SPFA(,col[],v2,had);
for(int v,u=;u<=sum;u++)
for(int i=hed[u];i;i=e[i][].next)
{
v=e[i][].v;
ans=max(ans,v1[v]+v2[u]);
}
for(int v,u=;u<=sum;u++)
for(int i=had[u];i;i=e[i][].next)
{
v=e[i][].v;
ans=max(ans,v1[u]+v2[v]);
}
printf("%d\n",ans-point[col[]]);
return ;
} int Hope=AC();
int main(){;}

SPFA AC

Topsort跑最多牧场数

 #include <cstdio>
#include <queue>
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b) const int INF(0x3f3f3f3f);
const int N(1e5+);
int n,head[N],sumedge;
struct Edge
{
int v,next;
Edge(int v=,int next=):v(v),next(next){}
}edge[N];
inline void ins(int u,int v)
{
edge[++sumedge]=Edge(v,head[u]);
head[u]=sumedge;
} int tim,dfn[N],low[N];
int top,Stack[N],instack[N];
int sumcol,col[N],point[N],rd[N],cd[N];
void DFS(int u)
{
low[u]=dfn[u]=++tim;
Stack[++top]=u; instack[u]=;
for(int v,i=head[u];i;i=edge[i].next)
{
v=edge[i].v;
if(!dfn[v]) DFS(v),low[u]=min(low[u],low[v]);
else if(instack[v]) low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
col[u]=++sumcol;
point[sumcol]++;
for(;Stack[top]!=u;top--)
{
point[sumcol]++;
col[Stack[top]]=sumcol;
instack[Stack[top]]=;
}
instack[u]=; top--;
}
} int hed[N],had[N],sum;
struct E
{
int v,next,w;
E(int v=,int next=,int w=):v(v),next(next),w(w){}
}e[N][];
inline void insert(int u,int v)
{
e[++sum][]=E(v,hed[u],point[v]);
hed[u]=sum;
e[sum][]=E(u,had[v],point[u]);
had[v]=sum;
} int v1[N],v2[N];
#define max(a,b) (a>b?a:b)
void Topsort(int op,int s,int *val,int *head,int *du)
{
std::queue<int>que;
for(int i=;i<=sumcol;i++)
{
if(!du[i]) que.push(i);
val[i]=-INF;
}
val[s]=point[s];
for(int u,v;!que.empty();)
{
u=que.front(); que.pop();
for(int i=head[u];i;i=e[i][op].next)
{
v=e[i][op].v;
val[v]=max(val[v],val[u]+e[i][op].w);
if(--du[v]==) que.push(v);
}
}
} inline void read(int &x)
{
x=; register char ch=getchar();
for(;ch>''||ch<'';) ch=getchar();
for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-'';
} int AC()
{
int m; read(n),read(m);
for(int u,v;m--;)
read(u),read(v),ins(u,v);
for(int i=;i<=n;i++)
if(!dfn[i]) DFS(i);
for(int v,u=;u<=n;u++)
for(int i=head[u];i;i=edge[i].next)
{
v=edge[i].v;
if(col[u]==col[v]) continue;
rd[col[v]]++,cd[col[u]]++;
insert(col[u],col[v]);
}
int ans=-INF;
Topsort(,col[],v1,hed,rd);
Topsort(,col[],v2,had,cd);
for(int v,u=;u<=sum;u++)
for(int i=hed[u];i;i=e[i][].next)
{
v=e[i][].v;
ans=max(ans,v1[v]+v2[u]);
}
for(int v,u=;u<=sum;u++)
for(int i=had[u];i;i=e[i][].next)
{
v=e[i][].v;
ans=max(ans,v1[u]+v2[v]);
}
printf("%d\n",ans-point[col[]]);
return ;
} int Hope=AC();
int main(){;}

Topsort AC

洛谷—— P3119 [USACO15JAN]草鉴定Grass Cownoisseur || BZOJ——T 3887: [Usaco2015 Jan]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

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

  5. 洛谷P3119 USACO15JAN 草鉴定

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

  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. 设计一部iphone手机用面向对象的方法

    main.m //编辑字体大小command + < //编译执行快捷键 com + R #import <Foundation/Foundation.h> #import &quo ...

  2. Ubuntu Tomcat Service

    只需要将%TOMCAT_HOME%/bin/catalina.sh文件拷贝到/etc/init.d/文件夹下,稍作编辑,然后注册成系统服务,是否设置自启动均可. 1. 编辑catalina.sh文件c ...

  3. 现代英特尔® 架构上的 TensorFlow* 优化——正如去年参加Intel AI会议一样,Intel自己提供了对接自己AI CPU优化版本的Tensorflow,下载链接见后,同时可以基于谷歌官方的tf版本直接编译生成安装包

    现代英特尔® 架构上的 TensorFlow* 优化 转自:https://software.intel.com/zh-cn/articles/tensorflow-optimizations-on- ...

  4. Hdu-6249 2017CCPC-Final G.Alice’s Stamps 动态规划

    题面 题意:给你n个集合,每个集合有L到R这些种类的邮票,让你选择其中的K个集合,使得最后选择的邮票种类尽可能多,N,L,R都<=2000 题解:容易乱想到网络流,可是再细想一下就会发现处理不了 ...

  5. nginx 限制ip/限制访问路径

    一.多站点统一限IP vim nginx.conf allow 127.0.0.1; deny all; # 以上代码解释: # deny all; 限制所有的ip # allow ip; 除了 这个 ...

  6. bind(),call(), apply()方法的区别是什么?

    bind(),call(), apply()方法的区别是什么? 共同点:改变this指向,任何调用都不在起作用 bind() 改变this的指向,不会调用函数,返回一个新的函数 var o ={a:' ...

  7. HDU1412 {A} + {B}

    2019-05-17 10:15:01 每个元素之间有一个空格隔开. 每行最后一的元素后面没有空格,区别于HDU人见人爱A - B 注意使用STL的时候要清空 .  a.clear(); #inclu ...

  8. vim产生的.swap文件

    转载自 http://ibeyond.blog.51cto.com/1988404/800138 有时候在用vim打开文件时提示类似以下的信息: E325: 注意发现交换文件 ".expor ...

  9. lua闭包函数

    function createCountdownTimer(second) local ms = second * local function countDown() ms = ms - retur ...

  10. RedHat/CentOS 手动挂载磁盘

    #创建挂载目录mkdir /mnt/sdamkdir /mnt/sdbmkdir /mnt/sdcmkdir /mnt/sddmkdir /mnt/sdemkdir /mnt/sdfmkdir /mn ...