#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std; const int maxn = ;
const int INF = 0x3f3f3f; int n,m;
char a[maxn][];
int b[maxn][]; struct TwoSat{
int n;
vector<int> G[maxn*];
bool mark[*maxn];
int s[*maxn],cnt; void init(int n){
this->n = n;
memset(mark,,sizeof(mark));
for(int i=;i<=*n;i++) G[i].clear();
} void add_clause(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^);
} bool dfs(int u){
if(mark[u^]) return false;
if(mark[u]) return true;
mark[u] = true;
s[cnt++] = u; //记录你所选择的路径;
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] && a[i/][] == '+'){
ans[pv++] = b[i/][];
}
else if(mark[i+] && a[i/][] == '+'){
ans[pv++] = b[i/][];
}
}
sort(ans,ans+pv);
int num = unique(ans,ans+pv) - ans;
printf("%d",num);
for(int i=;i<num;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",&n,&m);
solver.init(n);
vector<int> p1[],p2[],n1[],n2[];
for(int i=;i<n;i++){
int temp;
scanf("%c%c%d%c%c%d",&temp,&a[i][],&b[i][],&temp,&a[i][],&b[i][]);
if(a[i][] == '+'){
p1[b[i][]].push_back(i);
p2[b[i][]].push_back();
}
else{
n1[b[i][]].push_back(i);
n2[b[i][]].push_back();
}
if(a[i][] == '+'){
p1[b[i][]].push_back(i);
p2[b[i][]].push_back();
}
else{
n1[b[i][]].push_back(i);
n2[b[i][]].push_back();
}
}
for(int i=;i<=m;i++){
for(int j=;j<p1[i].size();j++)
for(int k=;k<n1[i].size();k++){
solver.add_clause(p1[i][j],p2[i][j],n1[i][k],n2[i][k]);
}
}
if(solver.solve()){
printf("Case %d: Yes\n",t);
solver.print();
}
else printf("Case %d: No\n",t);
}
}

题目链接:http://lightoj.com/volume_showproblem.php?problem=1251

lightoj 1251 (Two_Sat)的更多相关文章

  1. 2-Sat+输出可行解(个人模版)

    2-Sat+输出可行解: //LightOJ 1251 #include<stdio.h> #include<string.h> #include<vector> ...

  2. BZOJ 1251: 序列终结者 [splay]

    1251: 序列终结者 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 3778  Solved: 1583[Submit][Status][Discu ...

  3. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

  4. 区间DP LightOJ 1422 Halloween Costumes

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

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

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

  6. hduoj 1251 统计难题

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory ...

  7. hdu 1251 统计难题 (字典树入门题)

    /******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...

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

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

  9. LightOJ Beginners Problems 部分题解

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

随机推荐

  1. ubuntu JDK

    第一步:下载jdk-7-linux-i586.tar.gz 第二步:解压安装 cd /usr/libsudo mkdir jvm cd {你的目录jdk压缩文件目录} sudo tar zxvf jd ...

  2. XFire中Services.xml 配置的一些细节

    昨天第一次调XFire引擎,放在Tomcat里之后老是报错,检查了很久没有发现什么问题,一直很费解. 后来在网上看到一篇文章<XFire services.xml 配置文件补充说明>,根绝 ...

  3. offie2010设置前两页和后面显示不同页码的方法

    1.在需要设置的第二页文档后面点击一下,让光标进入,再菜单上找到"页面布局"—“分栏符”—“下一页”(如图) 2.插入—页码—页面底端(如图) 3.点击页码附近的—“链接到前一页面 ...

  4. 【vc】1_Windows程序内部运行机制

    创建一个Win32应用程序步骤: 1.编写WinMain函数; 2.创建窗口(步骤如下): a.设计(一个)窗口类(WNDCLASS) b.注册(该)窗口类. c.创建窗口. d.显示并更新窗口. 3 ...

  5. javascript——闭包

    <script type="text/javascript"> //什么是闭包: //是指语法域位于某个特定的区域,具有持续参照(读写)位于该区域内自身范围之外的执行域 ...

  6. golang bufio writer,reader 缓存规则

    读,写,缓冲区可以杜绝频繁的读,写动作1.写缓存,如果一次write的长度大于buffer长度那么久发送当前缓冲区的内容并且发送要写入的内容,就是不在缓存了.如果发送的内容小于buffer长度,就按缓 ...

  7. DEDECMS使用SQL命令批量替换语句

    1.更改文章中的内容 update dede_addonarticle set body=replace(body,'原来的字符','替换后的字符') 2,替换文章标题 update dede_arc ...

  8. php报警:Strict Standards: Only variables should be passed by reference in

    错误原因 因为end函数的原因. end函数: mixed end    ( array &$array   ) 你可以看到end的参数是一个引用(reference),而你只能把一个变量的引 ...

  9. 我学C的那些年[ch01]:浅淡C语言的编译过程

    前几天大致学习了C语言的编译过程,那么今天就和大家分享一下 首先,编译C语言,需要一个文本编辑器(windows自带的也行),和一个MinGW编译器(需要配置环境),就可以将.c文件编译成.exe文件 ...

  10. Ubuntu下安装和配置Apache2

    http://www.blogjava.net/duanzhimin528/archive/2010/03/05/314564.html 在Ubuntu中安装apache 安装指令:sudo apt- ...