input
stringlengths
20
127k
target
stringlengths
20
119k
problem_id
stringlengths
6
6
n = int(eval(input())) l = list(map(int, input().split())) def is_triangle(a,b,c): if a < b + c and b < a + c: return True else: return False l.sort(reverse=True) ans = 0 for i in range(n): for j in range(i+1,n): for k in range(j+1,n): if is_triangle(l...
n = int(eval(input())) l = list(map(int, input().split())) l.sort(reverse=True) ans = 0 #1番長い辺(a)は固定する for i in range(n - 2): #2番目に長い辺(b)=次に長い辺とする j = i + 1 #3番目の長さの辺(c)=リストの中で最短の辺からスタート k = n - 1 while j < k: #b<a+c,c<a+b は自明。a<b+cを判定する。 #満たす場合、このbにおいて、cまでの全ての要素は...
p02888
import sys readline = sys.stdin.readline N = int(readline()) L = list(map(int,readline().split())) L = sorted(L) # 2本固定すると、もう一本として選べるのは、2本の和より長いものだけ # これを二分探索で数える #print(L) import bisect ans = 0 for i in range(N - 1): for j in range(i + 1,N): limit = L[i] + L[j] ng = len(L) ok = j ...
import sys readline = sys.stdin.readline N = int(readline()) L = list(map(int,readline().split())) L = sorted(L) # 2本固定すると、もう一本として選べるのは、2本の和より長いものだけ # これを二分探索で数える #print(L) import bisect ans = 0 for i in range(N - 1): for j in range(i + 1,N): limit = L[i] + L[j] ok = bisect.bisect_left(L, l...
p02888
# https://atcoder.jp/contests/abc143/tasks/abc143_d from bisect import bisect_left, bisect_right import sys input = sys.stdin.readline n = int(eval(input())) # 入力が1つ # map(int, input().split()) # 入力が複数 L = [int(i) for i in input().split()] L.sort() pairs = [] for i in range(n): for j in range(i + 1, n): ...
# https://atcoder.jp/contests/abc143/tasks/abc143_d from bisect import bisect_left, bisect_right import sys input = sys.stdin.readline n = int(eval(input())) # 入力が1つ # map(int, input().split()) # 入力が複数 L = [int(i) for i in input().split()] L.sort() pairs = [] for i in range(n): for j in range(i + 1, n): ...
p02888
# https://atcoder.jp/contests/abc143/tasks/abc143_d from bisect import bisect_left, bisect_right import sys input = sys.stdin.readline n = int(eval(input())) # 入力が1つ # map(int, input().split()) # 入力が複数 L = [int(i) for i in input().split()] L.sort() pairs = [] for i in range(n): for j in range(i + 1, n): ...
# https://atcoder.jp/contests/abc143/tasks/abc143_d from bisect import bisect_left, bisect_right import sys input = sys.stdin.readline n = int(eval(input())) # 入力が1つ # map(int, input().split()) # 入力が複数 L = [int(i) for i in input().split()] L.sort() ans = 0 for i in range(n): for j in range(i + 1, n): ...
p02888
n = int(eval(input())) xs = list(map(int, input().split())) xs = sorted(list(xs)) ts = [] for i in range(n): for j in range(i+1, n): ts.append(xs[i] + xs[j]) ts.sort() i = 0 j = 0 r = 0 xs.append(10**18) while True: if j == len(xs) or i == len(ts): break if ts[i] <= xs[j]: ...
n = int(eval(input())) X = list(map(int, input().split())) X.sort() k=r=0 for i in range(n): k = i for j in range(i+1, n): while k<n and X[i]+X[j]>X[k]: k +=1 r += k-j-1 print(r)
p02888
n,*l=list(map(int,open(0).read().split())) l.sort() a=0 for i in range(n): for j in range(i+1,n): for k in range(j+1,n): if l[k]>=l[i]+l[j]: break if l[i]<l[j]+l[k] and l[j]<l[k]+l[i] and l[k]<l[i]+l[j]: a+=1 print(a)
import bisect n,*l=list(map(int,open(0).read().split())) l.sort() a=0 for i in range(n): for j in range(i+1,n): k=l[i]+l[j] a+=max(0,bisect.bisect_left(l,k)-j-1) print(a)
p02888
N = int(eval(input())) L = list(map(int, input().split())) L.sort() from bisect import bisect_left cnt = 0 for i in range(0, N - 2): for j in range(i + 1, N - 1): t = L[i] + L[j] idx = bisect_left(L, t, lo=j + 1, hi=len(L)) cnt += max(0, idx - j - 1) print(cnt)
N = int(eval(input())) L = list(map(int, input().split())) L.sort() from bisect import bisect_left cnt = 0 for i in range(0, N - 2): for j in range(i + 1, N - 1): t = L[i] + L[j] idx = bisect_left(L, t, j + 1, len(L)) cnt += max(0, idx - j - 1) print(cnt)
p02888
N = int(eval(input())) L = list(map(int, input().split())) L.sort() from bisect import bisect_left cnt = 0 for i in range(0, N - 2): for j in range(i + 1, N - 1): t = L[i] + L[j] idx = bisect_left(L, t, j + 1, len(L)) cnt += max(0, idx - j - 1) print(cnt)
N = int(eval(input())) L = list(map(int, input().split())) L.sort() from bisect import bisect_left cnt = 0 for i in range(0,N-2): for j in range(i+1,N-1): t = L[i]+L[j] idx = bisect_left(L,t,lo=j+1) cnt += max(0,idx-j-1) print(cnt)
p02888
import sys readline = sys.stdin.readline N = int(readline()) L = list(map(int, readline().split())) ans = 0 for i in range(N): for j in range(N): if i == j: continue for k in range(N): if i == k or j == k: continue if (L[i] < L[j] +...
import sys import bisect readline = sys.stdin.readline N = int(readline()) L = list(map(int, readline().split())) L.sort() ans = 0 for i in range(N): for j in range(i, N): if i == j: continue ab = L[i] + L[j] to = bisect.bisect_left(L, ab, j, N) ans += to - 1 - j print...
p02888
# -*- coding: utf-8 -*- import sys import math from bisect import bisect_left from bisect import bisect_right import collections import copy import heapq from collections import defaultdict from heapq import heappop, heappush import itertools input = sys.stdin.readline ##### リストの 二分木検索 ##### # bisect_lef...
# -*- coding: utf-8 -*- import sys import math from bisect import bisect_left from bisect import bisect_right import collections import copy import heapq from collections import defaultdict from heapq import heappop, heappush import itertools input = sys.stdin.readline ##### リストの 二分木検索 ##### # bisect_lef...
p02888
from itertools import combinations n = int(eval(input())) l = list( map(int, input().split())) ans = 0 l3s = list(combinations(l,3)) for l3 in l3s: if abs(l3[1] - l3[2]) < l3[0] < (l3[1] + l3[2]): ans += 1 print(ans)
n = int(eval(input())) l = list( map(int, input().split())) ans = 0 lsorted = sorted(l, reverse=True) for longest in range(len(l)-2): for second in range(longest+1, len(l)): for k in range(second+1, len(l)): if lsorted[longest] - lsorted[second] - lsorted[k] >= 0: ...
p02888
from bisect import bisect_left n = int(eval(input())) l = list( map(int, input().split())) ans = 0 l.sort() for i in range(n-2): shortest = l[i] for j in range(i+1, n-1): second = l[j] k = bisect_left(l[j+1:], shortest + second) ans += k print(ans)
from bisect import bisect_left n = int(eval(input())) l = list( map(int, input().split())) ans = 0 l.sort() for i in range(n-2): shortest = l[i] for j in range(i+1, n-1): second = l[j] # このスライスしたやつと地味にTLEになる... # k = bisect_left(l[j], shortest + second) # ans += k...
p02888
import bisect n = int(eval(input())) l = sorted(list(map(int, input().split()))) dic = {} ans = 0 for i in range(n-1): for j in range(i+1, n): tmp = l[i] + l[j] if tmp in list(dic.keys()): index2 = dic[tmp] else: index2 = bisect.bisect_left(l, tmp) dic[tmp] = index2 ...
import bisect as bs n = int(eval(input())) l = sorted(list(map(int,input().split()))) count = 0 for i in range(n): for j in range(i+1,n): index = bs.bisect_left(l,l[i]+l[j]) count += index - j - 1 print(count)
p02888
import itertools,bisect N =int(eval(input())) L = list(map(int,input().split())) L.sort() def j(a,b,c): if a<b+c and c<b+a and b<a+c: return True else: return False cnt = 0 l = list(itertools.combinations(list(range(N)),2)) for i1,i2 in l: a,b = L[i1],L[i2] if a == b: ib = bisect...
import bisect N =int(eval(input())) L = sorted(list(map(int,input().split()))) cnt = 0 for i in range(N): for j in range(i+1,N): cnt += bisect.bisect_left(L,L[i]+L[j]) - (j+1) print(cnt)
p02888
import itertools n = eval(input()) l = list(map(int,input().split())) ans = 0 for i in list(itertools.combinations(l,3)): i = sorted(i) a,b,c = i[0],i[1],i[2] if c<a+b: ans += 1 print(ans)
n = int(eval(input())) l = list(map(int,input().split())) l = sorted(l) ans = 0 for c in range(n): a=0 b=c-1 while a<b: if l[a]+l[b]>l[c]: ans+=b-a b-=1 else: a+=1 print(ans)
p02888
import itertools n=int(eval(input())) l=list(map(int,input().split())) ans=0 for i in itertools.combinations(l,3): a,b,c=list(map(int,i)) if a<b+c and b<c+a and c<a+b: ans+=1 print(ans)
N=int(eval(input())) l=list(map(int,input().split())) l.sort() count=0 import bisect for i in range(N): for j in range(i+1,N-1): m=bisect.bisect_left(l,l[i]+l[j]) count+=m-(j+1) print(count)
p02888
n=int(eval(input())) l=[0]*n l[:]=list(map(int,input().split())) ans=0 m=10**3 l=sorted(l)[::-1] for i in range(n-2): m=min(m,l[i]) for j in range(i+1,n-1): for k in range(j+1,n): if m<l[j]+l[k]: ans+=1 else: break print(ans)
import bisect N=int(eval(input())) L=list(map(int,input().split())) L=sorted(L) ans=0 for i in range(N): for j in range(i+1,N): k=bisect.bisect_left(L,L[i]+L[j]) ans+=k-j-1 print(ans)
p02888
ni = lambda: int(eval(input())) nm = lambda: list(map(int, input().split())) nl = lambda: list(map(int, input().split())) n = ni() l = nl() l = sorted(l)[::-1] ans = 0 for a in range(n): fl = 0 A = l[a] for b in range(a+1,n): B = l[b] if A > B*2: break ...
import bisect ni = lambda: int(eval(input())) nm = lambda: list(map(int, input().split())) nl = lambda: list(map(int, input().split())) n = ni() l = nl() l = sorted(l) ans = 0 for a in range(n): fl = 0 A = l[a] for b in range(a+1,n): B = l[b] idx = bisect.bisect_left...
p02888
import bisect import sys sys.setrecursionlimit(10**9) input = sys.stdin.readline N = int(eval(input())) L = list(map(int, input().split())) L = sorted(L) ans = 0 for i in range(N-2): for j in range(i+1, N-1): min_sum = L[i]+L[j] more_than = bisect.bisect_left(L[j+1:], min_sum) ans += mor...
from bisect import bisect_left N = int(eval(input())) L = sorted(list(map(int, input().split()))) ans = 0 for i in range(N): for j in range(i+1,N): ans += bisect_left(L, L[i]+L[j])-j-1 print(ans)
p02888
import sys from bisect import bisect_left, bisect_right read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines sys.setrecursionlimit(10 ** 9) INF = 1 << 60 MOD = 1000000007 def main(): N, *L = list(map(int, read().split())) L.sort() ans = 0 for i, a in ...
import sys from itertools import accumulate read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines sys.setrecursionlimit(10 ** 9) INF = 1 << 60 MOD = 1000000007 def main(): N, *L = list(map(int, read().split())) L.sort() max_L = max(L) C = [0] * (max_...
p02888
import sys from itertools import accumulate read = sys.stdin.buffer.read def main(): N, *L = list(map(int, read().split())) L.sort() max_L = L[-1] C = [0] * (max_L + 1) for l in L: C[l] += 1 C = list(accumulate(C)) ans = 0 for i, a in enumerate(L): ...
import sys from itertools import accumulate read = sys.stdin.buffer.read def main(): N, *L = list(map(int, read().split())) L.sort() max_L = L[-1] C = [0] * (max_L + 1) for l in L: C[l] += 1 C = list(accumulate(C)) ans = 0 for i in range(N): for j...
p02888
from itertools import combinations def main(): N = int(eval(input())) L = list(map(int, input().split())) L = sorted(L) ans = 0 comb = [p for p in combinations(L, 3)] for a, b, c in comb: if c < a + b: ans += 1 print(ans) if __name__ == "__main__": ...
from bisect import bisect_left def main(): N = int(eval(input())) L = list(map(int, input().split())) L = sorted(L) ans = 0 for i in range(N-2): a = L[i] for j in range(i+1, N-1): k = bisect_left(L[j+1:], a + L[j]) ans += k print(ans) ...
p02888
import bisect N=int(input().strip()) L=list(map(int,input().strip().split())) L.sort() MAX=L[-1] count=0 for i in range(N-2): for j in range(i+1,N-1): # print(i,j) # print(L[i+1:-1]) minInd=j+1 # print(minInd) if L[minInd]> L[i]+L[j]: continue ...
import bisect N = int(eval(input())) L = list(map(int, input().split())) L.sort() c = 0 for i in range(N-2): for j in range(i+1, N-1): c += bisect.bisect_right(L, L[i]+L[j]-1) - j - 1 print(c)
p02888
from bisect import bisect_left,bisect_right n=int(eval(input())) l=list(map(int,input().split())) l.sort() ans=0 for i in range(n-2): for j in range(i+1,n-1): bc=l[i]+l[j] idx=bisect_left(l,bc) ans+=idx-j-1 print(ans)
from bisect import bisect_left,bisect_right n=int(eval(input())) l=list(map(int,input().split())) l.sort() ans=0 a=[] for i in range(n-2): for j in range(i+1,n-1): t=l[i]+l[j] cnt=bisect_left(l,t) ans+=cnt-j-1 print(ans)
p02888
n=int(eval(input())) l=list(map(int,input().split())) l.sort() ans=0 from bisect import bisect_left for i in range(n-2): for j in range(i+1,n-1): ab=l[i]+l[j] idx=bisect_left(l,ab) ans+=max(idx-j-1,0) print(ans)
n=int(eval(input())) l=list(map(int,input().split())) from bisect import bisect_left l.sort() ans=0 for i in range(n-2): for j in range(i+1,n-1): t=l[i]+l[j] idx=bisect_left(l,t) ans+=max(idx-j-1,0) print(ans)
p02888
import bisect n = int(eval(input())) L = list(map(int, input().split())) L.sort() ans = 0 for i in range(n - 2): a = L[i] for j in range(i + 1, n - 1): b = L[j] c_min = b - a c_max = a + b if c_max > c_min + 1: l = L[j+1:] i_min = bisect.bis...
import bisect n = int(eval(input())) L = list(map(int, input().split())) L.sort() ans = 0 for i in range(n): for j in range(i+1, n): k = bisect.bisect_left(L, L[i]+L[j]) ans += k-1 - j print(ans)
p02888
n=int(eval(input())) a=list(map(int,input().split())) a.sort() ans=0 for i in range(n-2): for j in range(i+1,n-1): b=a[i]+a[j] k=j+1 while a[k]<b: k+=1 if k>=n: break ans+=k-j-1 print(ans)
import bisect n=int(eval(input())) a=list(map(int,input().split())) a.sort() ans=0 for i in range(n-2): for j in range(i+1,n-1): ans+=bisect.bisect_left(a,a[i]+a[j])-j-1 print(ans)
p02888
import sys N=int(eval(input())) L=list(map(int,sys.stdin.readline().split())) counter=0 L.sort() for i,A in enumerate(L[:-2]): for j,B in enumerate(L[i+1:-1],i+1): l=j r=len(L)-1 c=(r+l)//2 while abs(r-l)>1: if A>abs(L[c]-B): b=c ...
N=int(eval(input())) L=list(map(int,input().split())) counter=0 L.sort() for i in range(N-2): k=i+2 for j in range(i+1,N-1): while k < N and L[k] < L[i] + L[j]: k += 1 counter += k - j -1 print(counter)
p02888
n = int(eval(input())) nums = list(map(int,input().split(" "))) nums.sort() total = 0 for i in range(n): for j in range(i+1,n): k = j+1 while k < n and nums[k] < nums[i] + nums[j]: k+=1 total += k-j -1 print(total)
n = int(eval(input())) nums = list(map(int,input().split(" "))) nums.sort() total = 0 for i in range(n-1,-1,-1): l = 0 r = i-1 while l < r: if nums[l] + nums[r] > nums[i]: total += r - l r -= 1 else: l += 1 print(total)
p02888
import bisect def main(): N = int(eval(input())) L = list(map(int, input().split())) L = sorted(L) ans = 0 for i in range(N): for j in range(i+1, N): X = L[i] + L[j] l = bisect.bisect_left(L, X, lo=j+1) ans += len(L[j+1:l]) #pr...
import bisect def main(): N = int(eval(input())) L = list(map(int, input().split())) L = sorted(L) ans = 0 for i in range(N): for j in range(i+1, N): X = L[i] + L[j] l = bisect.bisect_left(L, X, lo=j+1) ans += (l-(j+1)) #print(...
p02888
n = int(eval(input())) l = sorted([int(x) for x in input().split()]) ret = 0 for i in range(len(l)): a = l[i] for j in range(i + 1, len(l)): b = l[j] for k in range(j+1, len(l)): c = l[k] if c >= a+b: break if a<b+c and b<a+c and c<a+b: ret += 1 print(ret)
n = int(eval(input())) a = sorted([int(x) for x in input().split()]) ret = 0 for i in range(n-1, 0, -1): l = 0 r = i-1 while l<r: if a[l]+a[r]>a[i]: ret += r - l r -= 1 else: l += 1 print(ret)
p02888
# D - Triangles n = (int)(eval(input())) length = list(map(int, input().split())) sum = 0 for i in range(n): for j in range(n): for k in range(n): a = length[i] b = length[j] c = length[k] if i >= j or i >= k or j >= k: con...
# D - Triangles import bisect n = (int)(eval(input())) length = list(map(int, input().split())) sum = 0 length.sort() for i in range(n): for j in range(n): a = length[i] b = length[j] if i < j: k = bisect.bisect_left(length, a + b) ...
p02888
import bisect N = int(eval(input())) lst =list(map(int, input().split())) lst.sort() ret = 0 for i in range(N): for j in range(i + 1, N): n = lst[i] + lst[j] idx = bisect.bisect_left(lst, n) ret += max(0, idx - j - 1) print(ret)
import bisect N = int(eval(input())) lst =list(map(int, input().split())) lst.sort() ret = 0 for i in range(N): for j in range(i + 1, N): n = lst[i] + lst[j] idx = bisect.bisect_left(lst, n, j + 1, N) ret += max(0, idx - j - 1) print(ret)
p02888
import itertools N = int(eval(input())) L = list(map(int,input().split())) # N = 2000 # L = list(range(N)) # # 重複を消す # L = list(set(L)) # N = len(L) # Lを小さい順にソート L = sorted(L) # j=0〜max(L)について,L[i]<jを満たす最大のi: X[j] = i X = [] for j in range(L[-1]+1): flag = False for i in range(N-1,-1,-1)...
import itertools import bisect N = int(eval(input())) L = list(map(int,input().split())) # Lを小さい順にソート L = sorted(L) # a <= b <= cを仮定してもおk # a,bを全探索し,条件を満たすcの数をO(N)未満の方法で求めればよい ans = 0 for i in range(N-2): a = L[i] for j in range(i+1,N-1): b = L[j] # Lの中で条件b-a<c...
p02888
N = int(eval(input())) L = list(map(int,input().split())) L.sort(reverse=True) ans = 0 for i in range (0,N-2): max = sum(x >= L[i] / 2 for x in L) for j in range (i+1,max): n = sum(x > L[i]-L[j] for x in L[j+1:N]) ans = ans + n print(ans)
N = int(eval(input())) L = list(map(int,input().split())) L.sort() ans = 0 for i in range (N-1,1,-1): s = 0 l = i - 1 while l > s: if L[s] + L[l] > L[i]: ans = ans + (l - s) l = l - 1 else: s = s + 1 print(ans)
p02888
n = int(eval(input())) l = list(map(int,input().split())) a = 0 l.sort() for i in range(n): for j in range(i+1,n): for k in range(j+1,n): if l[i] + l[j] > l[k]: a += 1 print(a)
import bisect n = int(eval(input())) l = list(map(int,input().split())) l.sort() ans = 0 for b in range(n): for a in range(b): ans += bisect.bisect_left(l,l[a]+l[b])-(b+1) print(ans)
p02888
from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product # import math # import numpy as np # from operator import xor import sys sys.setrecursionlimit(10 ** 5 + 10) # input = sys.stdin.readline def resolve(): N=int(eval(input())) L=list(map(int,in...
import bisect # from collections import Counter, deque # from copy import copy, deepcopy # from fractions import gcd # from functools import reduce # from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product # import math # import numpy as np # from operator i...
p02888
import itertools N = int(eval(input())) L = list(map(int,input().split())) edge = list(itertools.combinations(L,3)) con = 0 for i in range(0,len(edge)): edge[i] = list(edge[i]) edge[i].sort(reverse = True) a = edge[i][0] b = edge[i][1] c = edge[i][2] if a < b + c: con += 1 print(con)
from bisect import bisect_left N = int(eval(input())) L = list(map(int,input().split())) L.sort() con = 0 for i in range(0,N): for j in range(i + 1,N): a = L[i] b = L[j] #a + bよりでかい最小の数のインデックス p = bisect_left(L,a + b) #cとbの間の区間を取る con += (p - 1) - j print(con)
p02888
#!/usr/bin/env python3 N = int(eval(input())) L = sorted([int(s) for s in input().split()]) MAX_SEARCH = max(L) + 1 def check_search(middle, idx_short2, l_lng, l_lows): return l_lows[middle] + l_lows[idx_short2] > l_lng l_lows = L[:2] triangles = 0 for l_lng in L[2:]: for idx, l_low in en...
#!/usr/bin/env python3 N = int(eval(input())) L = sorted([int(s) for s in input().split()]) def check_search(middle, idx_short2, l_lng, l_lows): return l_lows[middle] + l_lows[idx_short2] > l_lng l_lows = L[:2] triangles = 0 for l_lng in L[2:]: for idx, l_low in enumerate(l_lows): l...
p02888
import itertools N = int(eval(input())) l = list(map(int, input().split())) t = list(itertools.combinations(l, 3)) count = 0 for i in t: i_s = sorted(i) a,b,c = i_s if b <a+c and c<a+b: count+=1 print(count)
import bisect n = int(eval(input())) l = list(map(int,input().split())) l.sort() ans =0 for i in range(1,n): for j in range(i): p = l[i]+ l[j] q = bisect.bisect_left(l,p) ans += q-i-1 print(ans)
p02888
n,m=list(map(int,input().split())) class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.paren...
class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): ...
p03045
N, M = list(map(int, input().split())) class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return se...
import sys def input(): return sys.stdin.readline().strip() def mapint(): return list(map(int, input().split())) sys.setrecursionlimit(10**9) N, M = mapint() class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x]...
p03045
class UnionFind(): def __init__(self, n): self.n = n self.root = [-1]*n self.rank = [0]*n def find(self, x): if self.root[x-1] < 0: return x else: self.root[x-1] = self.find(self.root[x-1]) return self.root[x-1] def u...
class UnionFind(): def __init__(self, n): self.n = n self.root = [-1]*n self.rank = [0]*n def find(self, x): if self.root[x-1] < 0: return x else: self.root[x-1] = self.find(self.root[x-1]) return self.root[x-1] def u...
p03045
class UnionFind(): def __init__(self, n): self.n = n self.parents = [i for i in range(n)] self.rank = [0 for _ in range(n)] self.size = [1 for _ in range(n)] def find(self, x): root = x while self.parents[root] != root: root = self.parents[r...
class UnionFind(): def __init__(self, n): self.n = n self.parents = [i for i in range(n)] self.rank = [0 for _ in range(n)] self.size = [1 for _ in range(n)] def find(self, x): root = x while self.parents[root] != root: root = self.parents[r...
p03045
import sys sys.setrecursionlimit(10 ** 7) def dfs(s): if visited[s]: return visited[s] = 1 for to in t[s]: if visited[to]: continue dfs(to) N, M = list(map(int,input().split())) t = [[] for _ in range(N)] visited = [0] * N for _ in range(M): X, Y...
N, M = list(map(int,input().split())) t = [[] for _ in range(N)] visited = [0] * N for _ in range(M): X, Y, Z = list(map(int,input().split())) X -= 1 Y -= 1 t[X].append(Y) t[Y].append(X) ans = 0 for i in range(N): if visited[i]: continue ans += 1 stack = [i] wh...
p03045
import sys input = sys.stdin.readline class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return ...
import sys input = sys.stdin.readline class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return ...
p03045
import sys sys.setrecursionlimit(100000) def visit_tree(now, num): visited[now] = num for i in range(len(tree[now])): if not visited[tree[now][i]]: visit_tree(tree[now][i], num) n, m = list(map(int, input().split())) tree = [[] for _ in range(n)] for i in range(m): x, y, z ...
import sys sys.setrecursionlimit(10000000) def visit_tree(now, num): visited[now] = num for i in range(len(tree[now])): if not visited[tree[now][i]]: visit_tree(tree[now][i], num) n, m = list(map(int, input().split())) tree = [[] for _ in range(n)] for i in range(m): x, y, ...
p03045
N, M = list(map(int, input().split())) nodes = [i for i in range(N)] def root(x): while nodes[x] != x: x = nodes[x] return x def same(x, y): return root(x) == root(y) def unite(x, y): if not same(x, y): nodes[root(y)] = root(x) for _ in range(M): X, Y, Z =...
N, M = list(map(int, input().split())) nodes = [i for i in range(N)] def root(x): if nodes[x] == x: return x else: nodes[x] = root(nodes[x]) return nodes[x] def same(x, y): return root(x) == root(y) def unite(x, y): if not same(x, y): nodes[root(x)] = ...
p03045
N,M=list(map(int,input().split())) class UnionFind(): def __init__(self,n): self.parent={} for i in range(n): self.parent[i]=[i] def find_parent(self,x): import sys parent=self.parent for key in list(parent.keys()): if x in parent[key]: return key ...
class UnionFind: def __init__(self, n): self.n = n self.p = [e for e in range(n)] self.rank = [0] * n self.size = [1] * n def same(self, u, v): return self.find_set(u) == self.find_set(v) def unite(self, u, v): u = self.find_set(u) v = se...
p03045
import queue import sys sys.setrecursionlimit(10**6) N, M = list(map(int, input().split())) XYZ = [] for _ in range(M): XYZ.append(tuple(map(int, input().split()))) G=[[] for _ in range(N+1)] for el in XYZ: G[el[0]].append(el[1]) G[el[1]].append(el[0]) #print(G) seen=[False]*(N+1) todo=qu...
import queue import sys sys.setrecursionlimit(10**6) N, M = list(map(int, input().split())) XYZ = [] for _ in range(M): XYZ.append(tuple(map(int, input().split()))) G=[[] for _ in range(N+1)] for el in XYZ: G[el[0]].append(el[1]) G[el[1]].append(el[0]) #print(G) seen=[False]*(N+1) todo=qu...
p03045
N,M=list(map(int,input().split())) root=[-1]*N def search(i): if root[i]>=0: root[i]=search(root[i]) return root[i] return i for i in range(M): x,y,z=list(map(int,input().split())) x-=1 y-=1 rx=search(x) ry=search(y) if rx==ry: continue elif roo...
N,M=list(map(int,input().split())) root=[-1]*N def find(i): global root if root[i]>=0: root[i]=find(root[i]) return root[i] return i for i in range(M): X,Y,Z=list(map(int,input().split())) rx=find(X-1) ry=find(Y-1) if rx==ry: continue elif root[rx]<=...
p03045
n, m = list(map(int, input().split())) xy = [i for i in range(n + 1)] def root(x): i = x a = [] while i != xy[i]: i = xy[i] a += [i] for b in a: xy[b] = i return i for _ in range(m): x, y = list(map(int, input().split()[:2])) xy[root(x)] = root(...
n, m = list(map(int, input().split())) xy = [i for i in range(n + 1)] def root(x): i = x while i != xy[i]: i = xy[i] xy[x] = i return i for _ in range(m): x, y = list(map(int, input().split()[:2])) xy[root(x)] = root(y) print((sum(i == v for i, v in enumerate(xy)) ...
p03045
#!/usr/bin/env python3 #abc126 e import sys sys.setrecursionlimit(100000) N,M = list(map(int,input().split())) XYZ = [list(map(int,input().split())) for _ in range(M)] G = [[] for _ in range(N)] for i in XYZ: G[i[0]-1].append(i[1]) G[i[1]-1].append(i[0]) check = [False for _ in range(N)]#各頂点を訪...
#!/usr/bin/env python3 #abc126 e import sys sys.setrecursionlimit(1000000) N,M = list(map(int,input().split())) XYZ = [list(map(int,input().split())) for _ in range(M)] G = [[] for _ in range(N)] for i in XYZ: G[i[0]-1].append(i[1]) G[i[1]-1].append(i[0]) check = [False for _ in range(N)]#各頂点を...
p03045
import sys;sys.setrecursionlimit(10**9) class u():#UnionFind f-根探し u-連結 s-連結? c-要素数 def __init__(self,n):self.n,self.r=[-1]*n,[0]*n def f(self,x): if self.n[x]<0:return x else:self.n[x]=self.f(self.n[x]);return self.n[x] def u(self,x,y): x,y=self.f(x),self.f(y) if x==y:return elif sel...
import sys;sys.setrecursionlimit(10**9) class UnionFind: def __init__(self,n): self.n=[-1]*n self.r=[0]*n self.siz=n def find_root(self,x): if self.n[x]<0: return x else: self.n[x]=self.find_root(self.n[x]) return self.n[x] def unite(self,x,y): x=self.find_ro...
p03045
# -*- coding: utf-8 -*- import sys sys.setrecursionlimit(10 ** 6) def dfs(n): # print('Search Start: ' + str(n)) if checked[n]: # print('Checked: ' + str(n)) return checked[n] = True for e in edge: if n in e: x, y = e if n != x: ...
# -*- coding: utf-8 -*- import sys sys.setrecursionlimit(10 ** 6) class UnionFind(object): def __init__(self, n): self.parent = [i for i in range(n)] self.rank = [0 for _ in range(n)] self.size = [1 for _ in range(n)] self.components = n def find(self, x): ...
p03045
from collections import deque n, m = list(map(int, input().split())) xyz = {i:[] for i in range(1, n+1)} for _ in range(m): x, y, z = list(map(int, input().split())) xyz[x].append((x, y, z)) xyz[y].append((y, x, z)) connects = [0]*(n+1) group = 1 for i in range(1, n+1): if connects[i] > 0:...
from collections import deque n, m = list(map(int, input().split())) xyz = [] for _ in range(m): x, y, z = list(map(int, input().split())) xyz.append((x, y, z)) uf = [-1]*(n+1) def find(i): if uf[i] < 0: return i else: uf[i] = find(uf[i]) return uf[i] for x, ...
p03045
from collections import deque n, m = list(map(int, input().split())) xyz = [[] for i in range(n+1)] for _ in range(m): x, y, z = list(map(int, input().split())) xyz[x].append(y) xyz[y].append(x) ans = 0 visited = [False]*(n+1) for x in range(1, n+1): if visited[x]: continue ...
import sys sys.setrecursionlimit(10**9) n, m = list(map(int, input().split())) xyz = [[] for i in range(n+1)] for _ in range(m): x, y, z = list(map(int, input().split())) xyz[x].append(y) xyz[y].append(x) ans = 0 visited = [False]*(n+1) def dfs(x): visited[x] = True for y in xyz[...
p03045
class UnionFind(object): def __init__(self, N): self.parent = [-1] * N #parent[i] は(i+1)番目の要素が含まれる要素数の(-1)倍 #要素が負-->その数を親とするグループに属する要素の数×(-1) #要素が正-->親のindex #Aがどのグループに属しているかを調べる def root(self, A): if self.parent[A-1] < 0: #負の数-->その数は親 return A self.par...
import sys input = sys.stdin.readline sys.setrecursionlimit(10 ** 9) MOD = 10 ** 9 + 7 N, M = list(map(int, input().split())) class UnionFind(object): def __init__(self, N): self.parent = [-1] * N #parent[i] は(i+1)番目の要素が含まれる要素数の(-1)倍 #要素が負-->その数を親とするグループに属する要素の数×(-1) #要素が正-->親のindex ...
p03045
N, M = list(map(int, input().split())) G = [set([n]) for n in range(N)] for m in range(M): X, Y, Z = list(map(int, input().split())) X -= 1 Y -= 1 Gx = G[X] Gy = G[Y] Gxy = Gx | Gy for idx in Gxy: G[idx] = Gxy print((len(set([tuple(g) for g in G]))))
class UnionFind: def __init__(self, n): self.depth = [0] * n self.parent = list(range(n)) def find(self, x): if x == self.parent[x]: return x self.parent[x] = self.find(self.parent[x]) return self.parent[x] def unite(self, x, y): x, ...
p03045
import sys input = sys.stdin.readline n,m = list(map(int,input().split())) #Union Find par = [] #親 rank = [] #木の深さ #初期化 for i in range(n): #par[i]:i rank[i]:0 par.append(i) rank.append(0) #木の根を求める def find(x,par): if par[x] == x: return x else: return find...
#Union Find import sys input = sys.stdin.readline n,m = list(map(int,input().split())) par = [i for i in range(n)] #親 rank = [0]*n #木の深さ #木の根を求める def find(x,par): if par[x] == x: return x else: par[x] = par[par[x]] return find(par[x],par) #xとyの属する集合の併合 def unite(x,...
p03045
#Union Find import sys input = sys.stdin.readline n,m = list(map(int,input().split())) #根なら-size,子なら親の頂点 par = [-1]*n #xの根を求める def find(x): if par[x] < 0: return x else: par[x] = find(par[x]) return par[x] #xとyの属する集合の併合 def unite(x,y): x = find(x) y = find(...
def main(): import sys input = sys.stdin.readline #Union Find #xの根を求める def find(x): if par[x] < 0: return x else: par[x] = find(par[x]) return par[x] #xとyの属する集合の併合 def unite(x,y): x = find(x) y = find(y) ...
p03045
# Union-Findデータ構造 class UnionFind: def __init__(self, numV): self.pars = list(range(numV)) self.ranks = [0] * numV def find(self, x): if self.pars[x] == x: return x else: self.pars[x] = self.find(self.pars[x]) return self.pars[x] def union(se...
class UnionFind: def __init__(self, numV): self.pars = list(range(numV)) self.ranks = [0] * numV def getRoot(self, x): par = self.pars[x] if par != x: self.pars[x] = par = self.getRoot(par) return par def merge(self, x, y): x, y = self.ge...
p03045
from queue import Queue N, M = list(map(int, input().split())) d = {} for i in range(1, N+1): d[i] = [] for i in range(M): x, y, z = list(map(int, input().split())) d[x].append(y) d[y].append(x) isChecked = [False for i in range(N)] ans = 0 for k in d: if isChecked[k-1]: con...
class UnionFindTree: _node_num = 0 _parent_list = [] _tree_len = [] _num_trees = 0 def __init__(self, node_num): self._node_num = node_num self._parent_list = [-1 for i in range(node_num+1)] self._tree_len = [0 for i in range(node_num+1)] self._num_trees = ...
p03045
# -*- coding: utf-8 -*- import bisect import heapq import math import random import sys from collections import Counter, defaultdict, deque from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal from functools import lru_cache, reduce from itertools import combinations, combinations_with_replacement, produc...
# -*- coding: utf-8 -*- import bisect import heapq import math import random import sys from collections import Counter, defaultdict, deque from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal from functools import lru_cache, reduce from itertools import combinations, combinations_with_replacement, produc...
p03045
# Union Find 木 from sys import setrecursionlimit def find(parent, i): t = parent[i] if t < 0: return i t = find(parent, t) parent[i] = t return t def unite(parent, i, j): i = find(parent, i) j = find(parent, j) if i == j: return parent[j] += pare...
# Union Find 木 from sys import setrecursionlimit def find(parent, i): t = parent[i] if t < 0: return i t = find(parent, t) parent[i] = t return t def unite(parent, i, j): i = find(parent, i) j = find(parent, j) if i == j: return parent[j] += pare...
p03045
n,m=list(map(int,input().split())) #Union-Find par=[-1 for i in range(n)] def root(a): if par[a]<0: return a else: return root(par[a]) def size(a): return -par[root(a)] def connect(a,b): a=root(a) b=root(b) if a==b: return False if size(a)<size(...
n,m=list(map(int,input().split())) #Union-Find par=[-1 for i in range(n)] def root(a): if par[a]<0: return a else: return root(par[a]) def size(a): return -par[root(a)] def connect(a,b): a=root(a) b=root(b) if a==b: return False if size(a)<size(...
p03045
n,m=list(map(int,input().split())) class UnionFind(): def __init__(self,n): self.n=n self.root=[-1]*(n+1) self.rnk=[0]*(n+1) def Find_Root(self,x): if self.root[x]<0: return x else: self.root[x]=self.Find_Root(self.root[x]) ...
n,m=list(map(int,input().split())) import sys input=sys.stdin.readline class UnionFind(): def __init__(self,n): self.n=n self.root=[-1]*(n+1) self.rnk=[0]*(n+1) def Find_Root(self,x): if self.root[x]<0: return x else: self.root[x]=s...
p03045
# https://atcoder.jp/contests/abc126/tasks/abc126_e import itertools from collections import Counter from collections import defaultdict import bisect # Union find class UnionFind(): def __init__(self,size): self.table = [-1 for _ in range(size)] # 負の値の場合根を表す。正の値は次の要素を返す、根まで続く #集合の代表を求める...
# https://atcoder.jp/contests/abc126/tasks/abc126_e import itertools from collections import Counter from collections import defaultdict import bisect # Union find class UnionFind(): def __init__(self,size): self.table = [-1 for _ in range(size)] # 負の値の場合根を表す。正の値は次の要素を返す、根まで続く #集合の代表を求める...
p03045
import sys input = sys.stdin.readline class UnionFind: def __init__(self, size): self.parent = [-1] * size self.rank = [1] * size self.groups = size def get_root(self, node): parent = self.parent[node] if parent == -1: root = node else...
import sys input = sys.stdin.readline class UnionFind: def __init__(self, size): self.parent = [-1] * size self.rank = [1] * size self.groups = size def get_root(self, node): parent = self.parent[node] if parent == -1: root = node else...
p03045
import sys input = sys.stdin.readline class UnionFind: def __init__(self, size): self.parent = [-1] * size self.rank = [1] * size self.groups = size def get_root(self, node): parent = self.parent[node] if parent == -1: root = node else...
class UnionFind: def __init__(self, size): self.parent = [-1] * size self.rank = [1] * size self.groups = size def get_root(self, node): parent = self.parent[node] if parent == -1: root = node else: root = self.get_root(parent) ...
p03045
#!/usr/bin/env python3 import sys class UnionFind: def __init__(self, n): # par = Parent Number or NoV self.par = [-1 for i in range(n+1)] # rank = Tree Height self.rank = [0] * (n+1) # 自分が所属する集合の数を返す def size(self, x): return -self.find(self.par[x])...
#!/usr/bin/env python3 import sys sys.setrecursionlimit(10000000) INF = 1<<32 class UnionFind: def __init__(self, n): # par = Parent Number or NoV self.par = [-1] * (n+1) # rank = Tree Height self.rank = [0] * (n+1) # 自分が所属する集合の数を返す def size(self, x): ...
p03045
class UnionFind : def __init__(self, size) : self.parent = list(range(size)) self.height = [0] * size self.size = [1] * size self.component = size def root(self, index) : if self.parent[index] == index : # 根の場合 return index rootIndex = self...
class UnionFind: def __init__(self, size): self.parent = list(range(size)) self.height = [0] * size self.size = [1] * size self.componentCount = size def root(self, index): if self.parent[index] == index: # 根の場合 return index rootIndex = sel...
p03045
import sys sys.setrecursionlimit(10**9) N, M = list(map(int, input().split())) paths = [[] for _ in range(N)] for _ in range(M): a, b, z = list(map(int, input().split())) paths[a-1].append(b-1) paths[b-1].append(a-1) visited = [False] * N def dfs(node, prev): if visited[node]: ...
import sys sys.setrecursionlimit(10**9) N, M = list(map(int, input().split())) class Union_Find: def __init__(self,n): self.parent = [i for i in range(n)] self.rank = [1 for i in range(n)] def find(self,x): if self.parent[x] == x: return x else: ...
p03045
n, m = list(map(int, input().split())) tree = [-i for i in range(n+1)] rank = [1 for _i in range(n+1)] def search_root(num, tree=tree): if tree[num] < 0: return num else: tree[num] = search_root(tree[num]) return tree[num] for _i in range(m): x, y, z = list(map(int, input...
n, m = list(map(int, input().split())) tree = [-i for i in range(n+1)] def search_root(num, tree=tree): if tree[num] < 0: return num else: tree[num] = search_root(tree[num]) return tree[num] for _i in range(m): x, y, z = list(map(int, input().split())) x, y = [searc...
p03045
n, m = list(map(int, input().split())) tree = [-i for i in range(n+1)] for _i in range(m): x, y, z = list(map(int, input().split())) while tree[x]>0: x = tree[x] while tree[y]>0: y = tree[y] if x==y: continue else: tree[x] = y print((sum(1 for i in tree ...
n, m = list(map(int, input().split())) tree = [-i for i in range(n+1)] for _i in range(m): x, y, z = list(map(int, input().split())) while tree[x]>0: x = tree[x] while tree[y]>0: y = tree[y] x, y = sorted([x, y]) if x==y: continue else: tree[y] = x ...
p03045
class UnionFind: def __init__(self, n): self.par = [i for i in range(n)] self.rank = [0]*n def find(self, x): if self.par[x] == x: return x else: self.par[x] = self.find(self.par[x]) return self.par[x] def unite(self, x, y): ...
class UnionFind: def __init__(self, n): self.par = [i for i in range(n)] self.rank = [0]*n def find(self, x): if self.par[x] == x: return x else: self.par[x] = self.find(self.par[x]) return self.par[x] def unite(self, x, y): ...
p03045
class Node: def __init__(self, val): self.num = val self.val = val self.is_parent = True self.parent = None self.childs = [] def show(self): print(self.num, end=" ") print(self.is_parent, end=" ") for child in self.childs: ...
class Node: def __init__(self, val): self.num = val self.val = val self.is_parent = True self.parent = None self.childs = [] def show(self): print(self.num, end=" ") print(self.is_parent, end=" ") for child in self.childs: ...
p03045
class Node: def __init__(self, val): self.num = val self.val = val self.is_parent = True self.parent = None self.childs = [] def show(self): print(self.num, end=" ") print(self.is_parent, end=" ") for child in self.childs: ...
class Node: def __init__(self, val): self.num = val self.val = val self.is_parent = True self.parent = None def root(node): depth = 0 while node.parent != None: node = node.parent depth += 1 return node, depth N, M = list(map(int, input().s...
p03045
class Node: def __init__(self, val): self.num = val self.val = val self.is_parent = True self.parent = None def root(node): depth = 0 while node.parent != None: node = node.parent depth += 1 return node, depth N, M = list(map(int, input().s...
class Node: def __init__(self): self.is_parent = True self.parent = None def root(node): depth = 0 while node.parent != None: node = node.parent depth += 1 return node, depth N, M = list(map(int, input().split())) roots = [Node() for i in range(N)] count...
p03045
n,m=list(map(int,input().split())) a=[] for i in range(m): x,y,z=list(map(int,input().split())) a.append((x-1,y-1,z)) class union: def __init__(self,num): self.par=[-1]*num def get_par(self,node): if self.par[node]==-1: return node else: x=se...
n,m=list(map(int,input().split())) a=[] for i in range(m): x,y,z=list(map(int,input().split())) a.append((x-1,y-1)) class union: def __init__(self,num): self.par=[-1]*num def get_par(self,node): if self.par[node]==-1: return node else: x=self...
p03045
class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): ...
class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): ...
p03045
from collections import defaultdict, deque, Counter from heapq import heappush, heappop, heapify import math from bisect import bisect_left, bisect_right import random from itertools import permutations, accumulate, combinations import sys import string INF = float('inf') def LI(): return list(map(int, s...
from collections import defaultdict, deque, Counter from heapq import heappush, heappop, heapify import math from bisect import bisect_left, bisect_right import random from itertools import permutations, accumulate, combinations import sys import string INF = float('inf') def LI(): return list(map(int, s...
p03045
class UnionFind(): def __init__(self, n): self.n = n self.root = [-1] * (n+1) def Find_Root(self, x): if self.root[x] < 0: return x else: self.root[x] = self.Find_Root(self.root[x]) return self.root[x] def Unite(self, x, y):...
#最大公約数 def gcd(x, y): if y == 0: return x else: return gcd(y, x % y) #最小公倍数 def lcm(x, y): return (x * y) // gcd(x, y) #エストラテネスの篩 def isPrime(): MAX = 10**5 is_prime = [1] * (MAX+1) is_prime[0] = 0 is_prime[1] = 0 for i in range(2, MAX+1): if i...
p03045
N, M = list(map(int, input().split())) relation = [[] for _ in range(N+1)] for _ in range(M) : X, Y, Z = list(map(int, input().split())) relation[X].append(Y) relation[Y].append(X) visited = [False] * (N + 1) def dfs(init) : queue = [init] while queue : x_ = queue.pop(0) visited[x_] = True...
N, M = list(map(int,input().split())) parent = [-1] * (N + 1) def root(x): while parent[x] >= 0: x = parent[x] return x def unite(x,y): root_x = root(x) root_y = root(y) if root_x != root_y : if parent[root_x] > parent[root_y] : parent[root_x]=root_y else : if parent[root_x] == par...
p03045
N, M = list(map(int, input().split())) ans = [-1] * N def find(x): global ans if ans[x] == -1: return x else: return find(ans[x]) for _ in range(M): x, y, z = list(map(int, input().split())) x -= 1 y -= 1 xx = find(x) yy = find(y) if ans[x] == an...
N, M = list(map(int, input().split())) ans = [-1] * N def find(x): global ans if ans[x] == -1: return x else: ans[x] = find(ans[x]) return ans[x] for _ in range(M): x, y, z = list(map(int, input().split())) x -= 1 y -= 1 xx = find(x) yy = fin...
p03045
N, M = list(map(int, input().split())) ans = [-1] * N def find(x): global ans if ans[x] == -1: return x else: ans[x] = find(ans[x]) return ans[x] for _ in range(M): x, y, z = list(map(int, input().split())) x -= 1 y -= 1 xx = find(x) yy = fin...
N, M = list(map(int, input().split())) UF = [-1] * (N+1) def find(x): global UF if UF[x] == -1: return x else: UF[x] = find(UF[x]) return UF[x] def union(x,y): global UF xx = find(x) yy = find(y) if UF[x] == UF[y] == -1: UF[y] = xx retur...
p03045
from collections import deque N, M = list(map(int, input().split())) X = [ list(map(int, input().split())) for _ in range(M)] Y = [ [] for _ in range(N)] for x in X: Y[x[0]-1].append((x[0], x[1], x[2])) Y[x[1]-1].append((x[1], x[0], x[2])) # print(Y) visited = [False] * N count = 0 que = deque()...
class UnionFind: def __init__(self, n): self.par = [i for i in range(n)] self.rank = [0 for _ in range(n)] def find_root(self, x): if self.par[x] == x: return x self.par[x] = self.find_root(self.par[x]) return self.par[x] def union...
p03045
def main(): n,m = list(map(int,input().split())) graph = {} for i in range(n): graph[i+1] = [] for i in range(m): x,y,z = list(map(int,input().split())) graph[x] += [y] graph[y] += [x] dis = [-1 for i in range (n)] ans = 0 for i in range(n): ...
def main(): n,m = list(map(int,input().split())) cards = [-1 for i in range(n)] mg = {} for i in range(n): mg[i+1] = [] for i in range(m): x,y,z = list(map(int,input().split())) mg[x].append(y) mg[y].append(x) ans = 0 for i in range(n): if ...
p03045
from collections import deque N,M = list(map(int,input().split())) E = [[] for _ in range(N+1)] for i in range(M): x,y,z = list(map(int,input().split())) E[x].append(y) E[y].append(x) d = [-1]*(N+1) def dfs(start,num): q = deque([start]) while(q): v = q.pop() if(d[v] ...
from collections import deque N,M = list(map(int,input().split())) E = [[] for _ in range(N+1)] for i in range(M): x,y,z = list(map(int,input().split())) E[x].append(y) E[y].append(x) d = [-1]*(N+1) def dfs(start,num): q = deque([start]) d[start] = num while(q): v = q.pop...
p03045
class UnionFind: def __init__(self, n): self.v = [-1 for _ in range(n)] # 根(負): 連結頂点数 * (-1) / 子(正): 根の頂点番号(0-indexed) def find(self, x): # xを含む木における根の頂点番号を返す if self.v[x] < 0: # (負)は根 return x else: # 根の頂点番号 self.v[x] = self.find(self.v[x]) # uniteで...
class UnionFind: def __init__(self, n): self.v = [-1 for _ in range(n)] def find(self, x): if self.v[x] < 0: return x else: self.v[x] = self.find(self.v[x]) return self.v[x] def unite(self, x, y): x = self.find(x) y =...
p03045
class UnionFind: def __init__(self, n): self.v = [-1 for _ in range(n)] def find(self, x): if self.v[x] < 0: return x else: self.v[x] = self.find(self.v[x]) return self.v[x] def unite(self, x, y): x = self.find(x) y =...
class UnionFind: def __init__(self, n): self.v = [-1 for _ in range(n)] def find(self, x): if self.v[x] < 0: return x else: self.v[x] = self.find(self.v[x]) return self.v[x] def unite(self, x, y): x = self.find(x) y =...
p03045
class UnionFind(): def __init__(self, n): self.parent = [-1 for _ in range(n)] # 正==子: 根の頂点番号 / 負==根: 連結頂点数 def find(self, x): if self.parent[x] < 0: return x else: self.parent[x] = self.find(self.parent[x]) return self.parent[x] ...
import sys input = sys.stdin.readline class UnionFind: def __init__(self, n): self.v = [-1 for _ in range(n)] # 根(負): 連結頂点数 * (-1) / 子(正): 根の頂点番号(0-indexed) def find(self, x): # xを含む木における根の頂点番号を返す if self.v[x] < 0: # (負)は根 return x else: # 根の頂点番号 ...
p03045
import sys sys.setrecursionlimit(2*10**5) N, M = list(map(int, input().split())) X, Y, Z = ( list(zip(*(list(map(int, input().split())) for _ in range(M)))) if M else ((), ()) ) G = [set() for _ in range(N + 1)] for x, y in zip(X, Y): G[x].add(y) G[y].add(x) dp = [-1 for _ in range(...
# 入力 N, M = list(map(int, input().split())) X, Y, Z = ( list(zip(*(list(map(int, input().split())) for _ in range(M)))) if M else ((), (), ()) ) # UnionFind木で連結成分の個数を数える class UnionFindTree: def __init__(self, n): self.p = [i for i in range(n + 1)] self.r = [0 for _ in range(n ...
p03045
n,m=list(map(int,input().split())) g = [[] for _ in range(n)] for i in range(m): x,y,z=list(map(int,input().split())) g[x-1].append(y-1) g[y-1].append(x-1) check = [False]*n cnt=0 import queue for i in range(n): if check[i]: continue cnt+=1 check[i]=True q=queue.Queue() q.put(i) while...
n,m=list(map(int,input().split())) g = [[] for _ in range(n)] for i in range(m): x,y,z=list(map(int,input().split())) g[x-1].append(y-1) g[y-1].append(x-1) check = [False]*n cnt=0 import queue q=queue.Queue() for i in range(n): if check[i]: continue cnt+=1 check[i]=True q.put(i) while ...
p03045
#!usr/bin/env python3 from collections import defaultdict from collections import deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return list(map(int, sys.stdin.readline().split())) def I(): return int(sys.stdin.readline()) def LS():return list(map(list, ...
#!usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop from itertools import permutations import sys import math import bisect def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in...
p03045
n,m=list(map(int, input().split())) tree=[[] for _ in range(n)] for i in range(m): x,y,w = list(map(int,input().split())) tree[x-1].append(y-1) tree[y-1].append(x-1) singles = [ i for i in range(n) if tree[i]==[]] visited=[False]*n for i in singles: visited[i] = True ans=len(singles) ...
def main(): import sys input = sys.stdin.readline n,m=list(map(int, input().split())) tree=[[] for _ in range(n)] for i in range(m): x,y,w = list(map(int,input().split())) tree[x-1].append(y-1) tree[y-1].append(x-1) singles = [ i for i in range(n) if ...
p03045
n,m=list(map(int,input().split())) #初期値が0の辞書 from collections import defaultdict xyz = defaultdict(lambda: set([])) for _ in range(m): x,y,z=list(map(int,input().split())) xyz[x].add(y) xyz[y].add(x) no_checked=[0]*n cnt=0 for i in range(1,n+1): if no_checked[i-1]==0: cnt+=1 ...
import sys sys.setrecursionlimit(1000000) n,m=list(map(int,input().split())) #初期値が0の辞書 from collections import defaultdict xyz = defaultdict(lambda: set([])) for _ in range(m): x,y,z=list(map(int,input().split())) xyz[x].add(y) xyz[y].add(x) no_checked=[0]*n def DFS(x): global no_che...
p03045
class UnionFind: def __init__(self, size): self.data = [-1 for _ in range(size)] def find(self, x): if self.data[x] < 0: return x else: self.data[x] = self.find(self.data[x]) return self.data[x] def union(self, x, y): x, y = self....
class UnionFind: def __init__(self, size): self.data = [-1 for _ in range(size)] def find(self, x): if self.data[x] < 0: return x else: self.data[x] = self.find(self.data[x]) return self.data[x] def union(self, x, y): x, y = self....
p03045
class UFT(): #Union-find tree class def __init__(self, N): self.tree = [int(i) for i in range(N)] self.rank = [0 for i in range(N)] def find(self, a): if self.tree[a] == a: return a else: self.tree[a] = self.find(self.tree[a]) return self.tree[...
import sys class UFT: #Union-find tree class def __init__(self, N): self.tree = [int(i) for i in range(N)] self.rank = [0 for i in range(N)] def find(self, a): if self.tree[a] == a: return a else: self.tree[a] = self.find(self.tree[a]) retur...
p03045
import sys input = sys.stdin.readline n,m = list(map(int,input().split())) #Union Find #xの根を求める def find(x): if par[x] < 0: return x else: par[x] = find(par[x]) return par[x] #xとyの属する集合の併合 def unite(x,y): x = find(x) y = find(y) if x == y: re...
import sys input = lambda : sys.stdin.readline().rstrip() n, m = list(map(int, input().split())) #Union Find #xの根を求める def find(x): if par[x] < 0: return x else: par[x] = find(par[x]) return par[x] #xとyの属する集合の併合 def unite(x,y): x = find(x) y = find(y) if...
p03045