HDU 4115 Eliminate the Conflict 2-SAT

11 篇文章 0 订阅

Alice已知N轮剪刀石头布中Bob出的是什么。
同时Alice存在(a,b,c),c=0表示第a和b轮出拳相同,否则不同。
问Alice是否存在可行方案不输。

因为必须不输,所以实际只有2种选择。。按冲突建图就好了

#include <cstdio>
#include <memory.h>
#include <cmath>
#include <algorithm>
using namespace std;
#define sqr(i) ((i)*(i))
#define ms(i) memset(i,0,sizeof(i))
const int N = 20005, M = 600000;
int a[N], b[N];
struct TwoSAT {
    int h[N], p[M], v[M], vis[N], stk[N], top, cnt;

    void add(int a, int x, int b, int y) {
        a = 2 * a + x; b = 2 * b + y;
        p[++cnt] = h[a ^ 1]; v[cnt] = b; h[a ^ 1] = cnt;
        p[++cnt] = h[b ^ 1]; v[cnt] = a; h[b ^ 1] = cnt;
    }

    void init() {
        cnt = 0; ms(vis); ms(h);
    }

    bool solve(int n) {
        for(int i=0;i<2*n;i+=2) if (!vis[i]&&!vis[i^1]) {
            top = 0;
            if (!dfs(i)) {
                while (top) vis[stk[--top]] = 0;
                if (!dfs(i + 1)) return 0;
            }
        }
        return 1;
    }

    bool dfs(int x) {
        if (vis[x ^ 1]) return 0;
        if (vis[x]) return 1;
        vis[x] = 1; stk[top++] = x;
        for (int i = h[x]; i; i = p[i])
            if (!dfs(v[i])) return 0;
        return 1;
    }
} g;

int main() {
    #define id(i,j) ((i)*2+(j))
    int n, m, i, u, v, w, t, kase = 0;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &n, &m);
        g.init();
        for(i=0;i<n;++i) {
            scanf("%d", a + i);
            b[i] = a[i] % 3 + 1;
            if (a[i] > b[i]) swap(a[i], b[i]);
        }
        while (m--) {
            scanf("%d%d%d", &u, &v, &w);
            --u; --v;
            if (w == 0) {
                if (a[u] != a[v]) g.add(u,0,v,0);
                if (a[u] != b[v]) g.add(u,0,v,1);
                if (b[u] != a[v]) g.add(u,1,v,0);
                if (b[u] != b[v]) g.add(u,1,v,1);
            } else if (w == 1) {
                if (a[u] == a[v]) g.add(u,0,v,0);
                if (a[u] == b[v]) g.add(u,0,v,1);
                if (b[u] == a[v]) g.add(u,1,v,0);
                if (b[u] == b[v]) g.add(u,1,v,1);
            }
        }
        printf("Case #%d: ", ++kase);
        puts(g.solve(n) ? "yes" : "no");
    }
    return 0;
}

Eliminate the Conflict

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2056 Accepted Submission(s): 893

Problem Description

Conflicts are everywhere in the world, from the young to the elderly, from families to countries. Conflicts cause quarrels, fights or even wars. How wonderful the world will be if all conflicts can be eliminated.
Edward contributes his lifetime to invent a ‘Conflict Resolution Terminal’ and he has finally succeeded. This magic item has the ability to eliminate all the conflicts. It works like this:
If any two people have conflict, they should simply put their hands into the ‘Conflict Resolution Terminal’ (which is simply a plastic tube). Then they play ‘Rock, Paper and Scissors’ in it. After they have decided what they will play, the tube should be opened and no one will have the chance to change. Finally, the winner have the right to rule and the loser should obey it. Conflict Eliminated!
But the game is not that fair, because people may be following some patterns when they play, and if the pattern is founded by others, the others will win definitely.
Alice and Bob always have conflicts with each other so they use the ‘Conflict Resolution Terminal’ a lot. Sadly for Bob, Alice found his pattern and can predict how Bob plays precisely. She is very kind that doesn’t want to take advantage of that. So she tells Bob about it and they come up with a new way of eliminate the conflict:
They will play the ‘Rock, Paper and Scissors’ for N round. Bob will set up some restricts on Alice.
But the restrict can only be in the form of “you must play the same (or different) on the ith and jth rounds”. If Alice loses in any round or break any of the rules she loses, otherwise she wins.
Will Alice have a chance to win?

Input

The first line contains an integer T(1 <= T <= 50), indicating the number of test cases.
Each test case contains several lines.
The first line contains two integers N,M(1 <= N <= 10000, 1 <= M <= 10000), representing how many round they will play and how many restricts are there for Alice.
The next line contains N integers B1,B2, …,BN, where Bi represents what item Bob will play in the ith round. 1 represents Rock, 2 represents Paper, 3 represents Scissors.
The following M lines each contains three integers A,B,K(1 <= A,B <= N,K = 0 or 1) represent a restrict for Alice. If K equals 0, Alice must play the same on Ath and Bth round. If K equals 1, she must play different items on Ath and Bthround.

Output

For each test case in the input, print one line: “Case #X: Y”, where X is the test case number (starting with 1) and Y is “yes” or “no” represents whether Alice has a chance to win.

Sample Input

2
3 3
1 1 1
1 2 1
1 3 1
2 3 1
5 5
1 2 3 2 1
1 2 1
1 3 1
1 4 1
1 5 1
2 3 0

Sample Output

Case #1: no
Case #2: yes

Hint

‘Rock, Paper and Scissors’ is a game which played by two person. They should play Rock, Paper or Scissors by their hands at the same time.
Rock defeats scissors, scissors defeats paper and paper defeats rock. If two people play the same item, the game is tied..

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值