• <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個,不能確定。
            謝謝了
            18岁日韩内射颜射午夜久久成人| 波多野结衣AV无码久久一区| 国内精品久久国产大陆| 久久国产高清字幕中文| 久久av高潮av无码av喷吹| 综合久久久久久中文字幕亚洲国产国产综合一区首 | 国产精品无码久久综合| 久久精品国产99国产电影网| 久久国产精品免费| 一本色道久久88—综合亚洲精品| 久久人妻少妇嫩草AV无码专区| 久久精品男人影院| 久久精品国产AV一区二区三区 | 日韩亚洲欧美久久久www综合网 | 久久99精品久久久久久久不卡| 国产精品热久久无码av| 亚洲伊人久久精品影院| 久久精品这里只有精99品| 久久久精品人妻一区二区三区四| 欧美亚洲另类久久综合婷婷| 97久久久久人妻精品专区| 久久受www免费人成_看片中文| 久久国产精品-国产精品| 亚洲AV日韩精品久久久久久久| 久久久久97国产精华液好用吗| av色综合久久天堂av色综合在| 久久亚洲欧洲国产综合| 欧美精品一本久久男人的天堂| 无遮挡粉嫩小泬久久久久久久| 色综合久久天天综线观看| 亚洲一区二区三区日本久久九| 久久综合给合久久国产免费| 波多野结衣AV无码久久一区| 中文成人久久久久影院免费观看| 国产精品99久久久久久董美香 | 中文字幕无码精品亚洲资源网久久| 久久久久一级精品亚洲国产成人综合AV区 | 狠狠狠色丁香婷婷综合久久五月 | 色欲综合久久躁天天躁| 青青久久精品国产免费看| 久久夜色精品国产|