結果
問題 | No.1370 置換門松列 |
ユーザー |
![]() |
提出日時 | 2021-01-29 22:31:03 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 33 ms / 2,000 ms |
コード長 | 4,250 bytes |
コンパイル時間 | 14,809 ms |
コンパイル使用メモリ | 387,572 KB |
実行使用メモリ | 12,920 KB |
最終ジャッジ日時 | 2024-09-14 19:21:39 |
合計ジャッジ時間 | 16,667 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 25 |
ソースコード
#![allow(unused_macros)]#![allow(dead_code)]#![allow(unused_imports)]// # ファイル構成// - use宣言// - libモジュール// - main関数// - basicモジュール//// 常に使うテンプレートライブラリはbasicモジュール内にあります。// 問題に応じて使うライブラリlibモジュール内にコピペしています。// ライブラリのコードはこちら → https://github.com/RheoTommy/at_coder// Twitterはこちら → https://twitter.com/RheoTommyuse std::collections::*;use std::io::{stdout, BufWriter, Write};use crate::basic::*;use crate::lib::*;pub mod lib {}fn main() {let out = stdout();let mut writer = BufWriter::new(out.lock());let mut sc = Scanner::new();let n = sc.next_usize();let m = sc.next_usize();let a = (0..n).map(|_| sc.next_usize() - 1).collect::<Vec<_>>();let mut vertexes = vec![vec![]; m];let mut get_in = vec![0; m];for v in a.windows(3) {if !(v[0] != v[1] && v[0] != v[2] && v[1] != v[2]) {writeln!(writer, "{}", "No").unwrap();return;}}let mut f = true;for v in a.windows(2) {let l = v[0];let r = v[1];if f {vertexes[l].push(r);get_in[r] += 1;} else {vertexes[r].push(l);get_in[l] += 1;}f = !f;}let mut sorted = vec![];let mut queue = VecDeque::new();for i in 0..m {if get_in[i] == 0 {queue.push_back(i);}}while let Some(i) = queue.pop_front() {for &j in &vertexes[i] {get_in[j] -= 1;if get_in[j] == 0 {queue.push_back(j);}}sorted.push(i);}let mut ans = vec![0; m];let mut now = 0;for &si in &sorted {ans[si] = now;now += 1;}if sorted.len() != m {writeln!(writer, "{}", "No").unwrap();} else {writeln!(writer, "{}", "Yes").unwrap();let mut s = String::new();for si in ans {s.push_str(&(si + 1).to_string());s.push(' ');}writeln!(writer, "{}", s.trim()).unwrap();}}pub mod basic {pub const U_INF: u128 = 1 << 60;pub const I_INF: i128 = 1 << 60;pub struct Scanner {buf: std::collections::VecDeque<String>,reader: std::io::BufReader<std::io::Stdin>,}impl Scanner {pub fn new() -> Self {Self {buf: std::collections::VecDeque::new(),reader: std::io::BufReader::new(std::io::stdin()),}}fn scan_line(&mut self) {use std::io::BufRead;let mut flag = 0;while self.buf.is_empty() {let mut s = String::new();self.reader.read_line(&mut s).unwrap();let mut iter = s.split_whitespace().peekable();if iter.peek().is_none() {if flag >= 5 {panic!("There is no input!");}flag += 1;continue;}for si in iter {self.buf.push_back(si.to_string());}}}pub fn next<T: std::str::FromStr>(&mut self) -> T {self.scan_line();self.buf.pop_front().unwrap().parse().unwrap_or_else(|_| panic!("Couldn't parse!"))}pub fn next_usize(&mut self) -> usize {self.next()}pub fn next_int(&mut self) -> i128 {self.next()}pub fn next_uint(&mut self) -> u128 {self.next()}pub fn next_chars(&mut self) -> Vec<char> {self.next::<String>().chars().collect()}pub fn next_string(&mut self) -> String {self.next()}pub fn next_char(&mut self) -> char {self.next()}pub fn next_float(&mut self) -> f64 {self.next()}}}