POJ-3648(2-SAT)
来源:互联网 发布:手机淘宝显示访问受限 编辑:程序博客网 时间:2024/06/07 08:57
题目:http://poj.org/problem?id=3648
第四道2-SAT,本来以为很简单的,没想到WA了一个晚上~~~~(>_<)~~~~ 。刚开始没考虑到bride和groom的通奸情况,然后想到到了又考虑错了以为bride不能看到那个情夫,看了discuss才知道原来bride看到情夫也没关系,毕竟bride看不到自己,真是给跪了。。。
#include <cstdio>#include <cctype>#include <cstring>#include <vector>#include <algorithm>using namespace std;#define MAX_N 1005int N, M;bool predefined[MAX_N * 2];//those who must sit at bride's sidevector<int> out[MAX_N * 4];bool mark[MAX_N * 4];//mark[2i] = 1, i sit at bride side; mark[2i+1] = 1, i sit at groom sideint stack[MAX_N * 4], cnt;inline void addEdge(int i, int j){//i can not sit in the same side with jint x = i << 1, y = j << 1;out[x].push_back(y+1);out[y+1].push_back(x);out[x+1].push_back(y);out[y].push_back(x+1);}inline void arrange(int i, int j){if(i > j) swap(i, j);int x = i << 1, y = j << 1;if(i == 0){//j must seat at bride's side so that bride can not see herpredefined[j] = true;}else if(i == 1){//it does not matter as the bride can no see herself}else{//i and j can not both sit at groom's sideout[x+1].push_back(y);out[y+1].push_back(x);}}inline int IDtoInt(const char* s){//husbands' ids are even, wives' ids are oddint id = 0;for(; isdigit(*s); ++s) id = id * 10 + *s - '0';if(*s == 'h') return id << 1;return id * 2 + 1;}inline char* IntToID(char* s, int n){if(n & 1) sprintf(s, "%dw", n >> 1);else sprintf(s, "%dh", n >> 1);return s;}void init(){memset(predefined, 0, N<<1);memset(mark, 0, N<<2);for(int i = N*4-1; i > -1; --i) out[i].clear();}void build(){int i, x;char l[8], r[8];for(i = 0; i < N; ++i){x = i << 1;addEdge(x, x + 1);}for(i = 0; i < M; ++i){//those can not sit at the same tablescanf("%s%s", l, r);arrange(IDtoInt(l), IDtoInt(r));}}bool dfs(int x){if(mark[x ^ 1]) return false;if(mark[x]) return true;mark[x] = true;stack[cnt++] = x;const vector<int>& v = out[x];for(int i = v.size() - 1; i > -1; --i){if(!dfs(v[i])) return false;}return true;}bool test(){int i, n = N << 1;if(!dfs(0 << 1 | 1)) return false;//groom must sit at groom's sideif(!dfs(1 << 1)) return false;//bride must sit at bride's side//those predefined must sit at bride's sidefor(i = 2; i < n; ++i){if(predefined[i] && !dfs(i << 1)) return false;}//normal 2-SATfor(i = 2; i < n; ++i){cnt = 0;if(!dfs(i << 1)){while(cnt) mark[stack[--cnt]] = false;if(!dfs(i << 1 | 1)) return false;}}return true;}void print(){int i, j, n = N << 1, sum = 0;char s[8];bool first = true;for(i = 2; i < n && sum < N; i += 2){//check if the husband or the wife should sit at the bride's sidej = i;if(!mark[i << 1]) j = i ^ 1;++sum;if(first) first = false;else putchar(' ');printf("%s", IntToID(s, j));}puts("");}int main(){while(scanf("%d%d", &N, &M), N){init();build();if(test()) print();else puts("bad luck");}return 0;}
0 0
- POJ-3648(2-SAT)
- 【2-SAT】POJ 3648
- POJ 3648 2-SAT
- POJ 3648 Wedding(2-SAT)
- poj 3648 Wedding(2-sat)
- poj 3648 Wedding(2-SAT)
- POJ 3648 Wedding(2-SAT)
- POJ 3648 Wedding(2-SAT)
- POJ 3648-Wedding(2-SAT)
- poj 3648 Wedding 2sat
- poj 3648 Wedding 2-SAT
- 2-sat->poj 3648 Wedding
- POJ 3648 Wedding(2-SAT)
- 【POJ】3648 Wedding 2-sat
- poj 3648 Wedding 2-sat
- POJ 3648 Wedding 2-SAT
- poj 3648 2-SAT算法
- POJ 3648Wedding 2-sat
- 软件工程作业2——坦克大战小游戏
- C/C++的内存管理
- Linux 防火墙iptables 学习笔记(三)iptables命令详解和举例
- 升级 YUSEMITE后,MAMP PRO Apache 不能启动?提示:Apache couldn't be started. Please check the log file for more
- 苹果推送机制APNs(一)_原文转自_唐韧_Ryan
- POJ-3648(2-SAT)
- UVA11125 - Arrange Some Marbles(dp)
- 模块设计无处不在
- 杭电2045
- 优化eclipse
- Linux 防火墙iptables 学习笔记(二)参数指令
- PHP上传文件(学习)
- gdb调试多进程
- Linux 防火墙iptables 学习笔记(一)入门要领