bzoj3887: [Usaco2015 Jan]Grass Cownoisseur
题意:
给一个有向图,然后选一条路径起点终点都为1的路径出来,有一次机会可以沿某条边逆方向走,问最多有多少个点可以被经过?(一个点在路径中无论出现多少正整数次对答案的贡献均为1)
=>有向图我们先考虑缩点。然后观察缩点后的图可以发现新的路径中必定只有一条边是反向的才符合条件。那么我们可以联想到某道最短路的题将边反向存一遍后分别从s和t跑一跑。那么这里bfs跑一跑就行了。然后有一个坑点:这种重建图的注意es和edges不然es会在中途就被修改掉了。。。
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
#define qwq(x) for(edge *o=head[x];o;o=o->next)
#define qaq(x) for(edge *o=hd[x];o;o=o->next)
#define TAT(x) for(edge *o=eo[x];o;o=o->next)
int read(){
int x=0;char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=x*10+c-'0',c=getchar();
return x;
}
const int nmax=1e5+5;
const int inf=0x7f7f7f7f; struct edge{
int to;edge *next;
};edge es[nmax],edges[nmax<<1],*pt=es,*head[nmax],*hd[nmax],*eo[nmax];
void add(int u,int v){
pt->to=v;pt->next=head[u];head[u]=pt++;
}
void adde(int u,int v){
pt->to=v;pt->next=hd[u];hd[u]=pt++;
pt->to=u;pt->next=eo[v];eo[v]=pt++;
}
int pre[nmax],dfs_clock=0,scc_cnt=0,sccno[nmax],sm[nmax];
stack<int>s;
int dfs(int x){
int lowu=pre[x]=++dfs_clock;s.push(x);
qwq(x){
if(!pre[o->to]) lowu=min(lowu,dfs(o->to));
else if(!sccno[o->to]) lowu=min(lowu,pre[o->to]);
}
if(lowu==pre[x]){
++scc_cnt;int tc=0;
while(1){
int tx=s.top();s.pop();
sccno[tx]=scc_cnt;++tc;
if(x==tx) break;
}
sm[scc_cnt]=tc;
}
return lowu;
} queue<int>q;bool vis[nmax];int f[nmax],g[nmax];
int test_cnt=0;
void bfs1(int x){
q.push(x);f[x]=sm[x];int tx;clr(vis,0);
while(!q.empty()){
tx=q.front();q.pop();vis[tx]=0;
qaq(tx) if(f[o->to]<f[tx]+sm[o->to]){
f[o->to]=f[tx]+sm[o->to];
if(!vis[o->to]) q.push(o->to),vis[o->to]=1;
}
}
}
void bfs2(int x){
q.push(x);g[x]=sm[x];int tx;clr(vis,0);
while(!q.empty()){
tx=q.front();q.pop();vis[tx]=0;
TAT(tx) if(g[o->to]<g[tx]+sm[o->to]){
g[o->to]=g[tx]+sm[o->to];
if(!vis[o->to]) q.push(o->to),vis[o->to]=1;
}
}
} int main(){
int n=read(),m=read(),u,v;
rep(i,1,m) u=read(),v=read(),add(u,v);
rep(i,1,n) if(!pre[i]) dfs(i);
//rep(i,1,n) printf("%d ",sccno[i]);printf("\n"); pt=edges;
rep(i,1,n) qwq(i) if(sccno[i]!=sccno[o->to]) adde(sccno[i],sccno[o->to]);
bfs1(sccno[1]);bfs2(sccno[1]);
//rep(i,1,scc_cnt) printf("%d %d\n",f[i],g[i]); int ans=sm[sccno[1]];
rep(i,1,scc_cnt) qaq(i) {
if(g[i]&&f[o->to]) ans=max(ans,g[i]+f[o->to]-sm[sccno[1]]);
}
printf("%d\n",ans);return 0;
}
3887: [Usaco2015 Jan]Grass Cownoisseur
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 179 Solved: 92
[Submit][Status][Discuss]
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
Output
Sample Input
1 2
3 1
2 5
2 4
3 7
3 5
3 6
6 5
7 2
4 7
Sample Output
HINT
Source
bzoj3887: [Usaco2015 Jan]Grass Cownoisseur的更多相关文章
- BZOJ3887 [Usaco2015 Jan] Grass Cownoisseur 【tarjan】【DP】*
BZOJ3887 [Usaco2015 Jan] Grass Cownoisseur Description In an effort to better manage the grazing pat ...
- 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的路径出来,有一次机会可以沿某条边逆方向走,问最多有多少个点可以被经过?(一个点在 ...
- 洛谷—— 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 ...
- [Usaco2015 Jan]Grass Cownoisseur Tarjan缩点+SPFA
考试的时候忘了缩点,人为dfs模拟缩点,没想到竟然跑了30分,RB爆发... 边是可以重复走的,所以在同一个强连通分量里,无论从那个点进入从哪个点出,所有的点一定能被一条路走到. 要使用缩点. 然后我 ...
- [Usaco2015 Jan]Grass Cownoisseur 图论 tarjan spfa
先缩点,对于缩点后的DAG,正反跑spfa,枚举每条边进行翻转即可 #include<cstdio> #include<cstring> #include<iostrea ...
- BZOJ 3887/Luogu P3119: [Usaco2015 Jan]Grass Cownoisseur (强连通分量+最长路)
分层建图,反向边建在两层之间,两层内部分别建正向边,tarjan缩点后,拓扑排序求一次1所在强连通分量和1+n所在强联通分量的最长路(长度定义为路径上的强联通分量内部点数和).然后由于1所在强连通分量 ...
- BZOJ 3887: [Usaco2015 Jan]Grass Cownoisseur tarjan + spfa
Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) ...
随机推荐
- 51nod1118(递推)
题目链接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1118 题意: 中文题诶~ 思路: 因为机器人只能往下或者右 ...
- CF 979D Kuro and GCD and XOR and SUM(异或 Trie)
CF 979D Kuro and GCD and XOR and SUM(异或 Trie) 给出q(<=1e5)个操作.操作分两种,一种是插入一个数u(<=1e5),另一种是给出三个数x, ...
- 洛谷P1275 魔板
P1275 魔板 题目描述 有这样一种魔板:它是一个长方形的面板,被划分成n行m列的n*m个方格.每个方格内有一个小灯泡,灯泡的状态有两种(亮或暗).我们可以通过若干操作使魔板从一个状态改变为另一个状 ...
- P2939 [USACO09FEB]改造路Revamping Trails(分层图最短路)
传送门 完了我好像连分层图最短路都不会了……果然还是太菜了…… 具体来说就是记录一个步数表示免费了几条边,在dijkstra的时候以步数为第一关键字,距离为第二关键字.枚举边的时候分别枚举免不免费下一 ...
- RequireJS 2.0 API之配置项
转载自http://blog.csdn.net/kevinwon1985/article/details/8155267 RequireJS 把每一个依赖项当做一个script标签,使用 head.a ...
- Java基础笔记(十四)——封装
封装(好比ATM机) 将类的某些信息隐藏在类内部,不允许外部程序直接访问(隐藏对象的信息),通过该类提供的方法来实现对隐藏信息的操作和访问(留出访问的接口). 特点: 1.只能通过规定的方法访问数据. ...
- Jmeter 自动化测试报告扩展(转 Todo 需要修正)
首先了解下生成测试报告的过程,我们看到的测试报告是由.jtl格式转换为.html,html报告的样式由extras目录下xsl文件决定.优化测试报告需要分为两部分内容,首先我们要优化输出的测试内容,其 ...
- 设计模式——工厂方法(Factory Method)
定义一个用于创建对象的接口,让子类决定实例化哪一个类.工厂方法使一个类的实例化延迟到其子类. ——DP UML类图 模式说明 抽象业务基类 实际业务类的公共基类,也是工厂要创建的所有对象的父类,这部分 ...
- 原svn账户清除,及使用新用户名密码操作方法
原svn账户清除,及使用新用户名密码操作方法 第一步:先清除原svn账户信息,如图示,电脑桌面右击“ToroiseSVN--Settings”. 在Settings中,选择Saved Data中的Cl ...
- 非关系型数据库---Memcached
一.概述 1.Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库的负载: 2.Memcached通过 在内存中缓存对象和数据 来减少读取数据库的次数,从而提升网站的 ...