传送门

\(2-sat\)的模板题,首先得出题目中的二元关系为:对于有矛盾的\(x_i,x_j\),至多选择一个,那么连边\(x_i\rightarrow x_j',x_j\rightarrow x_i'\)。

在这里这个关系是明确的关系,我们连边时只用连明确的关系即可。假设不选\(x_i\),我们也不知道\(x_j\)选不选,可选可不选,所以不用从\(x_i'\)出发连边。

然后题目因为要求字典序最小,据说\(trajan\)缩点那样搞可能出问题,因为数据范围比较小,所以直接\(O(nm)\)暴力染色就行了QAQ

代码如下:

/*
* Author: heyuhhh
* Created Time: 2019/11/29 14:52:58
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <cstring>
#include <iomanip>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << '\n'; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
#define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 8005; int n, m;
vector <int> g[N << 1]; int get(int x) {
if(x % 2) return x + 1;
return x - 1;
} int cnt;
int col[N << 1], tmp[N]; bool paint(int x) {
if(col[x]) {
if(col[x] % 2 == 0) return false;
return true;
}
tmp[++cnt] = x;
col[x] = 1; col[get(x)] = 2;
for(auto y : g[x]) {
if(!paint(y)) return false;
}
return true;
} bool work() {
memset(col, 0, sizeof(col));
for(int i = 1; i <= 2 * n; i++) {
if(col[i]) continue;
cnt = 0;
if(!paint(i)) {
for(int j = 1; j <= cnt; j++) col[tmp[j]] = col[get(tmp[j])] = 0;
cnt = 0;
if(!paint(get(i))) return false;
}
}
return true;
} void run(){
for(int i = 1; i <= 2 * n; i++) g[i].clear();
for(int i = 1; i <= m; i++) {
int u, v; cin >> u >> v;
g[u].push_back(get(v));
g[v].push_back(get(u));
g[get(u)].push_back(v);
g[get(v)].push_back(u);
}
if(work()) {
for(int i = 1; i <= 2 * n; i++)
if(col[i] == 1) cout << i << '\n';
} else cout << "NIE" << '\n';
} int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
while(cin >> n >> m) run();
return 0;
}

【HDU1814】Peaceful Commission(2-sat+暴力染色)的更多相关文章

  1. hdu1814 Peaceful Commission

    hdu1814 Peaceful Commission 题意:2-sat裸题,打印字典序最小的 我写了三个 染色做法,正解 scc做法,不管字典序 scc做法,错误的字典序贪心 #include &l ...

  2. HDU1814(Peaceful Commission) 【2-SAT DFS暴力求最小字典序的模板】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1814 题意:给出一个数n,代表有n个党派,每个党派要求派出其中一个人去参加会议,且只能派出一人.给出m ...

  3. HDU1814 Peaceful Commission 2-sat

    原文链接http://www.cnblogs.com/zhouzhendong/p/8099115.html 题目传送门 - HDU1814 题面 Description 根据宪法,Byteland民 ...

  4. hdu1814 Peaceful Commission——2-SAT

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1814 第一次的2-SAT,推荐博客:https://blog.csdn.net/jarjingx/arti ...

  5. HDU-1814 Peaceful Commission 2sat

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1814 简单的2sat题. //STATUS:C++_AC_390MS_996KB #include & ...

  6. hdu1814 Peaceful Commission,2-sat

    题目大意:一国有n个党派.每一个党派在议会中都有2个代表,现要组建和平委员会,要从每一个党派在议会的代表中选出1人,一共n人组成和平委员会.已知有一些代表之间存在仇恨,也就是说他们不能同一时候被选为和 ...

  7. 【2-SAT(最小字典序/暴力染色)】HDU1814-Peaceful Commission

    [题目大意] 和平委员会每个党派有2个人,只能派出其中1个,其中有一些人之间互相讨厌不能同时派出.求出派遣方案,如果有多种方案输出字典序最小的方案. [思路] 最小字典序只能用暴力染色.初始时均没有染 ...

  8. 2-sat——暴力染色输出方案hdu1814

    因为要求输出字典序最小的解,所以用暴力染色 具体有点像二分图染色 遍历0-2*n-1个点,尝试将每个点染成1,该点所能到达的所有点都要染成1 如果不行,则把上该点的影响消除,再把对立点染成1,如果还不 ...

  9. 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 ...

随机推荐

  1. 初级模拟电路:3-10 BJT实现开关电路

    回到目录 1. 基本用法 用BJT晶体管实现开关功能是经常会用到的实用电路.和逻辑门电路类似,当BJT用于开关电路时,也只工作于饱和区和截止区. 开关功能的实现电路如下图所示,负载可以是发光二极管.电 ...

  2. linux下关闭selinux

    找到 /etc/sysconfig/selinux 文件 修改 SELINUX=enable 使之 SELINUX=disable 重启 reboot

  3. Linux别名工具alias的使用

    偶然发现Linux支持自定义命令 现在Linux环境上经常有多个python环境,但是python2马上要停止维护了,所以我想多使用python3,但是Linux上的python命令调用的是pytho ...

  4. 以太网驱动的流程浅析(二)-Ifconfig的详细代码流程【原创】

    以太网驱动流程浅析(二)-ifconfig的详细代码流程 Author:张昺华 Email:920052390@qq.com Time:2019年3月23日星期六 此文也在我的个人公众号以及<L ...

  5. May 26th, 2019. Week 22nd, Sunday

    A real loser is somebody that's so afraid of not winning, they don't even try. 真正的失败者,是那些因为害怕不能成功,就连 ...

  6. github.com/pkg/errors库学习

    为了理解go error,进一步学习github.com/pkg/errors作的训练. http://www.shtml.net/article/content/tok/48369/id/37733 ...

  7. css 适配

    https://blog.csdn.net/weixin_35467885/article/details/80778992 1.通过link方法 link方法引入媒体类型其实就是在标签引用样式的时候 ...

  8. csrf攻击与csrf防御

    CSRF(Cross-site request forgery)跨站请求伪造,也被称为“One Click Attack”或者Session Riding,通常缩写为CSRF或者XSRF,是一种对网站 ...

  9. IT兄弟连 HTML5教程 HTML5的基本语法 了解Web

    HTML也是计算机编程语言,但由于功能简单易用,不涉及业务逻辑,算是编程语言中最简单的了.其实学习HTML这门语言,就是在学习一个个HTML标记的使用,标记的名称和使用不是自定义的,它的功能及用法是已 ...

  10. 关于powermock报错org.powermock.reflect.exceptions.FieldNotFoundException: Field 'fTestClass' was not found in class org.junit.internal.runners.MethodValidator.问题解决

    事件背景 使用PowerMock模拟一个局部变量,添加@RunWith(PowerMockRunner.class).@PrepareForTest(StudentService.class)注解成功 ...