• <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>

            POI 2001 Peaceful Commission 2-SAT問(wèn)題

            The Public Peace Commission should be legislated in Parliament of The Democratic Republic of Byteland according to The Very Important Law. Unfortunately one of the obstacles is the fact that some deputies do not get on with some others.

            The Commission has to fulfill the following conditions:

            • Each party has exactly one representative in the Commission,
            • If two deputies do not like each other, they cannot both belong to the Commission.

            Each party has exactly two deputies in the Parliament. All of them are numbered from 1 to 2n. Deputies with numbers 2i-1 and 2i belong to the i-th party .

            Task

            Write a program, which:

            • reads from the text file SPO.IN the number of parties and the pairs of deputies that are not on friendly terms,
            • decides whether it is possible to establish the Commission, and if so, proposes the list of members,
            • writes the result in the text file SPO.OUT.

            Input

            In the first line of the text file SPO.IN there are two non-negative integers n and m. They denote respectively: the number of parties, 1 <= n <= 8000, and the number of pairs of deputies, who do not like each other, 0 <= m <=2 0000. In each of the following m lines there is written one pair of integers a and b, 1 <= a < b <= 2n, separated by a single space. It means that the deputies a and b do not like each other.

            There are multiple test cases. Process to end of file.

            Output

            The text file SPO.OUT should contain one word NIE (means NO in Polish), if the setting up of the Commission is impossible. In case when setting up of the Commission is possible the file SPO.OUT should contain n integers from the interval from 1 to 2n, written in the ascending order, indicating numbers of deputies who can form the Commission. Each of these numbers should be written in a separate line. If the Commission can be formed in various ways, your program may write any of them.

            Sample Input

            3 2
            1 3
            2 4

            Sample Output

            1
            4
            5
            
               最近看了2篇關(guān)于2-SAT問(wèn)題的IOI論文,對(duì)2-SAT問(wèn)題的O(m)時(shí)間復(fù)雜度的解法也有了一定的了解,找了道POI 2001的題來(lái)做,在WA了無(wú)數(shù)次之后終于過(guò)了,跑了1.44s,效率還可以。
            2篇論文分別是<<由對(duì)稱性解2-SAT問(wèn)題>>和<<2-SAT解法淺析>>。
            //2-SAT問(wèn)題
            //求出所有強(qiáng)連通分量,如果有矛盾點(diǎn)同處于一個(gè)連通分量則無(wú)解
            //縮點(diǎn),將原圖反向建立DAG圖
            //按拓?fù)渑判蛑?,找一個(gè)未著色點(diǎn)x,染成紅色
            //將與x矛盾的頂點(diǎn)及其子孫染為藍(lán)色
            //直到所有頂點(diǎn)均被染色,紅色即為2-SAT的一組解
            #include <iostream>
            #include 
            <vector>
            #include 
            <queue>
            using namespace std;

            const int MAXN = 16010;//2*8000
            char color[MAXN];//染色
            bool visit[MAXN];
            queue
            <int> q1,q2;
            vector
            < vector<int> > adj; //原圖
            vector< vector<int> > radj;//逆向圖
            vector< vector<int> > dag; //縮點(diǎn)后的逆向DAG圖
            int n,m,cnt,id[MAXN],order[MAXN],ind[MAXN];//強(qiáng)連通分量,訪問(wèn)順序,入度

            void dfs(int u){
                visit[u]
            =true;
                
            int i,len=adj[u].size();
                
            for(i=0;i<len;i++)
                    
            if(!visit[adj[u][i]])
                        dfs(adj[u][i]);
                order[cnt
            ++]=u;
            }

            void rdfs(int u){
                visit[u]
            =true;
                id[u]
            =cnt;
                
            int i,len=radj[u].size();
                
            for(i=0;i<len;i++)
                    
            if(!visit[radj[u][i]])
                        rdfs(radj[u][i]);
            }

            void korasaju(){
                
            int i;
                memset(visit,
            false,sizeof(visit));
                
            for(cnt=0,i=1;i<=2*n;i++)
                    
            if(!visit[i]) dfs(i);
                memset(id,
            0,sizeof(id));
                memset(visit,
            false,sizeof(visit));
                
            for(cnt=0,i=2*n-1;i>=0;i--)
                    
            if(!visit[order[i]])
                        cnt
            ++,rdfs(order[i]);
            }

            bool solvable(){
                
            for(int i=1;i<=n;i++)
                    
            if(id[2*i-1]==id[2*i])
                        
            return false;
                
            return true;
            }

            void topsort(){
                
            int i,j,len,now,p,pid;    
                
            while(!q1.empty()){
                    now
            =q1.front();
                    q1.pop();
                    
            if(color[now]!=0continue ;
                    color[now]
            ='R';
                    ind[now]
            =-1;
                    
            for(i=1;i<=2*n;i++){
                        
            if(id[i]==now){
                            p
            =(i%2)?i+1:i-1;
                            pid
            =id[p];                        
                            q2.push(pid);
                            
            while(!q2.empty()){
                                pid
            =q2.front();
                                q2.pop();
                                
            if(color[pid]=='B'continue ;            
                                color[pid]
            ='B';
                                
            int len=dag[pid].size();
                                
            for(j=0;j<len;j++)
                                    q2.push(dag[pid][j]);
                            }

                        }

                    }

                    len
            =dag[now].size();
                    
            for(i=0;i<len;i++){
                        ind[dag[now][i]]
            --;
                        
            if(ind[dag[now][i]]==0) q1.push(dag[now][i]);        
                    }

                }

            }

            int main(){
                
            int i,j,x,y,xx,yy,len;
                
            while(scanf("%d %d",&n,&m)!=EOF){
                    adj.assign(
            2*n+1,vector<int>());
                    radj.assign(
            2*n+1,vector<int>());
                    
            for(i=0;i<m;i++){
                        scanf(
            "%d %d",&x,&y);
                        xx
            =(x%2)?x+1:x-1;
                        yy
            =(y%2)?y+1:y-1;
                        adj[x].push_back(yy);
                        adj[y].push_back(xx);
                        radj[yy].push_back(x);
                        radj[xx].push_back(y);
                    }

                    korasaju();
                    
            if(!solvable()) puts("NIE");
                    
            else{
                        dag.assign(cnt
            +1,vector<int>());
                        memset(ind,
            0,sizeof(ind));
                        memset(color,
            0,sizeof(color));
                        
            for(i=1;i<=2*n;i++){
                            len
            =adj[i].size();
                            
            for(j=0;j<len;j++)
                                
            if(id[i]!=id[adj[i][j]]){
                                    dag[id[adj[i][j]]].push_back(id[i]);
                                    ind[id[i]]
            ++;
                                }

                        }

                        
            for(i=1;i<=cnt;i++)
                            
            if(ind[i]==0) q1.push(i);
                        topsort();
                        
            for(i=1;i<=n;i++){
                            
            if(color[id[2*i-1]]=='R') printf("%d\n",2*i-1);
                            
            else printf("%d\n",2*i);
                        }

                    }

                }

                
            return 0;
            }

            posted on 2009-06-07 18:59 極限定律 閱讀(1181) 評(píng)論(1)  編輯 收藏 引用 所屬分類: ACM/ICPC

            評(píng)論

            # re: POI 2001 Peaceful Commission 2-SAT問(wèn)題 2014-05-05 12:35 zzhhbyt

            您用的求scc的算法應(yīng)該是叫做kosaraju而不是korasaju吧?  回復(fù)  更多評(píng)論   

            <2009年6月>
            31123456
            78910111213
            14151617181920
            21222324252627
            2829301234
            567891011

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(10)

            隨筆分類

            隨筆檔案

            友情鏈接

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            国产福利电影一区二区三区久久久久成人精品综合 | 91精品国产乱码久久久久久| 囯产极品美女高潮无套久久久| 麻豆av久久av盛宴av| 99久久综合狠狠综合久久止| 国产一区二区三区久久精品| 伊人久久大香线蕉无码麻豆| 久久91综合国产91久久精品| 人人狠狠综合久久亚洲| 国产精品美女久久久| 亚洲欧美日韩精品久久亚洲区 | 亚洲精品乱码久久久久久按摩 | 久久线看观看精品香蕉国产| 亚洲综合伊人久久大杳蕉| 国产美女久久久| 欧洲人妻丰满av无码久久不卡| 色天使久久综合网天天| 久久99精品久久久久久动态图| 国产精品久久婷婷六月丁香| 97久久久精品综合88久久| 偷窥少妇久久久久久久久| 久久精品国产国产精品四凭| 99久久精品免费国产大片| 99精品国产在热久久| 中文字幕久久久久人妻| 伊人久久五月天| 精品熟女少妇AV免费久久| 人妻无码αv中文字幕久久琪琪布| 青青青国产成人久久111网站| 久久国产色av免费看| 免费一级做a爰片久久毛片潮| 久久久久亚洲AV无码去区首| 国产欧美久久一区二区| 99久久人妻无码精品系列| 久久人人爽人人爽人人片AV不| 伊人久久大香线蕉综合Av| 狠狠色婷婷久久综合频道日韩| 久久这里都是精品| 亚洲午夜久久久影院| 一本一本久久A久久综合精品| 国产精品99久久久精品无码|