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

7 7
1 3
3 4
3 5
4 6
2 3
5 6
6 7

Sample Output

4
样例说明:

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)的更多相关文章

  1. BZOJ 1638 [Usaco2007 Mar]Cow Traffic 奶牛交通:记忆化搜索【图中边的经过次数】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1638 题意: 给你一个有向图,n个点,m条有向边. 对于所有从入度为0的点到n的路径,找出 ...

  2. bzoj 1638: [Usaco2007 Mar]Cow Traffic 奶牛交通【记忆化搜索】

    震惊!记忆化搜索忘记返回map值调了半小时! 边(u,v)的经过次数是:能到u的牛数*v到n的方案数.正反两次连边,dfs两次即可 #include<iostream> #include& ...

  3. 1638: [Usaco2007 Mar]Cow Traffic 奶牛交通

    1638: [Usaco2007 Mar]Cow Traffic 奶牛交通 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 618  Solved: 217 ...

  4. BZOJ1638: [Usaco2007 Mar]Cow Traffic 奶牛交通

    1638: [Usaco2007 Mar]Cow Traffic 奶牛交通 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 571  Solved: 199 ...

  5. 【动态规划】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> ...

  6. BZOJ 1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏( floyd )

    直接floyd.. ---------------------------------------------------------------------------- #include<c ...

  7. BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐( dfs )

    直接从每个奶牛所在的farm dfs , 然后算一下.. ----------------------------------------------------------------------- ...

  8. BZOJ 1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏

    Description Farmer John 想让她的奶牛准备郡级跳跃比赛,贝茜和她的伙伴们正在练习跨栏.她们很累,所以她们想消耗最少的能量来跨栏. 显然,对于一头奶牛跳过几个矮栏是很容易的,但是高 ...

  9. BZOJ 1641 [Usaco2007 Nov]Cow Hurdles 奶牛跨栏:新版floyd【路径上最大边最小】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1641 题意: 给你一个有向图,n个点(n <= 300),m条边,边权为h[i]. ...

随机推荐

  1. 算法笔记_128:完美洗牌算法(Java)

    目录 1 问题描述 2 解决方案 2.1位置置换算法 2.2 走环算法   1 问题描述 有一个长度为2n的数组{a1,a2,a3,...,an,b1,b2,b3,...,bn},希望排序后变成{a1 ...

  2. 偏最小二乘回归(PLSR)- 1 概览

    1. 概览 偏最小二乘算法,因其仅仅利用数据X和Y中部分信息(partial information)来建模,所以得此名字.其总体处理框架体现在下面两图中. 建议先看第2部分,对pls算法有初步了解后 ...

  3. 最全的iOS物理引擎demo

    概述 最全的iOS物理引擎demo,实现重力.碰撞.推力.摆动.碰撞+重力.重力弹跳.仿摩拜单车贴纸效果.防iMessage滚动效果.防百度外卖首页重力感应等效果! 详细 代码下载:http://ww ...

  4. PHP-php.ini中文版

    今天细看了下配置文件 有很多没用过的 就从网上搜了一篇 常看看 ;;;;;;;;;;;;;;;; 简介 ;;;;;;;;;;;;;;;;; 本文并非是对英文版 php.ini 的简单翻译,而是参考了众 ...

  5. HTML5 学习笔记 应用程序缓存

    使用html5 通过创建cache manifest文件,可以轻松地创建web应用的离线版本. html5引入了应用程序缓存,这意味着web应用可进行缓存,并可在没有因特网连接时进行访问. 应用程序缓 ...

  6. 工作总结 表单提交中 Input 设置 disabled readonly

    input框里面添加disabled属性之后,该内容就无法向上提交  需要的时候也可以再移除disabled readonly这个属性来禁止用户修改, 可以正常提交. Hiddent 隐藏  也可以正 ...

  7. C# 基础: new 和 overrider 的区别

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. 转 多线程 闭锁(Latch) 栅栏(CyclicBarrier)

    java多线程并发系列之闭锁(Latch)和栅栏(CyclicBarrier) 标签: java并发编程 2015-05-28 16:45 2939人阅读 评论(0) 收藏 举报 本文章已收录于: . ...

  9. MockServer 入门

    忽略元数据末回到原数据开始处 MockServer介绍及文档 借鉴公司的文档 http://mock-server.com github:https://github.com/jamesdbloom/ ...

  10. JEECG环境搭建(Maven版本)-总结Eclipse

    1.安装sql server 数据库: 解决办法: 控制面板→程序和功能→启用或关闭Windows功能 .Net Framework 3.5这一项未被完全选中(应为对勾,如果是方块也不可以) 选中该项 ...