Los solution to a problem Valley P3825 [[NOI2017] Games

Group faces questions from four yuan \ ((i, h_i, j , h_j) \) limit the selection of car models, is not difficult to think of this question use \ (2-SAT \) to solve.

Consider converted to \ (2-SAT \) model, found that in addition to the map \ (x \) , other maps are only two car models can participate, then put both models into two states.

If \ (S_i = A \) , the status is \ (B \) and \ (C \) .

If \ (S_i = B \) , the status is \ (A \) and \ (C \) .

If \ (S_i = C \) , the status is \ (A \) and \ (B \) .

Discussion then four-tuple, set \ (I \) is the input state, \ (^ I \ Prime \) to another state.

If the section \ (I \) field, \ (H_i \) is not available, is not performed even edges.

If the section \ (I \) field, \ (H_i \) is available, in the \ (J \) field, \ (h_j \) is not available, from \ (I \) to \ (i ^ \ prime \) even side, represents not selected \ (I \) .

If both are available, from \ (I \) to \ (J \) connected edge, indicates when the select \ (I \) , then must choose \ (J \) , while the \ (j ^ \ prime \) to \ (i ^ \ prime \) connected edges, if not represented here selected \ (J \) , is not necessarily selected from the group \ (I \) .

Continue to consider how to deal with the map \ (the X-\) , found that the number \ (d \ leqslant8 \) , the data size is small, then we can use \ (dfs \) to enumerate all possible cases again, and then check whether legitimate.

We need only consider the map \ (x \) is equivalent to map \ (a \) and maps \ (b \) in both cases, because at this time has included \ (A, B, C \ ) three models of.

Time complexity is \ (O (2 ^ D (n-m +)) \) .

Still find many of the implementation details, clear to see the code.

\(code:\)

#include<bits/stdc++.h>
#define maxn 1000010
using namespace std;
template<typename T> inline void read(T &x)
{
    x=0;char c=getchar();bool flag=false;
    while(!isdigit(c)){if(c=='-')flag=true;c=getchar();}
    while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
    if(flag)x=-x;
}
int n,d,m,x_cnt;
bool flag;
char s[maxn],a[5],b[5],c1[maxn],c2[maxn];
int pos[maxn];
struct node
{
    int x,y;
    char a,b;
}t[maxn];
struct edge
{
    int to,nxt;
}e[maxn];
int head[maxn],edge_cnt;
void add(int from,int to)
{
    e[++edge_cnt]=(edge){to,head[from]};
    head[from]=edge_cnt;
}
int dfn_cnt,co_cnt,top;
int dfn[maxn],low[maxn],co[maxn],st[maxn];
bool vis[maxn];
void tarjan(int x)
{
    dfn[x]=low[x]=++dfn_cnt;
    st[++top]=x;
    vis[x]=true;
    for(int i=head[x];i;i=e[i].nxt)
    {
        int y=e[i].to;
        if(!dfn[y])
        {
            tarjan(y);;
            low[x]=min(low[x],low[y]);
        }
        else if(vis[y])
            low[x]=min(low[x],dfn[y]);
    }
    if(low[x]==dfn[x])
    {
        co_cnt++;
        int now;
        do
        {
            now=st[top--];
            vis[now]=false;
            co[now]=co_cnt;
        }while(now!=x);
    }
}
bool check()
{
    for(int i=1;i<=2*n;++i)
        if(!dfn[i])
            tarjan(i);
    for(int i=1;i<=n;++i)
        if(co[i]==co[i+n])
            return false;
    return true;
}
void clear()
{
    edge_cnt=dfn_cnt=co_cnt=top=0;
    memset(st,0,sizeof(st));
    memset(co,0,sizeof(co));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(vis,0,sizeof(vis));
    memset(head,0,sizeof(head));
}
void work()
{
    clear();
    for(int i=1;i<=m;++i)
    {
        int x=t[i].x,y=t[i].y;
        char a=t[i].a,b=t[i].b;
        if(s[x]==a) continue;
        if(s[y]==b)
        {
            add(x+(a==c2[x])*n,x+(a==c1[x])*n);
            continue;
        }
        add(x+(a==c2[x])*n,y+(b==c2[y])*n);
        add(y+(b==c1[y])*n,x+(a==c1[x])*n);
    }
    if(check())
    {
        flag=true;
        for(int i=1;i<=n;i++)
        {   
            if(co[i]<co[i+n]) printf("%c",c1[i]);
            else printf("%c",c2[i]);
        }
    }
}
void dfs(int x)
{
    if(flag) return;
    if(x==d+1)
    {
        work();
        return;
    }
    int now=pos[x];
    s[now]='A',c1[now]='B',c2[now]='C',dfs(x+1);
    s[now]='B',c1[now]='A',c2[now]='C',dfs(x+1);
}
int main()
{
    read(n),read(d);
    scanf("%s",s+1);
    for(int i=1;i<=n;++i)
    {
        s[i]+='A'-'a';
        if(s[i]=='A') c1[i]='B',c2[i]='C';
        if(s[i]=='B') c1[i]='A',c2[i]='C';
        if(s[i]=='C') c1[i]='A',c2[i]='B';
        if(s[i]=='X') pos[++x_cnt]=i;
    }
    read(m);
    for(int i=1;i<=m;++i)
    {
        read(t[i].x),scanf("%s",a),read(t[i].y),scanf("%s",b);
        t[i].a=a[0],t[i].b=b[0];
    }
    dfs(1);
    if(!flag) printf("-1");
    return 0;
}

Guess you like

Origin www.cnblogs.com/lhm-/p/12229833.html