poj3905 Perfect Election pascal代码

来源:互联网 发布:excel快速录入数据 编辑:程序博客网 时间:2024/05/16 07:27
Perfect Election
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 825 Accepted: 378

Description

In a country (my memory fails to say which), the candidates {1, 2 ..., N} are running in the parliamentary election. An opinion poll asks the question "For any two candidates of your own choice, which election result would make you happy?". The accepted answers are shown in the table below, where the candidates i and j are not necessarily different, i.e. it may happen that i=j. There are M poll answers, some of which may be similar or identical. The problem is to decide whether there can be an election outcome (It may happen that all candidates fail to be elected, or all are elected, or only a part of them are elected. All these are acceptable election outcomes.) that conforms to all M answers. We say that such an election outcome is perfect. The result of the problem is 1 if a perfect election outcome does exist and 0 otherwise.

Input

Each data set corresponds to an instance of the problem and starts with two integral numbers: 1≤N≤1000 and 1≤M≤1000000. The data set continues with M pairs ±i ±j of signed numbers, 1≤i,j≤N. Each pair encodes a poll answer as follows: 

Accepted answers to the poll questionEncodingI would be happy if at least one from i and j is elected.+i +jI would be happy if at least one from i and j is not elected.-i -jI would be happy if i is elected or j is not elected or both events happen.+i -jI would be happy if i is not elected or j is elected or both events happen.-i +j

The input data are separated by white spaces, terminate with an end of file, and are correct.

Output

For each data set the program prints the result of the encoded election problem. The result, 1 or 0, is printed on the standard output from the beginning of a line. There must be no empty lines on output.

Sample Input

3 3  +1 +2  -1 +2  -1 -3 2 3  -1 +2  -1 -2  +1 -2 2 4  -1 +2  -1 -2  +1 -2  +1 +2 2 8  +1 +2  +2 +1  +1 -2  +1 -2  -2 +1  -1 +1  -2 -2  +1 -1

Sample Output

1101

Hint

For the first data set the result of the problem is 1; there are several perfect election outcomes, e.g. 1 is not elected, 2 is elected, 3 is not elected. The result for the second data set is justified by the perfect election outcome: 1 is not elected, 2 is not elected. The result for the third data set is 0. According to the answers -1 +2 and -1 -2 the candidate 1 must not be elected, whereas the answers +1 -2 and +1 +2 say that candidate 1 must be elected. There is no perfect election outcome. For the fourth data set notice that there are similar or identical poll answers and that some answers mention a single candidate. The result is 1.

pascal代码:
var  a:array [1..2010,1..2010] of longint;  low,dfn,st,c,belong:array [1..2010] of longint;  v,f:array [1..2010] of boolean;  k:array [1..2000,1..2000] of boolean;  i,j,m,n,x,y,d,xx,yy,t,p,tt,c1,c2:longint;  fff:boolean;function min(x,y:longint):longint;begin  if x>y then  exit(y);  exit(x);end;function pp:longint;begin  f[st[t]]:=false;  dec(t);  exit(st[t+1]);end;procedure add(x,y:longint);begin  inc(c[x]);  a[x,c[x]]:=y;end;procedure check;var  i:longint;begin  for i:=1 to n do  if belong[i]=belong[n+i] then  begin    fff:=false;    exit;  end;end;procedure tarjan(x:longint);var  i,y:longint;begin  inc(d);  low[x]:=d;  dfn[x]:=d;  inc(t);  st[t]:=x;  f[x]:=true;  for i:=1 to c[x] do  begin    if not v[a[x,i]] then    begin      v[a[x,i]]:=true;      tarjan(a[x,i]);      low[x]:=min(low[x],low[a[x,i]]);    end else    begin      if f[a[x,i]] then      low[x]:=min(low[x],dfn[a[x,i]]);    end;  end;  if dfn[x]=low[x] then  begin    inc(p);    belong[x]:=p;    y:=x-1;    while x<>y do    begin      y:=pp;      belong[y]:=p;    end;  end;end;begin  while not eof do  begin    t:=0;    p:=0;    d:=0;    fillchar(belong,sizeof(belong),0);    fillchar(low,sizeof(low),0);    fillchar(dfn,sizeof(dfn),0);    fillchar(st,sizeof(st),0);    fillchar(f,sizeof(f),false);    fillchar(v,sizeof(v),false);    fillchar(a,sizeof(a),0);    fillchar(c,sizeof(c),0);    fillchar(k,sizeof(k),true);    read(n,m);    for i:=1 to m do    begin      read(x,y);      if x>0 then      begin        if y>0 then        begin          add(n+x,y);          add(n+y,x);        end else        begin          add(n+x,n+abs(y));          add(abs(y),x);        end;      end else      begin        if y>0 then        begin         add(abs(x),y);         add(n+y,n+abs(x));        end        else        begin          add(abs(x),n+abs(y));          add(abs(y),n+abs(x));        end;      end;    end;    for i:=1 to n*2 do    if not v[i] then    begin      v[i]:=true;      tarjan(i);    end;    fff:=true;    check;    if fff then    writeln('1') else    writeln('0');    readln;  end;end.

0 0