poj3272_toposort

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

7 7
1 3
3 4
3 5
4 6
2 3
5 6
6 7
ans:4
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
6-->7 maxnum=4

分析:设start_routes[i] 为入度为0的源点到节点i的路径条数,    end_routes[i] 为节点i到汇点N的路径条数。

首先通过toposort求出上述两个变量,然后枚举每条边(s为始点,t为终点),所有边的f[s]*g[t]的最大值即为答案。

注意:

1,这个题有重边,但是重边不能去,每条边都要加到对应的路径中。

2,还有一个奇怪的事情,一开始MLE,然后对比大牛的代码,开始改。将栈改成数组模拟也不行,最后我把主函数中的一堆memset函数删除了,虽然我知道这些memset函数多此一举。但是我觉得这不应该是MLE的原因吧。但是删了之后就过了,过了!求解释!!!

3.这个题的内存要求开二维数组肯定超了,但是这里过了,说明测试数据较小。

代码:

View Code
  1 #include <iostream>
  2 #include <stdio.h>
  3 #include <string.h>
  4 #include <stack>
  5 using namespace std;
  6 //8164K 32MS
  7 const int maxnum=5001;
  8 int graph[maxnum][maxnum];
  9 int indegree[maxnum],outdegree[maxnum];
 10 int start_routes[maxnum],end_routes[maxnum];
 11 int n,m;
 12 stack<int> s;
 13 
 14 void toposort()
 15 {
 16     int i;
 17     for(i=1;i<=n;i++)
 18         if(!indegree[i])
 19         {
 20             start_routes[i]=1;
 21             s.push(i);
 22         }
 23 
 24     int t;
 25     while(!s.empty())
 26     {
 27         t=s.top();
 28         s.pop();
 29         for(i=1;i<=n;i++)
 30             if(graph[t][i])
 31             {
 32                 start_routes[i]+=start_routes[t]*graph[t][i];
 33                 indegree[i]-=graph[t][i];
 34                 if(indegree[i]==0)
 35                     s.push(i);
 36             }
 37     }
 38 }
 39 
 40 void Toposort()
 41 {
 42     end_routes[n]=1;
 43     s.push(n);
 44     int t,i;
 45     while(!s.empty())
 46     {
 47         t=s.top();
 48         s.pop();
 49         for(i=1;i<=n;i++)
 50             if(graph[i][t])
 51             {
 52                 end_routes[i]+=end_routes[t]*graph[i][t];
 53                 outdegree[i]-=graph[i][t];
 54                 if(outdegree[i]==0)
 55                     s.push(i);
 56             }
 57     }
 58 }
 59 
 60 int main()
 61 {
 62     scanf("%d%d",&n,&m);
 63 //    memset(graph,0,sizeof(graph));
 64 //    memset(indegree,0,sizeof(indegree));
 65 //    memset(outdegree,0,sizeof(outdegree));
 66 //    memset(start_routes,0,sizeof(start_routes));
 67 //    memset(end_routes,0,sizeof(end_routes));
 68     int i,j,u,v;
 69     for(i=0;i<m;i++)
 70     {
 71         scanf("%d%d",&u,&v);
 72         graph[u][v]++;
 73         indegree[v]++;
 74         outdegree[u]++;
 75     }
 76     toposort();
 77     Toposort();
 78     int ans=0,temp;
 79     for(i=1;i<=n;i++)
 80         for(j=1;j<=n;j++)
 81             if(graph[i][j])
 82             {
 83                 temp=start_routes[i]*end_routes[j];
 84                 if(temp>ans)
 85                     ans=temp;
 86             }
 87     printf("%d\n",ans);
 88     return 0;
 89 }
 90 /*
 91 7 7
 92 1 3
 93 3 4
 94 3 5
 95 4 6
 96 2 3
 97 5 6
 98 6 7
 99 ans:4
100 Here are the four possible paths that lead to the barn:
101 1 3 4 6 7
102 1 3 5 6 7
103 2 3 4 6 7
104 2 3 5 6 7
105 6-->7 maxnum=4
106 */

tju oj2787

 

posted @ 2012-08-23 19:05  pushing my way  阅读(288)  评论(0编辑  收藏  举报