UVALive 6467 Strahler Order(拓扑序列)
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(拓扑序列)的更多相关文章
- UVALive 6467 Strahler Order 拓扑排序
这题是今天下午BNU SUMMER TRAINING的C题 是队友给的解题思路,用拓扑排序然后就可以了 最后是3A 其中两次RE竟然是因为: scanf("%d",mm); ORZ ...
- UVALive 6467 Strahler Order
> 题目链接 题意:给定一个有向图,顶点代表水池,入度为零的定点代表水源,等级是1,他们延河道(有向边)冲撞,对于普通的水池来说,题目给定判断它等级的两个准则,问出度为零的那个点的等级是多少. ...
- 图结构练习——判断给定图是否存在合法拓扑序列(dfs算法(第一个代码),邻接矩阵(前两个代码),邻接表(第三个代码))
sdut 2140 图结构练习——判断给定图是否存在合法拓扑序列 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 给定一个有向图 ...
- SDUT OJ 数据结构实验之图论十:判断给定图是否存在合法拓扑序列
数据结构实验之图论十:判断给定图是否存在合法拓扑序列 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Prob ...
- Southern African 2001 框架折叠 (拓扑序列的应用)
本文链接:http://www.cnblogs.com/Ash-ly/p/5398377.html 题目:考虑五个图片堆叠在一起,比如下面的9 * 8 的矩阵表示的是这些图片的边缘框. 现在上面的图片 ...
- SDUT2140图结构练习——判断给定图是否存在合法拓扑序列
拓扑序列的判断方法为不存在有向环,代码实现的话有两种,一种是直接去判断是否存在环,较为难理解一些,另一种的话去判断结点入度,如果存在的入度为0的点大于一个,则该有向图肯定不存在一个确定的拓扑序列 #i ...
- acwing 848 有向图的拓扑序列
地址 https://www.acwing.com/problem/content/description/850/ 题目描述给定一个n个点m条边的有向图,图中可能存在重边和自环. 请输出任意一个该有 ...
- SDUT-2140_判断给定图是否存在合法拓扑序列
数据结构实验之图论十:判断给定图是否存在合法拓扑序列 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给定一个有向图,判 ...
- PAT-1146(Topological Order)拓扑排序+判断一个序列是否满足拓扑序列
Topological Order PAT-1146 #include<iostream> #include<cstring> #include<string> # ...
随机推荐
- IntelliJ IDEA使用技巧整理
1 .写代码时用 Alt-Insert ( Code|Generate… )可以创建类里面任何字段的 getter 与 setter 方法. 2 .右键点击断点标记(在文本的左边栏里)激活速查菜单,你 ...
- bat设置windows计划任务
设置定时任务 @echo off set NAME=dailybackup :: set DAY=MON,TUE,WED,THU,FRI,SAT,SUN set COMMAND=cscript.exe ...
- C库函数重定向问题
C库函数重定向用户能定义自己的C语言库函数,连接器在连接时自动使用这些新的功能函数.这个过程叫做重定向C语言库函数,如下图所示.举例来说,用户有一个I/O设备(如UART).本来库函数fputc()是 ...
- jQuery----(类似抽奖转盘)高亮显示
效果如图: 原图 鼠标进入后开始变化图 实现需 ...
- hadoop体系架构
1.1 Hadoop 概念:hadoop是一个由Apache基金会所开发的分布式系统基础架构.是根据google发表的GFS(Google File System)论文产生过来的. ...
- python 3.x 实现简单用户登录
import os import sys import getpass login_username = 'admin' login_password = ' u = 0 while u < 3 ...
- c语言指针篇
不保证绝对正确,学习中所记录的一些笔记罢了,可能随着经历的增多,逐渐的完善, 不要完全相信我所记录的内容,可能因为编译协议版本的不同出现不同的结果. 也可能我写的根本就是错的,请相信自己动手得出来的结 ...
- d3 js emberjs handlerbarjs
http://handlebarsjs.com/ http://emberjs.com/ http://jsbin.com/d3ember-barchart/13/edit?html,output
- WPF 自定义ComboBox样式,自定义多选控件
原文:WPF 自定义ComboBox样式,自定义多选控件 一.ComboBox基本样式 ComboBox有两种状态,可编辑和不可编辑状态.通过设置IsEditable属性可以切换控件状态. 先看基本样 ...
- 矩阵类的python实现
科学计算离不开矩阵的运算.当然,python已经有非常好的现成的库:numpy. 我写这个矩阵类,并不是打算重新造一个轮子,只是作为一个练习,记录在此. 注:这个类的函数还没全部实现,慢慢在完善吧. ...