結果
問題 | No.1266 7 Colors |
ユーザー |
![]() |
提出日時 | 2020-10-24 00:16:46 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,351 ms / 3,000 ms |
コード長 | 2,123 bytes |
コンパイル時間 | 211 ms |
コンパイル使用メモリ | 82,380 KB |
実行使用メモリ | 232,320 KB |
最終ジャッジ日時 | 2024-07-21 14:10:05 |
合計ジャッジ時間 | 19,571 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
class UnionFindNode:def __init__(self, group_id, parent=None, value=None):self.group_id_ = group_idself.parent_ = parentself.value = valueself.rank_ = 1self.member_num_ = 1def is_root(self):return not self.parent_def root(self):parent = selfwhile not parent.is_root():parent = parent.parent_self.parent_ = parentreturn parentdef find(self):root = self.root()return root.group_id_def rank(self):root = self.root()return root.rank_def unite(self, unite_node):root = self.root()unite_root = unite_node.root()if root.group_id_ != unite_root.group_id_:if root.rank() > unite_root.rank():n_root, child = root, unite_rootelse:n_root, child = unite_root, rootchild.parent_ = n_rootn_root.rank_ = max(n_root.rank_, child.rank_ + 1)n_root.member_num_ = n_root.member_num_ + child.member_num_import sys;input=sys.stdin.readlineN, M, Q = map(int, input().split())nodes = [UnionFindNode(i) for i in range(N*7)]ss = []for i in range(N):s = input().strip()ss.append([c for c in s])n = i*7for j in range(7):if s[j] == "1" and s[(j+1)%7] == "1":nodes[n+j].unite(nodes[n+(j+1)%7])G = [set() for _ in range(N)]for _ in range(M):u, v = map(int, input().split())u-=1;v-=1;G[u].add(v)G[v].add(u)for i in range(7):if ss[u][i] == "1" and ss[v][i] == "1":nodes[u*7+i].unite(nodes[v*7+i])for _ in range(Q):a, b, c = map(int, input().split())if a == 2:print(nodes[(b-1)*7].root().member_num_)else:b-=1c-=1ss[b][c] = "1"if ss[b][(c+1)%7] == "1":nodes[b*7+c].unite(nodes[b*7+(c+1)%7])if ss[b][(c-1)%7] == "1":nodes[b*7+c].unite(nodes[b*7+(c-1)%7])for u in G[b]:if ss[u][c] == "1":nodes[b*7+c].unite(nodes[u*7+c])