uvalive4255 Guess --- 拓扑排序

按白书的解法,区间和转前缀和,知道任意两个前缀和Si,Sj的相对大小关系,找出一组Si的解即可。大小关系转化成图,注意相等的两个Si。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define MAXN 1200
using namespace std;
int n;
const int Size = 40;
char str[MAXN];
int Sum[Size];
int mp[Size][Size];
int d[Size];
queue<int> q;
int depth[Size];

void Toposort() {
    int low = -10;
    while(!q.empty()) q.pop();
    for(int i = 0;i <= n;i++) {
        if(d[i] == 0) {
            q.push(i);
            depth[i] = low;
        }
    }
    
    while(!q.empty()) {
        int cur = q.front();
        q.pop();
        Sum[cur] = depth[cur];
        low++;
        for(int i = 0;i <= n;i++) {
            if(i == cur) continue;
            if(mp[cur][i] != 0) {
                mp[cur][i]--;
                d[i]--;
                if(d[i] == 0) {
                    q.push(i);
                    depth[i] = low;
                }
            }
        }
        
    }
    for(int i = 1;i <= n;i++) {
        printf("%d%s",Sum[i]-Sum[i-1],i==n?"":" ");
    }
    printf("\n");
}

int main() {
    int T;
    scanf("%d",&T);
    while(T--) {
       memset(depth,0,sizeof(depth));
       memset(Sum,0,sizeof(Sum));
       memset(mp,0,sizeof(mp));
       memset(d,0,sizeof(d));
       scanf("%d",&n);
       scanf("%s",str);  
       int t = n;
       int pos = 0;
       for(int i = 1;i <= n;i++)
        for(int j = i;j <= n;j++) {
            char ch = str[pos++];
            if(ch == '+') {
                mp[i-1][j]++;
                d[j]++;
            } else if(ch == '-'){
                mp[j][i-1]++;
                d[i-1]++;
            }
        }
        Toposort();

    }

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值