結果
| 問題 |
No.1266 7 Colors
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-11-13 21:43:59 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 347 ms / 3,000 ms |
| コード長 | 3,946 bytes |
| コンパイル時間 | 13,932 ms |
| コンパイル使用メモリ | 377,296 KB |
| 実行使用メモリ | 38,604 KB |
| 最終ジャッジ日時 | 2024-09-15 03:06:58 |
| 合計ジャッジ時間 | 21,641 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge6 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
コンパイルメッセージ
warning: unused import: `std::collections::HashSet`
--> src/main.rs:1:5
|
1 | use std::collections::HashSet;
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
warning: field `n` is never read
--> src/main.rs:5:5
|
4 | struct UnionFind {
| --------- field in this struct
5 | n: usize,
| ^
|
= note: `#[warn(dead_code)]` on by default
warning: method `chain_cnt` is never used
--> src/main.rs:48:8
|
11 | impl UnionFind {
| -------------- method in this implementation
...
48 | fn chain_cnt(&mut self, i: usize) -> usize {
| ^^^^^^^^^
ソースコード
use std::collections::HashSet;
struct UnionFind {
n: usize,
parents: Vec<usize>,
depths: Vec<usize>,
chains: Vec<usize>,
}
impl UnionFind {
fn new(n: usize) -> Self {
UnionFind {
n: n,
parents: (0..n).collect(),
depths: vec![0; n],
chains: vec![1; n],
}
}
fn equiv(&mut self, a: usize, b: usize) -> bool {
self.find(a) == self.find(b)
}
fn unite(&mut self, a: usize, b: usize) {
if self.equiv(a, b) { return; }
let x = self.parents[a];
let y = self.parents[b];
if self.depths[x] > self.depths[y] {
self.parents[x] = self.parents[y];
self.chains[y] += self.chains[x];
} else {
self.parents[y] = self.parents[x];
self.chains[x] += self.chains[y];
if self.depths[x] == self.depths[y] {
self.depths[x] += 1;
}
}
}
fn find(&mut self, a: usize) -> usize {
if self.parents[a] == a { return a; }
let p = self.find(self.parents[a]);
self.parents[a] = p;
p
}
fn chain_cnt(&mut self, i: usize) -> usize {
let idx = self.find(i);
self.chains[idx]
}
}
fn main() {
let mut nmq = String::new();
std::io::stdin().read_line(&mut nmq).ok();
let nmq: Vec<usize> = nmq.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
let n = nmq[0];
let m = nmq[1];
let q = nmq[2];
let mut uf = UnionFind::new(7*n);
let mut size = vec![1usize; 7*n];
let mut colors = vec![vec![false; 7]; n];
let mut paths = vec![vec![]; n];
let merge = |left: usize, right: usize, uf: &mut UnionFind, size: &mut Vec<usize>| {
let pu = uf.find(left);
let pv = uf.find(right);
if pu == pv { return; }
uf.unite(pu, pv);
if pu == uf.find(pu) {
size[pu] += size[pv];
size[pv] = 0;
} else {
size[pv] += size[pu];
size[pu] = 0;
}
};
for i in 0..n {
let mut s = String::new();
std::io::stdin().read_line(&mut s).ok();
let s = s.trim().chars().map(|c| c as usize - '0' as usize).collect::<Vec<_>>();
for j in 0..7 {
let idx = (j+1) % 7;
colors[i][j] = s[j] == 1;
if s[j] == 1 && s[idx] == 1 {
merge(i+j*n, i+idx*n, &mut uf, &mut size);
}
}
}
for _ in 0..m {
let mut uv = String::new();
std::io::stdin().read_line(&mut uv).ok();
let uv: Vec<usize> = uv.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
let u = uv[0]-1;
let v = uv[1]-1;
paths[u].push(v);
paths[v].push(u);
for i in 0..7 {
if colors[u][i] && colors[v][i] {
merge(u+i*n, v+i*n, &mut uf, &mut size);
}
}
}
let queries = (0..q).map(|_| {
let mut temp = String::new();
std::io::stdin().read_line(&mut temp).ok();
let temp: Vec<usize> = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
(temp[0], temp[1], temp[2])
})
.collect::<Vec<_>>();
for &(t, x, y) in queries.iter() {
if t == 1 {
let x = x-1;
let y = y-1;
colors[x][y] = true;
let pidx = (7+y-1) % 7;
let nidx = (y+1) % 7;
if colors[x][pidx] {
merge(x+y*n, x+pidx*n, &mut uf, &mut size);
}
if colors[x][nidx] {
merge(x+y*n, x+nidx*n, &mut uf, &mut size);
}
for &v in paths[x].iter() {
if colors[v][y] {
merge(x + y*n, v + y*n, &mut uf, &mut size);
}
}
} else {
println!("{}", size[uf.find(x-1)]);
}
}
}