提交记录 32754


用户 题目 状态 得分 用时 内存 语言 代码长度
saffah_dsh_260814 noi17d. 【NOI2017】游戏 Wrong Answer 10 18.742 ms 15816 KB C++ 3.67 KB
提交时间 评测时间
2026-08-14 20:06:25 2026-08-14 20:06:30
// NOI2017 游戏 (noi17d) correct solver - 2-SAT with 2^d enumeration over 'x'
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

static const int MAXN = 50005;
static int n, d, m;
static char S[MAXN];
static int xpos[10];       // positions of 'x' (0-based), count d
static int cur[10];        // 0 -> 'a', 1 -> 'b' for each x

// 2-SAT graph: 2*n nodes
static vector<int> g[2*MAXN];
static int dfn[2*MAXN], low[2*MAXN], bel[2*MAXN];
static int stk[2*MAXN], top, ts, scc;
static bool instk[2*MAXN];

// constraints
static int ci[MAXN*2], cj[MAXN*2]; char chi[MAXN*2], chj[MAXN*2];

static inline int carId(int i, char car){
    // what node corresponds to "position i uses car"? 0 -> first allowed, 1 -> second allowed
    // allowed cars for position i depend on S[i] and cur[] for x
    char a0, a1;
    char typ = S[i];
    if(typ == 'x'){
        // find its x index
        int xi = -1;
        for(int k=0;k<d;k++) if(xpos[k]==i){ xi=k; break; }
        typ = (cur[xi]==0) ? 'a' : 'b';
    }
    if(typ=='a'){ a0='B'; a1='C'; }
    else if(typ=='b'){ a0='A'; a1='C'; }
    else { a0='A'; a1='B'; } // 'c'
    if(car==a0) return 2*i;
    if(car==a1) return 2*i+1;
    return -1;
}

static inline void addEdge(int u, int v){ g[u].push_back(v); }

static void tarjan(int u){
    dfn[u]=low[u]=++ts; stk[top++]=u; instk[u]=true;
    for(int v: g[u]){
        if(!dfn[v]){ tarjan(v); low[u]=min(low[u],low[v]); }
        else if(instk[v]) low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u]){
        ++scc;
        int v;
        do { v=stk[--top]; instk[v]=false; bel[v]=scc; } while(v!=u);
    }
}

static bool solve2sat(){
    int N = 2*n;
    for(int i=0;i<N;i++){ dfn[i]=low[i]=bel[i]=0; instk[i]=false; }
    ts=0; scc=0; top=0;
    for(int i=0;i<N;i++) if(!dfn[i]) tarjan(i);
    for(int i=0;i<n;i++) if(bel[2*i]==bel[2*i+1]) return false;
    return true;
}

int main(){
    if(scanf("%d %d",&n,&d)!=2) return 0;
    scanf("%s", S);
    scanf("%d", &m);
    // collect x positions
    int xc = 0;
    for(int i=0;i<n;i++) if(S[i]=='x') xpos[xc++]=i;
    for(int t=0;t<m;t++){
        int i,j; char hi[2], hj[2];
        scanf("%d %s %d %s", &i, hi, &j, hj);
        ci[t]=i-1; chi[t]=hi[0]; cj[t]=j-1; chj[t]=hj[0];
    }
    // enumerate
    for(int mask=0; mask < (1<<d); mask++){
        for(int k=0;k<d;k++) cur[k] = (mask>>k)&1;
        for(int i=0;i<2*n;i++) g[i].clear();
        for(int t=0;t<m;t++){
            int i=ci[t], j=cj[t]; char hi=chi[t], hj=chj[t];
            int a = carId(i, hi);
            if(a < 0) continue; // hi not allowed at i
            int b = carId(j, hj);
            if(b < 0){
                // i cannot use hi -> force i to use the other car
                int other = (a == 2*i) ? (2*i+1) : (2*i);
                addEdge(a, other);
            } else {
                addEdge(a, b);
                int na = (a == 2*i) ? (2*i+1) : (2*i);
                int nb = (b == 2*j) ? (2*j+1) : (2*j);
                addEdge(nb, na);
            }
        }
        if(solve2sat()){
            // output assignment: choose car whose bel is larger (Tarjan gives reverse topo)
            for(int i=0;i<n;i++){
                char a0,a1;
                char typ=S[i];
                if(typ=='x'){
                    int xi=-1; for(int k=0;k<d;k++) if(xpos[k]==i){xi=k;break;}
                    typ=(cur[xi]==0)?'a':'b';
                }
                if(typ=='a'){a0='B';a1='C';}
                else if(typ=='b'){a0='A';a1='C';}
                else {a0='A';a1='B';}
                if(bel[2*i] > bel[2*i+1]) putchar(a0); else putchar(a1);
            }
            putchar('\n');
            return 0;
        }
    }
    printf("-1\n");
    return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #1339.32 us2 MB + 364 KBAcceptedScore: 5

Testcase #2332.14 us2 MB + 364 KBWrong AnswerScore: 0

Testcase #3338.02 us2 MB + 364 KBWrong AnswerScore: 0

Testcase #4338.36 us2 MB + 364 KBWrong AnswerScore: 0

Testcase #5336.18 us2 MB + 364 KBWrong AnswerScore: 0

Testcase #6335.33 us2 MB + 364 KBWrong AnswerScore: 0

Testcase #7339.92 us2 MB + 364 KBWrong AnswerScore: 0

Testcase #8340.77 us2 MB + 364 KBWrong AnswerScore: 0

Testcase #9351.09 us2 MB + 368 KBWrong AnswerScore: 0

Testcase #10378.05 us2 MB + 364 KBWrong AnswerScore: 0

Testcase #11379.11 us2 MB + 376 KBWrong AnswerScore: 0

Testcase #12381.28 us2 MB + 376 KBWrong AnswerScore: 0

Testcase #13381.54 us2 MB + 376 KBWrong AnswerScore: 0

Testcase #14380.51 us2 MB + 376 KBWrong AnswerScore: 0

Testcase #152.469 ms2 MB + 840 KBAcceptedScore: 5

Testcase #162.364 ms2 MB + 864 KBWrong AnswerScore: 0

Testcase #172.352 ms2 MB + 872 KBWrong AnswerScore: 0

Testcase #1811.159 ms15 MB + 208 KBWrong AnswerScore: 0

Testcase #1918.553 ms15 MB + 456 KBWrong AnswerScore: 0

Testcase #2018.742 ms15 MB + 376 KBWrong AnswerScore: 0


Judge Duck Online | 评测鸭在线
Server Time: 2026-09-11 14:04:17 | Loaded in 2 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