lightoj 1251 (Two_Sat)
#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)的更多相关文章
- 2-Sat+输出可行解(个人模版)
2-Sat+输出可行解: //LightOJ 1251 #include<stdio.h> #include<string.h> #include<vector> ...
- BZOJ 1251: 序列终结者 [splay]
1251: 序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 3778 Solved: 1583[Submit][Status][Discu ...
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- LightOj 1298 - One Theorem, One Year(DP + 欧拉)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1298 题意:给你两个数 n, p,表示一个数是由前 k 个素数组成的,共有 n 个素数 ...
- hduoj 1251 统计难题
http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory ...
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- 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 ...
随机推荐
- Nhibernate主子表查询
假如有AB两表,A为主,B为子:已知A表ID,查B表数据 ICriteria criteriaTotal = session.CreateCriteria<B>().CreateCrite ...
- Xcode 7真机测试详解
1.准备 注意:一定要让你的真机设备的系统版本和app的系统版本想对应,如果不对应就会出现一个很常见的问题:could not find developer disk image 首先,准备好下面的设 ...
- jQuery 获取并设置 CSS 类
通过 jQuery,可以很容易地对 CSS 元素进行操作. 切换 CSS 类 jQuery 操作 CSS jQuery 拥有若干进行 CSS 操作的方法.我们将学习下面这些: addClass() - ...
- 【转】无废话WCF系列教程
转自:http://www.cnblogs.com/iamlilinfeng/category/415833.html 看后感:这系列的作者李林峰写得真的不错,通过它的例子,让我对WCF有了一 ...
- PHPCMS V9 学习总结(转)
转自:http://www.cnblogs.com/Braveliu/p/5074930.html 在实现PHPCMS网站过程中,根据业务需求,我们遇到很多问题,特此总结如下,以便大家参考学习. [1 ...
- 用jQuery实现瀑布流效果学习笔记
jQuery一直没系统的学,只知道是js库,封装了好多js函数,方便了开发.以前做过一个原生的图片网站瀑布流效果,超级麻烦,这次用了jQuery方法,瞬间代码浓缩了,只有56行js代码.神奇的让我来把 ...
- AspNet MVC : 操作/控制器过滤器(action filter)
1.Action Filter Action Filter提供了在执行action/controller前后对请求/响应修改的能力,可以应用于action和控制器上,作用在控制器上将自动被应用到该控制 ...
- Lua 5.1 for Delphi 2010
This is a Lua 5.1 Wrapper for Delphi 2009 and Delphi 2010 which automatically creates OOP callback f ...
- IOS开发备忘
1. ios 真机调试时出现CopyPngFile error解决方法 说是读取的时候没有找到这张图片,检查了一下图片路径,没有问题,于是google之,找到两种解决方法 : 方法一:在build s ...
- C++实现base64编码(1)
下面的代码是php里面的base64编码逻辑,确实比我之前的要美观很多,我只是简单的用C++的类进行了一下封装,删除了一些没用的逻辑,基本上还是原来PHP的代码: #include <iostr ...