Perfect Election

Time Limit: 5000MS         Memory Limit: 65536K

Total Submissions: 964         Accepted: 431

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

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.

题意:有N个候选人,给出M个限制条件。这些条件可以分成4类

1,+i +j 表示 i 和 j 至少选一个;

2,-i  -j 表示 i 和 j 最多选一个;

3,+i -j 表示 选i 和 不选j 最少成立一个 ;

4,-i +j 表示 不选i 和 选j 最少成立一个;

问你有没有一种方案满足M个条件。

建图: 我用Ai表示i 被选上,!Ai表示i没有被选上。

对于1则有  非Ai -> Aj 和 非Aj -> Ai

对于2则有  Ai -> 非Aj 和 Aj -> 非Ai

对于3则有  Aj -> Ai 和 非Ai -> 非Aj

对于4则有  非Aj -> 非Ai 和 Ai -> Aj

记住三个符号六种基本建图方式,剩下的都可以修改:比如上面的4代表Ai and 非Aj=1,我们就是非Aj建图加上个Ai and Aj建图,自己画个图想想是不是?地址

AC代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define MAXN 2000+10
#define INF 1000000
#define eps 1e-5
using namespace std;
vector<int> G[MAXN];
int low[MAXN], dfn[MAXN];
int dfs_clock;
int sccno[MAXN], scc_cnt;
stack<int> S;
bool Instack[MAXN];
int N, M;
void init()
{
    for(int i = 1; i <= 2*N; i++) G[i].clear();
}
void getMap()
{
    int i, j;
    char op1, op2;
    while(M--)
    { 
        scanf("  %c%d %c%d", &op1, &i, &op2, &j);
        if(op1 == '+' && op2 == '+')
        {
            G[i + N].push_back(j);
            G[j + N].push_back(i);
        } 
        else if(op1 == '-' && op2 == '-')
        {
            G[i].push_back(j + N);
            G[j].push_back(i + N);
        }
        else if(op1 == '+' && op2 == '-')
        {
            G[i + N].push_back(j + N);//i若没有被选上 j一定没有被选上 
            G[j].push_back(i);//j被选上 i一定被选上 
        }
        else
        {
            G[i].push_back(j);//i被选上 j一定被选上 
            G[j + N].push_back(i + N);//j没有被选上 i一定没有被选上
        }
    }
}
void tarjan(int u, int fa)
{
    int v;
    low[u] = dfn[u] = ++dfs_clock;
    S.push(u);
    Instack[u] = true;
    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();
            sccno[v] = scc_cnt;
            Instack[v] = false;
            if(v == u) break;
        }
    }
}
void find_cut(int l, int r)
{
    memset(low, 0, sizeof(low));
    memset(dfn, 0, sizeof(dfn));
    memset(sccno, 0, sizeof(sccno));
    memset(Instack, false, sizeof(Instack));
    dfs_clock = scc_cnt = 0;
    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_cut(1, 2*N);
        solve();
    }
    return 0;
}

图论--2-SAT--POJ 3905 Perfect Election的更多相关文章

  1. POJ 3905 Perfect Election(2-sat)

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

  2. POJ 3905 Perfect Election (2-Sat)

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

  3. POJ 3905 Perfect Election

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

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

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

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

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

  6. OpenJudge 2810(1543) 完美立方 / Poj 1543 Perfect Cubes

    1.链接地址: http://bailian.openjudge.cn/practice/2810/ http://bailian.openjudge.cn/practice/1543/ http:/ ...

  7. POJ 3398 Perfect Service --最小支配集

    题目链接:http://poj.org/problem?id=3398 这题可以用两种上述讲的两种算法解:http://www.cnblogs.com/whatbeg/p/3776612.html 第 ...

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

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

  9. POJ 1730 Perfect Pth Powers(暴力枚举)

    题目链接: https://cn.vjudge.net/problem/POJ-1730 题目描述: We say that x is a perfect square if, for some in ...

随机推荐

  1. 2017蓝桥杯Excel地址(C++C组)

    题目:Excel地址Excel单元格的地址表示很有趣,它使用字母来表示列号.比如,A表示第1列,B表示第2列,Z表示第26列,AA表示第27列,AB表示第28列,BA表示第53列,....当然Exce ...

  2. android所有颜色

    <?xml version="1.0" encoding="utf-8" ?> <resources> <color name=& ...

  3. Docker php安装扩展步骤详解

    前言 此篇,主要是演示docker-php-source , docker-php-ext-install ,docker-php-enable-docker-configure 这四个命令到底是用来 ...

  4. svg 实践之屏幕坐标与svg元素坐标转换

    近期在做svg相关项目,很好用的东西要记下来: 1.基础知识就是根据 矩阵进行坐标转换,如下: : 屏幕坐标 = 矩阵* svg对象坐标 2.javascript有个方法用于获取 svg对象 的转换矩 ...

  5. 提高万恶的KPI,切忌要避开这六个低效的编程习惯

    作者:程序员小跃 Slogan:当你的才华还无法撑起你的野心时,那应该静下心来好好学习 上次的翻译,引起了很大的反响,大家都想知道自己和高级工程师的差距,看了我的文章,是不是都在默默地做着比较呢?如果 ...

  6. 068.Python框架Django之DRF视图集使用

    一 视图集与路由的使用 使用视图集ViewSet,可以将一系列逻辑相关的动作放到一个类中: list() 提供一组数据 retrieve() 提供单个数据 create() 创建数据 update() ...

  7. 教你如何入手用python实现简单爬虫微信公众号并下载视频

    主要功能 如何简单爬虫微信公众号 获取信息:标题.摘要.封面.文章地址 自动批量下载公众号内的视频 一.获取公众号信息:标题.摘要.封面.文章URL 操作步骤: 1.先自己申请一个公众号 2.登录自己 ...

  8. Python算法题:金字塔

    代码如下: #Python金字塔练习 """ 最大层数:max_level 当前层数:current_level 金字塔正序时: 每层的空格=最大层数-当前层数 每层的星 ...

  9. [转载]绕过CDN查找真实IP方法总结

    前言 类似备忘录形式记录一下,这里结合了几篇绕过CDN寻找真实IP的文章,总结一下绕过CDN查找真实的IP的方法 介绍 CDN的全称是Content Delivery Network,即内容分发网络. ...

  10. XSS语义分析的阶段性总结(一)

    本文作者:Kale 前言 由于X3Scan的研发已经有些进展了,所以对这一阶段的工作做一下总结!对于X3Scan的定位,我更加倾向于主动+被动的结合.主动的方面主要体现在可以主动抓取页面链接并发起请求 ...