UVA1423 - Guess

链接

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值