hdu1814(2-SAT)
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
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.
There are multiple test cases. Process to end of file.
#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)的更多相关文章
- 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 ...
- 学习笔记(two sat)
关于two sat算法 两篇很好的论文由对称性解2-SAT问题(伍昱), 赵爽 2-sat解法浅析(pdf). 一些题目的题解 poj 3207 poj 3678 poj 3683 poj 3648 ...
- Katu Puzzle POJ - 3678 (2 - sat)
有N个变量X1X1~XNXN,每个变量的可能取值为0或1. 给定M个算式,每个算式形如 XaopXb=cXaopXb=c,其中 a,b 是变量编号,c 是数字0或1,op 是 and,or,xor 三 ...
- LA 3211 飞机调度(2—SAT)
https://vjudge.net/problem/UVALive-3211 题意: 有n架飞机需要着陆,每架飞机都可以选择“早着陆”和“晚着陆”两种方式之一,且必须选择一种,第i架飞机的早着陆时间 ...
- spring定时任务详解(@Scheduled注解)( 转 李秀才的博客 )
在springMVC里使用spring的定时任务非常的简单,如下: (一)在xml里加入task的命名空间 xmlns:task="http://www.springframework.or ...
- MongoDB 聚合管道(Aggregation Pipeline)
管道概念 POSIX多线程的使用方式中, 有一种很重要的方式-----流水线(亦称为"管道")方式,"数据元素"流串行地被一组线程按顺序执行.它的使用架构可参考 ...
- mysql触发器,答题记录表同步教学跟踪(用户列表)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABVQAAAOOCAIAAABgEw4AAAAgAElEQVR4nOy92VcT27r/zX+xLtflvt
- Linux版Matlab R2015b的bug——脚本运行的陷阱(未解决)
0 系统+软件版本 系统:CentOS 6.7 x64, 内核 2.6.32-573.el6.x86_64软件:Matlab R2015b(包括威锋网和东北大学ipv6下载的资源,都测试过) 1 脚本 ...
- Quartz.net(调度框架) 使用Mysql作为存储
最近公司的做的项目中涉及到配置任务地址然后按照配置去目标地址提取相关的数据,所以今天上午在Internet上查看有关定时任务(调度任务)的相关信息,筛选半天然后查找到Quartz.net. Quart ...
- mysql 函数编程大全(持续更新)
insert ignore insert ignore表示,如果中已经存在相同的记录,则忽略当前新数据 如果您使用一个例如“SET col_name = col_name + 1”的赋值,则对位于右侧 ...
随机推荐
- 网站缓存技术(Redis、Memcached、Ehcache)
Redis 是什么? 通常而言目前的数据库分类有几种,包括 SQL/NSQL,,关系数据库,键值数据库等等等. 分类的标准也不一,Redis本质上也是一种键值数据库的,但它在保持键值数据库简单快捷特点 ...
- Lintcode---统计比给定整数小的数的个数
给定一个整数数组 (下标由 0 到 n-1,其中 n 表示数组的规模,数值范围由 0 到 10000),以及一个 查询列表.对于每一个查询,将会给你一个整数,请你返回该数组中小于给定整数的元素的数量. ...
- linux一条命令添加一个root级别账户并设置密码
内网机器提权添加账户,无回显,设置密码就不好弄,下面就是添加一个root级别的账户并设置密码的命令 ? 1 useradd -p `openssl passwd -1 -salt 'lsof' a ...
- signed distance field 算法
将二值图转化成signed distance field后,可以在双线性插值下实现平滑放大. 定义: 到前景的distance field:各点到最近前景点的距离. 到背景的distance fiel ...
- Eclipse添加中文语言包与下载
从Eclipse官网下载最新版本的Eclipse都是英文版的,不自带语言包.现在Eclipse有一个语言包项目,叫Eclipse Babel Project.如果需要语言包,可以联机从这儿下载. Ba ...
- C++语言基础(24)-四种类型转换运算符(static_cast、dynamic_cast、const_cast和reinterpret_cast)
一.static_cast static_cast 只能用于良性转换,这样的转换风险较低,一般不会发生什么意外,如: #include <iostream> #include <cs ...
- C预编译宏
/* ============================================================================ Name : c_test001.c A ...
- Javaweb开发中关于不同地方出现的绝对路径和相对路径
1.转发和包含路径 a)以“/”开头:相对当前项目路径,即默认为http://localhost:8080/项目名/ b)不以“/”开头:相对当前Servlet路径. eg:在Aservlet中写“B ...
- IEnumerable扩展
void Main() { // This uses a custom 'Pair' extension method, defined below. Customers .Select (c =&g ...
- Zookeeper实战
这里的实战主要是用Java API对Zookeeper进行相应的操作,来实现我们的业务需求.首先简单地测试一下Zookeeper节点的增删改查操作,然后测试两个例子,分别是分布式应用HA以及分布式锁. ...