結果
問題 | No.2290 UnUnion Find |
ユーザー |
|
提出日時 | 2024-07-04 21:36:46 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,852 bytes |
コンパイル時間 | 196 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 48,012 KB |
最終ジャッジ日時 | 2024-07-04 21:36:53 |
合計ジャッジ時間 | 5,454 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 1 TLE * 1 -- * 44 |
ソースコード
class DSU:def __init__(self, n):self.parent = list(range(n))self.rank = [1] * ndef find(self, x):if self.parent[x] != x:self.parent[x] = self.find(self.parent[x])return self.parent[x]def union(self, x, y):rootX = self.find(x)rootY = self.find(y)if rootX != rootY:if self.rank[rootX] > self.rank[rootY]:self.parent[rootY] = rootXelif self.rank[rootX] < self.rank[rootY]:self.parent[rootX] = rootYelse:self.parent[rootY] = rootXself.rank[rootX] += 1def process_queries(N, Q, queries):dsu = DSU(N)connected = [set() for _ in range(N)]results = []for query in queries:if query[0] == 1:u = query[1] - 1v = query[2] - 1dsu.union(u, v)connected[u].add(v)connected[v].add(u)elif query[0] == 2:v = query[1] - 1found = Falsefor i in range(N):if i != v and i not in connected[v] and dsu.find(i) != dsu.find(v):results.append(str(i + 1))found = Truebreakif not found:results.append('-1')return "\n".join(results)import sysinput = sys.stdin.readdata = input().split()N = int(data[0])Q = int(data[1])queries = []index = 2for _ in range(Q):query_type = int(data[index])if query_type == 1:u = int(data[index + 1])v = int(data[index + 2])queries.append([1, u, v])index += 3elif query_type == 2:v = int(data[index + 1])queries.append([2, v])index += 2print(process_queries(N, Q, queries))