2-SAT 裸题,搞之

#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std; const int maxn=+;
int N,M;
char s1[],s2[]; stack<int>S;
vector<int>G[maxn];
vector<int>FG[maxn];
int Belong[maxn];
int flag[maxn];
int Block; void init()
{
for(int i=; i<maxn; i++) G[i].clear();
for(int i=; i<maxn; i++) FG[i].clear();
memset(Belong,,sizeof Belong);
memset(flag,,sizeof flag);
while(!S.empty()) S.pop();
Block=;
} void addEgde(int x,int y)
{
G[x].push_back(y);
FG[y].push_back(x);
} void dfs1(int now)
{
flag[now]=;
for(int i=; i<G[now].size(); i++)
if(!flag[G[now][i]])
dfs1(G[now][i]);
S.push(now);
} void dfs2(int now)
{
Belong[now]=Block;
for(int i=; i<FG[now].size(); i++)
if(!Belong[FG[now][i]])
dfs2(FG[now][i]);
} bool judge()
{
for(int i=; i<*N; i++) if(!flag[i]) dfs1(i);
while(!S.empty())
{
int Top=S.top();
S.pop();
if(!Belong[Top])
{
Block++;
dfs2(Top);
}
}
for(int i=; i<N; i++)
if(Belong[*i]==Belong[*i+])
return ;
return ;
} int main()
{
while(~scanf("%d%d",&N,&M))
{
init();
for(int i=;i<=M;i++)
{
scanf("%s%s",s1,s2);
int num1=,num2=;
for(int i=;i<strlen(s1);i++) num1=num1*+s1[i]-'';
for(int i=;i<strlen(s2);i++) num2=num2*+s2[i]-''; num1--;num2--; if(s1[]=='+'&&s2[]=='+')
{
addEgde(*num1,*num2+);
addEgde(*num2,*num1+);
}
if(s1[]=='-'&&s2[]=='-')
{
addEgde(*num1+,*num2);
addEgde(*num2+,*num1);
}
if(s1[]=='+'&&s2[]=='-')
{
addEgde(*num1,*num2);
addEgde(*num2+,*num1+);
}
if(s1[]=='-'&&s2[]=='+')
{
addEgde(*num1+,*num2+);
addEgde(*num2,*num1);
}
}
if(judge()) printf("1\n");
else printf("0\n");
}
return ;
}

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 判断可行)

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

  4. 图论--2-SAT--POJ 3905 Perfect Election

    Perfect Election Time Limit: 5000MS         Memory Limit: 65536K Total Submissions: 964         Acce ...

  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. 你所不知道的mybatis居然也有拦截器

    对于mybatis的拦截器这个想法我来自于三个地方 也就是下面这个三个地方是可以使用的,其他的情况需要开发人员根据实际情况来使用. 1.对于分页的查询,我们可以对于分页的方法采用比较规范的命名,然后根 ...

  2. 1213 How Many Tables 简单的并查集问题

    my code: #include <cstdio>#include <cstring>#include<iostream>using namespace std; ...

  3. 在C#中使用反射调用internal的方法

    MSDN上解释Internal如下: The internal keyword is an access modifier for types and type members. Internal t ...

  4. Java-if 嵌套结构

    import java.util.Scanner; public class ifs{ public static void main(String args[]){ Scanner in=new S ...

  5. HAProxy与varnish

    Even if HAProxy can do TCP proxying, it is often used in front of web application, exactly where we ...

  6. DataBinding

    <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http ...

  7. Hibernate关于openSession和getCurrentSession的理解

    来源(转载):http://blog.csdn.net/woshisap/article/details/7024482 1:getCurrentSession会把Session和当前的线程关联起来, ...

  8. Spring Boot 系列教程3-MyBatis

    MyBatis MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache 迁移到了google code,并且改名为MyBatis .2013年11月迁移到Git ...

  9. php fsockopen

    1.PHP fsockopen函数说明: Open Internet or Unix domain socket connection(打开套接字链接) Initiates a socket conn ...

  10. UIGestureRecognizer 手势浅析

    目录[-] iOS开发中的手势体系——UIGestureRecognizer分析及其子类的使用 一.引言 二.手势的抽象类——UIGestureRecognizer 1.统一的初始化方法 2.手势状态 ...