UVALive 6467 Strahler Order 拓扑排序
这题是今天下午BNU SUMMER TRAINING的C题
是队友给的解题思路,用拓扑排序然后就可以了
最后是3A
其中两次RE竟然是因为:
scanf("%d",mm);
ORZ
以后能用CIN还是CIN吧 QAQ
贴代码了:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm> #define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = ;
int indgree[MAXN], map[MAXN][MAXN], ans[MAXN], cur[MAXN][MAXN];
int n;
void init(){
for(int i = ; i <= n; ++i){
for(int j = ; j <= n; ++j){
map[i][j] = i == j ? : INF;
}
}
memset(indgree, , sizeof(indgree));
memset(ans, , sizeof(ans));
memset(cur, , sizeof(cur));
} void insert_cur(int x, int num){
int pos = ;
while(){
if(cur[x][pos] == ){
cur[x][pos] = num;
break;
}
++pos;
}
} int sigma_cur(int x){
int temp_ans = ;
int Max = -INF;
for(int i = ; cur[x][i] != ; ++i){
if(cur[x][i] > Max) Max = cur[x][i];
}
int flag = ;
for(int i = ; cur[x][i] != ; ++i){
if(cur[x][i] == Max) ++flag;
}
if(flag >= ) ans[x] = ++Max;
else if(flag == ) ans[x] = Max;
return ans[x];
} void topsort(){
int i, j;
while(){
for(i = ; i <= n; ++i)
if(indgree[i] == ) break;
if(i != n + ){
for(j = ; j <= n; ++j)
if(map[i][j] == ){
map[i][j] = ;
--indgree[j];
insert_cur(j, ans[i]);
if(indgree[j] == ) ans[j] = sigma_cur(j);
}
indgree[i] = -;//pop i
}
else break;
}
} int main(){
int i, j, k, p, mm, a, b;
scanf("%d",&mm);
int numCase = ;
while(mm--){
init();
scanf("%d%d%d",&k,&n,&p);
while(p--){
scanf("%d%d",&a,&b);
++indgree[b];
map[a][b] = ;
}
for(i = ; i <= n; ++i){
if(indgree[i] == ) ans[i] = ;
}
topsort();
printf("%d %d\n",++numCase,ans[n]);
}
return ;
}
UVALive 6467 Strahler Order 拓扑排序的更多相关文章
- UVALive 6467 Strahler Order(拓扑序列)
In geology, a river system can be represented as a directed graph. Each river segment is an edge; wi ...
- UVALive 6467 Strahler Order
> 题目链接 题意:给定一个有向图,顶点代表水池,入度为零的定点代表水源,等级是1,他们延河道(有向边)冲撞,对于普通的水池来说,题目给定判断它等级的两个准则,问出度为零的那个点的等级是多少. ...
- uvalive 4255 Guess(拓扑排序)
算好题目,反正我没想到可以用图论做(虽然现在做的是图论专题= =) 首先是要把求每个位置上的值转化为求 “前缀和之差”,这是一个很有用的技巧 其次,由输入的(n+(n-1)+...+2+1)个符号,可 ...
- UVALive - 4255 - Guess (拓扑排序)
Guess 题目传送:Guess 白书例题 注意拓扑排序时,,入度同一时候为0的前缀和须要赋值为同一个数(这个数能够随机取.由于前缀和是累加的,每个a的数值都仅仅和前缀和之差有关).,由于此时能够看成 ...
- 【二分+拓扑排序】Milking Order @USACO 2018 US Open Contest, Gold/upc_exam_6348
目录 Milking Order @USACO 2018 US Open Contest, Gold/upc_exam_6348 PROBLEM 题目描述 输入 输出 样例输入 样例输出 提示 MEA ...
- UVALive 6264 Conservation --拓扑排序
题意:一个展览有n个步骤,告诉你每一步在那个场馆举行,总共2个场馆,跨越场馆需要1单位时间,先给你一些约束关系,比如步骤a要在b前执行,问最少的转移时间是多少. 解法:根据这些约束关系可以建立有向边, ...
- 【拓扑排序或差分约束】Guess UVALive - 4255
题目链接:https://cn.vjudge.net/contest/209473#problem/B 题目大意:对于n个数字,给出sum[j]-sum[i](sum表示前缀和)的符号(正负零),求一 ...
- 拓扑排序(Topological Order)UVa10305 Ordering Tasks
2016/5/19 17:39:07 拓扑排序,是对有向无环图(Directed Acylic Graph , DAG )进行的一种操作,这种操作是将DAG中的所有顶点排成一个线性序列,使得图中的任意 ...
- D - Guess UVALive - 4255 拓扑排序
Given a sequence of integers, a1, a2, . . . , an, we define its sign matrix S such that, for 1 ≤ i ≤ ...
随机推荐
- PHP mysqli类
<?php header("content-type:text/html;charset=utf-8"); $conn = new mysqli("localhos ...
- java时间验证工具
可以验证2014-02-21这种错误
- 操作 IoT 设备内嵌 SQLite
Win10 IoT C#开发 5 - 操作 IoT 设备内嵌 SQLite 数据库 CURD Windows 10 IoT Core 是微软针对物联网市场的一个重要产品,与以往的Windows版本 ...
- 细说SSO单点登录(转)
什么是SSO? 如果你已知道,请略过本节! SSO核心意义就一句话:一处登录,处处登录:一处注销,处处注销.即:在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统. 很多人容易把SS ...
- C++头文件的包含顺序研究
一.<Google C++ 编程风格指南>里的观点 公司在推行编码规范,领导提议基本上使用<Google C++ 编程风格指南>.其中<Google C++ 编程风格指南 ...
- 具体解释coredump
一,什么是coredump 我们常常听到大家说到程序core掉了,须要定位解决,这里说的大部分是指相应程序因为各种异常或者bug导致在执行过程中异常退出或者中止,而且在满足一定条件下(这里为什么说须要 ...
- JavaSE学习总结第17天_集合框架3
17.01 ArrayList集合的toString()方法源码解析 代码: Collection c = new ArrayList(); c.add("hello"); c ...
- poj2774之最长公共子串
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 18794 Accepted: 77 ...
- mongoDB初接触
首先去官网http://www.mongodb.org/ 下载mongoDB 下载后解压D:\mongodb 创建两个新文件夹data以及log 新建一个文件命名为mongodb.cfg dbpath ...
- 关于64位Windows7系统下INF的安装问题
原文 http://bbs.csdn.net/topics/360262492 我的电脑 ——>属性 ——>设备管理器 ——>操作 ——>添加过时硬件 但是,64位系统上报“指 ...