`

UVa 1423 - Guess 拓扑

 
阅读更多

连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=543&problem=4169&mosmsg=Submission+received+with+ID+11660014

 

Given a sequence of integers, a1a2,..., an <tex2html_verbatim_mark>, we define its sign matrix S such that, Sij = `` + " <tex2html_verbatim_mark>if ai +...+ aj > 0 <tex2html_verbatim_mark>; Sij = `` - " <tex2html_verbatim_mark>if ai +...+ aj < 0 <tex2html_verbatim_mark>; and Sij = ``0" <tex2html_verbatim_mark>otherwise.

For example, if (a1a2a3a4) = (- 1, 5, - 4, 2) <tex2html_verbatim_mark>, then its sign matrix S <tex2html_verbatim_mark>is a 4×4 <tex2html_verbatim_mark>matrix:

 

 

 


We say that the sequence (-1, 5, -4, 2) generates the sign matrix. A sign matrix is valid if it can be generated by a sequence of integers.

Given a sequence of integers, it is easy to compute its sign matrix. This problem is about the opposite direction: Given a valid sign matrix, find a sequence of integers that generates the sign matrix. Note that two or more different sequences of integers can generate the same sign matrix. For example, the sequence (-2, 5, -3, 1) generates the same sign matrix as the sequence (-1,5, -4,2).

Write a program that, given a valid sign matrix, can find a sequence of integers that generates the sign matrix. You may assume that every integer in a sequence is between -10 and 10, both inclusive.

 

Input 

Your program is to read from standard input. The input consists of T <tex2html_verbatim_mark>test cases. The number of test cases T<tex2html_verbatim_mark>is given in the first line of the input. Each test case consists of two lines. The first line contains an integer n <tex2html_verbatim_mark>(1$ \le$n$ \le$10) <tex2html_verbatim_mark>, where n <tex2html_verbatim_mark>is the length of a sequence of integers. The second line contains a string of n(n + 1)/2 <tex2html_verbatim_mark>characters such that the first n <tex2html_verbatim_mark>characters correspond to the first row of the sign matrix, the next n - 1 <tex2html_verbatim_mark>characters to the second row, ... <tex2html_verbatim_mark>, and the last character to the n <tex2html_verbatim_mark>-th row.

 

Output 

Your program is to write to standard output. For each test case, output exactly one line containing a sequence of n <tex2html_verbatim_mark>integers which generates the sign matrix. If more than one sequence generates the sign matrix, you may output any one of them. Every integer in the sequence must be between -10 and 10, both inclusive.

 

Sample Input 

 

3 
4 
-+0++++--+ 
2 
+++ 
5 
++0+-+-+--+-+--

 

Sample Output 

 

-2 5 -3 1 
3 4 
1 2 -3 4 -5

 

 

 

 

 

 

 

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 10 + 5;

int n, G[maxn][maxn]; // 注意本题结点编号为0~n
int c[maxn];
vector<int> topo;

bool dfs(int u){
  c[u] = -1;
  for(int v = 0; v <= n; v++) if(G[u][v]) {
    if(c[v]<0) return false;
    else if(!c[v]) dfs(v);
  }
  c[u] = 1; topo.push_back(u);
  return true;
}

bool toposort(){
  topo.clear();
  memset(c, 0, sizeof(c));
  for(int u = 0; u <= n; u++) if(!c[u])
    if(!dfs(u)) return false;
  reverse(topo.begin(), topo.end());
  return true;
}

// 用并查集合并相等结点
int pa[maxn];
int findset(int x) { return pa[x] != x ? pa[x] = findset(pa[x]) : x; }

int main() {
  int T;
  scanf("%d", &T);
  while(T--) {
    char input[100], S[11][11];
    scanf("%d%s", &n, input);
    int idx = 0;
    for(int i = 0; i <= n; i++) pa[i] = i;
    for(int i = 1; i <= n; i++)
      for(int j = i; j <= n; j++) {
        S[i][j] = input[idx++];
        if(S[i][j] == '0') pa[j] = i-1; // sum[j]-sum[i-1]=0,因此j和i-1是等价结点
      }

    // 若前缀和sum[a] < sum[b],连边a->b,在拓扑序中a会在b的前面
    memset(G, 0, sizeof(G));
    for(int i = 1; i <= n; i++)
      for(int j = i; j <= n; j++) {
        if(S[i][j] == '-') G[findset(j)][findset(i-1)] = 1; // sum[j]-sum[i-1] < 0
        if(S[i][j] == '+') G[findset(i-1)][findset(j)] = 1; // sum[j]-sum[i-1] > 0
      }
    toposort();
    int sum[maxn], cur = 0;
    for(int i = 0; i <= n; i++) sum[topo[i]] = cur++; // 按照拓扑序依次赋值0, 1, 2, ...
    for(int i = 1; i <= n; i++) {
      sum[i] = sum[findset(i)];
      if(i > 1) printf(" ");
      printf("%d", sum[i] - sum[i-1]); // 注意,sum[0]未必等于0
    }
    printf("\n");
  }
  return 0;
}

 

 

 

 

 

 

分享到:
评论

相关推荐

    UVaOJ-401(Palindromes).zip_401 Palindromes

    标题中的"UVaOJ-401(Palindromes)"表明这是一个关于解决UVa Online Judge(UVa OJ)上编号为401的编程挑战,该挑战的主题是"Palindromes",即回文串。回文串是指一个字符串无论从前读到后还是从后读到前都是相同的,...

    Uva 1510 - Neon Sign

    ### Uva 1510 - Neon Sign #### 问题背景与描述 在题目“Uva 1510 - Neon Sign”中,我们面对的是一个霓虹灯招牌设计问题。该霓虹灯招牌由一系列位于圆周上的角点组成,并通过发光管连接这些角点。发光管有两种...

    uva705-Slash-Maze-.rar_Slash_uva705

    【标题】"uva705-Slash-Maze-.rar_Slash_uva705" 指向的是一个在UVa Online Judge (UVa OJ) 上提交并通过的编程问题,具体为问题编号705,名为"Slash Maze"。这个压缩包很可能包含了该问题的解决方案源代码。 ...

    UVA100~200---52道题accept代码,均顺利accept过

    这些文件名代表的是在UVA(University of Virginia)在线判题系统上解决的编程题目,主要是C++语言编写的解决方案。UVA是一个知名的在线编程竞赛平台,它提供了大量的算法问题供程序员挑战,有助于提高编程技能和...

    开源项目-codingsince1985-UVa#uva-online-judge-solutions-in-golang.zip

    开源项目-codingsince1985-UVa#uva-online-judge-solutions-in-golang.zip,两年来每天都在解决一个uva在线裁判问题,算起来…

    UVA133-TheDoleQueue.zip_site:www.pudn.com_uva133

    《UVA133 - 救济金发放问题:The Dole Queue》 在计算机科学领域,算法是解决问题的关键工具,特别是在处理复杂数据结构和优化问题时。UVA(University of Virginia)在线判题系统提供了丰富的算法题目供程序员挑战...

    Algorithm-UVA-Solutions-in-Python.zip

    "Algorithm-UVA-Solutions-in-Python.zip"这个压缩包文件正是针对UVA竞赛中问题的Python 3解决方案集合。 Python作为一门易学且功能强大的编程语言,因其简洁的语法和丰富的库支持,成为了许多算法爱好者和开发者的...

    uva532-Dungeon-Master.rar_dungeon

    《UVA532 Dungeon Master:解密游戏编程的深度探索》 在计算机科学与编程领域,UVA(University of Virginia)在线判题系统是一个深受程序员喜爱的平台,它提供了丰富的算法题目供学习者挑战。其中,编号为532的...

    tpcw-nyu-uva-client 客户端

    "tpcw-nyu-uva-client 客户端"是一个专为TPCW(Transaction Processing Performance Council Workloads)设计的应用程序,由纽约大学(NYU)和弗吉尼亚大学(UVA)共同开发。这个客户端软件主要用于模拟和评估数据库...

Global site tag (gtag.js) - Google Analytics