poj 3207 2-sat
2015-08-21 15:03
351 查看
思路:把每一条线段当做点,则有“内”、“外”两种取值,然后在冲突的线段之间连边,判断是否有解即可。
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> using namespace std; const int N = 500; const int M = 1000000; int head[N * 2]; int s[N * 2]; bool mark[N * 2]; int n, e, c; struct Edge { int v, next; } edge[M]; void addEdge( int u, int v ) { edge[e].v = v; edge[e].next = head[u]; head[u] = e++; } void init() { e = 0; memset( head, -1, sizeof(head) ); memset( mark, false, sizeof(mark) ); } bool dfs( int u ) { if ( mark[u ^ 1] ) return false; if ( mark[u] ) return true; mark[u] = true; s[c++] = u; for ( int i = head[u]; i != -1; i = edge[i].next ) { int v = edge[i].v; if ( !dfs(v) ) return false; } return true; } bool solve() { for ( int i = 0; i < 2 * n; i += 2 ) { if ( !mark[i] && !mark[i + 1] ) { c = 0; if ( !dfs(i) ) { while ( c ) { c--; mark[s[c]] = false; } if ( !dfs( i + 1 ) ) return false; } } } return true; } struct T { int x, y; } t ; int main () { scanf("%d%d", &n, &n); init(); for ( int i = 0; i < n; i++ ) { scanf("%d%d", &t[i].x, &t[i].y); if ( t[i].x > t[i].y ) swap( t[i].x, t[i].y ); for ( int j = 0; j < i; j++ ) { if ( ( t[i].x < t[j].x && t[i].y < t[j].y && t[i].y > t[j].x ) || ( t[i].x > t[j].x && t[i].y > t[j].y && t[i].x < t[j].y ) ) { int li = i << 1, ri = li | 1; int lj = j << 1, rj = lj | 1; addEdge( li, rj ); addEdge( ri, lj ); addEdge( lj, ri ); addEdge( rj, li ); } } } if ( solve() ) { puts("panda is telling the truth..."); } else { puts("the evil panda is lying again"); } return 0; }
相关文章推荐
- HTML简介(css简单的样式)
- “#ifdef __cplusplus extern "C" { #endif”的定义
- Spring SetFactoryBean example
- 解读jquery源码时记录的一些知识点
- 数据库性能优化二:数据库表优化
- 数据库的基本概念
- jQuery $(,)多参数
- 安卓下拉刷新,pulltorefresh的使用
- mysql游标错误
- Django
- Echarts 单序列数据颜色color交替实现
- Network Saboteur(POJ_2531)
- 网页数据包抓取工具
- 数据库性能优化一:数据库自身优化(大数据量)
- poj-1129 Channel Allocation
- TwemProxy(nutcracker)参数选项及配置解析
- 关于Oracle ADF 11g的传值给bounded task flow
- 快速理解RequireJs
- jquery call 函数改变this作用域
- 解决ubuntu server mysql load data infile 导入本地文件ERROR 1148 (42000)错误。