• <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 閱讀(1657) 評論(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個,不能確定。
            謝謝了
            丁香五月网久久综合| 久久天天躁夜夜躁狠狠躁2022 | 久久国产色AV免费看| 久久久久成人精品无码中文字幕 | 99久久国产综合精品网成人影院| 久久精品无码一区二区无码| www.久久热| 久久久SS麻豆欧美国产日韩| 久久综合综合久久狠狠狠97色88| 亚洲国产成人久久精品99| 国产精品18久久久久久vr| 亚洲人成网站999久久久综合| 久久综合丁香激情久久| 亚洲伊人久久成综合人影院| 国产精品久久久久久影院| 亚洲人成无码www久久久| 国产ww久久久久久久久久| 久久精品中文闷骚内射| 亚洲Av无码国产情品久久| 久久综合九色综合久99| 99久久精品国产高清一区二区| 亚洲国产成人久久综合一区77| 老司机国内精品久久久久| 亚洲AV无码久久寂寞少妇| 亚洲日本va午夜中文字幕久久| 亚洲午夜久久影院| 久久亚洲国产中v天仙www| 狠狠狠色丁香婷婷综合久久五月| 亚洲中文字幕久久精品无码APP| 久久一本综合| 久久人人爽人人爽人人片AV麻豆| 成人精品一区二区久久久| 国内精品久久九九国产精品| 久久九九亚洲精品| 久久免费小视频| 99久久人人爽亚洲精品美女| 一本久久久久久久| 久久高清一级毛片| 久久综合久久综合亚洲| 久久亚洲精品无码观看不卡| 热久久视久久精品18|