POJ 3272 cow traffic

题目:
Cow Traffic
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 3117 Accepted: 833
Description


The bovine population boom down on the farm has caused serious congestion on the cow trails leading to the barn. Farmer John has decided to conduct a study to find the bottlenecks in order to relieve the 'traffic jams' at milking time.


The pasture contains a network of M (1 ≤ M ≤ 50,000) one-way trails, each of which connects exactly two different intersections from the set of N (1 ≤ N ≤ 5,000) intersections conveniently numbered 1..N; the barn is at intersection number N. Each trail connects one intersection point to another intersection point with a higher number. As a result, there are no cycles and, as they say on the farm, all trails lead to the barn. A pair of intersection points might be connected by more than one trail.


During milking time rush hour, the cows start from their respective grazing locations and head to the barn. The grazing locations are exactly those intersection points with no trails connecting into them. Each cow traverses a 'path', which is defined as a sequence of trails from a grazing location to the barn.


Help FJ finding the busiest trail(s) by computing the largest number of possible paths that contain any one trail. The answer is guaranteed to fit in a signed 32-bit integer.


Input


Line 1: Two space-separated integers: N and M. 
Lines 2..M+1: Two space-separated intersection points.
Output


Line 1: The maximum number of paths passing through any one trail.
Sample Input


7 7
1 3
3 4
3 5
4 6
2 3
5 6
6 7
Sample Output


4
Hint


Here are the four possible paths that lead to the barn: 
1 3 4 6 7 
1 3 5 6 7 
2 3 4 6 7 
2 3 5 6 7
Source


USACO 2007 March Silver








题意:
求从每个入度为零的点走到唯一的一个出度为零的点的所有走法中,经过次数最多的一条边被经过的次数,输入为点数和边数


进行正反两次拓扑排序,标记出每个节点作为弧尾和在反向图中作为弧尾时各自的入度。
每条弧的正向弧头的入读乘以反向弧头的反向入度,就是该边被经过的次数


代码:

Source Code

Problem: 3272  User: cxqphysics 
Memory: 1624K  Time: 16MS 
Language: G++  Result: Accepted 

Source Code 
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;

int next[50005];
int p[50005];
int v[5005];
int d[5005];
int w[5005];

int next1[50005];
int p1[50005];
int v1[5005];
int d1[5005];
int w1[5005];

int main()
{
	int n,m;
	while(~scanf("%d%d",&n,&m))
	{
		queue<int>q;
		queue<int>q1;
		memset(next,-1,sizeof(next));
		memset(p,-1,sizeof(p));
		memset(v,-1,sizeof(v));
		memset(d,0,sizeof(d));
		memset(w,0,sizeof(w));

		memset(next1,-1,sizeof(next1));
		memset(p1,-1,sizeof(p1));
		memset(v1,-1,sizeof(v1));
		memset(d1,0,sizeof(d1));
		memset(w1,0,sizeof(w1));

		int res=1;
		for(int i=1;i<=m;i++)
		{
			int a,b;
			scanf("%d%d",&a,&b);
			next[i]=v[a];
			p[i]=b;
			v[a]=i;
			d[b]++;

			next1[i]=v1[b];
			p1[i]=a;
			v1[b]=i;
			d1[a]++;
		}

		for(int i=1;i<=n;i++)
		{
			if(d[i]==0)
			{
				w[i]=1;
				q.push(i);
			}
			if(d1[i]==0)
			{
				w1[i]=1;
				q1.push(i);
			}
		}
		/*
		for(int i=1;i<=n;i++){
			printf("%d ",w[i]);
		}printf("\n");
		for(int i=1;i<=n;i++)
		{
			printf("%d ",w1[i]);
		}printf("\n");
		*/
		while(!q.empty())
		{
			int x=q.front();
			q.pop();
			for(int e=v[x];e!=-1;e=next[e])
			{
				w[p[e]]+=w[x];
				d[p[e]]--;
				if(d[p[e]]==0)
				{
					q.push(p[e]);
				}
			}
		}
		while(!q1.empty())
		{
			int x=q1.front();
			q1.pop();
			for(int e=v1[x];e!=-1;e=next1[e])
			{
				w1[p1[e]]+=w1[x];
				d1[p1[e]]--;
				if(d1[p1[e]]==0)
				{
					q1.push(p1[e]);
				}
			}
		}
		/*
		for(int i=1;i<=n;i++){
			printf("%d ",w[i]);
		}printf("\n");
		for(int i=1;i<=n;i++)
		{
			printf("%d ",w1[i]);
		}printf("\n");
		*/
		for(int i=1;i<=m;i++)
		{
			int rt=w[p1[i]]*w1[p[i]];
			if(rt>res)
			{
				res=rt;
			}
		}
		printf("%d\n",res);
	}
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值