poj--3905--Perfect Election(2-sat)
Time Limit: 5000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
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:
The input data are separated by white spaces, terminate with an end of file, and are correct.
Accepted answers to the poll question | Encoding |
I would be happy if at least one from i and j is elected. | +i +j |
I would be happy if at least one from i and j is not elected. | -i -j |
I would be happy if i is elected or j is not elected or both events happen. | +i -j |
I 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
1 1 0 1
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.
Source
+i +j i,j至少选一个
-i -j i和j至多一个
+i -j 选i和不选j不同时发生,也就是发生一个就好
-i +j 选j和不选i不同时发生
#include<stdio.h> #include<string.h> #include<queue> #include<stack> #include<vector> #include<algorithm> using namespace std; #define MAX 10000+10 int low[MAX],dfn[MAX]; int sccno[MAX]; int dfs_clock,scc_cnt; bool Instack[MAX]; stack<int>s; vector<int>G[MAX]; int m,n; void init() { for(int i=1;i<=2*n;i++) G[i].clear(); } void getmap() { while(m--) { char a,b; int i,j; scanf(" %c%d %c%d",&a,&i,&b,&j); if(a=='+'&&b=='+') { G[i+n].push_back(j); G[j+n].push_back(i); //G[i].push_back(j); } else if(a=='-'&&b=='-') { G[i].push_back(j+n); G[j].push_back(i+n); //G[i+n].push_back(j+n); } else if(a=='+'&&b=='-') { G[i+n].push_back(j+n); G[j].push_back(i); //G[i+n].push_back(j); } else { G[i].push_back(j); G[j+n].push_back(i+n); //G[i].push_back(j+n); } } } void tarjan(int u,int fa) { int v; low[u]=dfn[u]=++dfs_clock; Instack[u]=true; s.push(u); for(int i=0;i<G[u].size();i++) { v=G[u][i]; if(!dfn[v]) { tarjan(v,u); low[u]=min(low[u],low[v]); } else if(Instack[v]) low[u]=min(low[u],dfn[v]); } if(low[u]==dfn[u]) { ++scc_cnt; for(;;) { v=s.top(); s.pop(); Instack[v]=false; sccno[v]=scc_cnt; if(v==u) break; } } } void find(int l,int r) { memset(low,0,sizeof(low)); memset(dfn,0,sizeof(dfn)); memset(sccno,0,sizeof(sccno)); memset(Instack,false,sizeof(Instack)); for(int i=l;i<=r;i++) if(!dfn[i]) tarjan(i,-1); } void solve() { for(int i=1;i<=n;i++) { if(sccno[i]==sccno[i+n]) { printf("0\n"); return ; } } printf("1\n"); } int main() { while(scanf("%d%d",&n,&m)!=EOF) { init(); getmap(); find(1,2*n); solve(); } return 0; }