lightoj 1407 2-sat
这题的英语either...or....很蛋疼;
m中,1:x与y至少一个出席;2:x出席,y随便,x不出席,y也不出席----这有个坑,可以推出y出席x也一定出席(这个关系必须要连上);3x与y至少一个不出席 4,x与y有且只有一个出席。
对于k中的数据:我琢磨很久,关系在图上是建不了的,type为1时三个人至少有一个人要出席,则三个都不出席的情况是不允许的,dfs的时候判断一下就是了,同理type为2的情况。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int maxn = ;
const int maxe = ;
const int INF = 0x3f3f3f; int n,m,k;
struct node{
int type,x,y,z;
}a[]; struct TwoSat{
int n;
int mark[maxn*];
int s[maxn*],cnt;
vector<int> G[maxn*]; void init(int n){
this->n = n;
for(int i=;i<=*n+;i++) G[i].clear();
memset(mark,,sizeof(mark));
}
void add_clause1or3(int u,int uval,int v,int vval){
u = u* + uval; //u与v不共存;
v = v* + vval;
G[u].push_back(v^);
G[v].push_back(u^);
}
void add_clause2(int u,int uval,int v,int vval){
u = u* + uval;
v = v* + vval;
G[u].push_back(v);
G[v^].push_back(u^);
}
void add_clause4(int u,int uval,int v,int vval){
u = u* + uval;
v = v* + vval;
G[u^].push_back(v);
G[u].push_back(v^);
G[v^].push_back(u);
G[v].push_back(u^);
} bool JudgeWrong(){
for(int i=;i<k;i++){
if(a[i].type == ){
int x = *a[i].x+,y = *a[i].y+,z = *a[i].z+;
if(mark[x] && mark[y] && mark[z]) return true;
}
if(a[i].type == ){
int x = *a[i].x,y = *a[i].y,z = *a[i].z;
if(mark[x] && mark[y] && mark[z]) return true;
}
}
return false;
} bool dfs(int u){
if(mark[u^]) return false;
if(mark[u]) return true;
mark[u] = true;
s[cnt++] = u;
if(JudgeWrong()) return false; for(int i=;i<G[u].size();i++)
if(!dfs(G[u][i])) return false;
return true;
} bool solve(){
for(int i=;i<=*n;i+=)
if(!mark[i] && !mark[i+]){
cnt = ;
if(!dfs(i)){
while(cnt > ) mark[s[--cnt]] = false; //要回溯;
if(!dfs(i+)) return false;
}
} return true;
} void print(){
int ans[maxn],pv = ;
for(int i=;i<=*n;i+=)
if(mark[i]) ans[pv++] = i/; printf("%d",pv);
for(int i=;i<pv;i++) printf(" %d",ans[i]);
printf(".\n");
} }solver; int main()
{
//freopen("E:\\acm\\input.txt","r",stdin);
int T;
cin>>T;
for(int t=;t<=T;t++){
scanf("%d %d %d",&n,&m,&k);
solver.init(n);
for(int i=;i<=m;i++){
int type,x,y;
scanf("%d %d %d",&type,&x,&y);
if(type == ) solver.add_clause1or3(x,,y,);
else if(type == ) solver.add_clause2(x,,y,);
else if(type == ) solver.add_clause1or3(x,,y,);
else solver.add_clause4(x,,y,);
}
for(int i=;i<k;i++)
scanf("%d %d %d %d",&a[i].type,&a[i].x,&a[i].y,&a[i].z);
if(solver.solve()){
printf("Case %d: Possible ",t);
solver.print();
}
else printf("Case %d: Impossible.\n",t);
}
}
lightoj 1407 2-sat的更多相关文章
- Beauty of Array(模拟)
M - M Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit Status P ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- 多边形碰撞 -- SAT方法
检测凸多边形碰撞的一种简单的方法是SAT(Separating Axis Theorem),即分离轴定理. 原理:将多边形投影到一条向量上,看这两个多边形的投影是否重叠.如果不重叠,则认为这两个多边形 ...
- LightOj 1298 - One Theorem, One Year(DP + 欧拉)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1298 题意:给你两个数 n, p,表示一个数是由前 k 个素数组成的,共有 n 个素数 ...
- 1214 - Large Division -- LightOj(大数取余)
http://lightoj.com/volume_showproblem.php?problem=1214 这就是一道简单的大数取余. 还想还用到了同余定理: 所谓的同余,顾名思义,就是许多的数被一 ...
- LightOJ Beginners Problems 部分题解
相关代码请戳 https://coding.net/u/tiny656/p/LightOJ/git 1006 Hex-a-bonacci. 用数组模拟记录结果,注意取模 1008 Fibsieve's ...
- LightOJ 1341 唯一分解定理
Aladdin and the Flying Carpet Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld &a ...
- lightoj 1370 欧拉函数
A - Bi-shoe and Phi-shoe Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & % ...
- lightoj 1074 spfa判断负环
Extended Traffic Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Sub ...
随机推荐
- Xcode添加静态库以及编译选项配置常见问题
一,Xcode编译出现Link错误,出现"duplicate symbols for architecture i386 clang"提示.问题:链接时,项目有重名文件.解决:根据 ...
- iOS 小知识 - #if , #ifdef , #ifndef.
Q : 在项目的 .h 文件中, #ifndef XXX_h #define XXX_h //程序段1 #endif /* XXX_h */ 的作用? A : 如果 XXX.h 不存在,就引入 XX ...
- 10集合:List<T>,Dictionary<K,V>
List<T>泛型集合 List<T>是C#中一种快捷.易于使用的泛型集合类型,使用泛型编程为编写面向对象程序增加了极大的效率和灵活性. 1.List<T>用法 ...
- hdu_5276
//不管怎么样还是希望天天做笔记把,真是太懒了#include<iostream> #include<cstdio> #include<vector> #inclu ...
- Centos JAVA Eclipse
wget http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-linux-i586.tar.gz vi /etc/profile 在 ...
- Windows7 IIS7 无法启动计算机上的服务W3SVC如何修复
错误提示 启动iis7管理服务器提示:无法启动计算机上的服务W3SVC 启动Windows Process Activation Service服务,报错:6801 指定资源管理器中的事务支持未启动或 ...
- TabControl控件中TabPage的显示和隐藏
TabPage里面含有方法Hide和Show,但没有任何作用,实际隐藏和显示需要使用如下2个方法 方法一:此方法比较简单 TabPageServo.Parent = Nothing //隐藏 Ta ...
- Unable to find the ncurses libraries or the required header files解决
问题: 解决方法: sudo apt-get install ncurses-dev 参考:Unable to find the ncurses libraries or the required h ...
- PM加油站
老郭讲述深航CSM 1.需求有遗漏,人员水平不足:加班导致人员流失:但是这样,客户后来还是好评,并且项目被评为深航的标杆项目:老郭也是被指定为未来项目的项目经理:--!我想起了古时候的一句话:功夫在诗 ...
- HTML5 display:inline、block、inline-block的区别--备用
display:block就是将元素显示为块级元素. block元素的特点是: 总是在新行上开始: 高度,行高以及顶和底边距都可控制: 宽度缺省是它的容器的100%,除非设定一个宽度 <div& ...