結果

問題 No.3040 Aoiスコア
ユーザー The_Bouningeeeen
提出日時 2025-02-28 23:00:29
言語 Rust
(1.83.0 + proconio)
結果
WA  
実行時間 -
コード長 1,830 bytes
コンパイル時間 11,714 ms
コンパイル使用メモリ 400,604 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-06-20 21:00:21
合計ジャッジ時間 18,280 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 3
other AC * 2 WA * 21 TLE * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

use proconio::{
    input,
    marker::{Chars, Usize1},
};

fn main() {
    input! {
        n: usize,
        m: usize,
        s: Chars,
        ab: [(Usize1, Usize1); m],
    }

    let mut graph = vec![vec![]; n];
    for (a, b) in ab {
        graph[a].push(b);
        graph[b].push(a);
    }

    let question_count = s.iter().filter(|&&x| x == '?').count();
    let mut ans = 0;
    for start in 0..n {
        if s[start] == 'a' || s[start] == '?' {
            dfs(
                start,
                0,
                &mut ans,
                question_count,
                &mut vec![start],
                &s,
                &graph,
            );
        }
    }

    println!("{}", ans);
}

fn dfs(
    pos: usize,
    count: usize,
    ans: &mut usize,
    question_count: usize,
    route: &mut Vec<usize>,
    s: &Vec<char>,
    graph: &Vec<Vec<usize>>,
) {
    let target = match count {
        0 => 'o',
        1 => 'i',
        _ => unreachable!(),
    };

    for &next in &graph[pos] {
        if s[next] != target && s[next] != '?' {
            continue;
        }
        if s[next] == '?' && route.contains(&next) {
            continue;
        }

        if count + 1 == 2 {
            let rest_question_count =
                question_count - route.iter().filter(|&&i| s[i] == '?').count();
            *ans += pow(26, rest_question_count);
            *ans += 1;
            *ans %= 998244353;
            continue;
        }

        route.push(next);
        dfs(next, count + 1, ans, question_count, route, s, graph);
        route.pop();
    }
}

fn pow(mut n: usize, mut m: usize) -> usize {
    let mut r = 1;
    while m > 0 {
        if m & 1 == 1 {
            r *= n;
            r %= 998244353;
        }
        n *= n;
        n %= 998244353;
        m >>= 1;
    }
    r
}
0