Submission #19582627


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 Python (3.8.2)
Score 0
Code Size 1507 Byte
Status TLE
Exec Time 2207 ms
Memory 34824 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 600
Status
AC × 3
AC × 43
TLE × 6
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 26 ms 9036 KB
sample_02.txt AC 22 ms 9152 KB
sample_03.txt AC 23 ms 9200 KB
subtask_1_1.txt AC 24 ms 9032 KB
subtask_1_10.txt AC 179 ms 9344 KB
subtask_1_11.txt AC 1291 ms 9472 KB
subtask_1_12.txt AC 50 ms 16412 KB
subtask_1_13.txt AC 43 ms 9156 KB
subtask_1_14.txt AC 28 ms 9276 KB
subtask_1_15.txt AC 410 ms 9248 KB
subtask_1_16.txt AC 55 ms 9048 KB
subtask_1_17.txt AC 117 ms 9208 KB
subtask_1_18.txt AC 28 ms 9156 KB
subtask_1_19.txt AC 73 ms 15880 KB
subtask_1_2.txt AC 22 ms 9204 KB
subtask_1_20.txt AC 22 ms 9052 KB
subtask_1_21.txt AC 75 ms 9152 KB
subtask_1_22.txt AC 37 ms 12976 KB
subtask_1_23.txt AC 695 ms 23992 KB
subtask_1_24.txt AC 861 ms 23880 KB
subtask_1_25.txt AC 1081 ms 24148 KB
subtask_1_26.txt AC 1338 ms 25108 KB
subtask_1_27.txt TLE 2207 ms 33496 KB
subtask_1_28.txt TLE 2207 ms 33380 KB
subtask_1_29.txt AC 704 ms 23876 KB
subtask_1_3.txt AC 22 ms 9200 KB
subtask_1_30.txt AC 866 ms 24064 KB
subtask_1_31.txt AC 1061 ms 23976 KB
subtask_1_32.txt AC 1389 ms 25428 KB
subtask_1_33.txt TLE 2207 ms 33892 KB
subtask_1_34.txt TLE 2207 ms 34084 KB
subtask_1_35.txt AC 705 ms 23848 KB
subtask_1_36.txt AC 862 ms 23980 KB
subtask_1_37.txt AC 1068 ms 24152 KB
subtask_1_38.txt AC 1342 ms 24744 KB
subtask_1_39.txt AC 1941 ms 34328 KB
subtask_1_4.txt AC 95 ms 21144 KB
subtask_1_40.txt AC 1992 ms 34264 KB
subtask_1_41.txt AC 500 ms 9048 KB
subtask_1_42.txt AC 717 ms 9060 KB
subtask_1_43.txt TLE 2206 ms 26000 KB
subtask_1_44.txt TLE 2207 ms 34824 KB
subtask_1_45.txt AC 421 ms 9164 KB
subtask_1_46.txt AC 462 ms 24004 KB
subtask_1_5.txt AC 191 ms 9056 KB
subtask_1_6.txt AC 50 ms 16724 KB
subtask_1_7.txt AC 23 ms 9988 KB
subtask_1_8.txt AC 28 ms 10496 KB
subtask_1_9.txt AC 110 ms 10464 KB