1. 程式人生 > >Eliminate the Conflict HDU - 4115(2-sat 建圖 hhh)

Eliminate the Conflict HDU - 4115(2-sat 建圖 hhh)

ems clock memset void cloc define str mat sin

題意:

  石頭剪刀布 分別為1、2、3,有n輪,給出了小A這n輪出什麽,然後m行,每行三個數a b k,如果k為0 表示小B必須在第a輪和第b輪的策略一樣,如果k為1 表示小B在第a輪和第b輪的策略不一樣,如果又一輪小B輸了,那整個就輸了,求小B能否戰勝小A

解析:

  寫出來矛盾的情況 建圖就好啦

可能我建的麻煩了。。。不過。。我喜歡 hhhhh

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include 
<cctype> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <bitset> #define rap(i, a, n) for(int i=a; i<=n; i++) #define rep(i, a, n) for(int i=a; i<n; i++) #define lap(i, a, n) for(int i=n; i>=a; i--) #define
lep(i, a, n) for(int i=n; i>a; i--) #define rd(a) scanf("%d", &a) #define rlld(a) scanf("%lld", &a) #define rc(a) scanf("%c", &a) #define rs(a) scanf("%s", a) #define pd(a) printf("%d\n", a); #define plld(a) printf("%lld\n", a); #define pc(a) printf("%c\n", a); #define ps(a) printf("%s\n", a); #define
MOD 2018 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 1e5 + 10, INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff; int n, m; int a[maxn]; vector<int> G[maxn]; int sccno[maxn], vis[maxn], low[maxn], scc_clock, scc_cnt; stack<int> S; void init() { mem(sccno, 0); mem(vis, 0); mem(low, 0); scc_clock = scc_cnt = 0; for(int i = 0; i < maxn; i++) G[i].clear(); } void dfs(int u) { low[u] = vis[u] = ++scc_clock; S.push(u); for(int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if(!vis[v]) { dfs(v); low[u] = min(low[u], low[v]); } else if(!sccno[v]) { low[u] = min(low[u], vis[v]); } } if(low[u] == vis[u]) { scc_cnt++; for(;;) { int x = S.top(); S.pop(); sccno[x] = scc_cnt; if(x == u) break; } } } bool check() { for(int i = 0; i < n * 2; i += 2) if(sccno[i] == sccno[i + 1]) { return false; } return true; } int main() { int T, kase = 0; cin >> T; while(T--) { init(); int u, v, w; cin >> n >> m; for(int i = 0; i < n; i++) cin >> a[i]; for(int i = 1; i <= m; i++) { cin >> u >> v >> w; u--, v--; int x = a[u], y = a[v]; if(w == 1) { if(x == y) { G[u << 1].push_back(v << 1 | 1); G[v << 1 | 1].push_back(u << 1); G[u << 1 | 1].push_back(v << 1); G[v << 1].push_back(u << 1 | 1); } else if(x != y) { if(x == 1 && y == 2 || y == 1 && x == 2) { if(y == 1 && x == 2) swap(u, v); G[u << 1 | 1].push_back(v << 1 | 1), G[v << 1].push_back(u << 1); } else if(x == 1 && y == 3 || y == 1 && x == 3) { if(y == 1 && x == 3) swap(u, v); G[v << 1 | 1].push_back(u << 1 | 1), G[u << 1].push_back(v << 1); } else if(x == 2 && y == 3 || x == 3 && y == 2) { if(x == 3 && y == 2) swap(u, v); G[u << 1 | 1].push_back(v << 1 | 1), G[v << 1].push_back(u << 1); } } } else { if(x == y) { G[u << 1].push_back(v << 1), G[u << 1 | 1].push_back(v << 1 | 1); G[v << 1].push_back(u << 1), G[v << 1 | 1].push_back(u << 1 | 1); } else { if(x == 1 && y == 2 || x == 2 && y == 1) { if(x == 2 && y == 1) swap(u, v); G[u << 1 | 1].push_back(v << 1), G[v << 1].push_back(u << 1 | 1); G[u << 1].push_back(u << 1 | 1), G[v << 1 | 1].push_back(v << 1); } else if(x == 1 && y == 3 || x == 3 && y == 1) { if(x == 3 && y == 1) swap(u, v); G[u << 1].push_back(v << 1 | 1), G[v << 1 | 1].push_back(u << 1); G[u << 1 | 1].push_back(u << 1), G[v << 1].push_back(v << 1 | 1); } else if(x == 2 && y == 3 || y == 2 && x == 3) { if(y == 2 && x == 3) swap(u, v); G[u << 1 | 1].push_back(v << 1), G[v << 1].push_back(u << 1 | 1); G[u << 1].push_back(u << 1 | 1), G[v << 1 | 1].push_back(v << 1); } } } } for(int i = 0; i < n * 2; i++) if(!vis[i]) dfs(i); printf("Case #%d: ", ++kase); if(check()) { cout << "yes" << endl; } else cout << "no" << endl; } return 0; }

Eliminate the Conflict HDU - 4115(2-sat 建圖 hhh)