题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26961

思路:u表示留下,~u表示离开,同理v,对于+u,-v,我们可以这样来定义:若u离开,则v必须留下,如v离开,则u必须留下,于是我们可以连边u+n->v,v+n->u,后面的同理。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<vector>
#include<queue>
using namespace std;
#define MAXN 20000
#define MAXM 444444 struct Edge{
int v,next;
}edge[MAXM]; int n,m,NE;
int head[MAXN]; void Insert(int u,int v)
{
edge[NE].v=v;
edge[NE].next=head[u];
head[u]=NE++;
} int cnt,bcc_count;
int low[MAXN],dfn[MAXN],color[MAXN];
bool mark[MAXN];
stack<int>S; void Tarjan(int u)
{
low[u]=dfn[u]=++cnt;
mark[u]=true;
S.push(u);
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v;
if(dfn[v]==){
Tarjan(v);
low[u]=min(low[u],low[v]);
}else if(mark[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
int v;
bcc_count++;
do{
v=S.top();
S.pop();
mark[v]=false;
color[v]=bcc_count;
}while(u!=v);
}
} int opp[MAXN];
bool Judge()
{
for(int i=;i<=n;i++){
if(color[i]==color[i+n])return false;
//opp[x]保存的是和编号为x的连通分量矛盾的连通分量的编号
opp[color[i]]=color[i+n];
opp[color[i+n]]=color[i];
}
return true;
} vector<vector<int> >g;
vector<int>ans;
int degree[MAXN];
int vis[MAXN]; void TopSort()
{
queue<int>que;
for(int i=;i<=bcc_count;i++){
if(degree[i]==)que.push(i);
}
while(!que.empty()){
int u=que.front();
que.pop();
if(vis[u]==){
//染色
vis[u]=;
vis[opp[u]]=-;
}
for(int i=;i<g[u].size();i++){
degree[g[u][i]]--;
if(degree[g[u][i]]==)que.push(g[u][i]);
}
}
} int main()
{
int _case,t=,a,b,u,v;
scanf("%d",&_case);
while(_case--){
scanf("%d %d",&m,&n);
NE=;
memset(head,-,sizeof(head));
while(m--){
scanf("%d%d",&a,&b);
u=abs(a),v=abs(b);
if(a>&&b>)Insert(u+n,v),Insert(v+n,u);
else if(a>&&b<)Insert(u+n,v+n),Insert(v,u);
else if(a<&&b>)Insert(u,v),Insert(v+n,u+n);
else Insert(u,v+n),Insert(v,u+n);
}
cnt=bcc_count=;
memset(mark,false,sizeof(mark));
memset(dfn,,sizeof(dfn));
memset(color,,sizeof(color));
for(int i=;i<=*n;i++)if(dfn[i]==)Tarjan(i);
printf("Case %d: ",t++);
if(!Judge()){
puts("No");
continue;
}
puts("Yes");
//反向建图
memset(degree,,sizeof(degree));
g.clear();
g.resize(*n+);
for(int u=;u<=*n;u++){
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v;
if(color[u]!=color[v]){
g[color[v]].push_back(color[u]);
degree[color[u]]++;
}
}
}
memset(vis,,sizeof(vis));
TopSort();
ans.clear();
for(int i=;i<=n;i++){
if(vis[color[i]]==)ans.push_back(i);
}
printf("%d",(int)ans.size());
sort(ans.begin(),ans.end());
for(int i=;i<(int)ans.size();i++){
printf(" %d",ans[i]);
}
puts("");
}
return ;
}

