結果

問題 No.95 Alice and Graph
ユーザー nebocconebocco
提出日時 2021-02-12 20:28:00
言語 Rust
(1.77.0)
結果
AC  
実行時間 2,586 ms / 5,000 ms
コード長 5,566 bytes
コンパイル時間 2,077 ms
コンパイル使用メモリ 154,028 KB
実行使用メモリ 8,580 KB
最終ジャッジ日時 2023-09-27 01:43:41
合計ジャッジ時間 14,137 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 716 ms
8,492 KB
testcase_01 AC 2,577 ms
8,480 KB
testcase_02 AC 2,586 ms
8,580 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 369 ms
5,320 KB
testcase_05 AC 305 ms
5,256 KB
testcase_06 AC 322 ms
5,312 KB
testcase_07 AC 2,333 ms
8,552 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 715 ms
5,244 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #[allow(unused_macros)]
// macro_rules! input {
// 	(@read $io:tt @rest) => {};

// 	(@read $io:tt @rest mut $($rest:tt)*) => {
// 		input!(@read $io @mut [mut] @rest $($rest)*)
// 	};

// 	(@read $io:tt @rest $($rest:tt)*) => {
// 		input!(@read $io @mut [] @rest $($rest)*)
// 	};

// 	(@read $io:tt @mut [$($mut:tt)?] @rest $var:tt: $($rest:tt)*) => {
// 		input!(@read $io @mut[$($mut)*] @var $var @kind [] @rest $($rest)*)
// 	};

// 	(@read $io:tt @mut [$($mut:tt)?] @var $var:tt @kind [$($kind:tt)*] @rest) => {
//         let $($mut)* $var = input!(@kind $io:tt $($kind)*);
//     };
//     (@read $io:tt @mut [$($mut:tt)?] @var $var:tt @kind [$($kind:tt)*] @rest, $($rest:tt)*) => {
//         input!(@read $io @mut [$($mut)*] @var $var @kind [$($kind)*] @rest);
//         input!(@read $io @rest $($rest)*);
//     };
//     (@read $io:tt @mut [$($mut:tt)?] @var $var:tt @kind [$($kind:tt)*] @rest $tt:tt $($rest:tt)*) => {
//         input!(@read $io @mut [$($mut)*] @var $var @kind [$($kind)* $tt] @rest $($rest)*);
//     };

// 	(@kind $io:tt [$kind:tt; $len:tt]) => {
// 		io.scan_vec::<$kind>($len);
// 	};

// 	(@kind $io:tt $kind:tt) => {
// 		io.scan::<$kind>();
// 	};

// 	($($rest:tt)*) => {
// 		let mut io = IO::new();
// 		input!(@read io @rest $($rest)*)
// 	};
// }


fn main() {
	// input!{
	// 	n: usize, _m: usize, k: i32,
	// 	ed: [(usize, usize); _m],
	// }
	let mut io = IO::new();
	let (n, _m, k) = io.scan();
	let ed = io.scan_vec::<(usize, usize)>(_m);
	let mut d = vec![vec![k+1; n]; n];
	for i in 0..n {
		d[i][i] = 0;
	}
	for &(u, v) in &ed {
		d[u-1][v-1] = 1;
		d[v-1][u-1] = 1;
	}
	for v in 0..n {
		for u in 0..n {
			for w in 0..n {
				d[u][w] = std::cmp::min(d[u][w], d[u][v] + d[v][w]);
			}
		}
	}
	let mut vis = Vec::new();
	for i in (1..n).rev() {
		vis.push(i);
		let m = vis.len();
		let mut dp = vec![vec![k+1; m]; 1<<m];
		for j in 0..m {
			dp[1<<j][j] = d[0][vis[j]];
		}
		for bit in 1..1<<m {
			for j in 0..m {
				if bit & (1 << j) == 0 { continue; }
				if dp[bit][j] > k { continue; }
				for jj in 0..m {
					if bit & (1 << jj) == 0 {
						dp[bit | 1 << jj][jj] = std::cmp::min(
							dp[bit | 1 << jj][jj], dp[bit][j] + d[vis[j]][vis[jj]]
						);
					}
				}
			}
		}
		let goal = (1 << m) - 1;
		if (0..m).all(|j| dp[goal][j] > k) {
			vis.pop();
		}
	}
    println!("{}", vis.iter().map(|i| (1i64 << i) - 1).sum::<i64>());
}

