Perfect Election
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 438   Accepted: 223

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 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

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

Sample Output

  1. 1
  2. 1
  3. 0
  4. 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

大致题意:

    有n个候选人,m组要求,每组要求关系到候选人中的两个人,“+i +j”代表i和j中至少有一人被选中,“-i -j”代表i和j中至少有一人不被选中。“+i -j”代表i被选中和j不被选中这两个事件至少发生一个,“-i +j”代表i不被选中和j被选中这两个事件至少发生一个。问是否存在符合所有m项要求的方案存在。
 
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4.  
  5. using namespace std;
  6.  
  7. const int VM=;
  8. const int EM=;
  9.  
  10. struct Edge{
  11. int to,nxt;
  12. }edge[EM<<];
  13.  
  14. int n,m,cnt,dep,top,atype,head[VM];
  15. int dfn[VM],low[VM],vis[VM],belong[VM];
  16. int stack[VM];
  17.  
  18. void Init(){
  19. cnt=, atype=, dep=, top=;
  20. memset(head,-,sizeof(head));
  21. memset(vis,,sizeof(vis));
  22. memset(low,,sizeof(low));
  23. memset(dfn,,sizeof(dfn));
  24. memset(belong,,sizeof(belong));
  25. }
  26.  
  27. void addedge(int cu,int cv){
  28. edge[cnt].to=cv; edge[cnt].nxt=head[cu]; head[cu]=cnt++;
  29. }
  30.  
  31. void Tarjan(int u){
  32. dfn[u]=low[u]=++dep;
  33. stack[top++]=u;
  34. vis[u]=;
  35. for(int i=head[u];i!=-;i=edge[i].nxt){
  36. int v=edge[i].to;
  37. if(!dfn[v]){
  38. Tarjan(v);
  39. low[u]=min(low[u],low[v]);
  40. }else if(vis[v])
  41. low[u]=min(low[u],dfn[v]);
  42. }
  43. int j;
  44. if(dfn[u]==low[u]){
  45. atype++;
  46. do{
  47. j=stack[--top];
  48. belong[j]=atype;
  49. vis[j]=;
  50. }while(u!=j);
  51. }
  52. }
  53.  
  54. int abs(int x){
  55. return x<?-x:x;
  56. }
  57.  
  58. int main(){
  59.  
  60. //freopen("input.txt","r",stdin);
  61.  
  62. while(~scanf("%d%d",&n,&m)){
  63. Init();
  64. int u,v;
  65. for(int i=;i<m;i++){
  66. scanf("%d%d",&u,&v);
  67. int a=abs(u), b=abs(v);
  68. if(u> && v>){
  69. addedge(a+n,b);
  70. addedge(b+n,a);
  71. }
  72. if(u< && v<){
  73. addedge(a,b+n);
  74. addedge(b,a+n);
  75. }
  76. if(u> && v<){
  77. addedge(a+n,b+n);
  78. addedge(b,a);
  79. }
  80. if(u< && v>){
  81. addedge(a,b);
  82. addedge(b+n,a+n);
  83. }
  84. }
  85. for(int i=;i<=*n;i++)
  86. if(!dfn[i])
  87. Tarjan(i);
  88. int ans=;
  89. for(int i=;i<=n;i++)
  90. if(belong[i]==belong[i+n]){
  91. ans=;
  92. break;
  93. }
  94. printf("%d\n",ans);
  95. }
  96. return ;
  97. }

POJ 3905 Perfect Election (2-Sat)的更多相关文章

  1. POJ 3905 Perfect Election(2-sat)

    POJ 3905 Perfect Election id=3905" target="_blank" style="">题目链接 思路:非常裸的 ...

  2. 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 ...

  3. poj 1543 Perfect Cubes(注意剪枝)

    Perfect Cubes Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14901   Accepted: 7804 De ...

  4. POJ 3905 Perfect Election

    2-SAT 裸题,搞之 #include<cstdio> #include<cstring> #include<cmath> #include<stack&g ...

  5. POJ 3905 Perfect Election (2-SAT 判断可行)

    题意:有N个人参加选举,有M个条件,每个条件给出:i和j竞选与否会只要满足二者中的一项即可.问有没有方案使M个条件都满足. 分析:读懂题目即可发现是2-SAT的问题.因为只要每个条件中满足2个中的一个 ...

  6. POJ 3398 Perfect Service(树型动态规划,最小支配集)

    POJ 3398 Perfect Service(树型动态规划,最小支配集) Description A network is composed of N computers connected by ...

  7. 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 农夫约翰上个 ...

  8. POJ 2376 Cleaning Shifts(轮班打扫)

    POJ 2376 Cleaning Shifts(轮班打扫) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] Farmer ...

  9. POJ 3253 Fence Repair(修篱笆)

    POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS   Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...

随机推荐

  1. java根据图片路径下载到服务器方案 (转)

    http://www.cnblogs.com/thinkingandworkinghard/articles/5589484.html 平常做的工作中,有一部分是同步数据的.但是同步的过程中碰到个问题 ...

  2. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(三)安装spark2.2.1

    如何搭建配置centos虚拟机请参考<Kafka:ZK+Kafka+Spark Streaming集群环境搭建(一)VMW安装四台CentOS,并实现本机与它们能交互,虚拟机内部实现可以上网.& ...

  3. wifidog 源码初分析(2)-转

    上一篇分析了接入设备的首次浏览器访问请求如何通过 防火墙过滤规则 重定向到 wifidog 的 HTTP 服务中,本篇主要分析了 wifidog 在接收到 接入设备的 HTTP 访问请求后,如何将此 ...

  4. Python防止sql注入

    看了网上文章,说的都挺好的,给cursor.execute传递格式串和参数,就能防止注入,但是我写了代码,却死活跑不通,怀疑自己用了一个假的python 最后,发现原因可能是不同的数据库,对于字符串的 ...

  5. template.helper 多参数

    <script type="text/html" id="text4"> {{detail name classInfo schoolInfo}} ...

  6. 【转】application.properties 常见配置

    Various properties can be specified inside your application.properties/application.yml file or as co ...

  7. iOS开发技巧 - 使用UIPickerView来选择值

    (Swift) import UIKit class ViewController: UIViewController, UIPickerViewDataSource { var picker: UI ...

  8. 保存html代码

    function svcode(F) { if (document.all) { var F = $id(F); var E = window.open("", "_bl ...

  9. Android 之 沉浸式状态栏及顶部状态栏背景色设置

    现在很多应用都引用了沉浸式状态栏,如QQ,效果下图: 效果很酷炫,其实设置也很简单.但是,需要注意的是,这种效果只能在API19以及以上版本中才能够做到. 方法一: 首先,如果想让界面Activity ...

  10. Jenkins知识地图

    转自:http://blog.csdn.net/feiniao1221/article/details/10259449 这篇文章大概写于三个月前,当时写了个大纲列表,但是在CSDN上传资源实在不方便 ...