【BZOJ】1638: [Usaco2007 Mar]Cow Traffic 奶牛交通(dfs+dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1638
一条边(u, v)经过的数量=度0到u的数量×v到n的数量
两次记忆化dfs算出他们即可
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=5005, M=50005;
int n, m, ihead[N], cnt, in[N], f1[N], f2[N], a[M], b[M];
struct ED { int to, next; }e[M];
void add(int u, int v) {
e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v;
}
void dfs1(int x) {
if(ihead[x]==0) f1[x]=1;
for(int i=ihead[x]; i; i=e[i].next) {
if(!f1[e[i].to]) dfs1(e[i].to);
f1[x]+=f1[e[i].to];
}
}
void dfs2(int x) {
if(ihead[x]==0) f2[x]=1;
for(int i=ihead[x]; i; i=e[i].next) {
if(!f2[e[i].to]) dfs2(e[i].to);
f2[x]+=f2[e[i].to];
}
}
int main() {
read(n); read(m);
for1(i, 1, m) {
int u=getint(), v=getint();
a[i]=u, b[i]=v;
add(u, v);
in[v]=1;
}
for1(i, 1, n) if(!in[i]) dfs1(i);
CC(ihead, 0); cnt=0;
for1(i, 1, m) add(b[i], a[i]);
dfs2(n);
int ans=0;
for1(i, 1, m)
ans=max(ans, f1[b[i]]*f2[a[i]]);
print(ans);
return 0;
}
Description
农 场中,由于奶牛数量的迅速增长,通往奶牛宿舍的道路也出现了严重的交通拥堵问题.FJ打算找出最忙碌的道路来重点整治. 这个牧区包括一个由M (1 ≤ M ≤ 50,000)条单行道路(有向)组成的网络,以及 N (1 ≤ N ≤ 5,000)个交叉路口(编号为1..N),每一条道路连接两个不同的交叉路口.奶牛宿舍位于第N个路口.每一条道路都由编号较小的路口通向编号较大的路 口.这样就可以避免网络中出现环.显而易见,所有道路都通向奶牛宿舍.而两个交叉路口可能由不止一条边连接. 在准备睡觉的时候,所有奶牛都从他们各自所在的交叉路口走向奶牛宿舍,奶牛只会在入度为0的路口,且所有入度为0的路口都会有奶牛. 帮助FJ找出最忙碌的道路,即计算所有路径中通过某条道路的最大次数.答案保证可以用32位整数存储.
Input
第一行:两个用空格隔开的整数:N,M.
第二行到第M+1行:每行两个用空格隔开的整数ai,bi,表示一条道路从ai到bi.
Output
第一行: 一个整数,表示所有路径中通过某条道路的最大次数.
Sample Input
1 3
3 4
3 5
4 6
2 3
5 6
6 7
Sample Output
样例说明:
1 4
\ / \
3 6 -- 7
/ \ /
2 5
通向奶牛宿舍的所有路径:
1 3 4 6 7
1 3 5 6 7
2 3 4 6 7
2 3 5 6 7
HINT
Source
【BZOJ】1638: [Usaco2007 Mar]Cow Traffic 奶牛交通(dfs+dp)的更多相关文章
- BZOJ 1638 [Usaco2007 Mar]Cow Traffic 奶牛交通:记忆化搜索【图中边的经过次数】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1638 题意: 给你一个有向图,n个点,m条有向边. 对于所有从入度为0的点到n的路径,找出 ...
- bzoj 1638: [Usaco2007 Mar]Cow Traffic 奶牛交通【记忆化搜索】
震惊!记忆化搜索忘记返回map值调了半小时! 边(u,v)的经过次数是:能到u的牛数*v到n的方案数.正反两次连边,dfs两次即可 #include<iostream> #include& ...
- 1638: [Usaco2007 Mar]Cow Traffic 奶牛交通
1638: [Usaco2007 Mar]Cow Traffic 奶牛交通 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 618 Solved: 217 ...
- BZOJ1638: [Usaco2007 Mar]Cow Traffic 奶牛交通
1638: [Usaco2007 Mar]Cow Traffic 奶牛交通 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 571 Solved: 199 ...
- 【动态规划】bzoj1638 [Usaco2007 Mar]Cow Traffic 奶牛交通
设f[u]为从度数0到u的路径条数,f2[u]为从u到n的路径条数. ans=max{f[x[i]]*f2[y[i]]}(1<=i<=m). #include<cstdio> ...
- BZOJ 1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏( floyd )
直接floyd.. ---------------------------------------------------------------------------- #include<c ...
- BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐( dfs )
直接从每个奶牛所在的farm dfs , 然后算一下.. ----------------------------------------------------------------------- ...
- BZOJ 1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏
Description Farmer John 想让她的奶牛准备郡级跳跃比赛,贝茜和她的伙伴们正在练习跨栏.她们很累,所以她们想消耗最少的能量来跨栏. 显然,对于一头奶牛跳过几个矮栏是很容易的,但是高 ...
- BZOJ 1641 [Usaco2007 Nov]Cow Hurdles 奶牛跨栏:新版floyd【路径上最大边最小】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1641 题意: 给你一个有向图,n个点(n <= 300),m条边,边权为h[i]. ...
随机推荐
- Object.defineProperty 监听对象属性变化
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- OpenCV求取轮廓线
// Threshold.cpp : Defines the entry point for the console application. // #include "stdafx.h&q ...
- jsp标签(jsp动作元素)
<jsp:forward page=”/index.jsp”></jsp:forward>也是跳转. 可以用来配置为首页,来启动某个servlet. <jsp:inclu ...
- linux中echo的使用方法
1.echo命令我们经常使用的选项有两个,一个是-n,表示输出之后不换行.另外一个是-e,表示对于转义字符按对应的方式处理,假设不加-e那么对于转义字符会按普通字符处理. 2.echo输出时的转义字符 ...
- 使用samba实现linux与windows文件共享
1,安装samba sudo apt-get install samba ...
- Python爬虫实战案例:爬取爱奇艺VIP视频
一.实战背景 爱奇艺的VIP视频只有会员能看,普通用户只能看前6分钟.比如加勒比海盗5的URL:http://www.iqiyi.com/v_19rr7qhfg0.html#vfrm=19-9-0-1 ...
- Eclipse wtp project dependent project facets问题
wtp project会编译成一个war包,但在eclipse里debug时,依赖的project没有自动编译成jar包并打包到war的lib目录里. 原因:依赖project的facets未设置或未 ...
- 【转帖】云平台发现服务构建:为什么不使用ZooKeeper
http://www.chinacloud.cn/show.aspx?id=19979&cid=16 [日期:2015-04-29] 来源:dockerone 作者: [字体:大 中 小] ...
- 基于Redis的消息队列php-resque
转载:http://netstu.5iunix.net/archives/201305-835/ 最近的做一个短信群发的项目,需要用到消息队列.因此开始了我对消息队列选型的漫长路. 为什么选型会纠结呢 ...
- 微信公众平台消息接口PHP版
使用前提条件:拥有一个公网上的HTTP服务器主机空间,具有创建目录.上传文件等权限.推荐新浪的SAE.http://sae.sina.com.cn/ 首先请注册微信公众平台的账号,注册地址:http: ...