题目链接:http://poj.org/problem?id=3272

题目意思:n个点m条边的有向图,从所有入度为0的点出发到达n,问所有可能路径中,经过的某条路的最大次数是多少。边全是由标号小的到标号大的。

这道题求的是路径的最大通过数量,感觉比较典型,挺有意思的。

思路:建两个图一个正图,一个反图。将正图dfs一遍得到每个点到达n点的路径数,将反图dfs一遍得到每个点到达每个出度为0的点(奶牛的放牧地)的路径数,对于每条边正图上的终点路径数*反图上起点的路径数的最大值就是答案。

代码:

 //Author: xiaowuga
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <ctime>
#include <map>
#include <bitset>
#include <cctype>
#define maxx INT_MAX
#define minn INT_MIN
#define inf 0x3f3f3f3f
#define mem(s,ch) memset(s,ch,sizeof(s))
#define nc cout<<"nc"<<endl
#define sp " "
const long long N=*+;
using namespace std;
typedef long long LL;
typedef int II;
struct eage{
LL x,y;
}E[N];
vector<LL>p[+];
vector<LL>q[+];
LL n,m;
LL ansp[+];
LL ansq[+];
LL in[+];
LL vis[+];
void init(){
for(II i=;i<=n;i++){
p[i].clear();q[i].clear();
in[i]=vis[i]=ansq[i]=ansp[i]=;
}
}
LL dfsp(LL x){//正图
if(vis[x]) return ansp[x];
vis[x]=;
if(x==n) return ansp[x]=;
for(LL i=;i<p[x].size();i++){
LL t=p[x][i];
ansp[x]+=dfsp(t);
}
return ansp[x];
}
LL dfsq(LL x){//反图
if(vis[x]) return ansq[x];
vis[x]=;
if(!in[x]) return ansq[x]=;
for(LL i=;i<q[x].size();i++){
LL t=q[x][i];
ansq[x]+=dfsq(t);
}
return ansq[x];
}
void solve(){
for(LL i=;i<=n;i++){
if(!in[i]) dfsp(i);
}
mem(vis,);
dfsq(n);
LL ans=minn;
for(LL i=;i<m;i++){
LL x=E[i].x;
LL y=E[i].y;
ans=max(ans,ansp[y]*ansq[x]);
}
cout<<ans<<endl;
}
int main() {
ios::sync_with_stdio(false);cin.tie();
while(cin>>n>>m){
init();
for(II i=;i<m;i++){
cin>>E[i].x>>E[i].y;
p[E[i].x].push_back(E[i].y);//正图
q[E[i].y].push_back(E[i].x);//反图
in[E[i].y]++;
}
solve();
}
return ;
}

POJ3272 Cow Traffic的更多相关文章

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

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

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

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

  3. bzoj1638 / P2883 [USACO07MAR]牛交通Cow Traffic

    P2883 [USACO07MAR]牛交通Cow Traffic 对于每一条边$(u,v)$ 设入度为0的点到$u$有$f[u]$种走法 点$n$到$v$(通过反向边)有$f2[v]$种走法 显然经过 ...

  4. 拓扑排序/DP【洛谷P2883】 [USACO07MAR]牛交通Cow Traffic

    P2883 [USACO07MAR]牛交通Cow Traffic 随着牛的数量增加,农场的道路的拥挤现象十分严重,特别是在每天晚上的挤奶时间.为了解决这个问题,FJ决定研究这个问题,以能找到导致拥堵现 ...

  5. 【BZOJ】1638: [Usaco2007 Mar]Cow Traffic 奶牛交通(dfs+dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1638 一条边(u, v)经过的数量=度0到u的数量×v到n的数量 两次记忆化dfs算出他们即可 #i ...

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

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

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

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

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

  9. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

随机推荐

  1. UVA 146 ID Codes(下一个排列)

    C - ID Codes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Statu ...

  2. MySQL Fabric部署

    架构描写叙述: 一台主机上安装4个MySQL 服务,当中一个MySQL服务用于存储MySQL Fabric后台数据:另外3个MySQL服务用于主从架构測试.一个主+两个从. 第一部分:二进制方式安装M ...

  3. 关于linux下配置python3的virtualenvwrapper

    python版本管理&virtualenv管理 virtualenvwrapper是用来管理virtualenv的扩展包,用着很方便. 注意,在python3中,pip的安装:sudo apt ...

  4. apue编程之参考du代码利用递归写的一个简单的du命令的源代码

    #include <stdio.h> #include <stdlib.h> #include <glob.h> #include <string.h> ...

  5. pip install read time-out

    Problem ReadTimeoutError: HTTPSConnectionPool(host='pypi.python.org', port=443): Read timed out. 1 S ...

  6. C++-教程2-VS2010C++相关文件说明

    stdafx.h说明:stdafx的英文全称为:Standard Application Framework Extensions(标准应用程序框架的扩展).所谓头文件预编译,就是把一个工程(Proj ...

  7. CR, LF, CR/LF区别与关系

    前言 在文本处理中,CR(Carriage Return),LF(Line Feed),CR/LF是不同操作系统上使用的换行符,具体如下: Dos和Windows采用回车+换行CR/LF表示下一行 而 ...

  8. jQuery 中 attr() 和 prop() 方法的区别<转>

    前几天,有人给 Multiple Select 插件 提了问题: setSelects doesn't work in Firefox when using jquery 1.9.0 一直都在用 jQ ...

  9. Linux系统查看公网IP地址

    curl members.3322.org/dyndns/getip

  10. php5共存php7

    PHP7与PHP5共存于CentOS7 原文参考 原理 思路很简单:PHP5是通过yum安装的在/usr/,套接字在/var/run/php-fpm.socket,PHP7自己编译装在/usr/loc ...