2-SAT 求出可能的解,但是这个解要是字典序最小的,所以只能采用2-SAT基本思想来解。

从小到大开始,对一个可能的点染色,染为1,然后dfs其所有能到达的点,如果其中出现一个已经标号为-1的话,那么就说明这个点不能选。  然后把之前标好的号清除掉。 如果没有出现的话,那么就保持染色。 如果出现同一集合的两个点都不能染色的话就说明无解。 否则输出所有染为1的点。

Peaceful Commission

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1377    Accepted Submission(s): 332

Problem Description
The Public Peace Commission should be legislated in Parliament of The Democratic Republic of Byteland according to The Very Important Law. Unfortunately one of the obstacles is the fact that some deputies do not get on with some others.

The Commission has to fulfill the following conditions:
1.Each party has exactly one representative in the Commission,
2.If two deputies do not like each other, they cannot both belong to the Commission.

Each party has exactly two deputies in the Parliament. All of them are numbered from 1 to 2n. Deputies with numbers 2i-1 and 2i belong to the i-th party .

Task
Write a program, which:
1.reads from the text file SPO.IN the number of parties and the pairs of deputies that are not on friendly terms,
2.decides whether it is possible to establish the Commission, and if so, proposes the list of members,
3.writes the result in the text file SPO.OUT.

 
Input
In the first line of the text file SPO.IN there are two non-negative integers n and m. They denote respectively: the number of parties, 1 <= n <= 8000, and the number of pairs of deputies, who do not like each other, 0 <= m <=2 0000. In each of the following m lines there is written one pair of integers a and b, 1 <= a < b <= 2n, separated by a single space. It means that the deputies a and b do not like each other.
There are multiple test cases. Process to end of file.
 
Output
The text file SPO.OUT should contain one word NIE (means NO in Polish), if the setting up of the Commission is impossible. In case when setting up of the Commission is possible the file SPO.OUT should contain n integers from the interval from 1 to 2n, written in the ascending order, indicating numbers of deputies who can form the Commission. Each of these numbers should be written in a separate line. If the Commission can be formed in various ways, your program may write mininum number sequence.
 
Sample Input
3 2 1 3 2 4
 
Sample Output
1 4 5
 
Source
 
Recommend
威士忌
 
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <sstream>
#include <iostream>
using namespace std;
#define INF 0x3fffffff
#define N 21000 struct node
{
int to,next;
}edge[]; int n,m;
int pre[N],cnt;
int mark[N];
int stk[N]; //用来存一次染色的点
int top; int dfs(int s)
{
mark[s]=;
mark[s^]=-;
stk[top++]=s; for(int p=pre[s];p!=-;p=edge[p].next)
{
int v=edge[p].to;
if(mark[v]==)
{
if( dfs(v)== ) return ;
/*
stk[top++]=v;
mark[v]=1; //那么这个点就可以选
mark[v^1]=-1; //这个点就不可以选 ,可以知道的时候每次标号都是标两个,所以不会重复
*/
}
else
if(mark[v]==-)//说明不行。 那么直接放回! 不需要染色了
{
return ;
}
}
return ;
} void add_edge(int u,int v)
{
edge[cnt].to=v;
edge[cnt].next=pre[u];
pre[u]=cnt++;
} int main()
{
//freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
//freopen("C:\\Users\\Administrator\\Desktop\\in.txt","w",stdout);
while(scanf("%d%d",&n,&m)!=EOF)
{
cnt=;
memset(pre,-,sizeof(pre));
for(int i=;i<m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
x--; y--;
add_edge(x,y^);
add_edge(y,x^);
} memset(mark,,sizeof(mark)); //当mark==0 的时候用
int flag=;
for(int i=;i<*n;i+=)
{
top=;
if(mark[i]==)
{
continue;
}
if( (mark[i]!=- )&& dfs(i)== )//表示可以选择这个点,并且已经染好色了
{
continue;
}
else
{
// 如果之前的那个点不能选的话. 那把stk中的东西清掉
for(int j=;j<top;j++)
{
mark[stk[j]]=;
mark[stk[j]^]=;
}
top=; if(mark[i^]==) continue; //把这个点标成1 后,肯定已经把需要标记的选好 if(mark[i^]==- || dfs(i^)==) //如果这个点也不能选,那么就是无解了
{
flag=;
break;
} }
}
if(flag)
printf("NIE\n");
else
{
for(int i=;i<*n;i++)
{
if(mark[i]==)
printf("%d\n",i+);
}
}
}
return ;
}

