結果
問題 | No.416 旅行会社 |
ユーザー | vwxyz |
提出日時 | 2022-02-07 02:11:12 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
MLE
|
実行時間 | - |
コード長 | 3,861 bytes |
コンパイル時間 | 76 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 819,892 KB |
最終ジャッジ日時 | 2024-06-11 15:00:07 |
合計ジャッジ時間 | 9,006 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | MLE | - |
testcase_01 | -- | - |
testcase_02 | -- | - |
testcase_03 | -- | - |
testcase_04 | -- | - |
testcase_05 | -- | - |
testcase_06 | -- | - |
testcase_07 | -- | - |
testcase_08 | -- | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
ソースコード
import bisect import copy import decimal import fractions import heapq import itertools import math import random import sys import time from collections import Counter, deque,defaultdict from functools import lru_cache,reduce from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max def _heappush_max(heap,item): heap.append(item) heapq._siftdown_max(heap, 0, len(heap)-1) def _heappushpop_max(heap, item): if heap and item < heap[0]: item, heap[0] = heap[0], item heapq._siftup_max(heap, 0) return item from math import gcd as Gcd read=sys.stdin.read readline=sys.stdin.readline readlines=sys.stdin.readlines class UnionFind: def __init__(self,N,label=None,f=None,weighted=False): self.N=N self.parents=[None]*self.N self.size=[1]*self.N self.roots={i for i in range(self.N)} self.label=label if self.label!=None: self.label=[x for x in label] self.f=f self.weighted=weighted if self.weighted: self.weight=[0]*self.N def Find(self,x): stack=[] while self.parents[x]!=None: stack.append(x) x=self.parents[x] if self.weighted: w=0 for y in stack[::-1]: self.parents[y]=x w+=self.weight[y] self.weight[y]=w else: for y in stack[::-1]: self.parents[y]=x return x def Union(self,x,y,w=None): root_x=self.Find(x) root_y=self.Find(y) if root_x==root_y: if self.weighted: if self.weight[y]-self.weight[x]==w: return True else: return False else: if self.size[root_x]>self.size[root_y]: x,y=y,x root_x,root_y=root_y,root_x if self.weighted: w=-w self.parents[root_y]=root_x self.size[root_x]+=self.size[root_y] self.roots.remove(root_y) if self.label!=None: self.label[root_x]=self.f(self.label[root_x],self.label[root_y]) if self.weighted: self.weight[root_y]=w+self.weight[x]-self.weight[y] def Size(self,x): return self.size[self.Find(x)] def Same(self,x,y): return self.Find(x)==self.Find(y) def Label(self,x): return self.label[self.Find(x)] def Weight(self,x,y): root_x=self.Find(x) root_y=self.Find(y) if root_x!=root_y: return None return self.weight[y]-self.weight[x] def Roots(self): return list(self.roots) def Link_Components_Count(self): return len(self.roots) def __str__(self): linked_components=defaultdict(list) for x in range(self.N): linked_components[self.Find(x)].append(x) return '\n'.join(f'{r}: {m}' for r,m in linked_components.items()) N,M,Q=map(int,readline().split()) A,B=[],[] for _ in range(M): a,b=map(int,readline().split()) a-=1;b-=1 A.append(a) B.append(b) C,D=[],[] CD=set() for i in range(Q): c,d=map(int,readline().split()) c-=1;d-=1 C.append(c) D.append(d) CD.add((c,d)) def f(lst0,lst1): for x in lst1: lst0.append(x) return lst0 UF=UnionFind(N,label=[[x] for x in range(N)],f=f) for a,b in zip(A,B): if not (a,b) in CD: UF.Union(a,b) ans_lst=[0]*N for x in UF.Label(0): ans_lst[x]=-1 for i in range(Q-1,-1,-1): c,d=C[i],D[i] bl_c=UF.Same(c,0) bl_d=UF.Same(d,0) if not bl_c and bl_d: for x in UF.Label(c): ans_lst[x]=i+1 if bl_c and not bl_d: for x in UF.Label(d): ans_lst[x]=i+1 UF.Union(c,d) print(*ans_lst[1:],sep="\n")