hdu1814 Peaceful Commission

题意:2-sat裸题,打印字典序最小的


我写了三个

  1. 染色做法,正解
  2. scc做法,不管字典序
  3. 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的更多相关文章

  1. HDU1814 Peaceful Commission 2-sat

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

  2. HDU-1814 Peaceful Commission 2sat

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

  3. hdu1814 Peaceful Commission,2-sat

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

  4. hdu1814 Peaceful Commission——2-SAT

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

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

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

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

  7. hdu 1814 Peaceful Commission (2-sat 输出字典序最小的路径)

    Peaceful Commission Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  8. Peaceful Commission

    Peaceful Commission Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...

  9. HDOJ 1814 Peaceful Commission

    经典2sat裸题,dfs的2sat能够方便输出字典序最小的解... Peaceful Commission Time Limit: 10000/5000 MS (Java/Others)    Mem ...

随机推荐

  1. 初识正则表达式matcher.group

    matcher.group中group是匹配()的,group(0)指的是整个串,group(1) 指的是第一个括号里的内容,group(2)指的第二个括号里的内容,以此类推. 例如: str = & ...

  2. mysql性能优化分析 --- 下篇

    概要回顾 之前看过<高性能mysql>对mysql数据库有了系统化的理解,虽然没能达到精通,但有了概念,遇到问题时会有逻辑条理的分析; 这回继上次sql分析结果的一个继续延伸分析,我拿了; ...

  3. [Kubernetes]深入理解StatefulSet

    前面我写的一系列博客,如果你能够耐心看到这一篇,那你应该对一个概念就不是太陌生了:Deployment. 为什么提这个概念呢,这就要说到Deployment的一个不足了.Deployment不足以覆盖 ...

  4. TCP-IP详解笔记8

    TCP-IP详解笔记8 TCP超时与重传 下层网络层(IP)可能出现丢失, 重复或丢失包的情况, TCP协议提供了可靠的数据传输服务. TCP启动重传操作, 重传尚未确定的数据. 基于时间重传. 基于 ...

  5. Android应用市场的帮助类

    写了一个Android应用市场的帮助类,如下: public class MarketUtils { public static final String MARKET_DATA = "ma ...

  6. 关于el-upload上传

    <el-upload class="edit-input-upload" :action="config.baseUrl + '/joinus/candidate/ ...

  7. kmp算法 模板

    #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #inclu ...

  8. Gradle安装步骤

    一. Gralde介绍 Gradle是基于Groovy语言的项目自动化建构工具,在使用Gradle之前常用的构建工具有Ant和Maven,使用这些工具我们可以用来管理项目依赖,打包,部署和发布等.使用 ...

  9. 论文阅读笔记五十五:DenseBox: Unifying Landmark Localization with End to End Object Detection(CVPR2015)

    论文原址:https://arxiv.org/abs/1509.04874 github:https://github.com/CaptainEven/DenseBox 摘要 本文先提出了一个问题:如 ...

  10. mysql按天,小时,半小时,N分钟,分钟进行数据分组统计

    转自:https://blog.csdn.net/u010946448/article/details/83752984#_75