// ------------ io module start ------------

use std::io::{stdout, BufWriter, Read, StdoutLock, Write};

pub struct IO {
	iter: std::str::SplitAsciiWhitespace<'static>,
	buf: BufWriter<StdoutLock<'static>>,
}

impl IO {
	pub fn new() -> Self {
		let mut input = String::new();
		std::io::stdin().read_to_string(&mut input).unwrap();
		let input = Box::leak(input.into_boxed_str());
		let out = Box::new(stdout());
		IO {
			iter: input.split_ascii_whitespace(),
			buf: BufWriter::new(Box::leak(out).lock()),
		}
	}
	fn scan_str(&mut self) -> &'static str {
		self.iter.next().unwrap()
	}
	fn scan_raw(&mut self) -> &'static [u8] {
		self.scan_str().as_bytes()
	}
	pub fn scan<T: Scan>(&mut self) -> T {
		T::scan(self)
	}
	pub fn scan_vec<T: Scan>(&mut self, n: usize) -> Vec<T> {
		(0..n).map(|_| self.scan()).collect()
	}
}

impl IO {
	pub fn print<T: Print>(&mut self, x: T) {
		T::print(self, x);
	}
	pub fn println<T: Print>(&mut self, x: T) {
		self.print(x);
		self.print("\n");
	}
	pub fn iterln<T: Print, I: Iterator<Item = T>>(&mut self, mut iter: I, delim: &str) {
		if let Some(v) = iter.next() {
			self.print(v);
			for v in iter {
				self.print(delim);
				self.print(v);
			}
		}
		self.print("\n");
	}
	pub fn flush(&mut self) {
		self.buf.flush().unwrap();
	}
}

impl Default for IO {
	fn default() -> Self {
		Self::new()
	}
}

pub trait Scan {
	fn scan(io: &mut IO) -> Self;
}

macro_rules! impl_parse_int {
	($($t:tt),*) => {
		$(
			impl Scan for $t {
				fn scan(s: &mut IO) -> Self {
					let mut res = 0;
					let mut neg = false;
					for d in s.scan_raw() {
						if *d == b'-' {
							neg = true;
						} else {
							res *= 10;
							res += (*d - b'0') as $t;
						}
					}
					if neg { res = res.wrapping_neg(); }
					res
				}
			}
		)*
	};
}

impl_parse_int!(i16, i32, i64, isize, u16, u32, u64, usize);

impl<T: Scan, U: Scan> Scan for (T, U) {
	fn scan(s: &mut IO) -> Self {
		(T::scan(s), U::scan(s))
	}
}

impl<T: Scan, U: Scan, V: Scan> Scan for (T, U, V) {
	fn scan(s: &mut IO) -> Self {
		(T::scan(s), U::scan(s), V::scan(s))
	}
}

impl<T: Scan, U: Scan, V: Scan, W: Scan> Scan for (T, U, V, W) {
	fn scan(s: &mut IO) -> Self {
		(T::scan(s), U::scan(s), V::scan(s), W::scan(s))
	}
}

pub trait Print {
	fn print(w: &mut IO, x: Self);
}

macro_rules! impl_print_int {
	($($t:ty),*) => {
		$(
			impl Print for $t {
				fn print(w: &mut IO, x: Self) {
					w.buf.write_all(x.to_string().as_bytes()).unwrap();
				}
			}
		)*
	};
}

impl_print_int!(i16, i32, i64, isize, u16, u32, u64, usize);

impl Print for u8 {
	fn print(w: &mut IO, x: Self) {
		w.buf.write_all(&[x]).unwrap();
	}
}

impl Print for &[u8] {
	fn print(w: &mut IO, x: Self) {
		w.buf.write_all(x).unwrap();
	}
}

impl Print for &str {
	fn print(w: &mut IO, x: Self) {
		w.print(x.as_bytes());
	}
}

impl<T: Print, U: Print> Print for (T, U) {
	fn print(w: &mut IO, (x, y): Self) {
		w.print(x);
		w.print(" ");
		w.print(y);
	}
}

impl<T: Print, U: Print, V: Print> Print for (T, U, V) {
	fn print(w: &mut IO, (x, y, z): Self) {
		w.print(x);
		w.print(" ");
		w.print(y);
		w.print(" ");
		w.print(z);
	}
}


// ------------ io module end ------------
0