hdu1814 Peaceful Commission
Posted candy99
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
", 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
", i);
else printf("%d
", 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
", 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
", i, belong[a], belong[b]);
}
G::topo_sort();
for(int i=1; i<=n<<1; i++)
if(G::col[belong[i]] == 1) printf("%d
", 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
", 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
", 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
", 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
", 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
", i, belong[a], belong[b]);
}
G::topo_sort();
//for(int i=1; i<=n<<1; i++) printf("col %d %d %d
", i, belong[i], G::col[i]);
for(int i=1; i<=n<<1; i+=2) {
if(G::col[belong[i]] == 1) printf("%d
", i);
else printf("%d
", id(i));
}
}
}
以上是关于hdu1814 Peaceful Commission的主要内容,如果未能解决你的问题,请参考以下文章
HDU1814 Peaceful Commission 2-sat
HDU 1814 Peaceful Commission(2-sat 模板题输出最小字典序解决方式)
hdu1814Peaceful Commission2-SAT输出最小解