hdu1814(2-SAT)的更多相关文章

  1. POJ 3678 Katu Puzzle(2 - SAT) - from lanshui_Yang

    Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a ...

  2. 学习笔记(two sat)

    关于two sat算法 两篇很好的论文由对称性解2-SAT问题(伍昱), 赵爽 2-sat解法浅析(pdf). 一些题目的题解 poj 3207 poj 3678 poj 3683 poj 3648 ...

  3. Katu Puzzle POJ - 3678 (2 - sat)

    有N个变量X1X1~XNXN,每个变量的可能取值为0或1. 给定M个算式,每个算式形如 XaopXb=cXaopXb=c,其中 a,b 是变量编号,c 是数字0或1,op 是 and,or,xor 三 ...

  4. LA 3211 飞机调度(2—SAT)

    https://vjudge.net/problem/UVALive-3211 题意: 有n架飞机需要着陆,每架飞机都可以选择“早着陆”和“晚着陆”两种方式之一,且必须选择一种,第i架飞机的早着陆时间 ...

  5. spring定时任务详解(@Scheduled注解)( 转 李秀才的博客 )

    在springMVC里使用spring的定时任务非常的简单,如下: (一)在xml里加入task的命名空间 xmlns:task="http://www.springframework.or ...

  6. MongoDB 聚合管道(Aggregation Pipeline)

    管道概念 POSIX多线程的使用方式中, 有一种很重要的方式-----流水线(亦称为"管道")方式,"数据元素"流串行地被一组线程按顺序执行.它的使用架构可参考 ...

  7. mysql触发器,答题记录表同步教学跟踪(用户列表)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABVQAAAOOCAIAAABgEw4AAAAgAElEQVR4nOy92VcT27r/zX+xLtflvt

  8. Linux版Matlab R2015b的bug——脚本运行的陷阱(未解决)

    0 系统+软件版本 系统:CentOS 6.7 x64, 内核 2.6.32-573.el6.x86_64软件:Matlab R2015b(包括威锋网和东北大学ipv6下载的资源,都测试过) 1 脚本 ...

  9. Quartz.net(调度框架) 使用Mysql作为存储

    最近公司的做的项目中涉及到配置任务地址然后按照配置去目标地址提取相关的数据,所以今天上午在Internet上查看有关定时任务(调度任务)的相关信息,筛选半天然后查找到Quartz.net. Quart ...

  10. mysql 函数编程大全(持续更新)

    insert ignore insert ignore表示,如果中已经存在相同的记录,则忽略当前新数据 如果您使用一个例如“SET col_name = col_name + 1”的赋值,则对位于右侧 ...

随机推荐

  1. Drupal的钩子系统

    Drupal的很多功能都是可以定制的.以导航菜单为例,blog模块需要在菜单上添加一些功能,comment模块需要在菜单上添加一些功能,我们开发的自定义模块也需要在菜单上添加一些功能.Drupal开发 ...

  2. 常见SQL Server导入导出数据的几个工具

    摘自:http://www.cnblogs.com/chenxizhang/archive/2011/06/09/2076542.html 在我们的日常工作中,与数据库打交道的机会越来越多.这一篇文章 ...

  3. Random.org -- 真正的随机数生成器

    接触过程序设计的人一定对随机数不陌生.随机数的用途非常广,比方在測试.分布和统计程序.游戏中. 大多数编程语言也提供了随机数库.能够方便的使用. 只是从严格意义上来讲,这些程序生成的随机数并非真正的随 ...

  4. HTTP Cache怎样计算Age

    这里的Age指的是响应头Age.以下内容有部分翻译,也有部分自己的理解.欢迎讨论. 我们用now表示当前主机的当前时间,用request_time表示缓存发起请求的时间,用response_time表 ...

  5. Web App、Hybrid App、Native APP对比

  6. redis windows 下安装及使用

    1.下载redis https://github.com/MSOpenTech/redis 2.解压下载的文档,比如D:\devSoft\redis-2.8.19 redis-benchmark.ex ...

  7. POJ2185-Milking Grid(KMP,next数组的应用)

    Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6317   Accepted: 2648 Desc ...

  8. Atitit。激活某个程序的api attilax总结

    Atitit.激活某个程序的api attilax总结 1. 设置当前某窗口为当前窗口,有几个步骤要做:1.得到窗口句柄FindWindow2.切换键盘输入焦点AttachThreadInput3.显 ...

  9. 4.lists(双向链表)

    一.概述 是一个线性链表结构,它的数据由若干个节点构成,每一个节点都包括一个信息块(即实际存储的数据).一个前驱指针和一个后驱指针.它无需分配指定的内存大小且可以任意伸缩,这是因为它存储在非连续的内存 ...

  10. Ubuntu 的 apt-get 代理设置(zhuan)

    url: http://qixinglu.com/post/ubuntu_apt-get_proxy_setup.html 升级到 Ubuntu10.04 后,发现 apt-get 的代理设置有改变了 ...