2-SAT问题,麻烦就麻烦再需要输出解具体见赵爽论文。
可以判断有无解时,用一个映射,把与自己相对的“强联通分量”用hash映射出来,即hash[belong[i]] = belong[i+n]等等。这样在拓扑序染色时就比较方便了。
#include <iostream> #include <cstdlib> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <vector> #include <queue> #include <algorithm> #include <map> using namespace std; const int maxn = 1010; const int maxm = 1010*1010; struct Edge { int v, w; int next; }edge[maxm], edge2[maxm]; int first2[maxn]; int cnt2; int first[maxn], stack[maxn], ins[maxn], dfn[maxn], low[maxn]; int belong[maxn]; int n, m, k; int cnt; int scnt, top, tot; void init() { cnt = 0; cnt2 = 0; scnt = top = tot = 0; memset(first, -1, sizeof(first)); memset(first2, -1, sizeof(first2)); memset(dfn, 0, sizeof(dfn)); memset(ins, 0, sizeof(ins)); memset(low, 0, sizeof(low)); } void read_graph(int u, int v) { edge[cnt].v = v; edge[cnt].next = first[u], first[u] = cnt++; } void read_graph2(int u, int v) { edge2[cnt2].v = v; edge2[cnt2].next = first2[u], first2[u] = cnt2++; } void dfs(int u) { int v; low[u] = dfn[u] = ++tot; stack[top++] = u; ins[u] = 1; for(int e = first[u]; e != -1; e = edge[e].next) { v = edge[e].v; if(!dfn[v]) { dfs(v); low[u] = min(low[u], low[v]); } else if(ins[v]) { low[u] = min(low[u], dfn[v]); } } if(low[u] == dfn[u]) { scnt++; do { v = stack[--top]; belong[v] = scnt; ins[v] = 0; } while(u != v); } } void readint(int &x) { char c; while(!isdigit(c)) c = getchar(); x = 0; while(isdigit(c)) { x = x*10 + c-'0'; c = getchar(); } } void writeint(int x) { if(x > 9) writeint(x/10); putchar(x%10+'0'); } void Tarjan() { for(int v = 0; v < 2*n; v++) if(!dfn[v]) dfs(v); } void read_case() { init(); int x, y; char s1[10], s2[10]; while(m--) { int a, b; scanf("%d%s %d%s", &x, s1, &y, s2); if(s1[0] == 'h' && s2[0] == 'h') { read_graph(2*x+1, 2*y); read_graph(2*y+1, 2*x); } if(s1[0] == 'h' && s2[0] == 'w') { read_graph(2*x+1, 2*y+1); read_graph(2*y, 2*x); } if(s1[0] == 'w' && s2[0] == 'h') { read_graph(2*x, 2*y); read_graph(2*y+1, 2*x+1); } if(s1[0] == 'w' && s2[0] == 'w') { read_graph(2*x, 2*y+1); read_graph(2*y, 2*x+1); } } read_graph(0, 1); } int hash[maxn]; int check() { for(int i = 0; i < 2*n; i+=2) { if(belong[i] == belong[i+1]) return 0; else hash[belong[i]] = belong[i+1], hash[belong[i+1]] = belong[i]; } return 1; } int color[maxn]; int ind[maxn]; void toposort() { queue<int> Q; for(int i = 1; i <= scnt; i++) if(!ind[i]) Q.push(i); while(!Q.empty()) { int u = Q.front(); Q.pop(); if(!color[u]) color[u] = 1, color[hash[u]] = 2; for(int e = first2[u]; e != -1; e = edge2[e].next) { int v = edge2[e].v; if(--ind[v] == 0) Q.push(v); } } } void build() { memset(ind, 0, sizeof(ind)); memset(color, 0, sizeof(color)); for(int u = 0; u < 2*n; u++) { for(int e = first[u]; e != -1; e = edge[e].next) { int v = edge[e].v; if(belong[u] != belong[v]) { read_graph2(belong[v], belong[u]); ind[belong[u]]++; } } } toposort(); } void output() { for(int i = 2; i < 2*n; i++) { if(color[belong[i]] == color[belong[0]]) { if(i & 1) printf("%dh ", i/2); else printf("%dw ", i/2); } } printf(" "); } void solve() { read_case(); Tarjan(); if(check()) { build(); output(); } else printf("bad luck "); } int main() { while(~scanf("%d%d", &n, &m) && (n || m)) { solve(); } return 0; }