HDU 4115 Eliminate the Conflict 【2-SAT】


传送门:HDU 4115


Eliminate the Conflict
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

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…



题意:

Alice和Bob两个人玩剪刀石头布,他们改变游戏规则,Alice知道 n 轮里Bob每轮会出什么,其中 1 代表石头,2 代表布,3 代表剪刀。相对的Bob 将对 Alice 提出m条限制Alice必须遵守:

给出 m 行,每行为 a、b、k,如果 k 是 0,代表 Alice 第 a 轮与 b 轮 出拳必须相同,如果 k 是 1,代表 Alice 第 a 轮与第 b 轮出拳必须不相同

一旦 Alice 破坏限制规则,或者输了一局,就代表 Alice 彻底输了,求根据所给输入判断 Alice 能否获胜。


题解:

如果Alice想要获胜,那么她在遵守所有条件的情况下就不能输一场,即每轮只能打平或者赢。
每轮都知道Bob的出拳,由于只能打平和赢,所以Alice每轮也只有两个出拳的选择,加上限制条件,如此的话便满足2-SAT的条件,于是可以确定这题可以用2-SAT解决。

下面对m次的限制来分析矛盾:
假设第a轮可以出a1,a2,第b轮可以出b1,b2。
如果k=0,即两轮出拳应该相同

  • 如果a1!=b1,矛盾,所以应该建边 a1->b2,b1->a2(建立a2->b1是不对的,因为只知道a1,b1的关系
  • 如果a1!=b2,矛盾,所以应该建边 a1->b1,b2->a2
  • 如果a2!=b1,矛盾,建立 a2->b2,b1->a1
  • 如果a2!=b2,矛盾,建立 a2->b1,b2->a1

如果k=1,即两轮出拳应该不同,与k=0相似。

建完边,跑一遍2-SAT判断一下可行性即可。



AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=11000;
int B[N][2];
bool mark[2*N];
int stack[2*N];
int n,m,top,t;
vector<int> g[2*N];
void init()
{
  memset(mark,false,sizeof(mark));
  for(int i=0;i<2*N;i++)
  {
    g[i].clear();
  }
}
void addedge(int x,int idx,int y,int idy)
{
  x=2*x+idx;
  y=2*y+idy;
  g[x].push_back(y);
}
bool dfs(int x)
{
  if(mark[x^1]) return false;
  if(mark[x]) return true;
  mark[x]=true;
  stack[++top]=x;
  int len=g[x].size();
  for(int i=0;i<len;i++)
  {
    if(!dfs(g[x][i])) return false;
  }
  return true;
}
bool twosat()
{
  for(int i=0;i<2*n;i+=2)
  {
    if(!mark[i]&&!mark[i+1])
    {
      top=0;
      if(!dfs(i))
      {
        while(top>0)
          mark[stack[top--]]=false;
        if(!dfs(i+1)) return false;
      }
    }
  }
  return true;
}
int main()
{
  int a,b,k;
  scanf("%d",&t);
  int ca=0;
  while(t--)
  {
    init();
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
    {
      scanf("%d",&B[i][0]);
      B[i][0]--;//平局
      B[i][1]=(B[i][0]+1)%3;//赢
    }
    for(int i=0;i<m;i++)
    {
      scanf("%d%d%d",&a,&b,&k);
      a--;b--;
      if(k==0)//相同
      {
          if(B[a][0]!=B[b][0])//a1,b1不同
          {
            addedge(a,0,b,1);//a1->b2
            addedge(b,0,a,1);//b1->a2
          }
          if(B[a][0]!=B[b][1])//a1,b2不同
          {
            addedge(a,0,b,0);//a1->b1
            addedge(b,1,a,1);//b2->a2
          }
          if(B[a][1]!=B[b][0])//a2,b1不同
          {
            addedge(a,1,b,1);//a2->b2
            addedge(b,0,a,0);//b1->a1
          }
          if(B[a][1]!=B[b][1])//a2,b2不同
          {
            addedge(a,1,b,0);//a2->b1
            addedge(b,1,a,0);//b2->a1
          }
      }
      else//不同
      {
        if(B[a][0]==B[b][0])//a1,b1相同
        {
          addedge(a,0,b,1);//a1->b2
          addedge(b,0,a,1);//b1->a2
        }
        if(B[a][0]==B[b][1])//a1,b2相同
        {
          addedge(a,0,b,0);//a1->b1
          addedge(b,1,a,1);//b2->a2
        }
        if(B[a][1]==B[b][0])//a2,b1相同
        {
          addedge(a,1,b,1);//a2->b2
          addedge(b,0,a,0);//b1->a1
        }
        if(B[a][1]==B[b][1])//a2,b2相同
        {
          addedge(a,1,b,0);//a2->b1
          addedge(b,1,a,0);//b2->a1
        }
      }
    }
    if(twosat()) printf("Case #%d: yes\n",++ca);
    else printf("Case #%d: no\n",++ca);
  }
  return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值