結果

問題 No.2325 Skill Tree
ユーザー katonyonkokatonyonko
提出日時 2023-05-28 14:36:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,604 ms / 3,000 ms
コード長 3,130 bytes
コンパイル時間 270 ms
コンパイル使用メモリ 86,848 KB
実行使用メモリ 233,852 KB
最終ジャッジ日時 2023-08-27 10:14:34
合計ジャッジ時間 63,296 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
71,752 KB
testcase_01 AC 94 ms
71,692 KB
testcase_02 AC 98 ms
71,760 KB
testcase_03 AC 94 ms
71,516 KB
testcase_04 AC 96 ms
71,664 KB
testcase_05 AC 98 ms
71,560 KB
testcase_06 AC 95 ms
71,548 KB
testcase_07 AC 712 ms
122,308 KB
testcase_08 AC 824 ms
129,152 KB
testcase_09 AC 940 ms
137,296 KB
testcase_10 AC 1,241 ms
154,076 KB
testcase_11 AC 1,094 ms
149,592 KB
testcase_12 AC 1,974 ms
211,400 KB
testcase_13 AC 1,976 ms
212,968 KB
testcase_14 AC 1,980 ms
210,888 KB
testcase_15 AC 2,072 ms
211,904 KB
testcase_16 AC 1,961 ms
212,280 KB
testcase_17 AC 1,544 ms
191,972 KB
testcase_18 AC 1,549 ms
192,392 KB
testcase_19 AC 1,540 ms
191,564 KB
testcase_20 AC 1,603 ms
192,184 KB
testcase_21 AC 1,541 ms
192,208 KB
testcase_22 AC 2,430 ms
229,748 KB
testcase_23 AC 2,533 ms
231,236 KB
testcase_24 AC 2,437 ms
228,388 KB
testcase_25 AC 2,417 ms
227,732 KB
testcase_26 AC 2,415 ms
227,944 KB
testcase_27 AC 2,132 ms
215,744 KB
testcase_28 AC 2,118 ms
214,348 KB
testcase_29 AC 2,138 ms
214,016 KB
testcase_30 AC 2,177 ms
214,992 KB
testcase_31 AC 2,154 ms
213,736 KB
testcase_32 AC 1,699 ms
196,848 KB
testcase_33 AC 1,727 ms
197,180 KB
testcase_34 AC 1,710 ms
196,824 KB
testcase_35 AC 2,577 ms
233,180 KB
testcase_36 AC 2,604 ms
233,852 KB
testcase_37 AC 2,600 ms
232,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
0