hdu1814Peaceful Commission(2-SAT)
Peaceful Commission
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3556 Accepted Submission(s):
1173
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.
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.
(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.
1 3
2 4
4
5
/*
最小字典序
直接暴力枚举DFS,首先将所有的点都置为未染色,然后从第一个点开始DFS染色,我们先尝试将i染
成1(答案中的颜色),将~i染成2,然后dfs i的所有后继并染色,如果对于后继j没有染色,那么将j然
后为1,~j染成2。如果后继j已经被染成2,则说明不能选则i,如果j已经染成1,则说明可以。
那么这些后继就可以被选择。如果选择i的时候失败了,那么必定要选择~i,如果也失败,则说明无解。否则
按次序选取下一个未被染色的点。时间复杂度O(nm)。
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 16001 using namespace std;
int col[maxn],head[maxn],ans[maxn];
int a,b,n,m,num,tot,cnt;
struct node
{
int u,v,next;
}e[maxn<<]; inline void add(int u,int v)
{
e[++num].u=u;
e[num].v=v;
e[num].next=head[u];
head[u]=num;
} bool dfs(int u)
{
if(col[u]==) return true;
if(col[u]==) return false;
col[u]=;col[u^]=;ans[cnt++]=u;
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].v;
if(!dfs(v)) return false;
}
return true;
} bool solve()
{
memset(col,,sizeof col);
for(int i=;i<n;i++)
{
if(col[i]) continue;
cnt=;
if(!dfs(i))
{
for(int j=;j<cnt;j++)
{
col[ans[j]]=;
col[ans[j]^]=;
}
if(!dfs(i^))return false;
}
}
return true;
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
n<<=;num=;
memset(head,,sizeof head);
while(m--)
{
scanf("%d%d",&a,&b);
a--;b--;
add(a,b^);add(b,a^);
}
if(solve())
{
for (int i=;i<n;i++)
if(col[i]==)
printf("%d\n",i+);
}
else printf("NIE\n");
}
return ;
}
hdu1814Peaceful Commission(2-SAT)的更多相关文章
- 【2-SAT(最小字典序/暴力染色)】HDU1814-Peaceful Commission
[题目大意] 和平委员会每个党派有2个人,只能派出其中1个,其中有一些人之间互相讨厌不能同时派出.求出派遣方案,如果有多种方案输出字典序最小的方案. [思路] 最小字典序只能用暴力染色.初始时均没有染 ...
- HDU1814Peaceful Commission求2-sa最小字典序
#include <iostream> #include <cstdio> #include <vector> #include <cstring> # ...
- 初涉2-SAT
2-SAT:有趣的图论模型 什么是2-SAT SAT是适定性(Satisfiability)问题的简称.之所以研究2-sat是因为当k>2时,k-sat问题已经被证明是NPC的了. 2-sat问 ...
- Peaceful Commission
Peaceful Commission Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 多边形碰撞 -- SAT方法
检测凸多边形碰撞的一种简单的方法是SAT(Separating Axis Theorem),即分离轴定理. 原理:将多边形投影到一条向量上,看这两个多边形的投影是否重叠.如果不重叠,则认为这两个多边形 ...
- 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 ...
- hdu 1814 Peaceful Commission (2-sat 输出字典序最小的路径)
Peaceful Commission Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题)
HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题) Description T ...
- hdu1814 Peaceful Commission
hdu1814 Peaceful Commission 题意:2-sat裸题,打印字典序最小的 我写了三个 染色做法,正解 scc做法,不管字典序 scc做法,错误的字典序贪心 #include &l ...
随机推荐
- TestNG异常测试
用@Test(expectedExceptions = xxx) 声明 package com.janson; import org.testng.annotations.Test; public c ...
- TestNG忽略测试
用@Test(enabled = false) 声明需要被忽略执行的测试方法 package com.janson; import org.testng.annotations.Test; publi ...
- graph.h
#ifndef _GRAPH_#define _GRAPH_#include<stdio.h>#include<stdlib.h>#include<string.h> ...
- node-sass 安装失败
安装 npm install 时偶尔遇到报错:没有安装python或node-sass 安装失败的问题,百度之后发现是被墙了,但根据百度的方法换了淘宝镜像和用了vpn都安装失败, 原因可能是没有卸载之 ...
- mybatis example 排序 语句
mybatis example 排序 语句 IntegralInfoExample integral = new IntegralInfoExample(); integral.createCrite ...
- Leetcode 51.N后问题
N后问题 n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后问题的解决方案. ...
- -sql语句练习50题(Mysql学习练习版)
–1.学生表 Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 –2.课程表 Course(c_id,c_name,t_id) – –课 ...
- Ubuntu 16.04常用快捷键(转)
注意:在Linux下Win键就是Super键 启动器 Win(长按) 打开启动器,显示快捷键 Win + Tab 通过启动器切换应用程序 Win + 1到9 与点击启动器上的图标效果一样 Win + ...
- gh-ost: triggerless online schema migrations:Blog by Shlomi Noach:
http://code.openark.org/blog/category/mysql https://rj03hou.github.io/mysql/gh-ost/
- 每日五题(jsp)
1.forward 和 redirect 的差别 答: 1.从地址栏显示来说 forward是server请求资源,server直接訪问目标地址的URL,把那个URL的响应内容读取过来,然后把这些内容 ...