luogu3825 [NOI2017] game

[NOI2017] game

3-SAT problem? This is not a npc it. . .

However, we found that \ (the X-\ Leq 8 \) , so we can enumerate these \ (X \) prohibit the use of what type of car, it is clear that we need only to enumerate \ (A \) and \ (B \) on you can cover all the cases

After that is a classic 2-sat for Evaluating the

But some feel simple and clear thinking difficult to write

#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
#define lowbit(x) (x)&(-x)
#define sqr(x) (x)*(x)
#define rep(i,a,b) for (register int i=a;i<=b;i++)
#define per(i,a,b) for (register int i=a;i>=b;i--)
#define maxd 998244353
#define eps 1e-8
struct edgenode{
    int to,nxt;
}sq[200200];
int all=0,head[200100];
struct qnode{
    int a,opa,b,opb;
}q[200100];
stack<int> sta;
int n,m,d,ban[200050],tim=0,dfn[200100],low[200100],
    col[200100],scc=0,ans[200100];
char ch1[10],ch2[10],s[200100];
bool in[200100];
vector<int> posx;

int read()
{
    int 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;
}

void add(int u,int v)
{
    all++;sq[all].to=v;sq[all].nxt=head[u];head[u]=all;
}

void tarjan(int u)
{
    dfn[u]=low[u]=(++tim);
    in[u]=1;sta.push(u);
    int i;
    for (i=head[u];i;i=sq[i].nxt)
    {
        int v=sq[i].to;
        if (!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if (in[v]) low[u]=min(low[u],dfn[v]);
    }
    if (dfn[u]==low[u])
    {
        scc++;
        while (1)
        {
            int v=sta.top();sta.pop();
            col[v]=scc;in[v]=0;
            if (u==v) break;
        }
    }
}

int neg(int u)
{
    if (u>n) return u-n;else return u+n;
}

int trans(int u,int op)
{
    if (ban[u]==0)
    {
        if (op==1) return u;else return u+n;
    }
    else if (ban[u]==1)
    {
        if (op==0) return u;else return u+n;
    }
    else if (ban[u]==2)
    {
        if (op==0) return u;else return u+n;
    }
}

int untrans(int u,int op)
{
    if (op)
    {
        if (ban[u]==0) return 1;
        else if (ban[u]==1) return 0;
        else if (ban[u]==2) return 0;
    }
    else 
    {
        if (ban[u]==0) return 2;
        else if (ban[u]==1) return 2;
        else if (ban[u]==2) return 1;
    }
}

int work(int sta)
{
    all=0;scc=0;tim=0;
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(head,0,sizeof(head));
    rep(i,0,d-1)
    {
        if ((sta>>i)&1) ban[posx[i]]=2;else ban[posx[i]]=1;
    }
    rep(i,1,m)
    {
        int u=q[i].a,v=q[i].b;
        if (q[i].opa==ban[u]) continue;
        u=trans(u,q[i].opa);
        if (q[i].opb==ban[v]) {add(u,neg(u));continue;}
        v=trans(v,q[i].opb);
        add(u,v);add(neg(v),neg(u));
    }
    rep(i,1,n*2) if (!dfn[i]) tarjan(i);
    rep(i,1,n) if (col[i]==col[i+n]) return 0;
    rep(i,1,n) ans[i]=untrans(i,col[i+n]>col[i]);
    rep(i,1,n) putchar('A'+ans[i]);
    return 1;
}

int main()
{
    n=read();d=read();
    scanf("%s",s+1);
    rep(i,1,n)
        if (s[i]=='x') posx.push_back(i);
        else ban[i]=s[i]-'a';
    m=read();
    rep(i,1,m) 
    {
        scanf("%d%s%d%s",&q[i].a,ch1,&q[i].b,ch2);
        q[i].opa=ch1[0]-'A';
        q[i].opb=ch2[0]-'A';
    }
    rep(sta,0,(1<<d)-1)
        if (work(sta)) return 0;
    puts("-1");
    return 0;
}

Guess you like

Origin www.cnblogs.com/encodetalker/p/11141338.html