博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Grass Cownoisseur[Usaco2015 Jan]
阅读量:6690 次
发布时间:2019-06-25

本文共 4241 字,大约阅读时间需要 14 分钟。

题目描述

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)

输入

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.

输出

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 101 23 12 52 43 73 53 66 57 24 7

样例输出

6 题解
考试的时候调这道题的深搜调了很久,真是没法拯救的运行错误,浪费了很多时间最后还是全部注释掉。凭经验Usaco的题输出样例骗了10分。。。隐约感觉应该用tarjan,虽然前几天才看过但是并没有把握打准确,何况就算缩完点也不知道应该怎么处理。曾几何时做着tarjan的题复习spfa,如今做着综合题复习tarjan和spfa,时间过得多么快啊。    正解思路清晰而实现复杂。输入建原图,用tarjan缩点建出新图和边全部反向的图,点权即为强连通分量中的点数。然后在两个新图中各自从1所属的点跑一遍spfa,得出1到所有点和所有点到1的距离。把强连通分量里的边反向是没有意义的,所以枚举DAG(新图)中的所有边,它联通了一个回路,回路长为起点到1和1到终点的最长路之和。最后要注意的地方,算最长路时把1所在强连通分量计算在内,所以结果中应该除去重复加上的w[c[1]];第二点是如果正反任何一个最长路为0代表它不连通,这样的边是不能记为结果的。
#include
#include
#include
#include
using namespace std;const int sj=100010;int dfn[sj],low[sj],c[sj],jl[sj],dis[sj],s[sj];int o[sj],h[sj],l[sj],w[sj];int m,n,e1,e2,e3,cnt,ge,zh,ans;bool r[sj];struct B{ int v,ne;}b[sj];struct BS { int u,to,nex;}bs[sj];struct BSS { int to,nex;}bss[sj];inline int re(){ int jg=0,jk=0; jk=getchar()-'0'; if(jk>=0&&jk<=9) jg+=jk; jk=getchar()-'0'; while(jk>=0&&jk<=9) { jg*=10; jg+=jk; jk=getchar()-'0'; } return jg;}void add(int x,int y){ b[e1].v=y; b[e1].ne=h[x]; h[x]=e1++;}void ad2(int x,int y){ bs[e2].to=y; bs[e2].nex=l[x]; bs[e2].u=x; l[x]=e2++;}void ad3(int x,int y){ bss[e3].to=y; bss[e3].nex=o[x]; o[x]=e3++;}void init(){ n=re(); m=re(); memset(h,-1,sizeof(h)); memset(l,-1,sizeof(l)); memset(o,-1,sizeof(o)); int a1,a2; for(int i=1;i<=m;i++) { a1=re(); a2=re(); add(a1,a2); }}int bj(int x,int y){ return x
q; q.push(x); int f; while(!q.empty()) { f=q.front(); for(int i=l[f];i!=-1;i=bs[i].nex) { if(dis[bs[i].to]
q; q.push(x); int f; while(!q.empty()) { f=q.front(); for(int i=o[f];i!=-1;i=bss[i].nex) if(dis[bss[i].to]
y?x:y;}int main(){ init(); for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i); for(int i=1;i<=n;i++) for(int j=h[i];j!=-1;j=b[j].ne) if(c[i]!=c[b[j].v]) { ad2(c[i],c[b[j].v]); ad3(c[b[j].v],c[i]); } memset(r,0,sizeof(r)); memset(dis,0,sizeof(dis)); spfa1(c[1]); for(int i=1;i<=zh;i++) jl[i]=dis[i]; memset(r,0,sizeof(r)); memset(dis,0,sizeof(dis)); spfa2(c[1]); ans=1; for(int i=0;i

 

 
 

转载于:https://www.cnblogs.com/moyiii-/p/7182957.html

你可能感兴趣的文章
轻松看懂Java字节码
查看>>
2011年总结以及2012的展望
查看>>
AE TIN的切割
查看>>
ASP.NET图片上传,删除
查看>>
Visual Studio 2010 创建的WCF服务 第一个应用
查看>>
redis 下载启动,设置、查询超时时间
查看>>
WinForm构造函数的作用
查看>>
2016第42周五
查看>>
centos7 取消自动锁屏
查看>>
在IDEA中代码自动提示第一个字母大小写必须匹配的解决
查看>>
C++的字符串格式化库
查看>>
面向接口编程的好处和优点
查看>>
放过设计模式吧
查看>>
架构师必看-架构之美第14章-两个系统的故事:设计之城(一)
查看>>
从c++转到Python需要注意的地方
查看>>
mysql 利用触发器(Trigger)让代码更简单
查看>>
[译]ASP.NET Core 2.0 视图引擎
查看>>
(原)InsightFace及其mxnet代码
查看>>
OpenCV学习:实现简单的图像叠加
查看>>
Intent跳转到系统应用中的拨号界面、联系人界面、短信界面及其他
查看>>