【HDU1814】Peaceful Commission(2-sat+暴力染色)
\(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+暴力染色)的更多相关文章
- hdu1814 Peaceful Commission
hdu1814 Peaceful Commission 题意:2-sat裸题,打印字典序最小的 我写了三个 染色做法,正解 scc做法,不管字典序 scc做法,错误的字典序贪心 #include &l ...
- HDU1814(Peaceful Commission) 【2-SAT DFS暴力求最小字典序的模板】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1814 题意:给出一个数n,代表有n个党派,每个党派要求派出其中一个人去参加会议,且只能派出一人.给出m ...
- HDU1814 Peaceful Commission 2-sat
原文链接http://www.cnblogs.com/zhouzhendong/p/8099115.html 题目传送门 - HDU1814 题面 Description 根据宪法,Byteland民 ...
- hdu1814 Peaceful Commission——2-SAT
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1814 第一次的2-SAT,推荐博客:https://blog.csdn.net/jarjingx/arti ...
- HDU-1814 Peaceful Commission 2sat
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1814 简单的2sat题. //STATUS:C++_AC_390MS_996KB #include & ...
- hdu1814 Peaceful Commission,2-sat
题目大意:一国有n个党派.每一个党派在议会中都有2个代表,现要组建和平委员会,要从每一个党派在议会的代表中选出1人,一共n人组成和平委员会.已知有一些代表之间存在仇恨,也就是说他们不能同一时候被选为和 ...
- 【2-SAT(最小字典序/暴力染色)】HDU1814-Peaceful Commission
[题目大意] 和平委员会每个党派有2个人,只能派出其中1个,其中有一些人之间互相讨厌不能同时派出.求出派遣方案,如果有多种方案输出字典序最小的方案. [思路] 最小字典序只能用暴力染色.初始时均没有染 ...
- 2-sat——暴力染色输出方案hdu1814
因为要求输出字典序最小的解,所以用暴力染色 具体有点像二分图染色 遍历0-2*n-1个点,尝试将每个点染成1,该点所能到达的所有点都要染成1 如果不行,则把上该点的影响消除,再把对立点染成1,如果还不 ...
- 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 ...
随机推荐
- Flask报如下错误:SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
在同一个项目中由于flask_sqlalchemy版本不同,有时会报如下错误 错误信息如下: flask_sqlalchemy\__init__.py:: UserWarning: SQLALCHEM ...
- 在没有备份的情况下重新创建丢失的数据文件 (Doc ID 1149946.1)
Recreating a missing datafile with no backups (Doc ID 1149946.1) APPLIES TO: Oracle Database - Enter ...
- 【使用篇二】SpringBoot使用JdbcTemplate操作数据库(12)
Spring对数据库的操作在jdbc上面做了深层次的封装,提供了JdbcTemplate模板. 在SpringBoot使用JdbcTemplate很简单: 引入数据库驱动包(mysql或oracle) ...
- CSP-S 2019初赛前后小记
Preface 做完了初赛前的最后一场模拟赛,虽然这场做的挺好.但由于之前的一场难度很高,再加上自己犯了一些ZZ错误因此对初赛也有了一些不安之情 想起去年自己对于初赛的态度,实在是愧疚虽然去年文化课弃 ...
- 理解Promise.all,Promise.all与Promise.race的区别,如何让Promise.all在rejected失败后依然返回resolved成功结果
壹 ❀ 引 我在 es6入门4--promise详解 这篇文章中有详细介绍Promise对象的用法,文章主题更偏向于对于Promise概念的理解与各方法基本使用介绍:而世上一个比较有趣的问题就是,即 ...
- electron应用生成exe程序并打包过程记录
1.写好应用程序后,安装 electron-packager 在 package.json 文件中加入配置项目 "scripts": { "build": &q ...
- 自己实现LinkedList
public class MyLinkedList<E> { private Node first; private int size; public int size(){ return ...
- Elasticsearch 6.x版本全文检索学习之Search API
Elasticsearch 6.x版本全文检索学习之Search API. 1).Search API,实现对es中存储的数据进行查询分析,endpoind为_search,如下所示. 方式一.GET ...
- PlayJava Day008
今日所学: /* 2019.08.19开始学习,此为补档. */ 1.包装类:对基本数据类型进行封装,使其具有属性和方法. Integer s1 = new Integer(123) ; Intege ...
- PlayJava Day001
今日所学: /* 2019.08.19开始学习,此为补档. */ 三目(元)运算符 格式:(表达式)? 表达式为true返回值A : 表达式为false返回值B 例: String s=2>3 ...