結果

問題 No.2325 Skill Tree
ユーザー googol_S0googol_S0
提出日時 2023-05-28 14:13:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,227 ms / 3,000 ms
コード長 2,490 bytes
コンパイル時間 392 ms
コンパイル使用メモリ 87,160 KB
実行使用メモリ 171,092 KB
最終ジャッジ日時 2023-08-27 09:17:56
合計ジャッジ時間 36,137 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,524 KB
testcase_01 AC 74 ms
71,480 KB
testcase_02 AC 76 ms
71,472 KB
testcase_03 AC 77 ms
71,516 KB
testcase_04 AC 75 ms
71,288 KB
testcase_05 AC 76 ms
71,388 KB
testcase_06 AC 76 ms
71,316 KB
testcase_07 AC 578 ms
89,184 KB
testcase_08 AC 645 ms
124,336 KB
testcase_09 AC 661 ms
100,684 KB
testcase_10 AC 679 ms
133,892 KB
testcase_11 AC 714 ms
118,172 KB
testcase_12 AC 1,143 ms
159,016 KB
testcase_13 AC 1,137 ms
159,648 KB
testcase_14 AC 1,227 ms
171,092 KB
testcase_15 AC 1,158 ms
160,444 KB
testcase_16 AC 1,129 ms
159,644 KB
testcase_17 AC 1,122 ms
160,020 KB
testcase_18 AC 1,106 ms
158,420 KB
testcase_19 AC 1,112 ms
160,488 KB
testcase_20 AC 1,100 ms
159,744 KB
testcase_21 AC 1,105 ms
160,412 KB
testcase_22 AC 1,128 ms
158,868 KB
testcase_23 AC 1,133 ms
160,004 KB
testcase_24 AC 1,125 ms
160,392 KB
testcase_25 AC 1,108 ms
159,056 KB
testcase_26 AC 1,110 ms
158,968 KB
testcase_27 AC 1,001 ms
160,268 KB
testcase_28 AC 996 ms
160,740 KB
testcase_29 AC 988 ms
159,884 KB
testcase_30 AC 986 ms
160,076 KB
testcase_31 AC 990 ms
160,528 KB
testcase_32 AC 942 ms
160,664 KB
testcase_33 AC 944 ms
160,552 KB
testcase_34 AC 950 ms
160,736 KB
testcase_35 AC 990 ms
160,380 KB
testcase_36 AC 983 ms
160,232 KB
testcase_37 AC 996 ms
160,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def scc(N,edges):
    M=len(edges)
    start=[0]*(N+1)
    elist=[0]*M
    for e in edges:
        start[e[0]+1]+=1
    for i in range(1,N+1):
        start[i]+=start[i-1]
    counter=start[:]
    for e in edges:
        elist[counter[e[0]]]=e[1]
        counter[e[0]]+=1
    visited=[]
    low=[0]*N
    Ord=[-1]*N
    ids=[0]*N
    NG=[0,0]
    def dfs(v):
        stack=[(v,-1,0),(v,-1,1)]
        while stack:
            v,bef,t=stack.pop()
            if t:
                if bef!=-1 and Ord[v]!=-1:
                    low[bef]=min(low[bef],Ord[v])
                    stack.pop()
                    continue
                low[v]=NG[0]
                Ord[v]=NG[0]
                NG[0]+=1
                visited.append(v)
                for i in range(start[v],start[v+1]):
                    to=elist[i]
                    if Ord[to]==-1:
                        stack.append((to,v,0))
                        stack.append((to,v,1))
                    else:
                        low[v]=min(low[v],Ord[to])
            else:
                if low[v]==Ord[v]:
                    while(True):
                        u=visited.pop()
                        Ord[u]=N
                        ids[u]=NG[1]
                        if u==v:
                            break
                    NG[1]+=1
                low[bef]=min(low[bef],low[v])
    for i in range(N):
        if Ord[i]==-1:
            dfs(i)
    for i in range(N):
        ids[i]=NG[1]-1-ids[i]
    group_num=NG[1]
    counts=[0]*group_num
    for x in ids:
        counts[x]+=1
    groups=[[] for i in range(group_num)]
    for i in range(N):
        groups[ids[i]].append(i)
    return groups

N=int(input())
G=[]
X=[(0,-1)]
INF=10**15
for i in range(1,N):
    l,a=map(int,input().split())
    X.append((l,a-1))
    G.append((i,a-1))
S=scc(N,G)
for i in range(len(S)):
    if len(S[i])>1:
        for j in range(len(S[i])):
            X[S[i][j]]=(INF,0)
import sys
sys.setrecursionlimit(10**6)
L=[-1]*N
G2=[[] for i in range(N)]
for i in range(1,N):
    G2[i].append(X[i][1])
def dfs(n):
    if L[n]>=0:
        return L[n]
    L[n]=X[n][0]
    for i in range(len(G2[n])):
        L[n]=max(L[n],dfs(G2[n][i]))
    return L[n]

for i in range(N):
    dfs(i)
C=sorted(L)
from bisect import *
Q=int(input())
for i in range(Q):
    t,x=map(int,input().split())
    if t==1:
        print(bisect_right(C,x))
    else:
        if L[x-1]>=INF:
            print(-1)
        else:
            print(L[x-1])
0