use std::collections::HashSet; struct UnionFind { n: usize, parents: Vec, depths: Vec, chains: Vec, } 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 = 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| { 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::>(); 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 = 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 = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); (temp[0], temp[1], temp[2]) }) .collect::>(); 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)]); } } }