結果

問題 No.2325 Skill Tree
ユーザー googol_S0googol_S0
提出日時 2023-05-28 14:12:52
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,490 bytes
コンパイル時間 254 ms
コンパイル使用メモリ 11,240 KB
実行使用メモリ 124,788 KB
最終ジャッジ日時 2023-08-27 09:20:05
合計ジャッジ時間 92,858 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,652 KB
testcase_01 AC 18 ms
8,692 KB
testcase_02 AC 18 ms
8,524 KB
testcase_03 AC 18 ms
8,676 KB
testcase_04 AC 18 ms
8,520 KB
testcase_05 AC 18 ms
8,652 KB
testcase_06 AC 18 ms
8,680 KB
testcase_07 AC 1,022 ms
24,368 KB
testcase_08 AC 1,123 ms
56,416 KB
testcase_09 AC 1,320 ms
41,240 KB
testcase_10 AC 1,683 ms
89,308 KB
testcase_11 AC 1,653 ms
65,576 KB
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 TLE -
testcase_28 TLE -
testcase_29 TLE -
testcase_30 TLE -
testcase_31 TLE -
testcase_32 AC 2,968 ms
124,476 KB
testcase_33 AC 2,940 ms
124,528 KB
testcase_34 AC 2,986 ms
124,480 KB
testcase_35 TLE -
testcase_36 TLE -
testcase_37 TLE -
権限があれば一括ダウンロードができます

ソースコード

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