[NOI2017]游戏

Posted Cyhlnj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[NOI2017]游戏相关的知识,希望对你有一定的参考价值。

Sol

然而我的代码在\(UOJ\)上被\(hack\)\(\ TLE\)
但思路是没问题的\(TAT\)

如果没有\(x\),就是个\(2-SAT\)
我们爆搜\(x\)的地图是\(a\)还是\(b\)就好了
不用枚举它是\(c\),枚举\(a\)\(b\)就能保证正好选\(ABC\)三种车

我也不知道我的输出方案哪里学的
拓扑排序+染色\(QAQ\)

如果觉得自己的代码优秀就去\(UOJ\)上交吧(逃

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(1e5 + 5);
typedef int Arr[_];

IL int Input(){
    RG int x = 0, z = 1; RG char c = getchar();
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

Arr dfn, low, S, vis, first, head, col, i1, i2, d, oth, cho;
int n, m, D, cnt, pos[10], idx, num;
struct Edge{
    int to, next;
} edge[_ << 2], dag[_ << 2];
char s[_], h1[_], h2[_], tp1[200], tp2[200];
queue <int> Q;

IL void Add(RG int u, RG int v){
    edge[cnt] = (Edge){v, first[u]}, first[u] = cnt++;
}

IL void Add_DAG(RG int u, RG int v){
    dag[cnt] = (Edge){v, head[u]}, head[u] = cnt++;
}

IL void Tarjan(RG int u){
    dfn[u] = low[u] = ++idx, vis[S[++S[0]] = u] = 1;
    for(RG int e = first[u]; e != -1; e = edge[e].next){
        RG int v = edge[e].to;
        if(!dfn[v]) Tarjan(v), low[u] = min(low[u], low[v]);
        else if(vis[v]) low[u] = min(low[u], dfn[v]);
    }
    if(low[u] == dfn[u]){
        RG int v = S[S[0]--]; vis[v] = 0, col[v] = ++num;
        while(v != u) vis[v = S[S[0]--]] = 0, col[v] = num;
    }
}

IL void Del(RG int u){
    cho[u] = 0;
    for(RG int e = head[u]; e != -1; e = dag[e].next)
        if(cho[u] == -1) Del(dag[e].to);
}

IL void Calc(){
    RG int tot = n + n; num = idx = cnt = 0;
    for(RG int i = 1; i <= tot; ++i)
        cho[i] = head[i] = first[i] = -1, d[i] = vis[i] = oth[i] = dfn[i] = low[i] = col[i] = 0;
    for(RG int i = 1; i <= m; ++i){
        if(s[i1[i]] == h1[i]) continue;
        if(i1[i] == i2[i] && h1[i] == h2[i]) continue;
        RG int p1 = i1[i], p2 = i1[i] + n, p3 = i2[i], p4 = i2[i] + n;
        if(tp1[s[i1[i]]] != h1[i]) swap(p1, p2);
        if(tp1[s[i2[i]]] != h2[i]) swap(p3, p4);
        if(s[i2[i]] == h2[i]) Add(p1, p2);
        else Add(p1, p3), Add(p4, p2);
    }
    for(RG int i = 1; i <= tot; ++i) if(!dfn[i]) Tarjan(i);
    for(RG int i = 1; i <= n; ++i){
        if(col[i] == col[i + n]) return;
        oth[col[i]] = col[i + n], oth[col[i + n]] = col[i];
    }
    cnt = 0;
    for(RG int i = 1; i <= tot; ++i)
        for(RG int e = first[i]; e != -1; e = edge[e].next)
            if(col[edge[e].to] != col[i]) Add_DAG(col[edge[e].to], col[i]), ++d[col[i]];
    for(RG int i = 1; i <= num; ++i) if(!d[i]) Q.push(i);
    while(!Q.empty()){
        RG int u = Q.front(); Q.pop();
        if(cho[u] != -1) continue;
        cho[u] = 1, Del(oth[u]);
        for(RG int e = head[u]; e != -1; e = dag[e].next)
            if(!--d[dag[e].to]) Q.push(dag[e].to);
    }
    for(RG int i = 1; i <= n; ++i)
        (cho[col[i]] == 1) ? putchar(tp1[s[i]] - 'a' + 'A') : putchar(tp2[s[i]] - 'a' + 'A');
    puts(""); exit(0);
}

IL void Dfs(RG int x){
    if(x > D){
        Calc();
        return;
    }
    s[pos[x]] = 'a', Dfs(x + 1), s[pos[x]] = 'b', Dfs(x + 1);
}

int main(RG int argc, RG char *argv[]){
    tp1['a'] = 'b', tp1['b'] = 'c', tp1['c'] = 'a';
    tp2['a'] = 'c', tp2['b'] = 'a', tp2['c'] = 'b';
    n = Input(), D = Input(), scanf(" %s", s + 1), m = Input();
    for(RG int i = 1; i <= m; ++i)
        i1[i] = Input(), h1[i] = getchar() - 'A' + 'a', i2[i] = Input(), h2[i] = getchar() - 'A' + 'a';
    for(RG int i = 1, t = 0; i <= n; ++i) if(s[i] == 'x') pos[++t] = i;
    Dfs(1);
    puts("-1");
    return 0;
}

以上是关于[NOI2017]游戏的主要内容,如果未能解决你的问题,请参考以下文章

BZOJ4945[Noi2017]游戏 2-SAT

NOI2017 游戏

BZOJ 4945 [Noi2017]游戏

NOI2017游戏 2-sat算法

loj2305 noi2017 游戏

题解NOI2017游戏