• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            lzm

            who dare win.
            posts - 14, comments - 29, trackbacks - 0, articles - 0

            poj 1094 Sorting It All Out

            Posted on 2009-04-12 23:13 lzmagic 閱讀(1655) 評論(5)  編輯 收藏 引用 所屬分類: OJ
                wa了很多次,終于過了,咔咔~笑一下先。
                第一次很快用拓撲排序把代碼敲出來,提交,wa,仔細分析后,發現忽略了重復邊的情況,插入邊(u, v),如果圖g已經存在了邊(u,v),就不能再增加v的入度。
                很快改過來,提交,還是wa,很無奈,上網看看大牛評論,原來是只要當前輸入數據能“確定唯一”,及時后面輸入數據“有環”,也是判定為“確定唯一”。也就是說,每輸入一條邊,如果判定有結果,忽略后面輸入數據,即使后面輸入數據能改變結果。
                再改,再提交,還是wa,幾度欲放棄之,還是堅持下來了,上google找別人代碼來分析,結果發選只要當前輸入數據“有環”,及時當前輸入數據“不能確定唯一”,也是判定為“有環”。
                把判定順序改過后,提交,終于ac!!!
                呼~~~爽YY中……
            #include <iostream>
            #include 
            <fstream>
            #include 
            <sstream>
            #include 
            <string>
            #include 
            <vector>
            #include 
            <deque>
            #include 
            <list>
            #include 
            <queue>
            #include 
            <stack>
            #include 
            <set>
            #include 
            <map>
            #include 
            <bitset>
            #include 
            <iterator>
            #include 
            <algorithm>
            #include 
            <numeric>
            #include 
            <functional>
            #include 
            <climits>
            #include 
            <ctime>
            #include 
            <cstdlib>
            #include 
            <cctype>
            using namespace std;

            #define UNSURE 0 // 不能確定 
            #define CYCLED 1 // 存在回路
            #define SORTED 2 // 已經排序

            int n;                            // n :頂點個數 
            vector<list<int> > g;           // g :圖 
            vector<int> top;                // top :拓撲序列 
            vector<int> ins;                // ins :入度 

            int Topsort()
            {
                
            bool unsure(false);
                queue
            <int> que;
                vector
            <int> cnt(ins.begin(), ins.end());    // 用cnt存儲ins,避免修改ins。 
                for (int j = 0; j < n; ++j)
                    
            if (cnt[j] == 0) que.push(j);            // 入度為0的頂點入隊。 
                int u;
                list
            <int>::iterator it;
                top.clear();    
                
            while (!que.empty())
                
            {
                    
            if (que.size() != 1) unsure = true;        // 如果選擇頂點多余1個,不能確定。 
                    u = que.front(); que.pop();
                    top.push_back(u);
                    
            for (it = g[u].begin(); it != g[u].end(); ++it)
                        
            if (--cnt[*it] == 0) que.push(*it);
                }

                
            if (top.size() != n) return CYCLED;        // 先判斷是否有環,即使不能確定, 
                if (unsure) return UNSURE;                // 再判斷是否不能確定。 
                return SORTED;         
            }


            int main()
            {
                
            int steps, ans;
                
            string exp;
                
            int m, u, v, i;    
                
                
            while (cin >> n >> m, n != 0 || m != 0)    // 2 <= n <= 26
                {
                    g.assign(n, list
            <int>());
                    ins.assign(n, 
            0);
                    ans 
            = UNSURE;
                    
            for (steps = 0; ans == UNSURE && steps < m; ++steps)
                    
            {
                        cin 
            >> exp;
                        u 
            = int(exp[0- 'A'), v = int(exp[2- 'A');
                        
            if (find(g[u].begin(), g[u].end(), v) == g[u].end()) // 如果不存在邊(u, v),添加。 
                            g[u].push_back(v), ++ins[v];
                        ans 
            = Topsort(); 
                    }

                    
            for (i = steps; i < m; ++i) cin >> exp; // 處理剩下無用的輸入數據。 
                    
                    
            if (ans == UNSURE) // Output()
                        cout << "Sorted sequence cannot be determined." << endl; 
                    
            else if (ans == CYCLED)
                        cout 
            << "Inconsistency found after " << steps << " relations." << endl;
                    
            else
                    

                        cout 
            << "Sorted sequence determined after " << steps << " relations: ";
                        
            for (i = 0; i < n; i++) cout << char('A' + top[i]);
                        cout 
            << "." << endl;
                    }
             // end Output()
                }

                system(
            "pause");
                
            return 0;
            }


                ps.第一,要仔細看題,第二,還是要仔細看題。

            Feedback

            # re: poj 1094 Sorting It All Out  回復  更多評論   

            2009-04-15 15:34 by brightcoder
            期待繼續寫算法,比如通用的那種,算法導論上的

            # re: poj 1094 Sorting It All Out  回復  更多評論   

            2009-05-21 08:43 by dustman
            你寫的方法 跟算法導論上的不一樣

            算法導論上的是 DFS完記錄FINISH TIME 然后按遞減順序排序 排完后 檢查是否有沖突就可以判斷有沒環.

            我一直沒找到怎么判斷無向圖有環, 還有最小環問題

            # re: poj 1094 Sorting It All Out  回復  更多評論   

            2009-05-27 23:57 by phidias
            為什么我提交了你的程序是WA……

            # re: poj 1094 Sorting It All Out  回復  更多評論   

            2009-05-28 00:00 by Phidias
            在poj上WA,acm上AC了……奇怪……

            # re: poj 1094 Sorting It All Out  回復  更多評論   

            2009-12-29 22:35 by Turn
            原來是這樣判斷不確定的:
            if (que.size() != 1) unsure = true; // 如果選擇頂點多余1個,不能確定。
            謝謝了
            一本色道久久88精品综合| 久久九九全国免费| 久久精品国产亚洲AV嫖农村妇女| 久久国产色AV免费观看| 国产亚洲色婷婷久久99精品91| 怡红院日本一道日本久久| 亚洲午夜精品久久久久久app| 久久精品无码一区二区无码| 狠狠久久综合| 久久精品国产清高在天天线| 四虎影视久久久免费观看| 女人香蕉久久**毛片精品| 亚洲精品无码成人片久久| 色综合合久久天天综合绕视看| 亚洲精品无码久久久久去q| 久久www免费人成看国产片| 久久久久亚洲av无码专区| 久久国产成人午夜aⅴ影院| 九九久久99综合一区二区| 久久精品国产精品亚洲精品| 国产精品99久久久久久董美香| 久久婷婷五月综合97色一本一本| 亚洲国产精品狼友中文久久久| 国产精品久久久久一区二区三区| 久久久久亚洲av无码专区 | 久久国产精品-国产精品| 色综合久久无码五十路人妻| 久久综合久久综合亚洲| 久久久久一级精品亚洲国产成人综合AV区 | 国产精品久久成人影院| 亚洲熟妇无码另类久久久| 久久久久久伊人高潮影院| 伊人久久大香线蕉无码麻豆| 久久丝袜精品中文字幕| 日韩AV毛片精品久久久| 久久综合一区二区无码| 久久亚洲精品无码播放| 久久青青国产| 亚洲婷婷国产精品电影人久久| 三级片免费观看久久| 久久亚洲熟女cc98cm|