题意:定义为的正负号,给出符号矩阵,需要求出对应的一个序列。<=10,序列的长度n<=10
解法:首先根据连续和转化为前缀和之差的思想,设,则-,那么矩阵中的一项表示了两个之间的大小关系,设=0,问题转化为已知之间的大小关系,求出一个对应的序列。用拓扑排序求出它们的关系,构造一组解,然后很容易得出序列
#include<bits/stdc++.h>
using namespace std;
const int maxn = 15;
int f[maxn][maxn];
int topo[maxn];
int vis[maxn],temp;
int n;
int b[maxn];
bool dfs(int u){
vis[u] = -1;
for(int i = 0; i <= n; ++i){
if(f[u][i] == 1){
if(vis[i] < 0) return false;
if(!vis[i] && !dfs(i)) return false;
}
}
vis[u] = 1; topo[--temp] = u;
return true;
}
bool toposort(){
temp = n+1;
for(int i = 0; i <= n; ++i){
if(!vis[i]){
if(!dfs(i)){
return false;
}
}
}
return true;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
memset(vis,0,sizeof(vis));
memset(f,0,sizeof(f));
memset(topo,0,sizeof(topo));
memset(b,0,sizeof(b));
scanf("%d",&n);
char c;
getchar();
for(int i = 1; i <= n; ++i){
for(int j = i; j <= n; ++j){
scanf("%c",&c);
if(c=='+') f[i-1][j] = 1;
else if(c=='-') f[j][i-1] = 1;
else if(c=='0') f[i-1][j] = f[j][i-1] = -1;
}
}
toposort();
int loc0;
for(int i = 0; i <= n; ++i){
if(topo[i] == 0){
loc0 = i;
break;
}
}
int now = 0;
for(int i = loc0+1; i <= n; ++i){
if(f[topo[i]][topo[i-1]] != -1) b[topo[i]] = ++now;
else b[topo[i]] = now;
}
now = 0;
for(int i = loc0-1; i >= 0; --i){
if(f[topo[i]][topo[i+1]] != -1) b[topo[i]] = --now;
else b[topo[i]] = now;
}
for(int i = 1; i <= n; ++i){
printf("%d%c",b[i]-b[i-1]," \n"[i==n?1:0]);
}
}
return 0;
}