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> # ...
随机推荐
- 【转】对H264进行RTP封包原理
1. 引言 H.264/AVC 是ITU-T 视频编码专家组(VCEG)和ISO/IEC 动态图像专家组(MPEG )联合组成的联合视频组(JVT)共同努力制订的新一代视频编码标准,它最大的优 ...
- http 的request和response 在servlet的应用文件下载
一)response 我们通过浏览器访问网站的时候,处理响应的是response. 它由三部门组成:响应行.响应头.响应体 作用:往浏览器写东西. 1)响应行 格式:协议/版本 状态码 状态码说明. ...
- 动态验证码处理UI自动化获取处理
解决思路两种:1.直接linux catalina.out读取:一分每10s轮询出现新的直接读取返回<br>2.缓存注入cookie,先手动登录获取session 然后通过注入user,p ...
- 《You dont know JS》强制类型转换
强制类型转换 将值从一种类型转换为另一种类型通常称为类型转换,这是显式的情况.隐式的情况被称为强制类型转换 在书中,作者还提出一种区分方式: 类型转换发生在静态类型语言的编译阶段,强制类型转换发生在动 ...
- Property Injection in Asp.Net Core (转载)
问: I am trying to port an asp.net application to asp.net core. I have property injection (using ninj ...
- helpera64开发板下制作ubuntu rootfs镜像
下一篇路径:https://www.cnblogs.com/jizizh/p/10499448.html 环境: HelperA64开发板 Linux3.10内核 时间:2019.02.14 目标:定 ...
- Telnet模拟系统(Linux c)
第3章详细设计和实现 3.1相关技术 1)TCP编程,主要包括socket()函数.bind()函数.listen()函数.recv()函数.send()函数以及客户端的connect()函数. 2) ...
- VIM - tab 相关的简单配置
1. 概述 vim 是强大的文本编辑器 使用 vim 前, 需要做些简单配置, 来让 vim 更符合自己的操作习惯 想看配置项的, 直接到最后 2. 准备 安装 vim 略 配置文件 位置 /etc/ ...
- Egret 菜鸟级使用手册
首先,先安装好,然后,创建项目,弄好之后,在终端输入 egret run -a 开启服务 /*********************************华丽丽的分割线************** ...
- apple remote desktop
下面就是重点了: 1.下载v 3.7.2版本 :http://pan.baidu.com/s/1jGmrhuI 密码:vg2r 序列号: XARD-030-000-N-LXC-RP7-FBX-23 ...