Poj 1094 拓撲排序 水題

來源:互聯網
上載者:User

標籤:blog   os   for   io   div   re   

Sad..這麼水的題WA了無數發,題目要看仔細啊,留下來做個警告把

#include <cstdio>#include <cstring>#include <algorithm>#include <climits>#include <string>#include <iostream>#include <map>#include <cstdlib>#include <list>#include <set>#include <queue>#include <stack>using namespace std;typedef long long LL;const int maxn = 30;int N,M,fa[maxn],incnt[maxn];bool vis[maxn],sorted[maxn];int mp[maxn][maxn];int toposort(vector<int> &ans) {    int tmp[maxn];    bool flag = true;    ans.clear();    memset(vis,0,sizeof(vis));    for(int i = 1;i <= N;i++) tmp[i] = incnt[i];    for(int k = 1;k <= N;k++) {        int x,ncnt = 0;        for(int i = 1;i <= N;i++) if(!vis[i] && tmp[i] == 0) {            x = i; ncnt++;        }        if(ncnt > 1) flag = false; //就算不能判斷出來,不能直接return,還要繼續判斷是否有環        if(ncnt == 0) return -1;        vis[x] = true;        ans.push_back(x);        for(int i = 1;i <= N;i++) if(!vis[i] && mp[x][i] == 1) {            tmp[i]--;        }    }    return flag;}int main() {    while(scanf("%d%d",&N,&M),N) {        vector<int> ans;        memset(mp,-1,sizeof(mp));        memset(incnt,0,sizeof(incnt));        bool determined = false,inconsistency = false;        int cnt = 1,kase;        for(int i = 1;i <= M;i++) {            char buf[10]; scanf("%s",buf);            int a = buf[0] - ‘A‘ + 1,b = buf[2] - ‘A‘ + 1;            if(inconsistency || determined) continue;            if(mp[a][b] == -1) incnt[b]++;            mp[a][b] = true;            int ret = toposort(ans);            if(ret == 1) determined = true,kase = i;            if(ret == -1) inconsistency = true;            if(inconsistency) {                printf("Inconsistency found after %d relations.\n",i);                continue;            }        }        if(!inconsistency) {            if(determined) {                printf("Sorted sequence determined after %d relations: ",kase);                for(int i = 0;i < ans.size();i++) printf("%c",ans[i] - 1 + ‘A‘);                puts(".");            } else puts("Sorted sequence cannot be determined.");        }    }    return 0;}

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.