Peaceful Commission_hdu1814_2-sat

Problem Description

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: 
1.Each party has exactly one representative in the Commission, 
2.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: 
1.reads from the text file SPO.IN the number of parties and the pairs of deputies that are not on friendly terms, 
2.decides whether it is possible to establish the Commission, and if so, proposes the list of members, 
3.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 mininum number sequence. 

 

Sample Input

3 2

1 3

2 4

 

Sample Output

1

4

5

Source

POI 2001

题目大意:

现在有n个党派,每个党派有2个代表,我们需要从每个党派中选一个代表出来,构成一个n个人的立法委员会.但是可能有一些代表互相讨厌,所以他们不能同时出现在立法委员会中.现在问你是否存在一个合理的方案,且输出所有可能立法委员会的最小字典序结果。

思路:

赤裸裸的2-sat题,根据给的矛盾关系建图

  XY有矛盾,连边x->yy->x

题目要求方案,kosaraju求强连通分量后已经是拓扑序的所以比较方便

当然水题党还是可以dfs过,能多暴力就有多暴力。

源代码/pas:

 

type
  edge=record
    x,y,next:Longint;
  end;
var
  left,right,ls,comp,order,list:array[0..16000]of longint;
  v:array[0..16000]of boolean;
  e:array[1..400000]of edge;
  maxE,n,m:longint;
  f:boolean;
function opp(x:longint):longint;
begin
  opp:=x-1;
  if odd(x) then
  exit(x+1);
end;
procedure add(x,y:longint);
begin
  inc(maxE);
  e[maxE].x:=x;
  e[maxE].y:=y;
  e[maxE].next:=ls[x];
  ls[x]:=maxE;
end;
procedure dfs(x,dep:longint);
var
  i:longint;
begin
  v[x]:=true;
  i:=ls[x];
  while i>0 do
  begin
    if not v[e[i].y] then
    dfs(e[i].y,dep);
    i:=e[i].next;
  end;
  if dep=0 then
  begin
    inc(comp[0]);
    order[comp[0]]:=x;
  end
  else
  begin
    comp[x]:=comp[0];
    inc(list[0]);
    list[list[0]]:=x;
  end;
end;
procedure kosaraju;
var
  i,j,tmp:longint;
begin
  for i:=1 to n do
  if not v[i] then
  dfs(i,0);
  comp[0]:=0;
  list[0]:=0;
  tmp:=maxE;
  maxE:=0;
  fillchar(v,sizeof(V),false);
  fillchar(ls,sizeof(ls),0);
  for i:=1 to tmp do add(e[i].y,e[i].x);
  for i:=n downto 1 do
  begin
    j:=order[i];
    if not v[j] then
    begin
      inc(comp[0]);
      left[comp[0]]:=list[0]+1;
      dfs(j,1);
      right[comp[0]]:=list[0];
    end;
  end;
  f:=true;
  for i:=1 to n do
  if comp[i]=comp[opp(i)] then
  f:=false;
end;
procedure main;
var
  i,j,x,y:longint;
  ans:array[0..1000]of longint;
  check:boolean;
begin
  readln(n,m);
  n:=2*n;
  for i:=1 to m do
  begin
    read(x,y);
    add(x,opp(y));
    add(y,opp(x));
  end;
  kosaraju;
  if not f then
  writeln('NIE')
  else
  begin
    fillchar(ans,sizeof(ans),0);
    for i:=comp[0] downto 1 do
    begin
      check:=true;
      for j:=left[i] to right[i] do
      begin
        x:=list[j];
        if ans[comp[opp(x)]]=1 then
        check:=false;
      end;
      if check then ans[i]:=1;
    end;
    for i:=1 to n do
    if ans[comp[i]]=1 then writeln(i);
  end;
end;
begin
  main;
end.

 
   

转载于:https://www.cnblogs.com/olahiuj/p/5781307.html

你可能感兴趣的:(Peaceful Commission_hdu1814_2-sat)