結果

問題 No.2316 Freight Train
ユーザー ニックネームニックネーム
提出日時 2023-05-26 21:26:56
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,578 ms / 2,000 ms
コード長 611 bytes
コンパイル時間 489 ms
コンパイル使用メモリ 10,768 KB
実行使用メモリ 28,192 KB
最終ジャッジ日時 2023-08-26 10:12:16
合計ジャッジ時間 35,300 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,776 KB
testcase_01 AC 16 ms
7,788 KB
testcase_02 AC 16 ms
7,880 KB
testcase_03 AC 1,525 ms
28,192 KB
testcase_04 AC 824 ms
18,064 KB
testcase_05 AC 609 ms
17,824 KB
testcase_06 AC 203 ms
9,596 KB
testcase_07 AC 1,254 ms
12,612 KB
testcase_08 AC 913 ms
27,860 KB
testcase_09 AC 1,142 ms
20,732 KB
testcase_10 AC 1,225 ms
19,032 KB
testcase_11 AC 1,070 ms
25,992 KB
testcase_12 AC 1,305 ms
24,604 KB
testcase_13 AC 1,549 ms
27,964 KB
testcase_14 AC 1,578 ms
27,876 KB
testcase_15 AC 1,550 ms
27,916 KB
testcase_16 AC 1,553 ms
27,872 KB
testcase_17 AC 1,546 ms
27,904 KB
testcase_18 AC 1,539 ms
27,872 KB
testcase_19 AC 1,538 ms
28,000 KB
testcase_20 AC 1,540 ms
27,844 KB
testcase_21 AC 1,543 ms
27,876 KB
testcase_22 AC 1,547 ms
27,864 KB
testcase_23 AC 1,362 ms
24,964 KB
testcase_24 AC 1,415 ms
25,088 KB
testcase_25 AC 1,272 ms
24,960 KB
testcase_26 AC 1,214 ms
25,020 KB
testcase_27 AC 1,115 ms
7,788 KB
testcase_28 AC 16 ms
7,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UF:
    def __init__(self,n):
        self.p = [-1]*n
    def f(self,x):
        if self.p[x]<0: return x
        else: self.p[x] = self.f(self.p[x]); return self.p[x]
    def u(self,x,y):
        x = self.f(x); y = self.f(y)
        if x==y: return
        if -self.p[x]<-self.p[y]: x,y = y,x
        self.p[x] += self.p[y]; self.p[y] = x
    def s(self,x,y): return self.f(x) == self.f(y)
n,q = map(int,input().split())
uf = UF(n)
for i,v in enumerate(map(int,input().split())):
    if v !=-1: uf.u(i,v-1)
for _ in range(q):
    a,b = map(int,input().split())
    print("Yes" if uf.s(a-1,b-1) else "No")
0