BZOJ 3887 [Usaco2015 Jan]Grass Cownoisseur
来源:互联网 发布:linux内核裁剪步骤 编辑:程序博客网 时间:2024/05/29 17:20
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
Gold&鸣谢18357
哈哈,一开始只知道缩点,之后就苟且的看了题解。
求每一个点到1和1到每一个点的最大点权和。
注意一定要判断翻转一条边之后能否组成一个新环。
题解用的是拓扑排序,然而宝宝表示暴力搜索更适合我。
#include<iostream>#include<cstring>#include<cstdio>#include<stack>#include<queue>using namespace std;const int N=100005;int n,m,cnt,tim,dcnt,ans,hd[N],dfn[N],low[N],x[N],y[N],f[N][2],sz[N],belong[N],du[N][2];bool ins[N];stack<int>stk;queue<int>q;struct edge{ int to,nxt;}v[2*N];void addedge(int x,int y){ v[++cnt].to=y; v[cnt].nxt=hd[x]; hd[x]=cnt;}void tarjan(int u){ dfn[u]=low[u]=++cnt; ins[u]=1; stk.push(u); for(int i=hd[u];i;i=v[i].nxt) if(!dfn[v[i].to]) { tarjan(v[i].to); low[u]=min(low[u],low[v[i].to]); } else if(ins[v[i].to]) low[u]=min(low[u],dfn[v[i].to]); if(dfn[u]==low[u]) { ++dcnt; while(1) { int t=stk.top(); stk.pop(); belong[t]=dcnt; ins[t]=0; sz[dcnt]++; if(u==t) break; } }}void re(int ch){ cnt=0; memset(hd,0,sizeof(hd)); for(int i=1;i<=m;i++) if(belong[x[i]]!=belong[y[i]]) if(!ch) { addedge(belong[x[i]],belong[y[i]]); du[belong[y[i]]][ch]++; } else { addedge(belong[y[i]],belong[x[i]]); du[belong[x[i]]][ch]++; }}void dfs(int u,int res,int ch){ if(f[u][ch]<res+sz[u]) { f[u][ch]=res+sz[u]; for(int i=hd[u];i;i=v[i].nxt) dfs(v[i].to,f[u][ch],ch); }}int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) { scanf("%d%d",&x[i],&y[i]); addedge(x[i],y[i]); } for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i); re(0),dfs(belong[1],0,0); re(1),dfs(belong[1],0,1); for(int i=1;i<=m;i++) if(belong[x[i]]!=belong[y[i]]&&f[belong[x[i]]][1]&&f[belong[y[i]]][0]) ans=max(ans,f[belong[x[i]]][1]+f[belong[y[i]]][0]-sz[belong[1]]); printf("%d\n",ans); return 0;}
- bzoj 3887: [Usaco2015 Jan]Grass Cownoisseur
- BZOJ 3887 [Usaco2015 Jan]Grass Cownoisseur
- BZOJ 3887 Usaco2015 Jan Grass Cownoisseur Tarjan+拓扑排序
- bzoj 3887: [Usaco2015 Jan]Grass Cownoisseur(spfa+tarjan)
- BZOJ[3887][Usaco2015 Jan]Grass Cownoisseur Tarjan+拓扑排序
- BZOJ 【BZOJ3887】【Usaco2015 Jan】Grass Cownoisseur
- [Usaco2015 Jan]Grass Cownoisseur 图论 tarjan spfa
- 【BZOJ3887】【Usaco2015 Jan】Grass Cownoisseur 算法模块有点多
- [BZOJ3887][Usaco2015 Jan]Grass Cownoisseur(tarjan+spfa)
- [Usaco2015 Jan]Grass Cownoisseur Tarjan缩点+SPFA
- bzoj3887 [Usaco2015 Jan]Grass Cownoisseur tarjan+拓补排序
- [BZOJ3887][Usaco2015 Jan]Grass Cownoisseur(tarjan+spfa)
- BZOJ 3888: [Usaco2015 Jan]Stampede
- bzoj 3890: [Usaco2015 Jan]Meeting Time bfs
- bzoj 3889: [Usaco2015 Jan]Cow Routing SPFA
- [USACO15JAN]草鉴定Grass Cownoisseur
- BZOJ 3890 Usaco2015 Jan Meeting Time 拓扑图DP
- BZOJ 3890 Usaco2015 Jan Meeting Time 拓扑排序
- 一起做RGB-D SLAM (5)
- 快键键
- ubuntu wordpress建站
- zabbix创建监控项item,触发器triger监控mysql参数特殊参数状态
- win10 任务栏右键无反应
- BZOJ 3887 [Usaco2015 Jan]Grass Cownoisseur
- keil5 编译的时候不通过找不到sys.o文件,运行不通过解决方法(win10)
- ArcGis安装
- 剑指offer-面试题17-合并两个排序的链表
- centos7设置静态ip地址
- iscroll4中滑动页面触发点击事件
- Java图形界面(GUI) 动态获取上传或下载文件的路径问题
- 大数据技术Hadoop面试题,看看你能答对多少?答案在后面
- 关于Android使用ActionBarDrawerToggle时导航图标没有动画效果