题目链接


Solution

水水的套路题。

可以考虑到一个环内的点是可以都到达的,所以 \(tajan\) 求出一个 \(DAG\) 。

然后 \(DAG\) 上的点权值就是 \(scc\) 的大小。

对于那条可以反的边,直接建两层图就好了。

最后跑最长路,第一个节点的 \(scc\) 在第二张图上的对应节点的答案即为答案。

Code

#include<bits/stdc++.h>
#define N 200008
#define in(x) x=read()
using namespace std; int read()
{
char ch=getchar();int f=1,w=0;
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){w=w*10+ch-'0';ch=getchar();}
return f*w;
} struct sj{int fr,to,next,w;}a[N*4];
int dfn[N],low[N],cnt;
int bl[N],siz[N];
int head[N],size,n,m;
int sta[N],top,v[N],tot,dis[N]; void add(int x,int y,int w)
{
a[++size].to=y;
a[size].fr=x;
a[size].next=head[x];
head[x]=size;
a[size].w=w;
} void tarjan(int x)
{
dfn[x]=low[x]=++tot;
sta[++top]=x; v[x]=1;
for(int i=head[x];i;i=a[i].next)
{
int tt=a[i].to;
if(!dfn[tt]){
tarjan(tt);
low[x]=min(low[x],low[tt]);
}
else if(v[tt]) low[x]=min(low[x],dfn[tt]);
}
if(dfn[x]==low[x])
{
v[x]=0;
bl[x]=++cnt;
do{
siz[cnt]++;
v[sta[top]]=0;
bl[sta[top]]=cnt;
}while(sta[top--]!=x);
}
} void SPFA()
{
memset(v,0,sizeof(v));
int s=bl[1],t=bl[1]+cnt;
queue<int>q; q.push(s); v[s]=1; dis[s]=0;
while(!q.empty())
{
int x=q.front();q.pop();v[x]=0;
for(int i=head[x];i;i=a[i].next)
{
int tt=a[i].to;
if(dis[tt]<dis[x]+a[i].w)
{
dis[tt]=dis[x]+a[i].w;
if(!v[tt]) q.push(tt),v[tt]=1;
}
}
}
printf("%d",dis[bl[1]+cnt]);
} int main()
{
in(n),in(m);
for(int i=1;i<=m;i++)
{
int x,y; in(x),in(y);
add(x,y,0);
}
for(int i=1;i<=n;i++)
if(!dfn[i])tarjan(i); memset(head,0,sizeof(head));
for(int i=1;i<=m;i++)
{
int tt=a[i].to,fr=a[i].fr;
if(bl[tt]!=bl[fr])
{
add(bl[fr],bl[tt],siz[bl[fr]]);
add(bl[fr]+cnt,bl[tt]+cnt,siz[bl[fr]]);
add(bl[tt],bl[fr]+cnt,siz[bl[tt]]);
}
}
memset(dis,-1,sizeof(dis));
SPFA();
return 0;
}

[USACO15JAN]草鉴定Grass Cownoisseur (分层图,最长路,$Tarjan$)的更多相关文章

  1. P3119 [USACO15JAN]草鉴定Grass Cownoisseur 分层图或者跑两次最长路

    https://www.luogu.org/problemnew/show/P3119 题意 有一个有向图,允许最多走一次逆向的路,问从1再走回1,最多能经过几个点. 思路 (一)首先先缩点.自己在缩 ...

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

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

  3. 洛谷——P3119 [USACO15JAN]草鉴定Grass Cownoisseur

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

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

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

  5. 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur 解题报告

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

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

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

  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 (缩点+图上DP)

    题面 传送门:https://www.luogu.org/problemnew/show/P3119 Solution 这题显然要先把缩点做了. 然后我们就可以考虑如何处理走反向边的问题. 像我这样的 ...

  9. luogu P3119 [USACO15JAN]草鉴定Grass Cownoisseur

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

随机推荐

  1. RabbitMQ使用教程(五)如何保证队列里的消息99.99%被消费?

    1. 前情回顾 RabbitMQ使用教程(一)RabbitMQ环境安装配置及Hello World示例 RabbitMQ使用教程(二)RabbitMQ用户管理,角色管理及权限设置 RabbitMQ使用 ...

  2. "segmentation fault " when "import tensorflow as tf"

    https://github.com/tensorflow/tensorflow/issues/2034

  3. IPython安装过程 @win7 64bit

    http://www.360doc.com/content/14/0902/11/16740871_406476389.shtml 为了测验测验一下IPython的应用,今天折腾了好久的从安装包msi ...

  4. JT∕T 905 -2014 出租汽车服务管理信息系统的相关协议研究

    出租汽车服务管理信息系统(JT∕T 905 -2014) 国家的相关技术要求2014年7月正式出台,总体有四部分,   第 1 部分:总体技术要求:   第 2 部分:运营专用设备:   第 3 部分 ...

  5. 数据库引擎InnoDB和myisam的区别和联系

    1.ENGINE=InnoDB 数据库存储引擎,DEFAULT 默认,CHARSET=utf8 数据库字符编码 2.数据库的存储引擎, mysql中engine=innodb和engine=myisa ...

  6. 在Linux下搜索文件

    在Linux下搜索文件============================= 1,which 查找可执行文件的绝对路径 [root@aminglinux ~]# which cat /bin/ca ...

  7. [转载]本地配置的 *.dev,*.app域名 在谷歌浏览器中总是自动转跳到https上,导致不能访问?

    本地开发环境 .dev 不正常,找到文章mark一下 转自:https://segmentfault.com/q/1010000012339191

  8. JZOJ 3508. 【NOIP2013模拟11.5B组】好元素

    3508. [NOIP2013模拟11.5B组]好元素(good) (File IO): input:good.in output:good.out Time Limits: 2000 ms  Mem ...

  9. du 与df 统计系统磁盘不一致原因与解决方法

    事件起因: 同事发现云主机磁盘系统盘满了,准备清理系统盘,便利用du 命令统计了根目录下各文件夹的大小,发现统计的各文件夹的大小总和 加起来比 df 命令查看到的系统盘所使用空间 要小很多.这里记录下 ...

  10. 洛谷P1424小鱼的航程改进版

    题目链接https://www.luogu.org/problemnew/show/P1424