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”的赋值,则对位于右侧 ...
随机推荐
- resin设置jvm参数
http://www.quiee.com.cn/archives/592/ resin resin3.0 及前期版本内存设置, 如下: 启动时设置虚拟内存: unix> bin/httpd.sh ...
- Caused by: java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/
Caused by: Java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ ...
- IOT表
以前在接触索引的时候,就想过要是表字段太少,索引效果不是很不好吗,直接用索引不是更直接吗?后来因为懒惰也没有去查找相关资料.正好今天看到了table organization index,看了一下,实 ...
- centos7(vmware install) 安装EMQ注意事项 ---控制台远程访问
若想远端访问控制台,需打开对于端口 TCP 服务端口占用 EMQ 2.0 消息服务器默认占用的 TCP 端口包括: 1883 MQTT 协议端口 8883 MQTT/SSL 端口 8083 MQTT/ ...
- DEDECMS 添加栏目图片
当我们一个栏目列表都用缩略图来表示产,而不仅仅只是文字,如果没有这项功能会非常麻烦,网上有很多这方面的资料,但是都试过了有很多问题,自己研究一下,测试基本通过.需要新加字段 typeimg 后台执行S ...
- sklearn中xgboost模块中plot_importance函数(特征重要性)
# -*- coding: utf-8 -*- """ ######################################################### ...
- atitit。全局变量的设计与实现 java php的异同
atitit.全局变量的设计与实现 java php的异同 1. 超级全局(Superglobals)"变量 1 2. 全局变量和"global"关键字 1 3. 更加好 ...
- [华为机试练习题]5.IP地址推断有效性
题目 推断输入的字符串是不是一个有效的IP地址 具体描写叙述: 请实现例如以下接口 boolisIPAddressValid(constchar* pszIPAddr) 输入:pszIPAddr 字符 ...
- zooKeeper_《ZooKeeper官方指南》一致性保障
转 http://ifeve.com/zookeeper-consistency-guarantees/ 本文翻译自<ZooKeeper官方指南>,译者:追云,校对:追云 一致性保障 Zo ...
- 【ZT】NUMA架构的CPU -- 你真的用好了么?
本文从NUMA的介绍引出常见的NUMA使用中的陷阱,继而讨论对于NUMA系统的优化方法和一些值得关注的方向. 文章欢迎转载,但转载时请保留本段文字,并置于文章的顶部 作者:卢钧轶(cenalulu) ...