题目描述
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.
输入格式
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 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.
输出格式
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.
输入样例
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输出样例
1
1
0
1
分析
题目大意一共有n个人,m条民意,每条民意满足以下某种情况:
民意 | 格式 |
---|---|
若 i 与 j 至少一人当选,我会高兴 | +i +j |
若 i 与 j 至少一人不当选,我会高兴 | -i -j |
若 i 当选或 j 不当选,我会高兴 | +i -j |
若 i 不当选或 j 当选,我会高兴 | -i +j |
在这道题中,我们假设1表示当选,0表示不当选。那么对于被选举人i,如果他当选,则有<i+n,i>;如果他不当选,则有<i,i+n>。
那么对于如上四种情况,有:
若 i 与 j 至少一人当选:
- i不当选,j一定当选:<i+n,j>。
- j不当选,i一定当选:<j+n,i>。
若 i 与 j 至少一人不当选:
- i当选,j一定不当选:<i,j+n>。
- j当选,i一定不当选:<j,i+n>。
若 i 当选或 j 不当选:
- i不当选,j一定不当选:<i+n,j+n>。
- j当选,i一定当选:<j,i>。
若 i 不当选或 j 当选:
- i当选,j一定当选:<i,j>。
- j不当选,i一定不当选:<j+n,i+n>。
综上,按照上述原则进行建图,然后用tarjan算法求强连通分量,判断是否存在被选举人i满足强连通编号scc[i]==scc[i+n],有则输出0,否则输出1。
源程序
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <stack>
#define MAXN 2005
#define MAXM 1000005
using namespace std;
struct Edge{
int v,next;
Edge(){}
Edge(int v,int next):v(v),next(next){}
}edge[MAXM*2];
int EdgeCount,head[MAXN];
int n,m,block_cnt,scc_cnt;
int dfn[MAXN],low[MAXN],scc[MAXN];
bool vis[MAXN];
stack<int> s;
void addEdge(int u,int v)
{
edge[++EdgeCount]=Edge(v,head[u]);
head[u]=EdgeCount;
}
void init()
{
memset(head,0,sizeof(head));
memset(dfn,0,sizeof(dfn));
memset(scc,0,sizeof(scc));
memset(vis,false,sizeof(vis));
EdgeCount=block_cnt=scc_cnt=0;
}
void tarjan(int u)
{
dfn[u]=low[u]=++block_cnt; //时间戳
vis[u]=true; //标记入栈
s.push(u);
for(int i=head[u];i;i=edge[i].next){
int v=edge[i].v;
if(!dfn[v]){ //还没访问过
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v]) //访问过且还在栈中
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u]){ //满足强连通条件
scc_cnt++;
while(1){
int tmp=s.top();s.pop();
scc[tmp]=scc_cnt; //记录所属强连通分量编号
vis[tmp]=false; //标记出栈
if(tmp==u)break;
}
}
}
bool TwoSat()
{
for(int i=1;i<=2*n;i++) //缩点
if(!dfn[i])
tarjan(i);
for(int i=1;i<=n;i++)
if(scc[i]==scc[i+n]) //矛盾
return false;
return true;
}
int main()
{
while(~scanf("%d%d",&n,&m)){
init();
for(int i=1;i<=m;i++){
int a,b;
char op1,op2;
scanf(" %c%d %c%d",&op1,&a,&op2,&b);
if(op1=='+'&&op2=='+'){ //至少选一人
addEdge(a+n,b); //不选a,就选b
addEdge(b+n,a); //不选b,就选a
}
else if(op1=='-'&&op2=='-'){ //至少不选一人
addEdge(a,b+n); //选a,就不选b
addEdge(b,a+n); //选b,就不选a
}
else{ //一人选,一人不选
if(a==b)continue;
if(op1=='+'){ //条件3
addEdge(a+n,b+n); //不选a,就不选b
addEdge(b,a); //选b,就选a
}
else{ //条件4
addEdge(a,b); //选a,就选b
addEdge(b+n,a+n); //选不b,就不选a
}
}
}
if(TwoSat())
printf("1\n");
else
printf("0\n");
}
}