結果
問題 | No.2325 Skill Tree |
ユーザー | katonyonko |
提出日時 | 2023-05-28 14:36:52 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 2,410 ms / 3,000 ms |
コード長 | 3,130 bytes |
コンパイル時間 | 251 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 230,200 KB |
最終ジャッジ日時 | 2024-06-08 05:52:33 |
合計ジャッジ時間 | 56,629 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 44 ms
55,168 KB |
testcase_01 | AC | 43 ms
54,656 KB |
testcase_02 | AC | 44 ms
54,528 KB |
testcase_03 | AC | 42 ms
54,656 KB |
testcase_04 | AC | 44 ms
54,912 KB |
testcase_05 | AC | 43 ms
54,784 KB |
testcase_06 | AC | 43 ms
55,168 KB |
testcase_07 | AC | 623 ms
120,448 KB |
testcase_08 | AC | 712 ms
127,616 KB |
testcase_09 | AC | 794 ms
134,528 KB |
testcase_10 | AC | 1,074 ms
150,144 KB |
testcase_11 | AC | 973 ms
148,096 KB |
testcase_12 | AC | 1,798 ms
210,316 KB |
testcase_13 | AC | 1,746 ms
210,368 KB |
testcase_14 | AC | 1,738 ms
210,000 KB |
testcase_15 | AC | 1,832 ms
211,556 KB |
testcase_16 | AC | 1,772 ms
210,244 KB |
testcase_17 | AC | 1,341 ms
190,080 KB |
testcase_18 | AC | 1,361 ms
190,464 KB |
testcase_19 | AC | 1,339 ms
190,080 KB |
testcase_20 | AC | 1,404 ms
191,104 KB |
testcase_21 | AC | 1,341 ms
190,592 KB |
testcase_22 | AC | 2,156 ms
227,896 KB |
testcase_23 | AC | 2,206 ms
229,448 KB |
testcase_24 | AC | 2,168 ms
227,016 KB |
testcase_25 | AC | 2,181 ms
225,484 KB |
testcase_26 | AC | 2,293 ms
226,744 KB |
testcase_27 | AC | 2,047 ms
213,496 KB |
testcase_28 | AC | 1,942 ms
213,120 KB |
testcase_29 | AC | 1,948 ms
212,344 KB |
testcase_30 | AC | 1,924 ms
213,736 KB |
testcase_31 | AC | 1,920 ms
212,796 KB |
testcase_32 | AC | 1,523 ms
194,560 KB |
testcase_33 | AC | 1,530 ms
194,688 KB |
testcase_34 | AC | 1,518 ms
193,920 KB |
testcase_35 | AC | 2,358 ms
229,892 KB |
testcase_36 | AC | 2,410 ms
230,200 KB |
testcase_37 | AC | 2,390 ms
229,560 KB |
ソースコード
import io import sys _INPUT = """\ 6 5 5 1 10 1 8 3 38 4 7 1 1 1 8 1 10 2 2 2 3 2 4 2 5 4 10 4 10 2 10 3 5 1 1 1 10 2 2 2 3 2 4 """ def solve(test): import sys sys.setrecursionlimit(10**6) class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def group_count(self): return len(self.roots()) def all_group_members(self): return {r: self.members(r) for r in self.roots()} def __str__(self): return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots()) from collections import deque def bfs(G,s,z): D[s]=0 dq=deque() dq.append(s) while dq: x=dq.popleft() L[x]=z for y in G[x]: if D[y]>D[x]+1: D[y]=D[x]+1 dq.append(y) return D N=int(input()) W=[list(map(int,input().split())) for _ in range(N-1)] W=[(W[i][0],W[i][1]-1,i+1) for i in range(N-1)] Q=int(input()) inf=10**10 L=[-1]*N D=[inf]*N query=[list(map(int,input().split())) for _ in range(Q)] query1=[(query[i][1],inf,i) for i in range(Q) if query[i][0]==1] ans=[-1]*Q tmp=sorted(W+query1,key=lambda x: (x[0],x[1])) uf=UnionFind(N) G=[[] for _ in range(N)] for i in range(len(tmp)): l,t,idx=tmp[i] if t==inf: ans[idx]=uf.size(0) else: if uf.find(t)==uf.find(0) and uf.find(idx)!=uf.find(0): bfs(G,idx,l) if uf.find(idx)==uf.find(0) and uf.find(t)!=uf.find(0): bfs(G,t,l) uf.union(t,idx) G[t].append(idx) G[idx].append(t) for i in range(Q): d,y=query[i] if d==2: ans[i]=L[y-1] if test==0: print(*ans,sep='\n') else: return None def random_input(): from random import randint,shuffle N=randint(1,10) M=randint(1,N) A=list(range(1,M+1))+[randint(1,M) for _ in range(N-M)] shuffle(A) return (" ".join(map(str, [N,M]))+"\n"+" ".join(map(str, A))+"\n")*3 def simple_solve(): return [] def main(test): if test==0: solve(0) elif test==1: sys.stdin = io.StringIO(_INPUT) case_no=int(input()) for _ in range(case_no): solve(0) else: for i in range(1000): sys.stdin = io.StringIO(random_input()) x=solve(1) y=simple_solve() if x!=y: print(i,x,y) print(*[line for line in sys.stdin],sep='') break #0:提出用、1:与えられたテスト用、2:ストレステスト用 main(0)