#include<bits/stdc++.h>
#define debu
using namespace std;
const int INF=99999;
struct point
{
int x,y;
point(int a=0,int b=0):x(a),y(b) {}
};
const int maxn=15;
char st[60];
stack<int> s;
int d[maxn];
int n,sum[maxn];
vector<point> tmp;
int g[maxn][maxn];
void make()
{
tmp.clear();
int num=0;
memset(d,0,sizeof(d));
for(int i=0; i<n; i++)
{
for(int j=i+1; j<=n; j++)
{
if(st[num]=='+')
{
g[j][i]=1;
d[i]++;
}
if(st[num]=='-')
{
g[i][j]=1;
d[j]++;
}
if(st[num]=='0') tmp.push_back(point(i,j));
num++;
}
}
}
void solve()
{
while(!s.empty()) s.pop();
for(int i=0; i<=n; i++)
if(d[i]==0) s.push(i);
int tot=11;
while(!s.empty())
{
int u=s.top();
sum[u]=tot;
tot--;
s.pop();
for(int i=0; i<=n; i++)
if(g[u][i])
{
d[i]--;
if(!d[i]) s.push(i);
}
}
for(int i=0; i<tmp.size(); i++)
if(tmp[i].x!=INF||tmp[i].y!=INF)
{
int t=min(sum[tmp[i].x],sum[tmp[i].y]);
sum[tmp[i].x]=sum[tmp[i].y]=t;
}
else sum[tmp[i].x]=sum[tmp[i].y]=1;
for(int i=1; i<n; i++)
{
int x=sum[i]-sum[i-1];
printf("%d ",x);
}
printf("%d\n",sum[n]-sum[n-1]);
}
int main()
{
#ifdef debug
freopen("in.in","r",stdin);
#endif // debug
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
scanf("%s",st);
make();
solve();
}
return 0;
}
题目地址:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2256
题解:由题目矩阵,可得一系列大小关系(如S[0]-S[1]>0则从0连一条到1的有向边),做拓扑排序,当两数大小相等时,只选一数,最后更改另一数即可。若有数字不受限制则设为1(-10到10任意数)。