POJ 3905 Perfect Election (2-Sat)
|
Perfect Election
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. 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 Sample Output 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 |
大致题意:
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; const int VM=;
const int EM=; struct Edge{
int to,nxt;
}edge[EM<<]; int n,m,cnt,dep,top,atype,head[VM];
int dfn[VM],low[VM],vis[VM],belong[VM];
int stack[VM]; void Init(){
cnt=, atype=, dep=, top=;
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
memset(belong,,sizeof(belong));
} void addedge(int cu,int cv){
edge[cnt].to=cv; edge[cnt].nxt=head[cu]; head[cu]=cnt++;
} void Tarjan(int u){
dfn[u]=low[u]=++dep;
stack[top++]=u;
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].nxt){
int v=edge[i].to;
if(!dfn[v]){
Tarjan(v);
low[u]=min(low[u],low[v]);
}else if(vis[v])
low[u]=min(low[u],dfn[v]);
}
int j;
if(dfn[u]==low[u]){
atype++;
do{
j=stack[--top];
belong[j]=atype;
vis[j]=;
}while(u!=j);
}
} int abs(int x){
return x<?-x:x;
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d",&n,&m)){
Init();
int u,v;
for(int i=;i<m;i++){
scanf("%d%d",&u,&v);
int a=abs(u), b=abs(v);
if(u> && v>){
addedge(a+n,b);
addedge(b+n,a);
}
if(u< && v<){
addedge(a,b+n);
addedge(b,a+n);
}
if(u> && v<){
addedge(a+n,b+n);
addedge(b,a);
}
if(u< && v>){
addedge(a,b);
addedge(b+n,a+n);
}
}
for(int i=;i<=*n;i++)
if(!dfn[i])
Tarjan(i);
int ans=;
for(int i=;i<=n;i++)
if(belong[i]==belong[i+n]){
ans=;
break;
}
printf("%d\n",ans);
}
return ;
}
POJ 3905 Perfect Election (2-Sat)的更多相关文章
- POJ 3905 Perfect Election(2-sat)
POJ 3905 Perfect Election id=3905" target="_blank" style="">题目链接 思路:非常裸的 ...
- POJ 3678 Katu Puzzle(2 - SAT) - from lanshui_Yang
Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a ...
- poj 1543 Perfect Cubes(注意剪枝)
Perfect Cubes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14901 Accepted: 7804 De ...
- POJ 3905 Perfect Election
2-SAT 裸题,搞之 #include<cstdio> #include<cstring> #include<cmath> #include<stack&g ...
- POJ 3905 Perfect Election (2-SAT 判断可行)
题意:有N个人参加选举,有M个条件,每个条件给出:i和j竞选与否会只要满足二者中的一项即可.问有没有方案使M个条件都满足. 分析:读懂题目即可发现是2-SAT的问题.因为只要每个条件中满足2个中的一个 ...
- POJ 3398 Perfect Service(树型动态规划,最小支配集)
POJ 3398 Perfect Service(树型动态规划,最小支配集) Description A network is composed of N computers connected by ...
- Luogu 1894 [USACO4.2]完美的牛栏The Perfect Stall / POJ 1274 The Perfect Stall(二分图最大匹配)
Luogu 1894 [USACO4.2]完美的牛栏The Perfect Stall / POJ 1274 The Perfect Stall(二分图最大匹配) Description 农夫约翰上个 ...
- POJ 2376 Cleaning Shifts(轮班打扫)
POJ 2376 Cleaning Shifts(轮班打扫) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] Farmer ...
- POJ 3253 Fence Repair(修篱笆)
POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
随机推荐
- 如何启动docker service
From powershell prompt following works for me with no issues restart-service *docker* [注意] 我试了一下,这个命 ...
- GPUImage API文档之GLProgram类
GLProgram是GPUImage中代表openGL ES 中的program,具有glprogram功能. 属性 @property(readwrite, nonatomic) BOOL init ...
- 浅谈压缩感知(十四):傅里叶矩阵与小波变换矩阵的MATLAB实现
主要内容: 傅里叶矩阵及其MATLAB实现 小波变换矩阵及其MATLAB实现 傅里叶矩阵及其MATLAB实现 傅里叶矩阵的定义:(来源: http://mathworld.wolfram.com/F ...
- JAVA Eclipse如何导入已有的项目
File-Import,然后在弹出的窗口中输入exit,会自动提示下面的选项(已存在的项目) 把项目源代码放到Eclipse的工作目录,然后找到 导入完成
- SuperMap打包部署要点
折腾了一段时间,终于要发布一个版本了,但SuperMap程序怎么发布呢,需要些什么必要条件呢?本来想问问超图的技术人员的,但都没人理我,估计都去开大会去了. 下面是自己测试出来的结果,主要是根据Sup ...
- android中使用spinner组件,以key,value的方式
接着上一篇文章的内容:android中使用spinner组件 稍做修改,以key,value的方式,在实际使用中,经常需要获取的值并不一定跟显示的内容一致. 需要先添加一个对象类,用来描述key,va ...
- Linux下使用Nohup后台运行程序
一.不输出日志的命令 #!/bin/bash echo "starting nohup..." nohup dotnet ./mynetcoreapp.dll > /dev/ ...
- LNMP一键安装包-CentOS/Ubuntu/Debian自动安装Nginx,MySQL,PHP
适用环境: 系统支持:CentOS.Ubuntu.Debian 内存要求:≥128M 安装了什么: 1.Nginx-1.2.1 2.MySQL 5.5.25 3.PHP 5.2.17或PHP 5.3. ...
- linux CPU占用率高(转)
来自:http://www.cnitblog.com/houcy/archive/2012/11/28/86801.html 1.用top命令查看哪个进程占用CPU高 gateway网关进程14094 ...
- 算法笔记_232:提取拼音首字母(Java)
目录 1 问题描述 2 解决方案 1 问题描述 在很多软件中,输入拼音的首写字母就可以快速定位到某个词条.比如,在铁路售票软件中,输入: “bj”就可以定位到“北京”.怎样在自己的软件中实现这个功 ...