POJ - 3905 Perfect Election(2-SAT)
来源:互联网 发布:mysql #什么意思 编辑:程序博客网 时间:2024/04/30 17:26
题目大意:题意挺好理解的
解题思路:就按照题意来建边就可以了
#include <cstdio>#include <cstring>#include <vector>using namespace std;const int MAXNODE = 2010;vector<int> G[MAXNODE];bool mark[MAXNODE * 2];int Stack[MAXNODE * 2];int n, m, top;void AddClause(int x, int xval, int y, int yval) { x = x * 2 + xval; y = y * 2 + yval; G[x].push_back(y);}void init() { n++; for (int i = 0; i < 2 * n; i++) G[i].clear(); memset(mark, 0, sizeof(mark)); char s1[15], s2[15]; char sign1, sign2; int x, valx, y, valy; for (int i = 0; i < m; i++) { scanf("%s%s", s1, s2); sscanf(s1, "%c%d", &sign1, &x); sscanf(s2, "%c%d", &sign2, &y); valx = (sign1 == '+' ? 1 : 0); valy = (sign2 == '+' ? 1 : 0); AddClause(x, valx ^ 1, y, valy); AddClause(y, valy ^ 1, x, valx); }}bool dfs(int u) { if (mark[u ^ 1]) return false; if (mark[u]) return true; mark[u] = true; Stack[++top] = u; for (int i = 0; i < G[u].size(); i++) if (!dfs(G[u][i])) return false; return true;}void solve() { for (int i = 2; i < 2 * n; i += 2) { if (!mark[i] && !mark[i^1]) { top = 0; if (!dfs(i)) { while (top) mark[Stack[top--]] = false; if (!dfs(i ^ 1)) { printf("0\n"); return ; } } } } printf("1\n");}int main() { while (scanf("%d%d", &n, &m) != EOF) { init(); solve(); } return 0;}
0 0
- POJ 3905 - Perfect Election 简单2-sat
- POJ 3905 Perfect Election(简单2-SAT)
- 【POJ】3905 Perfect Election 2-sat
- POJ 3905 Perfect Election(2-sat)
- POJ 3905 Perfect Election(2-sat)
- poj 3905 Perfect Election 2-sat
- POJ - 3905 Perfect Election(2-SAT)
- POJ 3905 Perfect Election(2-SAT)
- poj--3905--Perfect Election(2-sat)
- poj 3905 Perfect Election(2-SAT判断简单)
- Poj 3905 Perfect Election(2-sat 可行性判定)
- POJ 3905 Perfect Election (初学2-Sat)
- poj 3905 Perfect Election 【2-sat 简单建图】
- 2-SAT:以 POJ 3905 - Perfect Election 为例
- Poj 3905 Perfect Election【2-SAT-------Tarjan强连通】
- POJ 3905Perfect Election 2-sat判断可行解
- poj3905 Perfect Election (2-SAT)
- POJ 3905 Perfect Election
- iPhone/iPad开发模拟器(Simulator)截屏
- 设置VMware随系统开机自动启动并引导虚拟机操作系统
- div嵌套时背景图片不显示问题解决方法总结
- PHP采集利器 Snoopy 试用心得
- 单独的刷新验证码的方法
- POJ - 3905 Perfect Election(2-SAT)
- ORACLE TO_CHAR函数详解及其他格式转换说明
- 在线教学系统:C#实现网络电子白板、课件功能
- 软件杂谈《六》
- VC模拟文件拖拽
- eclipse 中文注释乱码问题的解决方法
- 第一讲 Objective-C开发入门
- 数据结构--单向链表(c++)
- 设计模式之原型模式