解题

首先缩点没啥可说的,然后考虑枚举这次逆行的边。具体来说在正常的图和反图上各跑一次最长路,然后注意减掉起点的贡献,用拓扑排序实现(我这里瞎写了个Bellman_Ford,其实在DAG上这好像和拓扑排序的复杂度是一样的=。=)

一个细节:注意可能整个图是个强连通分量,所以答案初始是起点所在的强联通分量的大小

 #include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
int dfn[N],low[N],col[N],stk[N],ins[N],inq[N],siz[N],dis[][N];
int p[N],noww[N],goal[N],P[N][],Noww[N][],Goal[N][];
int n,m,c,t1,t2,cnt,cnt0,cnt1,tot,top,ans;
queue<int> qs;
void link(int f,int t)
{
noww[++cnt]=p[f];
goal[cnt]=t,p[f]=cnt;
}
void relink(int f,int t)
{
Noww[++cnt0][]=P[f][];
Goal[cnt0][]=t,P[f][]=cnt0;
Noww[++cnt1][]=P[t][];
Goal[cnt1][]=f,P[t][]=cnt1;
}
void Tarjan_SCC(int nde)
{
dfn[nde]=low[nde]=++tot;
stk[++top]=nde,ins[nde]=true;
for(int i=p[nde];i;i=noww[i])
if(!dfn[goal[i]])
Tarjan_SCC(goal[i]),low[nde]=min(low[nde],low[goal[i]]);
else if(ins[goal[i]])
low[nde]=min(low[nde],low[goal[i]]);
if(dfn[nde]==low[nde])
{
c++; int tmp;
do
{
tmp=stk[top--];
ins[tmp]=false;
col[tmp]=c,siz[c]++;
}while(nde!=tmp);
}
}
void Bellman_Ford(int s,int t)
{
memset(dis[t],0xc0,sizeof dis[t]);
dis[t][s]=siz[s],qs.push(s),inq[s]=true;
while(!qs.empty())
{
int tn=qs.front();
qs.pop(),inq[tn]=false;
for(int i=P[tn][t];i;i=Noww[i][t])
if(dis[t][Goal[i][t]]<dis[t][tn]+siz[Goal[i][t]])
{
dis[t][Goal[i][t]]=dis[t][tn]+siz[Goal[i][t]];
if(!inq[Goal[i][t]])
qs.push(Goal[i][t]),inq[Goal[i][t]]=true;
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
scanf("%d%d",&t1,&t2),link(t1,t2);
for(int i=;i<=n;i++)
if(!dfn[i]) Tarjan_SCC(i);
for(int i=;i<=n;i++)
for(int j=p[i];j;j=noww[j])
if(col[i]!=col[goal[j]])
relink(col[i],col[goal[j]]);
Bellman_Ford(col[],),Bellman_Ford(col[],),ans=siz[col[]];
for(int i=;i<=c;i++)
for(int j=P[i][];j;j=Noww[j][])
ans=max(ans,dis[][i]+dis[][Goal[j][]]-siz[col[]]);
printf("%d",ans);
return ;
}

解题:USACO15JAN Grass Cownoisseur的更多相关文章

  1. P3119 [USACO15JAN]Grass Cownoisseur G

    P3119 [USACO15JAN]Grass Cownoisseur G tarjan缩点+分层图上跑 spfa最长路 约翰有 \(n\) 块草场,编号 \(1\) 到 \(n\),这些草场由若干条 ...

  2. [USACO15JAN]Grass Cownoisseur

    \(tarjan\)缩点+\(DAG\)上最长路. 求一个以\(1\)为起点的最长路和一个以\(1\)为终点的最长路,然后找那个逆行边就行了. 然后这个我\(RE\)了好久,原因是\(vector\) ...

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

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

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

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

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

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

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

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

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

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

  8. [补档][Usaco2015 Jan]Grass Cownoisseur

    [Usaco2015 Jan]Grass Cownoisseur 题目 给一个有向图,然后选一条路径起点终点都为1的路径出来,有一次机会可以沿某条边逆方向走,问最多有多少个点可以被经过? (一个点在路 ...

  9. BZOJ3887 [Usaco2015 Jan] Grass Cownoisseur 【tarjan】【DP】*

    BZOJ3887 [Usaco2015 Jan] Grass Cownoisseur Description In an effort to better manage the grazing pat ...

随机推荐

  1. Hyperledger Fabric服务器配置及修改Docker容器卷宗存储根目录/位置

    Hyperledger Fabric节点服务器对存储空间的消耗还是比较大的,在我实际生产体验的过程中,每一条请求数据大概仅2K左右,但实际占用空间远不止这点,每个节点都会对Block及链进行保存维护, ...

  2. 基于Python的信用评分卡模型分析(一)

    信用风险计量体系包括主体评级模型和债项评级两部分.主体评级和债项评级均有一系列评级模型组成,其中主体评级模型可用“四张卡”来表示,分别是A卡.B卡.C卡和F卡:债项评级模型通常按照主体的融资用途,分为 ...

  3. 通过exp命令对Oracle数据库进行备份操作(提供两种情况的备份:备份本地,备份远程的数据库)

    exp 用户名/密码@数据库所在ip地址:数据库端口号/数据库的service-name file=存储到的位置 这个是能成功的 http://www.2cto.com/database/201402 ...

  4. java把map转json

    JSONUtils.toJSONString(requestMap);    com.alibaba.fastjson.JSON <!-- https://mvnrepository.com/a ...

  5. 20162314 《Program Design & Data Structures》Learning Summary Of The Second Week

    20162314 2017-2018-1 <Program Design & Data Structures>Learning Summary Of The Second Week ...

  6. cnblogs.com用户体验

    一.是否提供了良好的体验给用户(同时提供价值)? 首先我觉得博客园给我们这些用户提供了良好的用户体验,博客园提供了一个纯净的技术交流空间,在这里我们可以找到几乎所有与IT技术有关的博文,而且可以在这里 ...

  7. Scanner的例子

    package com.firstDay.one; import java.util.Scanner; public class Information { /** * @param args */ ...

  8. TensorFlow问题“Attempting to use uninitialized value”

    1.出现的问题: 对已经保存好的模型,在进行重载并继续训练的过程中出现了以下问题: 2.解决办法: 在查找了相关资料后,了解到,该错误是指在从tfrecord中读取数据时一些参数未被初始化,如果直接r ...

  9. nginx配置hls

    备注:本来是想用浏览器播放hls,后来没有成功,最后使用flash播放rtmp的方案.所以下面的配置未使用. 修改/usr/local/nginx/conf/nginx.conf文件内容如下: wor ...

  10. ASP.NET MVC 4.0 参考源码索引

    http://www.projky.com/asp.netmvc/4.0/Microsoft/AspNet/Mvc/Facebook/FacebookAppSettingKeys.cs.htmlhtt ...