• Sorting It All Out


     1 #include <bits/stdc++.h>
     2 const int N=100000;
     3 using namespace std;
     4 vector<int>E[N];
     5 int in[N],n,pos,flag,ok,m,sort1[N],stop,tmp[N];
     6 char X,Y,O;
     7 queue<int>q;
     8 void init() {
     9     memset(in, 0, sizeof(in));
    10     for (int i = 0; i < n; i++) {
    11         E[i].clear();
    12     }
    13 }
    14 
    15 bool find(int u,int v) {
    16     for (int i = 0; i < E[u].size(); ++i)
    17         if (E[u][i] == v)
    18             return 1;
    19     return 0;
    20 }
    21 
    22 int toposort() {
    23     while (!q.empty()) {
    24         q.pop();
    25     }
    26     for (int i = 0; i < n; i++) {
    27         if (!in[i]) {
    28             q.push(i);
    29         }
    30     }
    31     pos = 0;
    32     bool f = 0;
    33     while (!q.empty()) {
    34         if (q.size() > 1) {
    35             f = 1;
    36         }
    37         int k = q.front();
    38         q.pop();
    39         sort1[pos++] = k;
    40         for (int i = 0; i < E[k].size(); i++) {
    41             if (--in[E[k][i]] == 0) {
    42                 q.push(E[k][i]);
    43             }
    44         }
    45     }
    46     if (pos < n) return 1;
    47     if (f) return 2;
    48     return 3;
    49 }
    50 
    51 
    52 int main() {
    53     while (~scanf("%d%d%*c", &n, &m)) {
    54         if (n == 0 && m == 0) {
    55             break;
    56         }
    57         init();
    58         flag = 2;
    59         ok = 0;
    60         for (int i = 1; i <= m; i++) {
    61             scanf("%c%c%c%*c", &X, &O, &Y);
    62             if (ok) {
    63                 continue;
    64             }
    65             int x = X - 'A', y = Y - 'A';
    66             if (O == '<' && !find(y, x)) {
    67                 E[y].push_back(x);
    68                 in[x]++;
    69             } else {
    70                 if (O == '>' && !find(x, y)) {
    71                     E[x].push_back(y);
    72                     in[y]++;
    73                 }
    74             }
    75             memcpy(tmp, in, sizeof(in));
    76             flag = toposort();
    77             memcpy(in, tmp, sizeof(in));
    78             if (flag != 2) {
    79                 stop = i;
    80                 ok = 1;
    81             }
    82         }
    83         if (flag == 3) {
    84             printf("Sorted sequence determined after %d relations: ", stop);
    85             for (int i = pos - 1; i >= 0; i--) {
    86                 printf("%c", sort1[i] + 'A');
    87             }
    88             printf(".
    ");
    89         }
    90         if (flag == 1) {
    91             printf("Inconsistency found after %d relations.
    ", stop);
    92         }
    93         if (flag == 2) {
    94             printf("Sorted sequence cannot be determined.
    ");
    95         }
    96     }
    97 }
    View Code
  • 相关阅读:
    .net Framework 4.5 MVC4 + RabbitMQ
    阿里云飞天系统的技术架构(转)
    ORA12899错误解决记录
    网络通讯函数测试记录
    .应该用CreateThread还是_beginthreadex(), 为什么?( 转载)
    发挥v$SQL视图的作用(oracle)
    ClickHouse笔记
    MySQL字段是JsonArray格式怎么查询数据
    Base64编码保存为图片,java工具类
    java两个线程交替打印数字
  • 原文地址:https://www.cnblogs.com/Accpted/p/11232315.html
Copyright © 2020-2023  润新知