UVALive4255-Guess (topological sorting)

Source: Internet
Author: User

Question Link


For a sequence A1, a2... an, we can calculate a symbol matrix s, where SIJ is the positive and negative signs of AI ++ AJ. Given the symbol matrix, a corresponding sequence must be output.

Train of Thought: use the technique of consecutive and converting to the difference between the prefix and the prefix as a vertex, so that we can establish the relationship between the edge and the level, and then use the topological sorting to solve the problem, first, a vertex with an inbound degree of 0 is deleted, and related edges are operated cyclically.

Code:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 30;char str[MAXN * 10];int in[MAXN], vis[MAXN], g[MAXN][MAXN], sum[MAXN];int n;void toposort() {    int d = 0, low = -10;    while (d <= n) {        memset(vis, 0, sizeof(vis));        for (int i = 0; i <= n; i++) {            if (in[i] == 0) {                sum[i] = low;                 in[i] = -1;                vis[i] = 1;                d++;            }        }        low++;        for (int i = 0; i <= n; i++) {            if (vis[i]) {                for (int j = 0; j <= n; j++)                     if (g[i][j])                        in[j]--;             }        }    } }int main() {    int cas;    scanf("%d", &cas);    while (cas--) {        scanf("%d", &n);               scanf("%s", str);        memset(in, 0, sizeof(in));        memset(g, 0, sizeof(g));        memset(sum, 0, sizeof(sum));        int cnt = 0;        for (int i = 1; i <= n; i++) {            for (int j = i; j <= n; j++) {                if (str[cnt] == '+') {                    in[j]++;                    g[i - 1][j] = 1;                 }                 else if (str[cnt] == '-') {                    in[i - 1]++;                      g[j][i - 1] = 1;                }                cnt++;            }        }                 toposort();        for (int i = 1; i <= n; i++) {            if (i == 1)                printf("%d", sum[i] - sum[i - 1]);            else                 printf(" %d", sum[i] - sum[i - 1]);         }        printf("\n");    }    return 0;}


UVALive4255-Guess (topological sorting)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.