题目链接: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. gnl总结(#,%,$)

    Ognl表达式struts标签“%,#,$” 1.什么是Ognl? OGNL(Object-Graphic Navigation Language),对象图道行语言.是一种可以方便操作对象属性的开源表 ...

  2. iOS-使用Xcode拉伸图片

    如果要制作一个类似于QQ消息气泡的图片,该如何制作呢?android中可以使用.9图片指定图片中的某一部分拉伸,那iOS中类似的功能要如何实现呢,Xcode提供了类似的功能.具体步骤如下: 1.选择需 ...

  3. 一个简单的web服务器

    写在前面 新的一年了,新的开始,打算重新看一遍asp.net本质论这本书,再重新认识一下,查漏补缺,认认真真的过一遍. 一个简单的web服务器 首先需要引入命名空间: System.Net,关于网络编 ...

  4. 在Microsoft-IIS/10.0上面部署mvc站点的时候,出现404的错误

    写在前面 在家自己弄了一个项目,想部署在电脑上用手机来访问,总是出现404的错误.路由什么的没有写错啊,最后发现是映射程序的问题,在安装的时候iis很多功能没有安装,又将iis的其他没有安装的功能勾选 ...

  5. 怎样把excel一列分成多列

    1,选定要分列的列. 2,点击“数据”-“分列”. 3,在选项栏中设置如图 4,选择分隔符 4,看,分开了吧!

  6. linux命令别名的使用

    语 法:alias[别名]=[指令名称] 1,查看该用户下的别名: alias 2,有的系统里没有ll这个命令,原因就是没有定义ll='ls -l --color=tty'这个别名 如果想永久生效,就 ...

  7. Help Me with the Game(imitate)

    Help Me with the Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3630   Accepted:  ...

  8. Radar Installation(贪心)

    Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 56826   Accepted: 12 ...

  9. MVC中的_viewstart.cshtml(没有设置Layout却引用了布局)

    今天Home视图中新增了一个视图,因为不需要设置Layout就没与管他,但是运行起来一看,自动引用了布局,分析了半天 也没看出是哪的错误? 后来尝试着在area中增加了一个同样的视图就没有问题,比较这 ...

  10. OpenGL顶点缓冲区对象(VBO)

    转载 http://blog.csdn.net/dreamcs/article/details/7702701 创建VBO        GL_ARB_vertex_buffer_object 扩展可 ...