Submission #19582633


Source Code Expand

Copy
class pp_UnionFind():
    def __init__(self,N):
        from bisect import bisect
        self.INF = 1e18
        *self.p, = range(N)
        self.sz = [1]*N
        self.H = [1]*N
        self.S = [[(0, 1)] for i in range(N)]
        self.T = [self.INF]*N
    def find(self,x,t):
        while self.T[x] <= t:
            x = self.p[x]
        return x
    def union(self,x,y,t):
        px = self.find(x, t)
        py = self.find(y, t)
        if px == py:
            return 0
        if self.H[py] < self.H[px]:
            self.p[py] = px
            self.T[py] = t
            self.sz[px] += self.sz[py]
            self.S[px].append((t, self.sz[px]))
        else:
            self.p[px] = py
            self.T[px] = t
            self.sz[py] += self.sz[px]
            self.S[py].append((t, self.sz[py]))
            self.H[py] = max(self.H[py], self.H[px]+1)
        return 1
    def size(self,x, t):
        y = self.find(x, t)
        idx = bisect(self,self.S[y], (t, self.INF))-1
        return self.S[y][idx]
n,m=map(int,input().split())
uf=pp_UnionFind(n)
for i in range(m):
    a,b=map(int,input().split())
    uf.union(a-1,b-1,i)
for i in range(int(input())):
    a,b=map(int,input().split())
    a-=1
    b-=1
    ok=m
    ng=-1
    while abs(ok-ng)>1:
        num=(ok+ng)//2
        if uf.find(a,num)==uf.find(b,num):
            ok=num
        else:
            ng=num
    if ok==m:
        print(-1)
    else:
        print(ok+1)

Submission Info

Submission Time
Task H - Union Sets
User nasubi24
Language PyPy3 (7.3.0)
Score 600
Code Size 1507 Byte
Status AC
Exec Time 905 ms
Memory 95540 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 49
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All sample_01.txt, sample_02.txt, sample_03.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_12.txt, subtask_1_13.txt, subtask_1_14.txt, subtask_1_15.txt, subtask_1_16.txt, subtask_1_17.txt, subtask_1_18.txt, subtask_1_19.txt, subtask_1_2.txt, subtask_1_20.txt, subtask_1_21.txt, subtask_1_22.txt, subtask_1_23.txt, subtask_1_24.txt, subtask_1_25.txt, subtask_1_26.txt, subtask_1_27.txt, subtask_1_28.txt, subtask_1_29.txt, subtask_1_3.txt, subtask_1_30.txt, subtask_1_31.txt, subtask_1_32.txt, subtask_1_33.txt, subtask_1_34.txt, subtask_1_35.txt, subtask_1_36.txt, subtask_1_37.txt, subtask_1_38.txt, subtask_1_39.txt, subtask_1_4.txt, subtask_1_40.txt, subtask_1_41.txt, subtask_1_42.txt, subtask_1_43.txt, subtask_1_44.txt, subtask_1_45.txt, subtask_1_46.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt
Case Name Status Exec Time Memory
sample_01.txt AC 164 ms 62252 KB
sample_02.txt AC 54 ms 62340 KB
sample_03.txt AC 52 ms 62248 KB
subtask_1_1.txt AC 46 ms 62128 KB
subtask_1_10.txt AC 193 ms 70896 KB
subtask_1_11.txt AC 511 ms 71876 KB
subtask_1_12.txt AC 95 ms 77620 KB
subtask_1_13.txt AC 106 ms 69576 KB
subtask_1_14.txt AC 85 ms 70116 KB
subtask_1_15.txt AC 263 ms 71884 KB
subtask_1_16.txt AC 144 ms 72628 KB
subtask_1_17.txt AC 140 ms 69844 KB
subtask_1_18.txt AC 92 ms 70364 KB
subtask_1_19.txt AC 129 ms 75864 KB
subtask_1_2.txt AC 51 ms 62308 KB
subtask_1_20.txt AC 72 ms 68084 KB
subtask_1_21.txt AC 130 ms 69468 KB
subtask_1_22.txt AC 71 ms 70676 KB
subtask_1_23.txt AC 379 ms 86596 KB
subtask_1_24.txt AC 393 ms 86568 KB
subtask_1_25.txt AC 424 ms 86564 KB
subtask_1_26.txt AC 505 ms 86652 KB
subtask_1_27.txt AC 905 ms 92388 KB
subtask_1_28.txt AC 897 ms 91056 KB
subtask_1_29.txt AC 402 ms 86544 KB
subtask_1_3.txt AC 55 ms 62956 KB
subtask_1_30.txt AC 403 ms 86292 KB
subtask_1_31.txt AC 411 ms 86464 KB
subtask_1_32.txt AC 501 ms 86780 KB
subtask_1_33.txt AC 847 ms 91076 KB
subtask_1_34.txt AC 860 ms 89460 KB
subtask_1_35.txt AC 386 ms 86744 KB
subtask_1_36.txt AC 391 ms 86436 KB
subtask_1_37.txt AC 439 ms 86752 KB
subtask_1_38.txt AC 483 ms 86656 KB
subtask_1_39.txt AC 668 ms 93032 KB
subtask_1_4.txt AC 142 ms 81272 KB
subtask_1_40.txt AC 706 ms 95540 KB
subtask_1_41.txt AC 377 ms 70324 KB
subtask_1_42.txt AC 443 ms 70784 KB
subtask_1_43.txt AC 789 ms 86232 KB
subtask_1_44.txt AC 873 ms 90688 KB
subtask_1_45.txt AC 353 ms 70948 KB
subtask_1_46.txt AC 376 ms 85728 KB
subtask_1_5.txt AC 200 ms 70492 KB
subtask_1_6.txt AC 93 ms 77776 KB
subtask_1_7.txt AC 56 ms 65344 KB
subtask_1_8.txt AC 57 ms 68180 KB
subtask_1_9.txt AC 148 ms 71644 KB