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 ...
随机推荐
- 进程识别号(PID)的理解
PID(Process Identification)操作系统里指进程识别号,也就是进程标识符.操作系统里每打开一个程序都会创建一个进程ID,即PID. PID(进程控制符)英文全称为Process ...
- python - zipfile
参考:http://www.cnblogs.com/sislcb/archive/2008/11/28/1342822.html zipfile - python处理zip文件的压缩与解压 ZipFi ...
- date日期比较和格式化方法
时间的比较 var now = new Date();//现在时间 var setDate = new Date(2017,0,5,17,55,55);//设定的时间 laert(now < s ...
- 如何:在 StackPanel 和 DockPanel 之间进行选择
虽然可以使用 DockPanel 或 StackPanel 来堆叠子元素,但这两个控件并不总是会产生相同的结果. 例如,子元素的放置顺序可能会影响 DockPanel 中子元素的大小,但不会影响 St ...
- java 全角、半角字符串转换
转自:http://www.cnblogs.com/modou/articles/2679815.html 加入了空字符串的验证 半角转全角的方法: /** * @Title: ToSBC * ...
- 【转】javascript变量作用域、匿名函数及闭包
下面这段话为摘抄,看到网上大多数人使用的是变量在使用的时候声明而不是在顶端声明,也可能考虑到js查找变量影响性能的问题,哪里用就在哪里声明,也很好. 在Javascript中,我们在写函数的时候往往需 ...
- python学习笔记(一)元组,序列,字典
python学习笔记(一)元组,序列,字典
- #module-django.db.models
Models A model is the single, definitive source of information about your data. It contains the esse ...
- 得到某个进程所有线程ID和入口地址
#include <windows.h> #include <tlhelp32.h> #include "iostream" using namespace ...
- HTML5中的 DOM 树
DOM (文档对象模型(Document Object Model)) 文档对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展标志语言的标准编程接口.Do ...