UVA - 1423 Guess (拓扑排序)
题意:已知矩阵S,求序列a。已知矩阵Sij = “ + ” if ai + . . . + aj > 0; Sij = “ − ” if ai + . . . + aj < 0; and Sij = “0” otherwise.
分析:
1、由Sij = ‘+’ 可知,ai + . . . + aj > 0,即sum[j] - sum[i - 1] > 0,即sum[j] > sum[i - 1],即j优先级比i - 1高,由j向i - 1连一条有向边,i - 1入度+1。
2、按优先级由高到低,依次从10递减赋值,可知各前缀和的大小,由此得出序列a。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 10 + 10;
const int MAXT = 100 + 10;
using namespace std;
char s[MAXT];
int indegree[MAXN];
int pic[MAXN][MAXN];
int sum[MAXN];
bool vis[MAXN];
int ans[MAXN];
int n;
void topsort(){
int num = 10, cnt = 0;
while(cnt < n + 1){
memset(vis, false, sizeof vis);
for(int i = 0; i <= n; ++i){
if(!indegree[i]){
sum[i] = num;
--indegree[i];
vis[i] = true;
++cnt;
}
}
for(int i = 0; i <= n; ++i){
if(vis[i]){
for(int j = 0; j <= n; ++j){
if(pic[i][j]){
--indegree[j];
}
}
}
}
--num;
}
}
int main(){
int T;
scanf("%d", &T);
while(T--){
memset(indegree, 0, sizeof indegree);
memset(pic, 0, sizeof pic);
scanf("%d", &n);
scanf("%s", s);
int cnt = 0;
for(int i = 1; i <= n; ++i){
for(int j = i; j <= n; ++j){
char c = s[cnt++];
if(c == '+'){
pic[j][i - 1] = 1;
++indegree[i - 1];
}
else if(c == '-'){
pic[i - 1][j] = 1;
++indegree[j];
}
}
}
topsort();
for(int i = 1; i <= n; ++i){
if(i != 1) printf(" ");
printf("%d", sum[i] - sum[i - 1]);
}
printf("\n");
}
return 0;
}
UVA - 1423 Guess (拓扑排序)的更多相关文章
- Ordering Tasks UVA - 10305(拓扑排序)
在一个有向图中,对所有的节点进行排序,要求没有一个节点指向它前面的节点. 先统计所有节点的入度,对于入度为0的节点就可以分离出来,然后把这个节点指向的节点的入度减一. 一直做改操作,直到所有的节点都被 ...
- UVA - 10305 【拓扑排序】
题意 给出一些任务的优先级别 将这些任务进行的时间 进行先后排序 思路 拓扑排序 将所以有先后关系的任务都连一条边 然后每次 输出 度为0 的任务 每次把 以这个任务为弧的边 都取消 相对应任务的度也 ...
- uvalive 6393(uva 1572) Self-Assembly 拓扑排序
题意: 给出一些正方形,这些正方形的每一条边都有一个标号.这些标号有两种形式:1.一个大写字母+一个加减号(如:A+, B-, A-......), 2.两个0(如:00):这些正方形能够任意翻转和旋 ...
- uva 1423 拓扑排序
刘书上例题 拓扑排序 #include <cstdio> #include <cstdlib> #include <cmath> #include <map ...
- UVA.10305 Ordering Tasks (拓扑排序)
UVA.10305 Ordering Tasks 题意分析 详解请移步 算法学习 拓扑排序(TopSort) 拓扑排序的裸题 基本方法是,indegree表示入度表,vector存后继节点.在tops ...
- UVA 1572 Self-Assembly(拓扑排序)
1 // 把一个图的所有结点排序,使得每一条有向边(u,v)对应的u都排在v的前面. 2 // 在图论中,这个问题称为拓扑排序.(toposort) 3 // 不难发现:如果图中存在有向环,则不存在拓 ...
- Uva 10305 - Ordering Tasks 拓扑排序基础水题 队列和dfs实现
今天刚学的拓扑排序,大概搞懂后发现这题是赤裸裸的水题. 于是按自己想法敲了一遍,用queue做的,也就是Kahn算法,复杂度o(V+E),调完交上去,WA了... 于是检查了一遍又交了一发,还是WA. ...
- UVa 10305 (拓扑排序) Ordering Tasks
题意: 经典的拓扑排序.有n个任务,然后某些任务必须安排在某些任务前面完成,输出一种满足要求的序列. 分析: 拓扑排序用离散里面的话来说就是将偏序关系拓展为全序关系.我们将“小于”这种关系看做一条有向 ...
- UVa 872 - Ordering 输出全拓扑排序
本题要求输出所有拓扑排序的序列. 还好本题的数据量不是非常大.限制在26个大写英文字母,故此能够使用递归法输出. 这个递归输出所有解在Leetcode非常多这种题目的,不小心的话,还是非常难调试的. ...
- [拓扑排序]Ordering Tasks UVA - 10305
拓扑排序模版题型: John has n tasks to do.Unfortunately, the tasks are not independent and the execution of o ...
随机推荐
- SRS源码——Listener
1. 整理了一下Listener相关的UML类图:
- get your sqlserver database back by using EMC NW NMM
Dear all Yes ~ We can backup our sqlserver by EMC NW NMM. That is true and NW is a very very powerfu ...
- 问题解决 : MyBatis一对一查询时,打印结果只有一条数据
问题截图:修改后,结果返回条数正确 问题解决: 因为有重名的列,建议起个别名
- Python中property属性的概论和使用方法
property属性 概念: 定义一个方法但是使用装饰器property,只可以有一个self形参 可以用这样的属性动态的获取属性的值 定义方式(经典类) class Fun(): @property ...
- php 增删改查范例(3)
编辑页面edit.php: <?php$id=$_GET['id'];$db= new mysqli('localhost','root','root','db_0808');$sql=&quo ...
- 怎么样运行jar
一.制作jar文件 在制作.jar 文件之前你必须先编译好你的.java文件.假设我们的文件目录是c:javamyJavahelloHello.java 现在假设Hello.java的文件内容为: / ...
- 认识iOS系统架构
关于本文: 文章主要介绍iOS系统架构中的四层结构的内容.常用的框架.大致的功能,然后对iOS开发人员的发展提出自己的一些拙见. 一.iOS系统是基于UNIX系统,所有从系统稳定性上来说的确比其他操作 ...
- Lesson 12 banks and their customers
Why is there no risk to the customer when a bank prints the customer's name on his cheques? When any ...
- Docker常用命令(命令大全)
容器生命周期管理 1. docker run:创建一个新的容器并运行一个命令 2. Docker start/stop/restart - docker start:启动一个或多个已经被停止的容器 - ...
- 一 SSH整合:Spring整合Struts2的两种方式,struts.xml管理Action&Bean管理Action
SSH回顾 1 引入jar包 Struts2的jar包 D:\Struts2\struts-2.3.35\apps\struts2-blank\WEB-INF\lib 开发基本包 Struts2有一 ...