洛谷——P3119 [USACO15JAN]草鉴定Grass Cownoisseur
P3119 [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.
输出格式:
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.
输入输出样例
7 10 1 2 3 1 2 5 2 4 3 7 3 5 3 6 6 5 7 2 4 7
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.
先tarjan缩点,然后建边。在这个时候我们需要建两条边,一条跑正向,一条反向,说白了就是建一条反向边。
然后在进行双向dfs,跑出从1点所能到达的点的最长链及能到达一点的最长链,更新到达当前点时所能更新出的最大值。
然后进行枚举每一条边,我们将其进行反向,看其反向后是否能连通整个图,若能,更新最大值。
我们更新结果的时候,枚举每一条可以反向的边,只有在这条边可以从1出来并且可以回到1时才可以使用。
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 210000
using namespace std;
int n,m,x,y,s,tot,tat,top,tim;
bool vis[N],vis1[N],vis2[N],vist1[N],vist2[N];
int xx[N],yy[N],in1[N],in2[N],head1[N],head2[N],dfn[N];
int low[N],sum[N],ans1[N],ans2[N],head[N],stack[N],belong[N];
queue<int>q;
int read()
{
,f=; char ch=getchar();
; ch=getchar();}
+ch-'; ch=getchar();}
return x*f;
}
struct Edge
{
int to,dis,next,from;
}edge[N],edge1[N],edge2[N];
int add(int x,int y)
{
tot++;
edge[tot].to=y;
edge[tot].next=head[x];
head[x]=tot;
}
int add1(int x,int y)
{
tat++;
edge1[tat].to=y;
edge1[tat].next=head1[x];
edge2[tat].to=x;
edge2[tat].next=head2[y];
head1[x]=head2[y]=tat;
}
int tarjan(int now)
{
dfn[now]=low[now]=++tim;
vis[now]=true; stack[++top]=now;
for(int i=head[now];i;i=edge[i].next)
{
int t=edge[i].to;
if(vis[t]) low[now]=min(low[now],dfn[t]);
else if(!dfn[t]) tarjan(t),low[now]=min(low[now],low[t]);
}
if(low[now]==dfn[now])
{
s++,belong[now]=s,sum[s]++;
for(;stack[top]!=now;top--)
belong[stack[top]]=s,vis[stack[top]]=false,sum[s]++;
vis[now]=false,top--;
}
}
int shink_point()
{
;i<=n;i++)
for(int j=head[i];j;j=edge[j].next)
if(belong[i]!=belong[edge[j].to])
add1(belong[i],belong[edge[j].to]);
}
int dfs1(int x)
{
vis1[x]=true;vist1[x]=true;
for(int i=head1[x];i;i=edge1[i].next)
{
int t=edge1[i].to;
if(ans1[t]<ans1[x]+sum[t])
{
ans1[t]=ans1[x]+sum[t];
dfs1(t);
}
}
vis1[x]=false;
}
int dfs2(int x)
{
vis2[x]=true;vist2[x]=true;
for(int i=head2[x];i;i=edge2[i].next)
{
int t=edge2[i].to;
if(ans2[t]<ans2[x]+sum[t])
{
ans2[t]=ans2[x]+sum[t];
dfs2(t);
}
}
vis2[x]=false;
}
int main()
{
n=read(),m=read();
;i<=m;i++)
xx[i]=read(),yy[i]=read(),add(xx[i],yy[i]);
;i<=n;i++)
if(!dfn[i]) tarjan(i);
shink_point();
ans1[belong[]]=ans2[belong[]]=sum[belong[]];
dfs1(belong[]);dfs2(belong[]);
*sum[belong[]];
;i<=m;i++)
{
x=belong[yy[i]],y=belong[xx[i]];
if(vist1[x]&&vist2[y])
answer=max(answer,ans1[x]+ans2[y]);
}
printf(]]);
;
}
拓扑排序:
这个题原来是打算用来练拓扑排序的,结果做了一天的拓扑排序发现不过样例、、、、
为什么会用拓扑排序??
因为我们用拓扑排序的话可以轻易地找到最长链。
怎么拓扑排序??
我们如果直接进行拓扑排序的话,我们会意识到一个问题:缩完点以后直接统计出来入度为零的点并非是我们所需要的点1,我们要跑最长链的话我们需要从1点开始跑,也就是说我们的起点必须是1,怎样做到这一点??我们要做到起点是一的话我们必须让1的入度为零,从一点开始更新与他相连的点。从新统计他们的入读,也就是说我们将这个可能出现环的图抽离成一颗树,这棵树的树根为1点。然后再进行拓扑排序,找出最长链。
最后在进行枚举边,进行更新、
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 210000
using namespace std;
int n,m,x,y,s,tot,tat,top,tim;
bool vis[N],vis1[N],vis2[N];
int xx[N],yy[N],in1[N],in2[N],head1[N],head2[N],dfn[N];
int low[N],sum[N],ans1[N],ans2[N],head[N],stack[N],belong[N];
int read()
{
,f=; char ch=getchar();
; ch=getchar();}
+ch-'; ch=getchar();}
return x*f;
}
struct Edge
{
int to,from,next;
}edge[N],edge1[N],edge2[N];
int add(int x,int y)
{
tot++;
edge[tot].to=y;
edge[tot].next=head[x];
head[x]=tot;
}
int add1(int x,int y)
{
tat++;
edge1[tat].to=y;
edge1[tat].next=head1[x];
edge2[tat].to=x;
edge2[tat].next=head2[y];
head1[x]=head2[y]=tat;
}
int tarjan(int now)
{
dfn[now]=low[now]=++tim;
vis[now]=true;stack[++top]=now;
for(int i=head[now];i;i=edge[i].next)
{
int t=edge[i].to;
if(vis[t]) low[now]=min(dfn[t],low[now]);
else if(!dfn[t]) tarjan(t),low[now]=min(low[t],low[now]);
}
if(low[now]==dfn[now])
{
s++,belong[now]=s,sum[s]++;
for(;stack[top]!=now;top--)
belong[stack[top]]=s,sum[s]++,vis[stack[top]]=false;
vis[now]=false;top--;
}
}
int shink_point()
{
;i<=m;i++)
for(int j=head[i];j;j=edge[j].next)
if(belong[i]!=belong[edge[j].to])
add1(belong[i],belong[edge[j].to]);
}
int dfs1(int s)
{
for(int i=head1[s];i;i=edge1[i].next)
{
int t=edge1[i].to;
if(!in1[t]) dfs1(t);
in1[t]++;
}
}
int dfs2(int s)
{
for(int i=head2[s];i;i=edge2[i].next)
{
int t=edge2[i].to;
if(!in2[t]) dfs2(t);
in2[t]++;
}
}
int tpsort(int *in,Edge *edge,int *head,bool *vis,int *ans)
{
queue<int>q;
q.push(belong[]);
while(!q.empty())
{
int x=q.front();q.pop();vis[x]=true;
for(int i=head[x];i;i=edge[i].next)
{
int t=edge[i].to;
in[t]--;
if(!in[t]) q.push(t);
ans[t]=max(ans[t],ans[x]+sum[t]);
}
}
}
int main()
{
n=read(),m=read();
;
;i<=m;i++)
xx[i]=read(),yy[i]=read(),add(xx[i],yy[i]);
;i<=n;i++)
if(!dfn[i]) tarjan(i);
shink_point();
dfs1(belong[]),dfs2(belong[]);
ans1[belong[]]=ans2[belong[]]=sum[belong[]];
tpsort(in1,edge1,head1,vis1,ans1);
tpsort(in2,edge2,head2,vis2,ans2);
answer=*sum[belong[]];
;i<=m;i++)
{
x=belong[yy[i]],y=belong[xx[i]];
if(vis1[x]&&vis2[y])
answer=max(answer,ans1[x]+ans2[y]);
}
printf(]]);
;
}
洛谷——P3119 [USACO15JAN]草鉴定Grass Cownoisseur的更多相关文章
- 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur 解题报告
P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 约翰有\(n\)块草场,编号1到\(n\),这些草场由若干条单行道相连.奶牛贝西是美味牧草的鉴赏家,她想到达尽可 ...
- 洛谷 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 || 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 ...
- 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur
屠龙宝刀点击就送 Tarjan缩点+拓扑排序 以后缩点后建图看n范围用vector ,或者直接用map+vector 结构体里数据要清空 代码: #include <cstring> #i ...
- 洛谷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 ...
- 洛谷P3119 USACO15JAN 草鉴定
题目描述 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,最多能经过几个点. 思路 (一)首先先缩点.自己在缩 ...
随机推荐
- Java堆分配参数总结
与Java应用程序堆内存相关的JVM参数有: -Xms:设置Java应用程序启动时的初始堆大小 -Xmx:设置Java应用程序能获得的最大堆大小 -Xss:设置线程栈的大小 -XX:MinHeapFr ...
- LNMP笔记:解决mail函数不能发送邮件
用LNMP环境,在探针里测试发送邮件,失败了.已经确定mail()函数是开启的. 问题根源 没有安装或启动 sendmail 组件 解决办法 我是新手,命令不熟,所以写的很详细,老鸟勿喷哦 1.重新安 ...
- 解决for循环下变量显示一致的问题
for(var i=0;i<10;i++){ setTimeOut(function(){ console.log("i:",i); },100) } 上面显示的打印出来结果 ...
- No-4.文件和目录常用命令
文件和目录常用命令 结构 查看目录内容 ls 切换目录 cd 创建和删除操作 touch rm mkdir 拷贝和移动文件 cp mv 查看文件内容 cat more grep 其他 echo 重定向 ...
- Maven实战读书笔记(六):Maven灵活构建
Maven为了支持构建的灵活性,内置了3大特性,即:属性.Profile和资源过滤. 6.1 Maven属性 Maven的属性与Java代码的常量有异曲同工之妙,都是为了消除重复,对相关内容进行统一管 ...
- Java开发者写SQL时常犯的10个错误
首页 所有文章 资讯 Web 架构 基础技术 书籍 教程 我要投稿 更多频道 » - 导航条 - 首页 所有文章 资讯 Web 架构 基础技术 书籍 教程 我要投稿 更多频道 » - iOS ...
- poj2368 Buttons
题目描述 题解: 非常简单的巴什博弈问题. 简单来说保证$L+1$是$K$的因数即可. 决策是,先手取$x$个,后手就取$L+1-x$个. 那个$L>=2$真的很坑. 代码: #include& ...
- SQL语句操作SQL SERVER数据库登录名、用户及权限
要想成功访问 SQL Server 数据库中的数据, 我们需要两个方面的授权: 获得准许连接 SQL Server 服务器的权利: 获得访问特定数据库中数据的权利(select, update, de ...
- Linux基础测试
目 录 第1章 文件及目录课后作业 1 第2章 Linux打包与压缩习题 1 第3章 Linux系统VIM编辑器习题 1 文件及目录课后作业 从/proc/meminfo中过滤出 ...
- c++_等差素数列
标题:等差素数列 2,3,5,7,11,13,....是素数序列.类似:7,37,67,97,127,157 这样完全由素数组成的等差数列,叫等差素数数列.上边的数列公差为30,长度为6. 2004年 ...