Submission #38284930
Source Code Expand
Copy
from collections import defaultdictfrom itertools import chainclass DSU():def __init__(self, n=None):self.dsu = defaultdict(lambda: -1)if n:for i in range(n): self.dsu[i]def leader(self, i):if self.dsu[i] < 0: return iself.dsu[i] = self.leader(self.dsu[i])return self.dsu[i]def merge(self, i, j):'''サイズの大きい方(x)に小さい方(y)をマージ'''if i == j: returnx = self.leader(i)y = self.leader(j)if x == y: return
from collections import defaultdict from itertools import chain class DSU(): def __init__(self, n=None): self.dsu = defaultdict(lambda: -1) if n: for i in range(n): self.dsu[i] def leader(self, i): if self.dsu[i] < 0: return i self.dsu[i] = self.leader(self.dsu[i]) return self.dsu[i] def merge(self, i, j): '''サイズの大きい方(x)に小さい方(y)をマージ''' if i == j: return x = self.leader(i) y = self.leader(j) if x == y: return if self.dsu[x] > self.dsu[y]: x, y = y, x self.dsu[x] += self.dsu[y] self.dsu[y] = x def members(self, i): l = self.leader(i) return [k for k, v in self.dsu.items() if self.leader(k) == l] def __len__(self): return len([1 for v in self.dsu.values() if v < 0]) def D(N, ST): mapper = {name: i for i, name in enumerate(set(chain.from_iterable(ST)))} dsu = DSU() for s, t in ST: if dsu.leader(mapper[s]) == dsu.leader(mapper[t]): return "No" dsu.merge(mapper[s], mapper[t]) return "Yes" N = int(input()) ST = [input().split() for _ in range(N)] print(D(N, ST))
Submission Info
Submission Time | |
---|---|
Task | D - Change Usernames |
User | arakaki_tokyo |
Language | PyPy3 (7.3.0) |
Score | 400 |
Code Size | 1295 Byte |
Status | AC |
Exec Time | 515 ms |
Memory | 156852 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 400 / 400 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | sample_01.txt, sample_02.txt, sample_03.txt |
All | random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, sample_01.txt, sample_02.txt, sample_03.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
random_01.txt | AC | 366 ms | 156852 KB |
random_02.txt | AC | 350 ms | 156544 KB |
random_03.txt | AC | 497 ms | 127648 KB |
random_04.txt | AC | 505 ms | 119472 KB |
random_05.txt | AC | 515 ms | 127392 KB |
random_06.txt | AC | 507 ms | 119404 KB |
random_07.txt | AC | 504 ms | 127752 KB |
random_08.txt | AC | 439 ms | 119372 KB |
random_09.txt | AC | 293 ms | 119328 KB |
random_10.txt | AC | 439 ms | 122320 KB |
random_11.txt | AC | 430 ms | 124232 KB |
random_12.txt | AC | 421 ms | 126392 KB |
random_13.txt | AC | 412 ms | 126684 KB |
random_14.txt | AC | 409 ms | 119328 KB |
random_15.txt | AC | 386 ms | 120388 KB |
random_16.txt | AC | 413 ms | 124504 KB |
random_17.txt | AC | 414 ms | 126612 KB |
random_18.txt | AC | 415 ms | 127016 KB |
random_19.txt | AC | 54 ms | 65408 KB |
sample_01.txt | AC | 53 ms | 65496 KB |
sample_02.txt | AC | 53 ms | 65492 KB |
sample_03.txt | AC | 50 ms | 65240 KB |