🦀
ALDS 1_11_A Graph by Rust
問題
無向グラフ構造を正方行列で表現する。
Sample Input
4
1 2 2 4
2 1 4
3 0
4 1 3
Sample Output
0 1 0 1
0 0 0 1
0 0 0 0
0 0 1 0
Rust
use std::io;
fn read<T: std::str::FromStr>() -> Vec<T> {
let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();
buf.trim().split(' ').flat_map(str::parse).collect()
}
fn main() {
let n = read::<usize>()[0];
let mut graph_mat = vec![ vec![0;n] ;n];
for i in 0..n {
// u k v v v...
let inputs = read::<usize>();
let u = inputs[0] - 1;
let k = inputs[1];
if inputs.len() > 2 {
for v in inputs[2..].into_iter() {
graph_mat[u][v-1] = 1;
}
}
}
for i in 0..n {
for j in 0..n {
if j > 0 { print!(" ");}
print!("{}", graph_mat[i][j]);
}
println!("");
}
}
Discussion