【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]. ...
随机推荐
- Project Euler:Problem 76 Counting summations
It is possible to write five as a sum in exactly six different ways: 4 + 1 3 + 2 3 + 1 + 1 2 + 2 + 1 ...
- 常见URL字符及URL编码值
大家上网的时候一定会看到很多这类情况有的网页地址都是%22%32%11%23%21等这种机器语言恐怕只有机器能马上辨认吧现在我把大概知道的总结一下 字符 ...
- django admin 如何去掉s 如何去掉django admin 各个模块后面的s
其中加上红色标记的内容,业务管理员后面就不会有 s 了 class UsrMngUser(models.Model): user_name = models.CharField("用户名称& ...
- Mysql 日期时间类型详解
MySQL 中有多种数据类型可以用于日期和时间的表示,不同的版本可能有所差异,表3-2 中列出了MySQL 5.0 中所支持的日期和时间类型. 这些数据类型的主要区别如下: * 如果要用来表示年月日 ...
- obj 格式注意事项
用Adreno Profiler分析图形效果的实现过程时,需要将特效涉及到的模型导出,以便进行多角度的详细查看,结果发现Adreno Profiler导出模型的功能有bug,总是报错并生成一个残缺的. ...
- min宏的学习
1.先上实现代码: #define __min(t1, t2, min1, min2, x, y) ({ \ t1 min1 = (x); \ t2 min2 = (y); \ (void) (&am ...
- linux 清内存
注意:首先我们需要使用sync指令,将所有未写的系统缓冲区写到磁盘中,包含已修改的 i-node.已延迟的块 I/O 和读写映射文件.否则在释放缓存的过程中,可能会丢失未保存的文件. 的值可以为0~3 ...
- 【C语言】23-typedef
一.typedef作用简介 * 我们可以使用typedef关键字为各种数据类型定义一个新名字(别名). 1 #include <stdio.h> 2 3 typedef int Integ ...
- 06、Windows 10 技术预览
随着 Windows 10 发布的,未来 Windows 平台都是统一开发模型,可以只写一个 Appx 包,就可以同时部署到 Windows/ Windowsw Phone/ Tablet /xbox ...
- python-爬图小样
python-爬某页面图 注意:python3+版本与python2有一定区别,需要注意多点. #! /usr/bin/env python3.5.4 # coding=utf-8 # 爬百度某贴吧页 ...