結果

問題 No.416 旅行会社
ユーザー cympfhcympfh
提出日時 2016-09-03 00:08:10
言語 Rust
(1.77.0 + proconio)
結果
MLE  
実行時間 -
コード長 3,541 bytes
コンパイル時間 18,478 ms
コンパイル使用メモリ 378,164 KB
実行使用メモリ 816,568 KB
最終ジャッジ日時 2024-11-15 18:08:09
合計ジャッジ時間 37,327 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 AC 41 ms
6,816 KB
testcase_02 AC 2 ms
6,820 KB
testcase_03 AC 3 ms
6,816 KB
testcase_04 AC 2 ms
6,820 KB
testcase_05 AC 2 ms
6,816 KB
testcase_06 AC 3 ms
6,816 KB
testcase_07 AC 4 ms
6,816 KB
testcase_08 AC 10 ms
6,820 KB
testcase_09 AC 137 ms
109,952 KB
testcase_10 MLE -
testcase_11 MLE -
testcase_12 MLE -
testcase_13 MLE -
testcase_14 MLE -
testcase_15 MLE -
testcase_16 MLE -
testcase_17 MLE -
testcase_18 MLE -
testcase_19 MLE -
testcase_20 MLE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `Write`
 --> src/main.rs:1:22
  |
1 | use std::io::{ self, Write };
  |                      ^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused imports: `max`, `min`
 --> src/main.rs:3:17
  |
3 | use std::cmp::{ min, max };
  |                 ^^^  ^^^

warning: unused import: `BinaryHeap`
 --> src/main.rs:4:25
  |
4 | use std::collections::{ BinaryHeap, VecDeque };
  |                         ^^^^^^^^^^

warning: unused macro definition: `trace`
 --> src/main.rs:6:14
  |
6 | macro_rules! trace {
  |              ^^^^^
  |
  = note: `#[warn(unused_macros)]` on by default

warning: unused macro definition: `swap`
  --> src/main.rs:11:14
   |
11 | macro_rules! swap { ($a:expr, $b:expr) => ({ let t = $b; $b = $a; $a = t; }) }
   |              ^^^^

warning: unused variable: `i`
  --> src/main.rs:25:9
   |
25 |     for i in 0..m {
   |         ^ help: if this is intentional, prefix it with an underscore: `_i`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `j`
  --> src/main.rs:48:9
   |
48 |     for j in 0..q {
   |         ^ help: if this is intentional, prefix it with an underscore: `_j`

ソースコード

diff #

use std::io::{ self, Write };
use std::str::FromStr;
use std::cmp::{ min, max };
use std::collections::{ BinaryHeap, VecDeque };

macro_rules! trace {
    ($var:expr) => ({
        let _ = writeln!(&mut std::io::stderr(), ">>> {} = {:?}", stringify!($var), $var);
    })
}
macro_rules! swap { ($a:expr, $b:expr) => ({ let t = $b; $b = $a; $a = t; }) }

fn main() {
    let mut sc = Scanner::new();
    let n: usize = sc.cin();
    let m: usize = sc.cin();
    let q: usize = sc.cin();

    let mut neigh = vec![ vec![]; n];
    let mut connected = vec![vec![false; n]; n];
    let mut queries = vec![];
    let mut ans = vec![q as i32; n];
    let mut reachable = vec![false; n];

    for i in 0..m {
        let a = sc.cin::<usize>() - 1;
        let b = sc.cin::<usize>() - 1;
        connected[a][b] = true;
        connected[b][a] = true;
        neigh[a].push(b);
        neigh[b].push(a);
    }

    {
        let mut stack = vec![0];
        while let Some(u) = stack.pop() {
            if reachable[u] { continue }
            reachable[u] = true;
            for &v in neigh[u].iter() {
                stack.push(v);
            }
        }
        for i in 0..n {
            if ! reachable[i] { ans[i] = 0 }
        }
    }

    for j in 0..q {
        let a = sc.cin::<usize>() - 1;
        let b = sc.cin::<usize>() - 1;
        connected[a][b] = false;
        connected[b][a] = false;
        queries.push((a, b));
    }

    {
        for i in 0..n { reachable[i] = false }
        let mut stack = vec![0];
        while let Some(u) = stack.pop() {
            if reachable[u] { continue }
            reachable[u] = true;
            for &v in neigh[u].iter() {
                if connected[u][v] {
                    stack.push(v)
                }
            }
        }
        for i in 0..n {
            if reachable[i] { ans[i] = -1 }
        }
    }

    for j in (0..q).rev() {

        let (a, b) = queries[j];
        connected[a][b] = true;
        connected[b][a] = true;

        let mut stack = vec![];
        if reachable[a] && ! reachable[b] {
            stack.push(b)
        } else if ! reachable[a] && reachable[b] {
            stack.push(a)
        }

        while let Some(u) = stack.pop() {
            if reachable[u] { continue }
            reachable[u] = true;
            ans[u] = j as i32 + 1;
            for &v in neigh[u].iter() {
                if connected[u][v] {
                    stack.push(v)
                }
            }
        }
    }

    for i in 1..n { println!("{}", ans[i]); }
}

#[allow(dead_code)]
struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
#[allow(dead_code)]
impl Scanner {
    fn new() -> Scanner { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
    fn reserve(&mut self) {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
    }
    fn cin<T: FromStr>(&mut self) -> T {
        self.reserve();
        match self.buffer.pop_front().unwrap().parse::<T>() {
            Ok(a) => a,
            Err(_) => panic!("parse err")
        }
    }
    fn get_char(&mut self) -> char {
        self.reserve();
        let head = self.buffer[0].chars().nth(0).unwrap();
        let tail = String::from( &self.buffer[0][1..] );
        if tail.len()>0 { self.buffer[0]=tail } else { self.buffer.pop_front(); }
        head
    }
}
0