BZOJ_3887_[Usaco2015 Jan]Grass Cownoisseur_强连通分量+拓扑排序+DP

Description

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)

Input

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

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.

Sample Input

7 10
1 2
3 1
2 5
2 4
3 7
3 5
3 6
6 5
7 2
4 7

Sample Output

6

考虑一个强连通分量内的点可以随便走,于是缩个点。
然后设f[i]表示1到i最多能走几个点,g[i]表示i到n最多能走几个点,由于没有环的存在,
我们枚举边,用f[u]+g[v]更新答案,最后答案需要减掉1所在scc的大小。
 
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 100500
int head[N],to[N],nxt[N],cnt,siz[N],bel[N],tot,dfn[N],low[N],scc,S[N],ins[N],n,m,xx[N],yy[N],Q[N],in[N],l,r;
int f[N],g[N],ans;
inline void add(int u,int v) {
to[++cnt]=v; nxt[cnt]=head[u]; head[u]=cnt;
}
void dfs(int x) {
int i; dfn[x]=low[x]=++tot; ins[x]=1; S[++S[0]]=x;
for(i=head[x];i;i=nxt[i]) {
if(!dfn[to[i]]) {
dfs(to[i]);
low[x]=min(low[x],low[to[i]]);
}else if(ins[to[i]]) {
low[x]=min(low[x],dfn[to[i]]);
}
}
if(dfn[x]==low[x]) {
int t=S[S[0]--];
scc++; siz[scc]++; bel[t]=scc; ins[t]=0;
while(x!=t) {
t=S[S[0]--]; siz[scc]++; bel[t]=scc; ins[t]=0;
}
}
}
int main() {
// freopen("wander.in","r",stdin);
// freopen("wander.out","w",stdout);
scanf("%d%d",&n,&m);
if(n==1) {
puts("1"); return 0;
}
int i,x,y;
for(i=1;i<=m;i++) {
scanf("%d%d",&x,&y); add(x,y); xx[i]=x; yy[i]=y;
}
for(i=1;i<=n;i++) if(!dfn[i]) dfs(i);
memset(head,0,sizeof(head)); cnt=0; tot=0;
for(i=1;i<=m;i++) {
x=bel[xx[i]]; y=bel[yy[i]];
if(x!=y) add(x,y),in[y]++,xx[++tot]=x,yy[tot]=y;
}
for(i=1;i<=scc;i++) f[i]=g[i]=-1000000;
int S=bel[1];f[S]=0; g[S]=0;
for(i=1;i<=scc;i++) if(!in[i]) Q[r++]=i;
while(l<r) {
x=Q[l++]; f[x]+=siz[x];
for(i=head[x];i;i=nxt[i]) {
f[to[i]]=max(f[to[i]],f[x]);
if((--in[to[i]])==0) Q[r++]=to[i];
}
}
memset(head,0,sizeof(head)); cnt=0; memset(in,0,sizeof(in));
for(i=1;i<=tot;i++) {
add(yy[i],xx[i]); in[xx[i]]++;
}
l=r=0;
for(i=1;i<=scc;i++) if(!in[i]) Q[r++]=i;
while(l<r) {
x=Q[l++]; g[x]+=siz[x];
for(i=head[x];i;i=nxt[i]) {
g[to[i]]=max(g[to[i]],g[x]);
if((--in[to[i]])==0) Q[r++]=to[i];
}
}
for(i=1;i<=tot;i++) {
ans=max(ans,max(f[xx[i]]+g[yy[i]],f[yy[i]]+g[xx[i]]));
}
printf("%d\n",ans-siz[S]);
}
/*
7 10
1 2
3 1
2 5
2 4
3 7
3 5
3 6
6 5
7 2
4 7
*/ /*
5 8
3 2
4 3
3 5
4 1
2 3
1 3
4 2
1 4
*/

