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 ...
随机推荐
- u盘复制提示文件过大
应该有很多个朋友也遇到过同样的问题,就是我们的u盘的明明可用的空间还有很多,甚至一个空的16g的u盘,但从window等操作系统向u盘拷贝文件的时候,却不能容下诸如iso4g的镜像文件,难道是生产u盘 ...
- ORA-01653:表空间扩展失败的问题
以下内容来源于ORA-01653:表空间扩展失败的问题 今天发现,原来设备的数据表空间只有5M,已经满了,上网去找发现要进行扩展空间. 一.脚本修改方式: ----查询表空间使用情况---使用DB ...
- VMware Workstation 11序列号一枚
VMware Workstation 11序列号: 1F04Z-6D111-7Z029-AV0Q4-3AEH8
- JS 通过系统时间限定 动态添加 select option
虽然是个简单的效果,还是需要积累一下,记录一下: 在八月一号之后,删除最后一项,新添加2016级 — — 2015级 2014级 2013级 2012级 在六月一号之后,删除最后一项,新添加2016级 ...
- 做了一个类似天猫鼠标经过icon的动画,记录一下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- highcharts实例教程二:结合php与mysql生成饼图
上回我们分析了用highcharts结合php和mysql生成折线图的实例,这次我们以技术cto网站搜索引擎流量为例利用highcharts生成饼图. 饼图通常用在我们需要直观地显示各个部分所占的比例 ...
- TatukGIS - GisDefs - CanonicalSQLName 函数
函数名称 CanonicalSQLName 所在单元 GisDefs 函数原型 function CanonicalSQLName(const _name: String; const _tem ...
- nth_element学习
今天学习到STL中的nth_element,她是一个默认能求第k小的数的方法,需要的头文件为algorithm. 默认为:nth_element(start, start+n, end) 使第n大元素 ...
- Oralce 字符串截取
update E_SYS_STRATEGY set sconfigvalue=(select Substr((select sconfigvalue from E_SYS_STRATEGY where ...
- Zabbix的LLD功能--Low-level discovery
感觉这个主题是和一个主题是一个系列,而且更基础. 准备深入研究一下... 相关文档准备: https://www.zabbix.com/documentation/2.0/manual/discove ...