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 ...
随机推荐
- angularjs kindEditor 中自定义按钮 弹出dialog
1.angular-kindeditor.js 第38行左右加 editorConfig.items = ["placehoder"]; 2.en.js 第234行 placeho ...
- Hessian(C#)介绍及使用说明
什么是Hessian? Hessian是Caucho开发的一种二进制Web Service协议.支持目前所有流行的开发平台. Hessia能干什么? hessian用来实现web服务. Hessia有 ...
- 后台线程,优先级,sleep,yield
1.后台线程,是指在程序运行的时候在后台提供一种通用服务的线程,并且这种线程并不属于程序中不可获取的部分.当所有非后台线程结束时,程序也就 终止了,同时会杀死进程中所有后台线程.main()是一个非后 ...
- NRPE: Unable to read output 问题处理总结
自定义nagios监控命令check_disk_data,首先在nagios服务端command.cfg定义了#'check_disk_data' command definitiondefine c ...
- MySQL 5.6 my.cnf 参数详细说明
# 以下选项会被MySQL客户端应用读取.# 注意只有MySQL附带的客户端应用程序保证可以读取这段内容.# 如果你想你自己的MySQL应用程序获取这些值.# 需要在MySQL客户端库初始化的时候指定 ...
- iOS7初体验(1)——第一个应用程序HelloWorld
iOS7 Beta已经发布了,迫不及待地下载了iOS 7及Xcode 5并体验了一下.先做一个简单的Hello World看看都有哪些变化吧. 1. 启动Xcode5-DP: 2. 从菜单选择File ...
- 更新xcode后插件失效问题——不针对特定版本的通用解决方法
一.Xcode更新后插件失效的原理 1.每次更新Xcode后插件都会失效,其实插件都还在这个目录好好的躺着呢: ~/Library/Application Support/Developer/Shar ...
- Xcode 使用自定义字体
添加对应的字体(.ttf或.odf)到工程的resurce,使用cocos2d中的FontLabel库,FontLabel继承于UILabel,象UILabel一样使用就好了 fontName直接使用 ...
- git学习小结 (笔记)
Modesty helps one to go forward, whereas conceit makes one lag behind. "虚心使人进步,骄傲使人落后" 注:本 ...
- linux 监控服务器脚本
#!/bin/bash ctime=`date +%x%T`monitor_dir=/home/jk/if [ ! -d $monitor_dir ]; then mkdir $monitor_ ...