• <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個,不能確定。
            謝謝了
            精品久久久久中文字幕一区| 久久国产精品77777| 久久久无码精品午夜| 午夜精品久久久久9999高清| 亚洲国产精品无码久久久不卡| 久久精品国产亚洲AV高清热| 久久综合五月丁香久久激情| 久久久av波多野一区二区| 狠狠色综合网站久久久久久久| 亚洲中文字幕伊人久久无码| 国产精品美女久久久久| 国产精品久久久久a影院| 久久亚洲国产精品一区二区| 亚洲国产精品一区二区久久hs| 伊人久久综合热线大杳蕉下载| 亚洲国产精品一区二区久久hs| 日韩中文久久| 国产精品美女久久久久AV福利| 久久国产免费观看精品3| 麻豆精品久久久久久久99蜜桃| 99久久国产综合精品网成人影院| 久久香蕉超碰97国产精品| 成人综合久久精品色婷婷| 欧美性猛交xxxx免费看久久久 | 国产日韩欧美久久| 国内精品久久久久影院一蜜桃 | 999久久久免费精品国产| 久久精品青青草原伊人| 思思久久精品在热线热| 日本亚洲色大成网站WWW久久| 精品久久久无码中文字幕天天| 久久精品国产影库免费看 | 久久精品成人免费观看97| 日本福利片国产午夜久久| 69久久精品无码一区二区| MM131亚洲国产美女久久| 狠狠色丁香久久婷婷综合五月| 国产精品免费看久久久| 久久精品嫩草影院| 色综合久久最新中文字幕| 久久久久亚洲爆乳少妇无|