博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BZOJ3887 [Usaco2015 Jan] Grass Cownoisseur 【tarjan】【DP】*
阅读量:5061 次
发布时间:2019-06-12

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

BZOJ3887 [Usaco2015 Jan] Grass Cownoisseur


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


给你一张有向图图,有一次走反向边的机会

然后问你从1出发回到1最多经过多少个点


首先想到的是tarjan缩点,一个强连通分量的大小显然只要进入了就可以全部吃下来

然后我们得到了一个DAG

考虑在这上面走一圈,有一条边可以反向做多能经过多少边

首先我们显然不能枚举那一个边是反向的,但是我们可以排除这个边随便考虑一下

我们正反建图,然后发现对于一条边(u>v)(u−>v),把这条边反向的贡献就是d[1>v]+d[u>1]d[1−>v]正向+d[u−>1]反向,然后我们就分别在正反的图上进行DP,也可以说是跑最长路

然后最后统计贡献就好了


tips:一定在DP的时候吧初值设为-INF,否则累计的时候会出事情,要考虑无法到达的情况


#include
using namespace std;#define N 100010#define pi pair
#define INF 0x3f3f3f3fint cnt_scc,tot=0,n,m;int dfn[N],low[N],vis[N]={
0};int siz[N]={
0},head[N]={
0};int bel[N];struct Edge{
int u,v,next;}E[N<<1];stack
s;void add(int u,int v){ E[++tot]=(Edge){u,v,head[u]}; head[u]=tot;}int tip=0;void tarjan(int u){ dfn[u]=low[u]=++tip; vis[u]=1; s.push(u); for(int i=head[u];i;i=E[i].next){ int v=E[i].v; if(!dfn[v])tarjan(v),low[u]=min(low[u],low[v]); else if(vis[v])low[u]=min(low[u],dfn[v]); } if(dfn[u]==low[u]){ cnt_scc++; while(s.top()!=u){ bel[s.top()]=cnt_scc; vis[s.top()]=0; s.pop(); } vis[s.top()]=0; bel[s.top()]=cnt_scc; s.pop(); }}map
mp;struct DAG{ Edge E[N<<1]; bool inq[N]; int head[N],tot; int dp[N],ru[N]; DAG(){ memset(head,0,sizeof(head)); for(int i=0;i
q; q.push(bel[1]); dp[bel[1]]=0; while(!q.empty()){ int u=q.front();q.pop(); inq[u]=0; for(int i=head[u];i;i=E[i].next){ int v=E[i].v; if(dp[v]

转载于:https://www.cnblogs.com/dream-maker-yk/p/9676284.html

你可能感兴趣的文章
Git学习资料
查看>>
phpstudy 无法启动,提示unable write to xxxxxphpstudy.ini
查看>>
javascript 的继承
查看>>
NServiceBus-安装
查看>>
(转)Loader ,URLLoader ,URLStream的区别
查看>>
[转]前端版本控制工具 gulp或者Grunt插件 基于nodejs
查看>>
Educational Codeforces Round 23 F. MEX Queries 离散化+线段树
查看>>
第04次作业-树
查看>>
2. Postman发送各种格式请求的方法
查看>>
Spring mvc annotation and xml example
查看>>
关于报错The specified child already has a parent的解决办法
查看>>
16 this和构造代码块
查看>>
LIN总线学习-总线逻辑
查看>>
Uva(10305)
查看>>
调整数组顺序使奇数位于偶数前面
查看>>
React Native知识3-TextInput组件
查看>>
MDK 添加芯片支持 以STM32F103ZE为例
查看>>
搭建 flask 应用
查看>>
C++ 约瑟夫环问题
查看>>
制作iOS应用图标的最简单方法
查看>>