传送门

\(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. ORA-04045: errors during recompilation/revalidation of LBACSYS.LBAC_EVENTS

    使用orachk工具检查数据库实例的时候,发现报告里面有类似下面这样一些错误(最近有给Oracle 10g应用补丁PSU 10.2.0.5.180717,不清楚是这个产生的还是其他原因导致),使用脚本 ...

  2. Linux开发环境搭建三 使用mount -t cifs 挂载windows共享目录方法与问题解决

    转载链接:https://blog.csdn.net/fuyuande/article/details/82915800 嵌入式开发通常是在linux环境下编译,windows下开发,这就需要在lin ...

  3. python接口自动化根据请求接口类型进行封装

    根据不同的请求类型(GET/POST)进行接口请求封装 import requests import json class RunMain: def __init__(self, url, metho ...

  4. optix之纹理使用

    1.在OpenGL中生成纹理texture optix中的纹理直接使用OpenGL纹理的id,不能跨越OpenGL纹理,所以我们先在OpenGL环境下生成一张纹理. 这个过程就很熟悉了: void W ...

  5. Redux API

    Redux API ​ Redux的API非常少.Redux定义了一系列的约定(contract),同时提供少量辅助函数来把这些约定整合到一起. ​ Redux只关心如何管理state.在实际的项目中 ...

  6. 201871010116-祁英红《面向对象程序设计(java)》第十四周学习总结

    博文正文开头格式:(2分) 项目 内容 <面向对象程序设计(java)> https://home.cnblogs.com/u/nwnu-daizh/ 这个作业的要求在哪里 https:/ ...

  7. 201871010133-赵永军《面向对象程序设计(java)》第一周学习总结

    <面向对象程序设计(java)>第一周学习总结 正文开头: 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 ...

  8. Vue入门(二)

    1.vue3.0安装 cnpm install -g @vue/cli 或者 yarn global add @vue/cli //创建项目 vue create hello-world //运行 n ...

  9. H5混合应用之webview元素定位工具

    一.工具选择 webview元素定位有三种方式: 使用driver.page_source方法,将获取到的页面内容写入到一个html文件中,然后使用浏览器打开html文件,使用F12调试用具进行元素定 ...

  10. go语言中map每次遍历的顺序不同-问题分析

    WHAT? 发现下面这段代码,多次运行出的结果是不一样的 mapper := make(map[int]string) mapper[1] = "1" mapper[2] = &q ...