题目如下:

约翰有n块草场,编号1到n,这些草场由若干条单行道相连。奶牛贝西是美味牧草的鉴赏家,她想到达尽可能多的草场去品尝牧草。

贝西总是从1号草场出发,最后回到1号草场。她想经过尽可能多的草场,贝西在通一个草场只吃一次草,所以一个草场可以经过多次。因为草场是单行道连接,这给贝西的品鉴工作带来了很大的不便,贝西想偷偷逆向行走一次,但最多只能有一次逆行。问,贝西最多能吃到多少个草场的牧草。

大意:

求一个有向图在走一次反向边或不走的情况下走的节点数最多的回到起点的路径的节点数

思路:

首先边可以重复走 所以缩点不会影响答案

缩点后得到的是一个DAG 我们有走一次反向边的机会 此时就相当于在DAG上加一条边 由于要从起点回到起点 这加的一条边的两端要满足一端起点能够到达 一端能通往起点

说到这里算法已经显而易见了 故不赘述了

下面是代码:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <memory.h>
#define r(x) x=read()
#define MAXX 100005
#define MIN(a,b) (a<b?a:b)
#define MAX(a,b) (a>b?a:b)
using namespace std;
int h[MAXX],h2[MAXX],cnt,dis[][MAXX],flag[MAXX];
int dfn[MAXX],low[MAXX],id[MAXX],w[MAXX],k,top,sta[MAXX],num;
int n,m,ans,ans2,u,to,st;
struct edge{int to,nex;}e[MAXX],yuan[MAXX];
void add(int u,int to)
{
cnt++;
e[cnt]=(edge){to,h[u]},h[u]=cnt;
}
void tarjan(int now)
{
sta[++top]=now;
dfn[now]=low[now]=++k;
for(int i=h[now];i;i=e[i].nex)
{
if(!dfn[e[i].to])
tarjan(e[i].to),low[now]=MIN(low[now],low[e[i].to]);
else
if(!id[e[i].to])
low[now]=MIN(low[now],dfn[e[i].to]);
}
if(low[now]==dfn[now])
{
id[now]=++num;
while(sta[top]!=now){id[sta[top]]=num;top--;}
top--;
}
}
void spfa(int now,int x)
{
queue<int>que;
dis[x][now]=w[now];
que.push(now);
flag[now]=;
while(!que.empty())
{
int p=que.front();que.pop();
for(int i=h[p];i;i=e[i].nex)
{
if(i%!=x) continue;
if(dis[x][e[i].to]<dis[x][p]+w[e[i].to])
{
dis[x][e[i].to]=dis[x][p]+w[e[i].to];
if(!flag[e[i].to])
flag[e[i].to]=,que.push(e[i].to);
}
}
flag[p]=;
}
}
void dfs(int now)
{
for(int i=h[now];i;i=e[i].nex)
{
int to=e[i].to;
if(i%==)
{
if(dis[][now]&&dis[][to])
ans=MAX(ans,dis[][now]+dis[][to]);
}
else
dfs(to);
}
}
bool cmp(edge a,edge b)
{
return id[a.to]==id[b.to]?id[a.nex]<id[b.nex]:id[a.to]<id[b.to];
}
int read()
{
char ch=;int w=;
while(ch<''||ch>''){ch=getchar();}
while(ch>=''&&ch<=''){w=w*+ch-'';ch=getchar();}
return w;
}
int main()
{
r(n),r(m);
for(int i=;i<=m;++i)
r(u),r(to),add(u,to),yuan[i]=(edge){to,u};
for(int i=;i<=n;++i)
if(!dfn[i])
tarjan(i);
for(int i=;i<=n;++i)
w[id[i]]++;
st=id[];
sort(yuan+,yuan+m+,cmp);
memset(h,,sizeof(h));
cnt=;
for(int i=;i<=m;++i)
{
int x=id[yuan[i].nex],y=id[yuan[i].to];
if((x==id[yuan[i-].nex]&&y==id[yuan[i-].to])||(x==y))
continue;
add(x,y),add(y,x);
}
spfa(st,),spfa(st,);
ans=w[st];
dfs(st);
if(ans>w[st]) printf("%d",ans-w[st]);
else printf("%d",ans);
return ;
}

