HDU 4115 Eliminate the Conflict (拆点+2-SAT)

Eliminate the Conflict

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


 

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

题意:

两个人玩石头剪刀布,玩n(n<=1e4)轮,m(m<=1e4)个限制条件,1表示石头,2表示布,3表示剪刀。

接下来n个数,给出每一轮对手出的啥。

接下来m行,每行三个数x,y,z  如果z=1,则你第x轮和第y轮出的不能相同,如果z=0,则你第x轮和第y轮出的必须相同。

如果这n轮你都没输(赢了或者平局),那么就输出yes,否则输出no。

思路:

这题真的够恶心的,把每一轮拆成三个点x,x+n,x+2*n 分别表示出石头、布、剪刀,然后x+3*n,x+4*n,x+5*n分别表示第x轮不出石头、布、剪刀。那么就变成了一个经典的2-SAT问题。

初始化:

如果第i轮对手出的石头,那么你就不能出剪刀,且如果你不出布那么你就得出石头,如果你不出石头那么你就得出布。

类比讨论三种情况。

同理,如果第i轮和第j轮对面出的啥,你就相应的加边,虽然加的比较多,但是仔细一看就会发现有规律。

代码(1A):

#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<string>
#include<queue>
#include<vector>
#include<map>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxn=60010;
int op[maxn],vis[maxn],low[maxn],dfn[maxn];
int pt[maxn],stk[maxn],color[maxn],pos[maxn],deg[maxn];
vector<int>vc[maxn],vc2[maxn];
int a[maxn];
int n,m,sig,cnt,tot,cont,nn;
void add(int x,int y){
    vc[x].push_back(y);
}
void top(){
    memset(pt,0,sizeof(pt));
    queue<int>s;
    for(int i=1;i<=sig;i++){
        if(deg[i]==0) s.push(i);
    }
    while(!s.empty()){
        int u=s.front();
        if(pt[u]==0){
            pt[u]=1;
            pt[pos[u]]=2;
        }
        s.pop();
        for(int i=0;i<vc2[u].size();i++){
            int v=vc2[u][i];
            deg[v]--;
            if(deg[v]==0) s.push(v);
        }
    }
    cont=0;
    for(int i=1;i<=n;i++) {
        if(pt[color[i]]==1) op[cont++]=i;
    }
}
void tarjan(int u){
    vis[u]=1;
    dfn[u]=low[u]=cnt++;
    stk[++tot]=u;
    for(int i=0;i<vc[u].size();i++){
        int v=vc[u][i];
        if(vis[v]==0) tarjan(v);
        if(vis[v]==1) low[u]=min(low[u],low[v]);
    }
    if(low[u]==dfn[u]) {
        sig++;
        do{
            vis[stk[tot]]=-1;
            color[stk[tot]]=sig;
        }while(stk[tot--]!=u);
    }
}
void init(){
    sig=0;cnt=1;tot=-1;
    memset(deg,0,sizeof(deg));
    memset(stk,0,sizeof(stk));
    memset(dfn,0,sizeof(dfn));
    memset(vis,0,sizeof(vis));
    memset(color,0,sizeof(color));
    for(int i=0;i<maxn;i++) {
        vc[i].clear();
        vc2[i].clear();
    }
}
int solve(){
    for(int i=1;i<=n*2;i++) {
        if(vis[i]==0) tarjan(i);
    }
    for(int i=1;i<=n;i++) {
        if(color[i]==color[i+n]) return 0;
        pos[color[i]]=color[i+n];
        pos[color[i+n]]=color[i];
    }
    for(int i=1;i<=n*2;i++){
        for(int j=0;j<vc[i].size();j++){
            int v=vc[i][j];
            if(color[i]!=color[v]){
                deg[color[i]]++;
                vc2[color[v]].push_back(color[i]);
            }
        }
    }
    top();
    return 1;
}
void cal(int x,int y)
{
    if(a[x]==1&&a[y]==1)
    {
        add(x+n,y);
        add(x,y+n);
        add(y,x+n);
        add(y+n,x);
    }// 1 shitou 2 bu 3 jiandao
    else if(a[x]==1&&a[y]==2)
    {
        add(x+n,y+2*n);
        add(y+2*n+nn,x);
        add(y+n,x);
        add(x+nn,y+2*n);
    }// 1 shitou 2 bu 3 jiandao
    else if(a[x]==1&&a[y]==3)
    {
        add(x,y+2*n);
        add(y+2*n+nn,x+n);
        add(y,x+n);
        add(x+n+nn,y+2*n);
    }
    // 1 shitou 2 bu 3 jiandao
    else if(a[x]==2&&a[y]==1)
    {
        add(x+n,y);
        add(y+nn,x+2*n);
        add(y+n,x+2*n);
        add(x+2*n+nn,y);
    }
    // 1 shitou 2 bu 3 jiandao
    else if(a[x]==2&&a[y]==2)
    {
        add(x+n,y+2*n);
        add(y+2*n+nn,x+2*n);
        add(y+n,x+2*n);
        add(x+2*n+nn,y+2*n);
    }
    // 1 shitou 2 bu 3 jiandao
    else if(a[x]==2&&a[y]==3)
    {
        add(x+2*n,y);
        add(y+nn,x+n);
        add(y+2*n,x+n);
        add(x+n+nn,y);
    }
    // 1 shitou 2 bu 3 jiandao
    else if(a[x]==3&&a[y]==1)
    {
        add(x,y+n);
        add(y+n+nn,x+2*n);
        add(y,x+2*n);
        add(x+2*n+nn,y+n);
    }
    // 1 shitou 2 bu 3 jiandao
    else if(a[x]==3&&a[y]==2)
    {
        add(x+2*n,y+n);
        add(y+n+nn,x);
        add(y+2*n,x);
        add(x+nn,y+n);
    }
    // 1 shitou 2 bu 3 jiandao
    else if(a[x]==3&&a[y]==3)
    {
        add(x,y+2*n);
        add(y+2*n+nn,x+2*n);
        add(y,x+2*n);
        add(x+2*n+nn,y+2*n);
    }
}
void cal2(int x,int y)
{
    if(a[x]==1&&a[y]==1)
    {
        add(x,y);
        add(y+nn,x+nn);
        add(y,x);
        add(x+nn,y+nn);

        add(x+n,y+n);
        add(y+n+nn,x+n+nn);
        add(y+n,x+n);
        add(x+n+nn,y+n+nn);
    }// 1 shitou 2 bu 3 jiandao
    else if(a[x]==1&&a[y]==2)
    {
        add(x+n+nn,x+n);
        add(y+n+nn,y+n);
    }// 1 shitou 2 bu 3 jiandao
    else if(a[x]==1&&a[y]==3)
    {
        add(x+nn,x);
        add(y+nn,y);
    }
    // 1 shitou 2 bu 3 jiandao
    else if(a[x]==2&&a[y]==1)
    {
        add(x+n+nn,x+n);
        add(y+n+nn,y+n);
    }
    // 1 shitou 2 bu 3 jiandao
    else if(a[x]==2&&a[y]==2)
    {
        add(x+n,y+n);
        add(y+n+nn,x+n+nn);
        add(y+n,x+n);
        add(x+n+nn,y+n+nn);

        add(x+2*n,y+2*n);
        add(y+2*n+nn,x+2*n+nn);
        add(y+2*n,x+2*n);
        add(x+2*n+nn,y+2*n+nn);
    }
    // 1 shitou 2 bu 3 jiandao
    else if(a[x]==2&&a[y]==3)
    {
        add(x+2*n+nn,x+2*n);
        add(y+2*n+nn,y+2*n);
    }
    // 1 shitou 2 bu 3 jiandao
    else if(a[x]==3&&a[y]==1)
    {
        add(x+nn,x);
        add(y+nn,y);
    }
    // 1 shitou 2 bu 3 jiandao
    else if(a[x]==3&&a[y]==2)
    {
        add(x+2*n+nn,x+2*n);
        add(y+2*n+nn,y+2*n);
    }
    // 1 shitou 2 bu 3 jiandao
    else if(a[x]==3&&a[y]==3)
    {
        add(x,y);
        add(y+nn,x+nn);
        add(y,x);
        add(x+nn,y+nn);

        add(x+2*n,y+2*n);
        add(y+2*n+nn,x+2*n+nn);
        add(y+2*n,x+2*n);
        add(x+2*n+nn,y+2*n+nn);
    }
}
int main(){
    int T,cas=1;
    scanf("%d",&T);
    while(T--) {
        scanf("%d%d",&n,&m);
        init();nn=3*n;
        // 1 shitou 2 bu 3 jiandao
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            if(a[i]==1)
            {
                add(i+2*n,i+2*n+nn);
                add(i+n+nn,i);
                add(i+nn,i+n);
            }
            else if(a[i]==2)
            {
                add(i,i+nn);
                add(i+2*n+nn,i+n);
                add(i+n+nn,i+2*n);
            }
            else
            {
                add(i+n,i+n+nn);
                add(i+nn,i+2*n);
                add(i+2*n+nn,i);
            }
            add(i,i+n+nn);
            add(i,i+2*n+nn);
            add(i+n,i+nn);
            add(i+n,i+2*n+nn);
            add(i+2*n,i+nn);
            add(i+2*n,i+n+nn);
        }
        for(int i=0;i<m;i++)
        {
            // 1 shitou 2 bu 3 jiandao
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            if(z) cal(x,y);
            else cal2(x,y);
        }
        n=3*n;
        int ans=solve();
        printf("Case #%d: ",cas++);
        if(ans==1) puts("yes");
        else puts("no");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值