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. python3 unittest框架失败重跑加截图支持python2,python3

    github源码地址下载:https://github.com/GoverSky/HTMLTestRunner_cn.git 解压文件后取出/HTMLTestRunner_cn.py文件丢进C:\Py ...

  2. Spring源码分析(六)解析和注册BeanDefinitions

    摘要:本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 当把文件转换为Document后,接下来的提取及注册bean就是我们的重 ...

  3. STM32中用 stop 模式 配合低功耗模式下的自动唤醒(AWU) 能否实现FreeRTOS tickless 模式

    已经实现  ,2018年11月17日11:56:42,具体 如下: 第一步 : 修改 void vPortSetupTimerInterrupt( void ) 函数 ,修改原来的 systick 定 ...

  4. Putty等工具中解决SSH连接超时断开的问题

    转自:http://www.putty.ws/putty-chaoshi 1 在 linux下的ssh命令:vim /etc/ssh/ssh_config 然后找到里面的ServerAliveInte ...

  5. Python的 GUI 框架

    Python的 GUI 框架 Tkinter Python内嵌的gui环境,使用TCL实现,python IDLE由Tkinter实现 历史悠久,perl中有对应的perlTk.Python标准安装包 ...

  6. 【LeetCode3】Longest Substring Without Repeating Characters★★

    题目描述: 解题思路: 借用网上大神的思想:the basic idea is, keep a hashmap which stores the characters in string as key ...

  7. C语言中数组定义方式

    <1>前言 大家首先来思考一个问题,若是我们想要定义两个变量,求这两个数的平均数,该怎么求呢? 例如:int a = 10,b = 20 int average = (a + b) / 2 ...

  8. linux 监控文件变化

    介绍 有时候我们常需要当文件变化的时候便触发某些脚本操作,比如说有文件更新了就同步文件到远程机器.在实现这个操作上,主要用到两个工具,一个是rsync,一个是inotifywait .inotifyw ...

  9. 2、rbac组件 后台布局模板,权限按钮,菜单,可拔插,路径重定向

    1.后台布局管理 https://www.cnblogs.com/venicid/p/7772742.html#_label0 1.通用模板 overflow: auto; //在a和b模板中进行切换 ...

  10. Gitlab+Jenkins学习之路(一)之Git基础

    1.GIT基础    GIT是一个分布式版本管理系统,速度快,适合大规模,跨地区多人协同开.SVN是一个集中式版本管理系统. (1)GIT生态 GIT分布式版本管理系统 Gitlab git私库解决方 ...