Tarjan水题系列(1):草鉴定Grass Cownoisseur [USACO15JAN]or[luogu P3119]的更多相关文章

  1. [USACO15JAN]草鉴定Grass Cownoisseur(分层图+tarjan)

    [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 In an effort to better manage the grazing patterns of his cows ...

  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 解题报告

    P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 约翰有\(n\)块草场,编号1到\(n\),这些草场由若干条单行道相连.奶牛贝西是美味牧草的鉴赏家,她想到达尽可 ...

  4. 【洛谷P3119】[USACO15JAN]草鉴定Grass Cownoisseur

    草鉴定Grass Cownoisseur 题目链接 约翰有n块草场,编号1到n,这些草场由若干条单行道相连.奶牛贝西是美味牧草的鉴赏家,她想到达尽可能多的草场去品尝牧草. 贝西总是从1号草场出发,最后 ...

  5. 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur (SCC缩点,SPFA最长路,枚举反边)

    P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 In an effort to better manage the grazing patterns of hi ...

  6. Tarjan水题系列(5):最大半连通子图 [ZJOI2007 luogu P2272]

    题目 大意: 缩点后转为求最长链的长度和最长链的个数 思路: 看懂题就会做系列 长度和个数都可以拓扑排序后DP求得 毕竟是2007年的题 代码: 如下 #include <cstdio> ...

  7. [USACO15JAN]草鉴定Grass Cownoisseur (分层图,最长路,$Tarjan$)

    题目链接 Solution 水水的套路题. 可以考虑到一个环内的点是可以都到达的,所以 \(tajan\) 求出一个 \(DAG\) . 然后 \(DAG\) 上的点权值就是 \(scc\) 的大小. ...

  8. Tarjan水题系列(4):HAOI2010 软件安装

    题目: 现在我们的手头有N个软件,对于一个软件i,它要占用Wi​的磁盘空间,它的价值为Vi​.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi​的和最大). ...

  9. Tarjan水题系列(3):HNOI2006 潘多拉的魔盒

    题目: 链接 大意: 盒子与盒子之间的关系构成一个有向图 求图上包含节点数最多的路径的节点数 思路: 有向图上求包含节点数最多的路径的节点数 可直接使用tarjan缩点后拓扑dp求得 在此不赘述 此题 ...

随机推荐

  1. 【leetcode】Path Sum IV

    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...

  2. 安装SQL2012出现[HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD)设置为 1

    本人安装SQL2012出现这个错误,找了三天三夜,终于把问题找出来,共享给有需要的人们,不用重新换系统 错误如下: 1,此问题是系统.net Framework版本冲突,首先下载.net Framew ...

  3. 常用深度学习框架(keras,pytorch.cntk,theano)conda 安装--未整理

    版本查询 cpu tensorflow conda env list source activate tensorflow python import tensorflow as tf 和 tf.__ ...

  4. floor函数用法

    floor(x),也写做Floor(x),其功能是“向下取整”,或者说“向下舍入”,即取不大于x的最大整数(与“四舍五入”不同,下取整是直接取按照数轴上最接近要求值的左边值,即不大于要求值的最大的那个 ...

  5. POJ 3260 The Fewest Coins(完全背包+多重背包=混合背包)

    题目代号:POJ 3260 题目链接:http://poj.org/problem?id=3260 The Fewest Coins Time Limit: 2000MS Memory Limit: ...

  6. Java虚拟机之JVM系统和内存模型

    1.类加载子系统 负责从文件系统或者网络中加载Class信息,加载的信息存放在一块称之为方法区的内存空间里. 2.方法区 存放类信息.常量信息.常量池信息.包括字符串字面量和数字常量等,方法区的大小决 ...

  7. Android视频处理 --处理视频第一帧缩略图

    从API 8开始,新增了一个类: android.media.ThumbnailUtils这个类提供了3个静态方法一个用来获取视频第一帧得到的Bitmap,2个对图片进行缩略处理. ? 1 publi ...

  8. zabbix配置通过远程命令来发送邮件

    1.安装好zabbix后,在/var/log/zabbix可以查看日志. 2.主机通过zabbix-get检查 yum install zabbix-get -y zabbix-get  -s 客户主 ...

  9. route Cmd详解

    第一条命令,配置外网网关: route -p add 0.0.0.0 mask 0.0.0.0 192.168.1.1 第二条命令,配置内网网关:route -p add 192.168.0.0 ma ...

  10. CentOS关闭系统不必要的端口

    注:以下所有操作均在CentOS 7.2 x86_64位系统下完成. 1)首先查看当前系统开放的端口号: # netstat -tlnup Active Internet connections (o ...