洛谷—— 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
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
Output
Sample Input
1 2
3 1
2 5
2 4
3 7
3 5
3 6
6 5
7 2
4 7
Sample Output
HINT
Source
先把原图缩点,跑出从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的更多相关文章
- 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur 解题报告
P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 约翰有\(n\)块草场,编号1到\(n\),这些草场由若干条单行道相连.奶牛贝西是美味牧草的鉴赏家,她想到达尽可 ...
- 洛谷——P3119 [USACO15JAN]草鉴定Grass Cownoisseur
P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 In an effort to better manage the grazing patterns of hi ...
- 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur (SCC缩点,SPFA最长路,枚举反边)
P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 In an effort to better manage the grazing patterns of hi ...
- 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur
屠龙宝刀点击就送 Tarjan缩点+拓扑排序 以后缩点后建图看n范围用vector ,或者直接用map+vector 结构体里数据要清空 代码: #include <cstring> #i ...
- 洛谷P3119 USACO15JAN 草鉴定
题目描述 In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-w ...
- 洛谷3119 [USACO15JAN]草鉴定Grass Cownoisseur
原题链接 显然一个强连通分量里所有草场都可以走到,所以先用\(tarjan\)找强连通并缩点. 对于缩点后的\(DAG\),先复制一张新图出来,然后对于原图中的每条边的终点向新图中该边对应的那条边的起 ...
- P3119 [USACO15JAN]草鉴定Grass Cownoisseur
题目描述 In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-w ...
- luogu P3119 [USACO15JAN]草鉴定Grass Cownoisseur
题目描述 In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-w ...
- P3119 [USACO15JAN]草鉴定Grass Cownoisseur 分层图或者跑两次最长路
https://www.luogu.org/problemnew/show/P3119 题意 有一个有向图,允许最多走一次逆向的路,问从1再走回1,最多能经过几个点. 思路 (一)首先先缩点.自己在缩 ...
随机推荐
- 什么是A记录、MX记录、CNAME记录具体介绍
什么是A记录: A (Address) 记录是用来指定主机名(或域名)相应的IP地址记录.用户能够将该域名下的站点服务器指向到自己的web server上. 同一时候也能够设置域名的子域名. 通俗来说 ...
- [Linux]非常方便的上传下载文件工具rz和sz
linux上非常方便的上传下载文件工具rz和sz (本文适合linux入门的朋友) [一般用于SecureCRT ssh中使用] █ 法一:直接用yum安装lrzsz(推荐) yum insta ...
- 轻快的vim(一):移动
断断续续的使用VIM也一年了,会的始终都是那么几个命令,效率极低 前几个星期把Windows换成了Linux Mint,基本上也稳定了下来 就今晚,我已经下定决心开始新的VIM之旅,顺便写一系列的笔记 ...
- Linux如何把以下文件夹修改为root权限?
inux 修改文件目录所有者例:要将当前目录下名 title 的文件夹及其子文件的所有者改为geust组的su用户,方法如下:#chown -R su.geust title-R 递归式地改变指定目录 ...
- C语言 - .c和.h文件的困惑
本质上没有任何区别. 只不过一般:.h文件是头文件,内含函数声明.宏定义.结构体定义等内容. .c文件是程序文件,内含函数实现,变量定义等内容.而且是什么后缀也没有关系,只不过编译器会默认对某些后缀的 ...
- Spark新愿景:让深度学习变得更加易于使用——见https://github.com/yahoo/TensorFlowOnSpark
Spark新愿景:让深度学习变得更加易于使用 转自:https://www.jianshu.com/p/07e8200b7cea 前言 Spark成功的实现了当年的承诺,让数据处理变得更容易,现在 ...
- Android常见面试题学习第一天(原创)
1. 内存泄漏 在android程序开发中,当一个对象已经不需要再使用了,本该被回收时,而另外一个正在使用的对象持有它的引用从而导致它不能被回收,这就导致本该被回收的对象不能被回收而停留在堆内存中,内 ...
- idea的环境变量设置(Enviroment variables)
- 在Hibernate映射文件里配置Sequence
<!--注意该id的数据类型以及<generator>节点下的<param>节点的写法--> <id name="id" type=&qu ...
- Mybatis xml约束文件的使用
一:准备.DTD约束文件 核心配置文件约束文件:mybatis-config.dtd <?xml version="1.0" encoding="UTF- ...