BZOJ_3887_[Usaco2015 Jan]Grass Cownoisseur_强连通分量+拓扑排序+DP的更多相关文章

  1. BZOJ 3887/Luogu P3119: [Usaco2015 Jan]Grass Cownoisseur (强连通分量+最长路)

    分层建图,反向边建在两层之间,两层内部分别建正向边,tarjan缩点后,拓扑排序求一次1所在强连通分量和1+n所在强联通分量的最长路(长度定义为路径上的强联通分量内部点数和).然后由于1所在强连通分量 ...

  2. BZOJ_2208_[Jsoi2010]连通数_强连通分量+拓扑排序+手写bitset

    BZOJ_2208_[Jsoi2010]连通数_强连通分量+拓扑排序+手写bitset Description Input 输入数据第一行是图顶点的数量,一个正整数N. 接下来N行,每行N个字符.第i ...

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

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

  4. poj 2762(强连通分量+拓扑排序)

    题目链接:http://poj.org/problem?id=2762 题意:给出一个有向图,判断任意的两个顶点(u,v)能否从u到达v,或v到达u,即单连通,输出Yes或No. 分析:对于同一个强连 ...

  5. BZOJ1924:[SDOI2010]所驼门王的宝藏(强连通分量,拓扑排序)

    Description Input 第一行给出三个正整数 N, R, C. 以下 N 行,每行给出一扇传送门的信息,包含三个正整数xi, yi, Ti,表示该传送门设在位于第 xi行第yi列的藏宝宫室 ...

  6. 2019ICPC(银川) - Delivery Route(强连通分量 + 拓扑排序 + dijkstra)

    Delivery Route 题目:有n个派送点,x条双向边,y条单向边,出发点是s,双向边的权值均为正,单向边的权值可以为负数,对于单向边给出了一个限制:如果u->v成立,则v->u一定 ...

  7. CDOJ 图论专题 A.不是图论 强连通分量+拓扑排序 经典

    题目链接  在其中纠错第一次wa代码 #include <cstdio> #include <cstring> #include <cstdlib> #includ ...

  8. POJ 2762 Going from u to v or from v to u?(强连通分量+拓扑排序)

    职务地址:id=2762">POJ 2762 先缩小点.进而推断网络拓扑结构是否每个号码1(排序我是想不出来这点的. .. ).由于假如有一层为2的话,那么从此之后这两个岔路的点就不可 ...

  9. [bzoj3887][Usaco2015 Jan]Grass Cownoisseur_trajan_拓扑排序_拓扑序dp

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

随机推荐

  1. utuntu16.04安装caffe+Matlab2017a+opencv3.1+CUDA8.0+cudnn6.0

    上午把tensorflow安装好了,下午和晚上装caffe的确很费劲. 默认CUDA,cuDNN可以用了 caffe官方安装教程 有些安装顺序自己也不清楚,简直就是碰运气 1. 安装之前依赖项 Gen ...

  2. drag-html

    <!doctype html><html><head><meta charset="UTF-8" /><title>Ca ...

  3. 【西祠日志】【07】努力努力,找资料,思考,怎么做asp图片上传

    [西祠日志][07]努力努力,找资料.思考.怎么做asp图片上传  (2015.07.23周四) 今天忘了带本子.直接写在书上了笔记,晚点还是夹在本子里. 学了这么久的web应用,一直都没时间去做一点 ...

  4. Sql语言复习

    一.创建数据库 创建和打开数据库 注意一点:在新建数据库的时候,一般放置数据文件与日志文件的位置,需要提前建立文件夹,不然会报错. 一般主数据文件,我们以.mdf结尾,次数据文件用.ndf结尾.对于日 ...

  5. C语言-多重背包问题

    多重背包问题 问题:有N种物品和一个容量为V的背包.第i种物品最多有n[i]件可用,每件费用是c[i],价值是w[i].求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大. 分 ...

  6. JavaScript闭包其一:闭包概论 函数式编程中一些基本定义

    http://www.nowamagic.net/librarys/veda/detail/1707前面介绍了作用域链和变量对象,现在再讲闭包就容易理解了.闭包其实大家都已经谈烂了.尽管如此,这里还是 ...

  7. c++ builder 版CreateAnonymousThread用法

    万一老师的<如今, Delphi 的多线程已经很易用了!>讲到了TThread.CreateAnonymousThread用法 如今我来讲在c++ builder使用 CreateAnon ...

  8. 为了发布博客方便,现从CSDN的naedzq整体迁移到cnblogs

    为了发布博客方便,现从CSDN的naedzq整体迁移到cnblogs

  9. Day1 [上]- 认识Python

    python简单介绍: python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为AB ...

  10. YARN和MapReduce的内存设置參考

    怎样确定Yarn中容器Container,Mapreduce相关參数的内存设置,对于初始集群,由于不知道集群的类型(如cpu密集.内存密集)我们须要依据经验提供给我们一个參考配置值,来作为基础的配置. ...