loj 1251(2-sat + 输出一组可行解)的更多相关文章

  1. loj 1407(2-sat + 枚举 + 输出一组可行解 )

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=27115 思路:有一个trick要注意:当情况为 2 x y 时,可 ...

  2. poj 3683(2-sat+输出一组可行解)

    题目链接:http://poj.org/problem?id=3683 思路:对于每个结婚仪式,只有在开始或结束时进行这两种选择,我们可以定义xi为真当且仅当在开始时进行.于是我们可以通过时间先后确定 ...

  3. poj 3683 2-sat问题,输出任意一组可行解

    /* 2sat问题 输出任意一组可行解 */ #include<stdio.h> #include<string.h> #include<stdlib.h> #in ...

  4. SAM I AM UVA - 11419(最小顶点覆盖+输出一组解)

    就是棋盘问题输出一组解 https://blog.csdn.net/llx523113241/article/details/47759745 http://www.matrix67.com/blog ...

  5. 2-sat 输出任意一组可行解&拓扑排序+缩点 poj3683

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8170   Accept ...

  6. C语言实现输出一组数字中的所有奇数

    /*第二题*/ #include<stdio.h> //输入186732468 //输出173 //输入12345677 //输出13577 main(){ ;//输入的数字,数字的长度 ...

  7. poj3683 2 -sat输出路径

    tarjan缩点,拓扑排序染色输出(貌似挑战上面没有拓扑啊,而且这样写还过了= =) 主要是找s,t,d,三者之间的关系,找出合取范式这题就很容易了 #include<map> #incl ...

  8. python应用-表格式输出一组数据

    def main(): names=['关羽','张飞','赵云','马超','貂蝉'] subjects=['语文','数学','Python'] table=[[0 for _ in range( ...

  9. 【C语言】(数组方式)输出一组成绩中的最高分与最低分

    两种不同方式获取最大值与最小值 代码1: #include <stdio.h> int main() { ], sum = , max, min; int i; printf(" ...

随机推荐

  1. Linux如何删除以分号开头的文件

    发现在创建文件时,有的时候会不小心创建以分号开头的文件. 如何删除呢? 执行  rm \;   即可删除 把以;号开头的文件名转义后再删除 创建文件:vi  index.php 或者vim  inde ...

  2. HDU4870_Rating_双号从零单排_高斯消元求期望

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Other ...

  3. Dual Palindromes

    Dual PalindromesMario Cruz (Colombia) & Hugo Rickeboer (Argentina) A number that reads the same ...

  4. java笔记--用ThreadLocal管理线程,Callable<V>接口实现有返回值的线程

    用ThreadLocal管理线程,Callable<V>接口实现有返回值的线程 ThreadLocal在我的笔记"关于线程同步"的第5种方式里面有介绍,这里就不多说了. ...

  5. 不同版本的name可以重复

    - validates :name, presence: true, uniqueness: { conditions: -> { where(:state.ne => 2) } }, l ...

  6. php增加对mysqli的支持

    php增加对mysqli的支持   我在fedora下使用yum安装的php和mysql,但是发现php不支持myslqi,只能编译一个mysqli的扩展给php用了. 方法如下: 1.下载php 2 ...

  7. 【SpringMVC】SpringMVC系列12之数据类型转换、格式化、校验

      12.数据类型转换.格式化.校验 12.1.数据绑定流程     Spring MVC 主框架将 ServletRequest 对象及目标方法的入参实例传递给 WebDataBinderFacto ...

  8. MVC3和MVC4相关问题

    从TFS获取的MVC项目生成一直提示这个错误,我的VS中MVC3和MVC4都有,TFS项目中的MVC版本应该是MVC3的,现在我要用这个项目,但是一直是这个错误,请高手指点,积分不多了,见谅 程序集“ ...

  9. wsp反编译

    最后出于好奇,我把wsp文件解压缩,看看里面是什么(如果您的机器上的压缩软件不能直接解压,可尝试修改后缀名为cab.).我看到的首先是一个清单文件(manifest.xml),一个DLL文件(Shar ...

  10. VIM中的折叠命令

    参考:http://blog.csdn.net/bruce0532/article/details/8497284 za:在折叠与展开间自由转换 zf:创建折叠 示例:zf 3j    #本行及以下3 ...