結果

問題 No.2267 群の公理
ユーザー haihamabossu
提出日時 2023-04-14 21:50:15
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,018 bytes
コンパイル時間 12,292 ms
コンパイル使用メモリ 403,076 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-10 12:33:33
合計ジャッジ時間 13,723 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #

fn f(a_matrix: &Vec<Vec<usize>>, n: usize, e: usize) -> bool {
    for i in 0..n {
        if a_matrix[i][e] != i || a_matrix[e][i] != i {
            return false;
        }
    }
    for i in 0..n {
        let mut ok = false;
        for j in 0..n {
            ok |= a_matrix[i][j] == e && a_matrix[j][i] == e;
        }
        if !ok {
            return false;
        }
    }
    true
}

fn solve(scanner: &mut Scanner) {
    let n: usize = scanner.next();
    let a_matrix: Vec<Vec<usize>> = (0..n)
        .map(|_| {
            (0..n)
                .map(|_| {
                    let a = scanner.next();
                    a
                })
                .collect()
        })
        .collect();
    for i in 0..n {
        for j in 0..n {
            for k in 0..n {
                let x = a_matrix[i][j];
                let left = a_matrix[x][k];
                let y = a_matrix[j][k];
                let right = a_matrix[i][y];
                if left != right {
                    println!("No");
                    return;
                }
            }
        }
    }
    for e in 0..n {
        if f(&a_matrix, n, e) {
            println!("Yes");
            return;
        }
    }
    println!("No");
}

fn main() {
    let mut scanner = Scanner::new();
    let t: usize = 1;
    for _ in 0..t {
        solve(&mut scanner);
    }
}

struct Scanner {
    buf: Vec<String>,
}

impl Scanner {
    fn new() -> Self {
        Self { buf: vec![] }
    }

    fn next<T: std::str::FromStr>(&mut self) -> T {
        loop {
            if let Some(x) = self.buf.pop() {
                return x.parse().ok().expect("");
            }
            let mut source = String::new();
            std::io::stdin().read_line(&mut source).expect("");
            self.buf = Self::split(source);
        }
    }

    fn split(source: String) -> Vec<String> {
        source
            .split_whitespace()
            .rev()
            .map(String::from)
            .collect::<Vec<_>>()
    }
}
0