链接
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4169
题解
先根据大小关系建图,用有向边表示大于号
等于的情况不用管,因为这张图是完全图而且题目说了它一定是合法的,那么也就是说相等的数字根据大小关系跑出来就肯定是相等的
在有向无环图上跑拓扑排序
代码
#include <bits/stdc++.h>
#define maxn 20
#define maxe 100
#define cl(x) memset(x,0,sizeof(x))
using namespace std;
struct Graph
{
int etot, head[maxn], to[maxe], next[maxe], w[maxe];
void clear(int N)
{
int i;
for(i=0;i<=N;i++)head[i]=0;
for(i=1;i<=etot;i++)to[i]=next[i]=w[i]=0;
N=etot=0;
}
void adde(int a, int b, int c){to[++etot]=b;w[etot]=c;next[etot]=head[a];head[a]=etot;}
}G;
int read(int x=0)
{
int c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
int S[maxn], indeg[maxn], n;
char s[maxe];
void bfs()
{
queue<int> q;
for(auto i=0;i<=n;i++)if(indeg[i]==0)q.emplace(i);
while(!q.empty())
{
auto x=q.front(); q.pop();
for(auto p=G.head[x];p;p=G.next[p])
{
S[G.to[p]]=min(S[G.to[p]],S[x]-1);
indeg[G.to[p]]--;
if(indeg[G.to[p]]==0)q.emplace(G.to[p]);
}
}
}
int main()
{
int T(read()), i, j, p;
while(T--)
{
n=read();
cl(s), cl(S), cl(indeg);
G.clear(n);
scanf("%s",s);
p=0;
for(i=0;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
auto c=s[p++];
if(c=='+')G.adde(j,i,0), indeg[i]++;
else if(c=='-')G.adde(i,j,0), indeg[j]++;
}
}
bfs();
for(i=1;i<=n;i++)S[i]-=S[0];
S[0]=0;
for(i=1;i<=n;i++)printf("%d ",S[i]-S[i-1]);
putchar(10);
}
return 0;
}