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 ...
随机推荐
- CentOS 7重装mysql编译过程报错解决方法
错误记录: [ 82%] Building C object libmysql/CMakeFiles/clientlib.dir/__/sql-common/client.c.o/usr/local/ ...
- Oracle主键自动生成_表and存储过程
-- Create table create table T_EB_SYS_DN_SEQUENCE_CONFIG ( sequence_id VARCHAR2(36) default sys_guid ...
- Orace数据库锁表的处理与总结<摘抄与总结一>
TM锁(表级锁)类型共有5种,分别称为共享锁(S锁).排它锁(X锁).行级共享锁(RS锁).行级排它锁(RX锁).共享行级排它锁(SRX锁) 当Oracle执行DML语句时,系统自动在所要操作的表上申 ...
- awr相关指标解析
awr相关指标解析 2016年11月11日 15:09
- 基于nodejs的消息中心
参考:http://t42dw.iteye.com/blog/1767013
- 挖潜无极限———数据挖掘技术与应用热点扫描[ZZ]
“我们把世界看成数学,并且把你也看成数学”——用这句话来说明数据挖掘技术的复合性和应用的广泛性似乎再好不过.如今,虽然一些行业在应用这一技术上仍然缺乏足够的主动,但一个不能阻挡的趋势是:已经有越来越多 ...
- servlet的doPost 和doGet和web文件结构
doPost和doGet分别由 tomcat自己来决定调用post 还是get 方式查询 get:url有少量的参数信息,一般用到查询那里 (像百度.. post一般用来提交大文件数据(二进制数据 d ...
- Codeforces 474F - Ant colony
注意到每个区间生存下来的蚂蚁的长度等于区间的gcd 于是可以先预处理出区间的gcd 然后二分查找就好了 预处理gcd我这里用的是倍增法 总的时间复杂度O(NlogN) /* Cf 271F 倍增求区间 ...
- C# ToString常用技巧总结
ToString是在开发中最常用的操作,其作用是将其它类型以字符串类型表示.例如: int i=2;i.ToString() //”2″Object o = new Object();o.ToStri ...
- html5系列.基础知识
兼容性问题 创建一个html5页面 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...