这题的英语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的更多相关文章

  1. Beauty of Array(模拟)

    M - M Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status P ...

  2. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  3. 多边形碰撞 -- SAT方法

    检测凸多边形碰撞的一种简单的方法是SAT(Separating Axis Theorem),即分离轴定理. 原理:将多边形投影到一条向量上,看这两个多边形的投影是否重叠.如果不重叠,则认为这两个多边形 ...

  4. LightOj 1298 - One Theorem, One Year(DP + 欧拉)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1298 题意:给你两个数 n, p,表示一个数是由前 k 个素数组成的,共有 n 个素数 ...

  5. 1214 - Large Division -- LightOj(大数取余)

    http://lightoj.com/volume_showproblem.php?problem=1214 这就是一道简单的大数取余. 还想还用到了同余定理: 所谓的同余,顾名思义,就是许多的数被一 ...

  6. LightOJ Beginners Problems 部分题解

    相关代码请戳 https://coding.net/u/tiny656/p/LightOJ/git 1006 Hex-a-bonacci. 用数组模拟记录结果,注意取模 1008 Fibsieve's ...

  7. LightOJ 1341 唯一分解定理

    Aladdin and the Flying Carpet Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld &a ...

  8. lightoj 1370 欧拉函数

    A - Bi-shoe and Phi-shoe Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & % ...

  9. lightoj 1074 spfa判断负环

     Extended Traffic Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Sub ...

随机推荐

  1. 实现多个ContentProvider对多张表进行操作

    http://blog.csdn.net/maylian7700/article/details/7365373 SQLite数据库直接操作类: DatabaseHelper.java package ...

  2. Hash - a javascript dictionary object.

    Hash,in wikipedia, may relevant to many stuffs. In javascript, hash is a group of name/value pairs w ...

  3. oracle备份表

    oracle与sql单表备份的区别 (     oracle中备份表: create table 备份表名 as select * from 原表 sql server中备份表: select * i ...

  4. AVAudioSession

    AVAudioSession类由AVFoundation框架引入.每个IOS应用都有一个音频会话.这个会话可以被AVAudioSession类的sharedInstance类方法访问,如下: AVAu ...

  5. kvc简单实现

      除了一般的赋值和取值的方法,我们还可以用Key-Value-Coding(KVC)键值编码来访问你要存取的类的属性 kvc: kvc    key value coding 键值对编码 可以通过 ...

  6. 07ADO.Net

    1.ADO.Net简介 代码示例: using (MySqlConnection conn = new MySqlConnection("Server=localhost;Database= ...

  7. ie67 设置最小宽度最小高度

    1.最小宽度 min-width:1003px; _width:expression((document.documentElement.clientWidth||document.body.clie ...

  8. sublime text snippet代码片断

    $0 代表补全代码后放的位置   0 的权重是最低的 $1 最高 也等于${1:}  ${1: name}    1输入点的序号(1权重最高) name 自动补全的默认值      <conte ...

  9. 【POJ2985】【Treap + 并查集】The k-th Largest Group

    Description Newman likes playing with cats. He possesses lots of cats in his home. Because the numbe ...

  10. 使用BeanUtils组件

    使用BeanUtils组件 前提 1:导入commons-beanutils-1.8.3.jar        //根据  本人使用的是1.8.3的版本 2:导入日志包      //就是loggin ...