POJ 3905 Perfect Election
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的更多相关文章
- POJ 3905 Perfect Election(2-sat)
POJ 3905 Perfect Election id=3905" target="_blank" style="">题目链接 思路:非常裸的 ...
- POJ 3905 Perfect Election (2-Sat)
Perfect Election Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 438 Accepted: 223 De ...
- POJ 3905 Perfect Election (2-SAT 判断可行)
题意:有N个人参加选举,有M个条件,每个条件给出:i和j竞选与否会只要满足二者中的一项即可.问有没有方案使M个条件都满足. 分析:读懂题目即可发现是2-SAT的问题.因为只要每个条件中满足2个中的一个 ...
- 图论--2-SAT--POJ 3905 Perfect Election
Perfect Election Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 964 Acce ...
- POJ 3398 Perfect Service(树型动态规划,最小支配集)
POJ 3398 Perfect Service(树型动态规划,最小支配集) Description A network is composed of N computers connected by ...
- OpenJudge 2810(1543) 完美立方 / Poj 1543 Perfect Cubes
1.链接地址: http://bailian.openjudge.cn/practice/2810/ http://bailian.openjudge.cn/practice/1543/ http:/ ...
- POJ 3398 Perfect Service --最小支配集
题目链接:http://poj.org/problem?id=3398 这题可以用两种上述讲的两种算法解:http://www.cnblogs.com/whatbeg/p/3776612.html 第 ...
- poj 1543 Perfect Cubes(注意剪枝)
Perfect Cubes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14901 Accepted: 7804 De ...
- POJ 1730 Perfect Pth Powers(暴力枚举)
题目链接: https://cn.vjudge.net/problem/POJ-1730 题目描述: We say that x is a perfect square if, for some in ...
随机推荐
- sort函数使用的基本知识
STL中就自带了排序函数sortsort 对给定区间所有元素进行排序 要使用此函数只需用#include <algorithm> sort即可使用,语法描述为:sort(begin,en ...
- LD_LIBRARY_PATH vs LIBRARY_PATH
LIBRARY_PATH is used by gcc before compilation to search for directories containing libraries that n ...
- Entity Framework技巧系列之十二 - Tip 46 - 50
提示46. 怎样使用Code-Only排除一个属性 这次是一个真正简单的问题,由StackOverflow上这个问题引出. 问题: 当我们使用Code-Only把一个类的信息告诉Entity F ...
- HDU1686:Oulipo
Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...
- 【Python爬虫实战--1】深入理解urllib;urllib2;requests
摘自:http://1oscar.github.io/blog/2015/07/05/%E6%B7%B1%E5%85%A5%E7%90%86%E8%A7%A3urllib;urllib2;reques ...
- postgres-xl 集体搭建(2)
$ pgxc_ctl prepare config minimal 生成一个单机伪分布集群配置简单模板 根据该模板进行简单修改就可以配置成一个集群 $ pgxc_ctl init all $ pgxc ...
- 贪心+树状数组维护一下 Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D
http://codeforces.com/contest/724/problem/D 题目大意:给你一个串,从串中挑选字符,挑选是有条件的,按照这个条件所挑选出来的字符集合sort一定是最后选择当中 ...
- HDU 2063 最大匹配的基础题
中文题,题目大意不说了. 思路:就是寻找最大匹配,最大匹配就是每次找增广路,如果存在增广,那就把增广路上面的边全部都翻转即可.这样说明能多匹配一个,+1即可. //看看会不会爆int!数组会不会少了一 ...
- oomph
http://blog.csdn.net/u011004037/article/details/45679573 这么好个功能起了这么操蛋个名字害得老子一直不知道他干啥的
- 省市便利 UIPicherView
@property (strong,nonatomic) UIPickerView *pickerV; @property (strong,nonatomic) NSArray *arr; @prop ...