BZOJ3887 [Usaco2015 Jan] Grass Cownoisseur 【tarjan】【DP】*
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)" role="presentation" style="position: relative;">(u−>v)(u−>v),把这条边反向的贡献就是d[1−>v]正向+d[u−>1]反向" role="presentation" style="position: relative;">d[1−>v]正向+d[u−>1]反向d[1−>v]正向+d[u−>1]反向,然后我们就分别在正反的图上进行DP,也可以说是跑最长路
然后最后统计贡献就好了
tips:一定在DP的时候吧初值设为-INF,否则累计的时候会出事情,要考虑无法到达的情况
#include<bits/stdc++.h>
using namespace std;
#define N 100010
#define pi pair<int,int>
#define INF 0x3f3f3f3f
int 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<int> 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<pi,int> 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<N;i++)dp[i]=-INF;
tot=0;
}
void add(int u,int v){
E[++tot]=(Edge){u,v,head[u]};
head[u]=tot;
}
void solve(){
queue<int> 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]<dp[u]+siz[v]){
dp[v]=dp[u]+siz[v];
if(!inq[v])q.push(v),inq[v]=1;
}
}
}
}
}g1,g2;
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int u,v;scanf("%d%d",&u,&v);
add(u,v);
}
for(int i=1;i<=n;i++)if(!dfn[i])tarjan(i);
for(int i=1;i<=n;i++)siz[bel[i]]++;
for(int i=1;i<=tot;i++){
int u=bel[E[i].u],v=bel[E[i].v];
if(u==v)continue;
if(mp[(pi){u,v}]||mp[(pi){v,u}])continue;
g1.add(u,v);
g2.add(v,u);
mp[(pi){u,v}]=mp[(pi){v,u}]=1;
}
g1.solve();
g2.solve();
int ans=0;
for(int i=1;i<=tot;i++){
int u=bel[E[i].u],v=bel[E[i].v];
ans=max(ans,g1.dp[v]+g2.dp[u]);
}
ans+=siz[bel[1]];
printf("%d",ans);
return 0;
}
BZOJ3887 [Usaco2015 Jan] Grass Cownoisseur 【tarjan】【DP】*的更多相关文章
- bzoj3887: [Usaco2015 Jan]Grass Cownoisseur
题意: 给一个有向图,然后选一条路径起点终点都为1的路径出来,有一次机会可以沿某条边逆方向走,问最多有多少个点可以被经过?(一个点在路径中无论出现多少正整数次对答案的贡献均为1) =>有向图我们 ...
- [Usaco2015 Jan]Grass Cownoisseur 图论 tarjan spfa
先缩点,对于缩点后的DAG,正反跑spfa,枚举每条边进行翻转即可 #include<cstdio> #include<cstring> #include<iostrea ...
- BZOJ3887 [Usaco2015 Jan]Grass Cownoisseur[缩点]
首先看得出缩点的套路.跑出DAG之后,考虑怎么用逆行条件.首先可以不用,这样只能待原地不动.用的话,考虑在DAG上向后走,必须得逆行到1号点缩点后所在点的前面,才能再走回去. 于是统计从1号点缩点所在 ...
- [补档][Usaco2015 Jan]Grass Cownoisseur
[Usaco2015 Jan]Grass Cownoisseur 题目 给一个有向图,然后选一条路径起点终点都为1的路径出来,有一次机会可以沿某条边逆方向走,问最多有多少个点可以被经过? (一个点在路 ...
- [bzoj3887][Usaco2015 Jan]Grass Cownoisseur_trajan_拓扑排序_拓扑序dp
[Usaco2015 Jan]Grass Cownoisseur 题目大意:给一个有向图,然后选一条路径起点终点都为1的路径出来,有一次机会可以沿某条边逆方向走,问最多有多少个点可以被经过?(一个点在 ...
- BZOJ_3887_[Usaco2015 Jan]Grass Cownoisseur_强连通分量+拓扑排序+DP
BZOJ_3887_[Usaco2015 Jan]Grass Cownoisseur_强连通分量+拓扑排序+DP Description In an effort to better manage t ...
- [Usaco2015 Jan]Grass Cownoisseur Tarjan缩点+SPFA
考试的时候忘了缩点,人为dfs模拟缩点,没想到竟然跑了30分,RB爆发... 边是可以重复走的,所以在同一个强连通分量里,无论从那个点进入从哪个点出,所有的点一定能被一条路走到. 要使用缩点. 然后我 ...
- BZOJ 3887: [Usaco2015 Jan]Grass Cownoisseur tarjan + spfa
Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) ...
- 洛谷—— P3119 [USACO15JAN]草鉴定Grass Cownoisseur || BZOJ——T 3887: [Usaco2015 Jan]Grass Cownoisseur
http://www.lydsy.com/JudgeOnline/problem.php?id=3887|| https://www.luogu.org/problem/show?pid=3119 D ...
随机推荐
- MySQL 乐观锁 悲观锁 共享锁 排他锁
乐观锁 乐观锁是逻辑概念上的锁,不是数据库自带的,需要我们自己去实现.乐观锁是指操作数据库时(更新操作),想法很乐观,认为这次的操作不会导致冲突,在操作数据时,并不进行任何其他的特殊处理(也就是不加锁 ...
- Win10配Theano环境和Keras框架
网络上有各种各样的win7 64bit安装theano的方法,我也试过好多,各种各样的问题.因为之前没了解过MinGw等东西,所以安装起来比较费劲,经过不断的尝试,最终我按照以下过程安装成功. 其实过 ...
- SpringBoot学习(2)
三.日志 1.日志框架 springboot:底层是spring框架,spring框架默认使用JCL; springboot选用SLF4j和logback; 2.SLF4j使用 1.如何在系统中使用S ...
- TestNG,多个场景结合运行Suite.xml
方法一.首先新增一个.xml文件(经过一段时间的练习,找到其他方法添加XML,如下) 再到文件中添加如下: <suite name = "Selenium school"&g ...
- Ubuntu 14.04配置虚拟主机
虚拟主机常用于在一个单独的IP地址上提供多个域名的网站服务.如果有人想在单个VPS的单个IP地址运行多个网站,这是非常有用的.在这个教程中,让我告诉你如何设置在Ubuntu 14.04 LTS的Apa ...
- bzo1016: [JSOI2008]最小生成树计数
现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一条边不同,则这两个最小生成树就是不同的).由于不同的最小生成树 ...
- 三十一 Python分布式爬虫打造搜索引擎Scrapy精讲—chrome谷歌浏览器无界面运行、scrapy-splash、splinter
1.chrome谷歌浏览器无界面运行 chrome谷歌浏览器无界面运行,主要运行在Linux系统,windows系统下不支持 chrome谷歌浏览器无界面运行需要一个模块,pyvirtualdispl ...
- Gruntjs提高生产力(三)
以下例子来自真实项目,有所删减 grunt-test project 目录结构如下 一我的目的: 1.在src-dev目录中开发最终产出于src目录 2.src-dev中的index目录相当于一个wi ...
- Struts2异常处理配置
<package name="lee" extends="struts-default"> <!--定义全局结构映射 --> <g ...
- 设置了width和height的a元素在IE11与IE11以下浏览器中的不同渲染方式
#welcomeMiddleBtn { display: block; width: 73px; height: 120px; margin: 0px auto; } <a id="w ...