2018-2019 ACM-ICPC, Asia Seoul Regional Contest K TV Show Game 2-sat

Topic Portal

Meaning of the questions:

  There are n individuals, k light, light red and blue two colors, everyone guessed the three kinds of color lights, ask how to arrange the color of light, so that each person has at least two lights guess is right.

Ideas:

  It is easy to think of 2-sat, but apparently enumerate everyone to guess the situation is not shown because guessed and guessed two of three in both cases it is difficult to engage. So we enumerate everyone guess what is wrong lamp light, if a light is wrong, then the other two must be right so that it meets the conditions.

  We use a two-dimensional vector of light, save: $ light of a certain color, choose the color belongs to the wrong person $, then a two-dimensional vector called people to save three errors each person's situation.

  Then enumerate color of each lamp in the function twosat's, if one color for all the wrong people who meet the conditions (meet dfs), the man selected for the color of the light must have been more to the excellent, so it is possible to enumerate.

  When dfs in check legality, enumerates all the wrong people, and other colors that person must be picked before they can.

  Look at the code it in conjunction with the text, the text alone clearly expressed little difficult.

#pragma GCC optimize (2)
#pragma G++ optimize (2)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
#include<cstdio>
#include<vector>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,b,a) for(int i=b;i>=a;i--)
#define clr(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define pii pair<int,int >
using namespace std;
typedef long long ll;
const int maxn=10010;
const int inf=0x3f3f3f3f;
ll rd()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int vis[maxn],sta[maxn],top;
struct node{
    int p1,p2,p3,c1,c2,c3;
}a[maxn];
vector<int >light[maxn],peo[maxn];
int n,k;
bool dfs(int u){
    if(vis[u^1])return false;
    if(vis[u])return true;
    vis[u]=1;
    sta[++top]=u;
    for(auto &x:light[u]){
        for(auto &v:peo[x]){
            if(v==u)continue;
            if(!dfs(v^1))return false;
        }
    }
    return true;
}
int two_sat(int n){
    for(int i=2;i<=n;i+=2){
        if(vis[i]||vis[i^1])continue;
        top=0;
        if(!dfs(i)){
            while(top){vis[sta[top--]]=0;}
            if(!dfs(i^1))return false;
        }
    }
    return true;
}
int main(){
    cin>>k>>n;
    rep(i,1,n){
        char xx,yy,zz;
        scanf("%d %c %d %c %d %c",&a[i].p1,&xx,&a[i].p2,&yy,&a[i].p3,&zz);
        a[i].c1=a[i].p1*2+(xx=='B');
        a[i].c2=a[i].p2*2+(yy=='B');
        a[i].c3=a[i].p3*2+(zz=='B');
        
        light[a[i].c1^1].push_back(i);
        peo[i].push_back(a[i].c1^1);

        light[a[i].c2^1].push_back(i);
        peo[i].push_back(a[i].c2^1);
        
        light[a[i].c3^1].push_back(i);
        peo[i].push_back(a[i].c3^1);
    }
    int f=two_sat(2*k);
    if(f==0){
        puts("-1");
    }else{
        for(int i=1;i<=k;i++){
            if(vis[i*2])putchar('R');
            else putchar('B');
        }
        puts("");
    }
}
/*
7 5
3 R 5 R 6 B
1 B 2 B 3 R
4 R 5 B 6 B
5 R 6 B 7 B
1 R 2 R 4 R


5 6
1 B 3 R 4 B
2 B 3 R 4 R
1 B 2 R 3 R
3 R 4 B 5 B
3 B 4 B 5 B
1 R 2 R 4 R
*/

 

Guess you like

Origin www.cnblogs.com/mountaink/p/11615502.html