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. mysql常用命令使用技巧

    一.连接Mysql格式: mysql -h主机地址 -u用户名 -p用户密码 1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root - ...

  2. hdu_4826_Labyrinth_2014百度之星(dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4826 题意:中文题,不解释 题解:dp搞,第一列只能从上往下走,所以先算出第一列的dp数组,然后开两个 ...

  3. vb脚本自动更新版本信息

    使用的串口显示软件为secureCrt,支持脚本功能,今天写了一个简单的软件升级脚本(VB脚本). 如下: # $language = "VBScript" # $interfac ...

  4. IIS7禁止后台访问

    设置只能内网访问 1.添加允许内网访问规则 2.编辑功能设置

  5. linuxmint卸载软件

    删除你已经卸载掉的软件包的命令为 sudo apt-get autoclean 还有一类软件包,我们每个人都应该删除,那就是你已经卸载了,但是一些只有它依赖而别的软件包都不需要的软件包还留在你的系统里 ...

  6. iptables防火墙详解

    iptables常用命令 iptables -nv -L 查看iptables列表 iptables -F 清空iptables规则 iptables-save > /etc/sysconfig ...

  7. vs 发布web应用程序时,找不到cs文件错误

    将*.aspx.*.ascx.*.master所有出错页面文件中的 CodeFile="******.aspx.cs" 批量替换成 Codebehind="******. ...

  8. PopUpManager弹出窗口

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...

  9. sinaBlog中小知识总结

    1.上拉刷新的时候记得移除老的数据(同样应用于其他地方) if (vc.currentPage == 1) { //上拉加载 记得移除新的 [vc.totalArr removeAllObjects] ...

  10. Anton and School

    Anton and School time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...