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.


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 ;
}

UVALive 6467 Strahler Order(拓扑序列)的更多相关文章

  1. UVALive 6467 Strahler Order 拓扑排序

    这题是今天下午BNU SUMMER TRAINING的C题 是队友给的解题思路,用拓扑排序然后就可以了 最后是3A 其中两次RE竟然是因为: scanf("%d",mm); ORZ ...

  2. UVALive 6467 Strahler Order

    > 题目链接 题意:给定一个有向图,顶点代表水池,入度为零的定点代表水源,等级是1,他们延河道(有向边)冲撞,对于普通的水池来说,题目给定判断它等级的两个准则,问出度为零的那个点的等级是多少. ...

  3. 图结构练习——判断给定图是否存在合法拓扑序列(dfs算法(第一个代码),邻接矩阵(前两个代码),邻接表(第三个代码))

    sdut 2140 图结构练习——判断给定图是否存在合法拓扑序列 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述  给定一个有向图 ...

  4. SDUT OJ 数据结构实验之图论十:判断给定图是否存在合法拓扑序列

    数据结构实验之图论十:判断给定图是否存在合法拓扑序列 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Prob ...

  5. Southern African 2001 框架折叠 (拓扑序列的应用)

    本文链接:http://www.cnblogs.com/Ash-ly/p/5398377.html 题目:考虑五个图片堆叠在一起,比如下面的9 * 8 的矩阵表示的是这些图片的边缘框. 现在上面的图片 ...

  6. SDUT2140图结构练习——判断给定图是否存在合法拓扑序列

    拓扑序列的判断方法为不存在有向环,代码实现的话有两种,一种是直接去判断是否存在环,较为难理解一些,另一种的话去判断结点入度,如果存在的入度为0的点大于一个,则该有向图肯定不存在一个确定的拓扑序列 #i ...

  7. acwing 848 有向图的拓扑序列

    地址 https://www.acwing.com/problem/content/description/850/ 题目描述给定一个n个点m条边的有向图,图中可能存在重边和自环. 请输出任意一个该有 ...

  8. SDUT-2140_判断给定图是否存在合法拓扑序列

    数据结构实验之图论十:判断给定图是否存在合法拓扑序列 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给定一个有向图,判 ...

  9. PAT-1146(Topological Order)拓扑排序+判断一个序列是否满足拓扑序列

    Topological Order PAT-1146 #include<iostream> #include<cstring> #include<string> # ...

随机推荐

  1. 【转】对H264进行RTP封包原理

    1. 引言     H.264/AVC 是ITU-T 视频编码专家组(VCEG)和ISO/IEC 动态图像专家组(MPEG )联合组成的联合视频组(JVT)共同努力制订的新一代视频编码标准,它最大的优 ...

  2. P1169 [ZJOI2007]棋盘制作

    题目描述 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑白相间的方阵,对应八八六十四卦,黑白对应阴阳. 而我们的 ...

  3. 史上最详细的codeblocks安装教程!!!

    codeblocks下载地址:https://www.cnblogs.com/yinbiao/p/8489748.html 下面是codeblocks具体的安装教程:

  4. Bootloader的结构和启动过程

    CPU上电后,会在某个地址开始执行,比如MIPS结构的CPU会从0xBFC00000取第一条指令,而ARM结构的CPU则从0x00000000开始,嵌入式开发板中,需要把存储器件ROM或Flash等映 ...

  5. where语句中不能直接使用聚合函数

    1.问题描述 select deptno ,avg(sal) from emp where count(*)>3 group by deptno; 在where 句中使用聚合函数count(*) ...

  6. 不安分的android开发者(小程序初尝试,前后台都自己做)

    前言 作为一个稍微有点想法的程序员来说,拥有一个自己开发,自己运营,完全属于自己的应用,应该是很多人的梦想.刚毕业那会,自己的工作是做游戏,于是也和朋友业余时间开发一些小游戏玩玩,可是终究不成气候,而 ...

  7. json keyname map

    var obj = { fname:'zhao', lname:'yao', parents:{ father:'zhao' }, children:[ { dname:'zhaoyiyi' } ] ...

  8. spring-data-jpa快速入门(一)——整合阿里Druid

    一.概述 官网:https://projects.spring.io/spring-data-jpa/ 1.什么是spring-data-jpa Spring Data JPA, part of th ...

  9. c++ 标准流文件

    一.标准流stdin,stdout,stderr   标准输入流stdin: 是程序可以读取其输入的位置.缺省情况下,进程从键盘读取 stdin . fscanf(stdin,"%d%d%f ...

  10. Codeforces 1155 D Beautiful Array DP,最大子段和

    题意 给出一个长度为\(n\)的数列和数字\(x\),经过最多一次操作将数列的一个子段的每个元素变为\(a[i]*x\),使该数列的最大子段和最大 分析 将这个数列分为3段考虑,第一段和第三段是未修改 ...