UVALive 6467 Strahler Order(拓扑序列)

时间:2021-06-12 17:34:58

In geology, a river system can be represented as a directed graph. Each river segment is an edge; with the edge pointing the same way the water flows. Nodes are either the source of a river segment (for example, a lake or spring), where river segments merge or diverge, or the mouth of the river.

Note: The number in a box is the order. The number in a circle is the node number.
The Strahler order of a river system is computed as follows.
• The order of each source node is 1.
• For every other node, let i be the highest order of all its upstream nodes. If just one upstream node has order i, then this node also has order i.

If two or more upstream nodes have order i, then this node has order i + 1.

UVALive 6467 Strahler Order(拓扑序列)
The order of the entire river system is the order of the mouth node. In this problem, the river system will have just one mouth. In the picture above, the Strahler order is three (3). You must write a program to determine the order of a given river system.
The actual river with the highest order is the Amazon, with order 12. The highest in the U.S. is the Mississippi, with order 10.Node M is the mouth of the river. It has no outgoing edges.

Input
The first line of input contains a single integer K, (1 ≤ K ≤ 1000), which is the number of data sets that follow.

Each data set should be processed identically and independently.Each data set consists of multiple lines of input. The first line of each data set contains three
positive integers, K, M and P (2 ≤ m ≤ 1000). K is the data set number. M is the number of nodes in the graph and P is the number of edges. The first line is followed by P lines, each describing an edge of the graph. The line will contain two positive integers, A and B, indicating that water flows from
node A to node B (1 ≤ A; B ≤ M). Node M is the mouth of the river. It has no outgoing edges.

Output

For each data set there is a single line of output. The line consists of the data set number, a single space and the order of the river system.

Sample Input

1

1 7 8

1 3

2 3

6 4

3 4

3 5

6 7

5 7

4 7

Sample Output

1 3

题目意思:这里给了一个地理上水文体系的一个实例,将河流看做一条线段,沿着箭头方向流,对于每个交点都会有一个等级序列,那些河流的源点等级序列是1,如果一个交点只有一条河流指向,那么它标为所指的那个点的等级。如果一个点有两个或以上相同等级的点指向。那么给它标为最大相同等级+1,如果有更大的话就标为更大的。求这个河流体系中最大的等级序列。

解题思路:其实这道题有点游戏中给武器合成加等级的意思,比如想要一把+5的宝刀,需要至少两把+4的才能合成;如果你有+3的,+4的,是合不出+5的武器的,只会是+4的武器。

该题所用到的算法是拓扑排序,就像剥洋葱,按照入度BFS将外围的点逐个去掉,同时记录等级序列。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define MAX 1010
using namespace std;
int maps[MAX][MAX];
int h[MAX];
queue<int>q;
int toposort(int n)
{
int i;
int s,mins;
int du[MAX];
int vis[MAX];
memset(du,,sizeof(du));
memset(vis,,sizeof(vis));
for(i=; i<=n; i++)
{
if(h[i]==)
{
du[i]=;
q.push(i);///现将源头点排到队列中
}
}
while(!q.empty())///BFS
{
s=q.front();
q.pop();
if(vis[s])
{
vis[s]=;
du[s]++;
}
for(i=; i<=n; i++)
{
if(maps[s][i])
{
if(du[s]==du[i])
{
vis[i]=;
}//刚开始为0如果相等代表之前连接过一个标号为du[s]的或者>=2个du[s]-1的
if(du[s]>du[i])
{
vis[i]=;
du[i]=du[s];
}
h[i]--;///拓扑排序,出点
if(h[i]==)
{
q.push(i);
}
}
}
}
mins=-;
for(i=; i<=n; i++)///找最大的点
{
if(du[i]>mins)
{
mins=du[i];
}
}
return mins;
}
int main()
{
int t;
int i,k,n,m,ans;
int u,v;
while(scanf("%d",&t)!=EOF)
{
while(t--)
{
while(!q.empty())
{
q.pop();
}
memset(maps,,sizeof(maps));
memset(h,,sizeof(h));
scanf("%d%d%d",&k,&n,&m);
for(i=; i<=m; i++)
{
scanf("%d%d",&u,&v);
maps[u][v]=;
h[v]++;///记录改点汇入支流的个数
}
ans=toposort(n);
printf("%d %d\n",k,ans);
}
}
return ;
}