hdu1814 Peaceful Commission
hdu1814 Peaceful Commission
题意:2-sat裸题,打印字典序最小的
我写了三个
- 染色做法,正解
- scc做法,不管字典序
- scc做法,错误的字典序贪心
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 2e4+5, M = 1e5+5;
inline int read() {
int x = 0, f = 1; char c = getchar();
while(c<'0' || c>'9') {if(c=='-') f=-1; c=getchar();}
while(c>='0' && c<='9') {x=x*10+c-'0'; c=getchar();}
return x * f;
}
int n, m;
struct edge {int v, ne;} e[M];
int cnt, h[N];
inline void ins(int u, int v) {
e[++cnt] = (edge) {v, h[u]}; h[u] = cnt;
}
inline int id(int x) {return ((x-1)^1)+1;}
int col[N], st[N], top;
bool dfs(int u) {
if(col[u]) return true;
if(col[id(u)]) return false;
col[u] = 1; st[top++] = u;
for(int i=h[u]; i; i=e[i].ne) {
int v = e[i].v;
if(!dfs(v)) return false;
}
return true;
}
bool check(int u) {
top = 1;
return dfs(u);
}
int main() {
freopen("in", "r", stdin);
while(cin >> n) {
cnt = 0;
memset(h, 0, sizeof(h));
memset(col, 0, sizeof(col));
m = read();
for(int i=1; i<=m; i++) {
int a = read(), b = read();
ins(a, id(b));
ins(b, id(a));
}
int flag = 0;
for(int i=1; i<=n<<1; i+=2) if(!col[i] && !col[id(i)]) {
if(!check(i)) {
while(top) col[ st[top--] ] = 0;
//for(int i=1; i<=n; i++) printf("col %d %d\n", i, col[i]);
if(!check(id(i))) {
puts("NIE"), flag = 1;
break;
}
}
}
if(flag) continue;
for(int i=1; i<=n<<1; i+=2) {
if(col[i]) printf("%d\n", i);
else printf("%d\n", i+1);
}
}
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 2e4+5, M = 1e5+5;
inline int read() {
int x = 0, f = 1; char c = getchar();
while(c<'0' || c>'9') {if(c=='-') f=-1; c=getchar();}
while(c>='0' && c<='9') {x=x*10+c-'0'; c=getchar();}
return x * f;
}
int n, m;
struct edge {int v, ne;} e[M];
int cnt, h[N];
inline void ins(int u, int v) {
e[++cnt] = (edge) {v, h[u]}; h[u] = cnt;
}
int dfn[N], dfc, scc, belong[N], low[N];
int st[N], top;
void dfs(int u) {
dfn[u] = low[u] = ++dfc;
st[++top] = u;
for(int i=h[u]; i; i=e[i].ne) {
int v = e[i].v;
if(!dfn[v]) {
dfs(v);
low[u] = min(low[u], low[v]);
} else if(!belong[v]) low[u] = min(low[u], dfn[v]);
}
if(dfn[u] == low[u]) {
scc++;
while(true) {
int x = st[top--];
belong[x] = scc;
if(x == u) break;
}
}
}
namespace G {
edge e[M];
int cnt, h[N], ind[N];
inline void ins(int u, int v) {
e[++cnt] = (edge) {v, h[u]}; h[u] = cnt;
ind[v] ++;
}
int q[N], head = 1, tail = 1;
#define pii pair<int, int>
#define fir first
#define sec second
//priority_queue<pii, vector<pii>, greater<pii> > q;
int col[N], opp[N];
void dfs_color(int u) {
if(col[u]) return;
col[u] = -1;
for(int i=h[u]; i; i=e[i].ne) dfs_color(e[i].v);
}
bool check() {
for(int i=1; i<=n; i++) if(belong[i] == belong[i+n]) return false;
return true;
}
void topo_sort() {
head = tail = 1;
for(int i=1; i<=scc; i++) if(!ind[i]) q[tail++] = i;
while(head != tail) {
int u = q[head++]; printf("uuu %d %d\n", u, col[u]);
if(col[u]) continue;
col[u] = 1;
dfs_color(opp[u]);
for(int i=h[u]; i; i=e[i].ne) {
int v = e[i].v;
ind[v] --;
if(ind[v] == 0) q[tail++] = v;
}
}
}
}
int main() {
freopen("in", "r", stdin);
while(cin >> n) {
m = read();
for(int i=1; i<=m; i++) {
int a = read(), b = read();
ins(a, b+n);
ins(b, a+n);
}
for(int i=1; i<=n<<1; i++) if(!dfn[i]) dfs(i);
if(!G::check()) {
puts("NIE");
continue;
}
for(int u=1; u<=n<<1; u++) {
int a = belong[u];
for(int i=h[u]; i; i=e[i].ne) {
int b = belong[e[i].v];
if(a != b) ins(b, a);
}
}
for(int i=1; i<=n; i++) {
int a = belong[2*i-1], b = belong[2*i];
G::opp[a] = b;
G::opp[b] = a;
printf("hi %d %d %d\n", i, belong[a], belong[b]);
}
G::topo_sort();
for(int i=1; i<=n<<1; i++)
if(G::col[belong[i]] == 1) printf("%d\n", i);
}
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 2e4+5, M = 1e5+5;
inline int read() {
int x = 0, f = 1; char c = getchar();
while(c<'0' || c>'9') {if(c=='-') f=-1; c=getchar();}
while(c>='0' && c<='9') {x=x*10+c-'0'; c=getchar();}
return x * f;
}
int n, m;
struct edge {int v, ne;} e[M];
int cnt, h[N];
inline void ins(int u, int v) {
e[++cnt] = (edge) {v, h[u]}; h[u] = cnt;
}
int dfn[N], dfc, scc, belong[N], low[N];
int st[N], top;
void dfs(int u) { //printf("dfs %d\n", u);
dfn[u] = low[u] = ++dfc;
st[++top] = u;
for(int i=h[u]; i; i=e[i].ne) {
int v = e[i].v;
if(!dfn[v]) {
dfs(v);
low[u] = min(low[u], low[v]);
} else if(!belong[v])
low[u] = min(low[u], dfn[v]);
}
if(dfn[u] == low[u]) {
scc++;
while(true) {
int x = st[top--];
belong[x] = scc;
if(x == u) break;
}
}
}
inline int id(int x) {
int t = ((x-1) >> 1) + 1;
if(x == t<<1) return x-1;
else return x+1;
}
int mn[N];
namespace G {
edge e[M];
int cnt, h[N], ind[N];
inline void ins(int u, int v) {
e[++cnt] = (edge) {v, h[u]}; h[u] = cnt;
ind[v] ++;
}
//int q[N], head = 1, tail = 1;
#define pii pair<int, int>
#define fir first
#define sec second
priority_queue<pii, vector<pii>, greater<pii> > q;
int col[N], opp[N];
void dfs_color(int u) {
if(col[u]) return;
col[u] = -1;
for(int i=h[u]; i; i=e[i].ne) dfs_color(e[i].v);
}
bool check() {
for(int i=1; i<=n; i++) if(belong[(i<<1)-1] == belong[i<<1]) return false;
return true;
}
void topo_sort() {
for(int i=1; i<=scc; i++) if(!ind[i]) q.push(make_pair(mn[i], i));
while(!q.empty()) {
int u = q.top().sec; q.pop(); printf("uuu %d %d\n", u, mn[u]);
if(col[u]) continue;
col[u] = 1;
dfs_color(opp[u]);
for(int i=h[u]; i; i=e[i].ne) {
int v = e[i].v;
ind[v] --;
if(ind[v] == 0) q.push(make_pair(mn[v], v));
}
}
}
}
int main() {
freopen("in", "r", stdin);
while(cin >> n) {
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(belong, 0, sizeof(belong));
dfc = scc = 0;
cnt = 0; G::cnt = 0;
memset(h, 0, sizeof(h));
memset(G::h, 0, sizeof(G::h));
memset(G::col, 0, sizeof(G::col));
memset(G::ind, 0, sizeof(G::ind));
memset(mn, 0x3f, sizeof(mn));
m = read();
for(int i=1; i<=m; i++) {
int a = read(), b = read();
ins(a, id(b));
ins(b, id(a));
//printf("id %d %d\n", id(a), id(b));
}
for(int i=1; i<=n<<1; i++) if(!dfn[i]) dfs(i);
if(!G::check()) {
puts("NIE");
continue;
}
for(int u=1; u<=n<<1; u++) {
int a = belong[u];
mn[a] = min(mn[a], u);
for(int i=h[u]; i; i=e[i].ne) {
int b = belong[e[i].v];
if(a != b) G::ins(b, a);
}
}
//for(int i=1; i<=n<<1; i++) printf("belong %d %d %d\n", i, belong[i], mn[belong[i]]);
for(int i=1; i<=n; i++) {
int a = belong[(i<<1)-1], b = belong[i<<1];
G::opp[a] = b;
G::opp[b] = a;
//printf("hi %d %d %d\n", i, belong[a], belong[b]);
}
G::topo_sort();
//for(int i=1; i<=n<<1; i++) printf("col %d %d %d\n", i, belong[i], G::col[i]);
for(int i=1; i<=n<<1; i+=2) {
if(G::col[belong[i]] == 1) printf("%d\n", i);
else printf("%d\n", id(i));
}
}
}
hdu1814 Peaceful Commission的更多相关文章
- HDU1814 Peaceful Commission 2-sat
原文链接http://www.cnblogs.com/zhouzhendong/p/8099115.html 题目传送门 - HDU1814 题面 Description 根据宪法,Byteland民 ...
- 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人组成和平委员会.已知有一些代表之间存在仇恨,也就是说他们不能同一时候被选为和 ...
- hdu1814 Peaceful Commission——2-SAT
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1814 第一次的2-SAT,推荐博客:https://blog.csdn.net/jarjingx/arti ...
- HDU1814(Peaceful Commission) 【2-SAT DFS暴力求最小字典序的模板】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1814 题意:给出一个数n,代表有n个党派,每个党派要求派出其中一个人去参加会议,且只能派出一人.给出m ...
- 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 ...
- hdu 1814 Peaceful Commission (2-sat 输出字典序最小的路径)
Peaceful Commission Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- Peaceful Commission
Peaceful Commission Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDOJ 1814 Peaceful Commission
经典2sat裸题,dfs的2sat能够方便输出字典序最小的解... Peaceful Commission Time Limit: 10000/5000 MS (Java/Others) Mem ...
随机推荐
- Technocup 2019 - Elimination Round 2
http://codeforces.com/contest/1031 (如果感觉一道题对于自己是有难度的,不要后退,懂0%的时候敲一遍,边敲边想,懂30%的时候敲一遍,边敲边想,懂60%的时候敲一遍, ...
- sql选择
关系型数据库遵循ACID规则 1.A (Atomicity) 原子性 原子性很容易理解,也就是说事务里的所有操作要么全部做完,要么都不做,事务成功的条件是事务里的所有操作都成功,只要有一个操作失败,整 ...
- 关于lamp环境搭建过程的教程
一.搭建lamp 的网址 https://lamp.sh/install.html 二.对于linux下上传图片或音频失败原因? 1.必须将文件夹的权限设置为apache 命令为:chown -R a ...
- oracle ORA-02292: 违反完整约束条件
我是处于工作中没用过oracle的状态,这不,记录下这个小小的问题.哈哈. 表是公司的平台组定义的.前几天为了测试程序,想删掉一些记录,然后使用delete语句,出现这个东东:oracle ORA-0 ...
- python 机器学习三剑客 之 Matplotlib
Matplotlib介绍: Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形 . 通过 Matplotlib,开发者可以仅需要几 ...
- P5302 [GXOI/GZOI2019]特技飞行
题目地址:P5302 [GXOI/GZOI2019]特技飞行 这里是官方题解(by lydrainbowcat) 题意 给 \(10^5\) 条直线,给 \(x = st\) 和 \(x = ed\) ...
- Linux启动activemq失败
第一种情况: 在网上查找错误,通过./activemq console命令可以查看到activemq启动的错误信息,另外在data/activemq.log文件中可以查看到错误日志. java.io. ...
- Linux库多重依赖
源文件: //world.cpp #include <stdio.h> void world(void) { printf("world.\n"); } //hello ...
- 结对-(first)
代码地址 https://github.com/CountZ3/bank.git 代码思想 允许进程动态地申请资源, 系统在每次实施资源分配之前,先计算资源分配的安全性, 若此次资源分配安全(即资源分 ...
- 总结web自动化测试页面常用字段的定位方法
在一次编写web自动脚本时,突然想到web页面常有的字段有:输入框,按钮,富文本输入框,下拉框选项,弹窗,表格,上传文件以及时间插件,以下总结的没有编写时间插件的用例了!以后碰到再更新, 以下是蹩脚代 ...