input stringlengths 20 127k | target stringlengths 20 119k | problem_id stringlengths 6 6 |
|---|---|---|
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.... | 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] # 親の値が更新され、更新された親の値が返る, 返るのは正... | p03108 |
N, M = list(map(int, input().split()))
ab = [tuple([int(x)-1 for x in input().split()]) for _ in range(M)]
class UnionFind:
def __init__(self, n):
self.par = [i for i in range(n+1)]
self.rank = [0] * (n+1) # 木の高さ
self.n = [1] * (n+1) # グループメンバー数
def find(self, x):
... | N, M = list(map(int, input().split()))
ab = [tuple([int(x)-1 for x in input().split()]) for _ in range(M)]
class UnionFind:
def __init__(self, n):
self.par = [i for i in range(n+1)]
self.rank = [0] * (n+1) # 木の高さ
self.n = [1] * (n+1) # グループメンバー数
def find(self, x):
... | p03108 |
class UnionFind():
def __init__(self, n):
self.parents = [-1] * n
def find(self, x):
tb = []
while True:
if self.parents[x] < 0:
for i in tb:
self.parents[i] = x
return x
else:
tb.app... | def xC2(x):
if x == 1:
return 0
else:
return x * (x-1) // 2
class UnionFind():
def __init__(self, n):
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents... | p03108 |
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input():
return sys.stdin.readline().rstrip()
def main():
class UnionFind():
def __init__(self,n):
self.par=[i for i in range(n)]
self.siz=[1]*n
def root(self,x):
wh... | import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input():
return sys.stdin.readline().rstrip()
def main():
class UnionFind():
def __init__(self,n):
self.par=[i for i in range(n)]
self.siz=[1]*n
def root(self,x):
wh... | p03108 |
N, M = list(map(int, input().split()))
lis = [[x, -1] for x in range(1, N + 1)]
Blis = [list(map(int, input().split())) for _ in range(M)]
inc = int(N*(N - 1)/2)
ans = [x for x in range(M)]
ans[M - 1] = inc
def root(a):
if lis[a - 1][1] < 0:
return lis[a - 1][0]
else:
k = root(lis[a - ... | N, M = list(map(int, input().split()))
lis = [[x, -1] for x in range(1, N + 1)]
Blis = [list(map(int, input().split())) for _ in range(M)]
inc = int(N*(N - 1)/2)
ans = [x for x in range(M)]
ans[M - 1] = inc
def root(a):
if lis[a - 1][1] < 0:
return lis[a - 1][0]
else:
k = root(lis[a - ... | p03108 |
N, M = list(map(int, input().split()))
lis = [[x, -1] for x in range(1, N + 1)]
Blis = [list(map(int, input().split())) for _ in range(M)]
inc = int(N*(N - 1)/2)
ans = [x for x in range(M)]
ans[M - 1] = inc
def root(a):
if lis[a - 1][1] < 0:
return lis[a - 1][0]
else:
k = root(lis[a - ... | N, M = list(map(int, input().split()))
Parent = [-1 for x in range(N + 5)]
lis = [list(map(int, input().split())) for _ in range(M)]
ini = int(N*(N - 1)/2)
ans = [x for x in range(M)]
ans[M - 1] = ini
def root(a):
if Parent[a] < 0:
return a
else:
k = root(Parent[a])
Parent[a] ... | p03108 |
N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for i in range(M)]
AB.reverse()
island = [[i+1] for i in range(N)]
ans = []
for m in range(M):
count = 0
for i in range(len(island)):
for j in range(i+1, len(island)):
count += len(island[i]) * len(island[j... | def find(idx):
global tree
while True:
if tree[idx].root == True:
return tree[idx].prev, tree[idx].size
else:
idx = tree[idx].prev
class Union_Find:
def __init__(self, prev):
self.root = True
self.size = 1
self.prev = prev
N, M ... | p03108 |
class UnionFind:
def __init__(self, n):
self.par = [i for i in range(n + 1)]
self.rank = [0] * (n + 1)
self.count = [1] * (n + 1)
# find
def find(self, x):
if self.par[x] == x:
return x
else:
self.par[x] = self.find(self.par[x])
... | class UnionFind:
def __init__(self, n):
self.par = [i for i in range(n + 1)]
self.rank = [0] * (n + 1)
self.count = [1] * (n + 1)
# find
def find(self, x):
if self.par[x] == x:
return x
else:
self.par[x] = self.find(self.par[x])
... | p03108 |
class UnionFind:
def __init__(self,n):
*self.table,=range(n)
self.rank=[1]*n
def root(self,x):
if self.table[x]==x:
return x
else:
self.table[x]=self.root(self.table[x])
return self.table[x]
def unite(self,x,y):
x=self.root(x)
y=self.root(y)
if x!=y:
... | def root(x):
if table[x]==x:
return x
else:
table[x]=root(table[x])
return table[x]
def unite(x,y):
x=root(x)
y=root(y)
if x!=y:
if rank[x]<rank[y]:
x,y=y,x
rank[x]+=rank[y]
table[y]=x
def same(x,y):
return root(x)==root(y)
def size(x):
return rank[root(x)]
... | p03108 |
def r(x):
while u[x]>=0:x=u[x]
return x
(n,m),_,*e=[map(int,t.split())for t in open(0).readlines()]
u=[-1]*n
a=[n*~-n//2]
for x,y in e[::-1]:
t=a[-1]
x,y=r(x-1),r(y-1)
if x!=y:
t-=u[x]*u[y]
u[x]+=u[y]
u[y]=x
a.append(t)
print(*a[::-1],sep='\n')
| def r(x):
while u[x]>=0:x=u[x]
return x
(n,m),_,*e=[map(int,t.split())for t in open(0).readlines()]
u=[-1]*n
a=[n*~-n//2]
for x,y in e[::-1]:
t=a[-1]
x,y=sorted((r(x-1),r(y-1)))
if x!=y:
t-=u[x]*u[y]
u[x]+=u[y]
u[y]=x
a.append(t)
print(*a[::-1],sep='\n')
| p03108 |
def r(x):
while u[x]>=0:x=u[x]
return x
(n,m),_,*e=[map(int,t.split())for t in open(0).readlines()]
u=[-1]*n
a=[n*~-n//2]
for x,y in e[::-1]:
t=a[-1]
x,y=sorted((r(x-1),r(y-1)))
if x!=y:
t-=u[x]*u[y]
u[x]+=u[y]
u[y]=x
a.append(t)
print(*a[::-1],sep='\n')
| def r(x):
while u[x]>=0:x=u[x]
return x
(n,m),_,*e=[map(int,t.split())for t in open(0).readlines()];u=[-1]*-~n;a=[n*~-n//2]
for x,y in e[::-1]:
t=a[-1];x,y=sorted((r(x),r(y)))
if x!=y:t-=u[x]*u[y];u[x]+=u[y];u[y]=x
a.append(t)
print(*a[::-1],sep='\n')
| p03108 |
r=lambda x:x if u[x]<0else r(u[x])
(n,m),_,*e=[map(int,t.split())for t in open(0).readlines()]
u=[-1]*-~n
a=[n*~-n//2]
for x,y in e[::-1]:
t=a[-1];x,y=sorted((r(x),r(y)))
if x!=y:t-=u[x]*u[y];u[x]+=u[y];u[y]=x
a+=[t]
print(*a[::-1],sep='\n')
| r=lambda x:x if u[x]<0else r(u[x])
(n,m),_,*e=[map(int,t.split())for t in open(0).readlines()]
u=[-1]*-~n
t=n*~-n//2
a=[t]
for x,y in e[::-1]:
x,y=sorted((r(x),r(y)))
if x!=y:t-=u[x]*u[y];u[x]+=u[y];u[y]=x
a+=[t]
print(*a[::-1],sep='\n')
| p03108 |
r=lambda x:x if u[x]<0else r(u[x])
(n,m),_,*e=[map(int,t.split())for t in open(0).readlines()]
u=[-1]*-~n
t=n*~-n//2
a=[t]
for x,y in e[::-1]:
x,y=sorted((r(x),r(y)))
if x!=y:t-=u[x]*u[y];u[x]+=u[y];u[y]=x
a+=[t]
print(*a[::-1],sep='\n')
| r=lambda x:x if u[x]<0else r(u[x])
n,m,*e=map(int,open(0).read().split())
u=[-1]*-~n
t=n*~-n//2
a=[t]
for x,y in zip(e[-2:1:-2],e[:1:-2]):
x,y=sorted((r(x),r(y)))
if x!=y:t-=u[x]*u[y];u[x]+=u[y];u[y]=x
a+=[t]
print(*a[::-1],sep='\n')
| p03108 |
r=lambda x:x if u[x]<0else r(u[x])
n,m,*e=map(int,open(0).read().split())
u=[-1]*-~n
t=n*~-n//2
a=t,
for x,y in zip(e[-2:1:-2],e[:1:-2]):
x,y=sorted((r(x),r(y)))
if x!=y:t-=u[x]*u[y];u[x]+=u[y];u[y]=x
a+=t,
print(*a[::-1],sep='\n')
| r=lambda x:x if u[x]<0else r(u[x])
n,m,*e=map(int,open(0).read().split())
u=[-1]*-~n
t=n*~-n//2
a=[t]
for x,y in zip(e[-2:1:-2],e[:1:-2]):
x,y=sorted((r(x),r(y)))
if x!=y:t-=u[x]*u[y];u[x]+=u[y];u[y]=x
a+=t,
print(*a[::-1],sep='\n')
| p03108 |
N, M =list(map(int, input().split()))
a, b = [], []
for i in range(M):
a_i, b_i = list(map(int, input().split()))
a.append(a_i -1)
b.append(b_i -1)
class UnionFind:
def __init__(self, n):
self.p = {}
self.r = {}
self.s = {}
for i in range(n):
... | class UnionFind:
def __init__(self, n):
self.p = [i for i in range(n)]
self.s = [1 for i in range(n)]
def find(self, x):
if x == self.p[x]:
return x
else:
self.p[x] = self.find(self.p[x])
return self.p[x]
def union... | p03108 |
# coding:utf-8
import sys
from collections import deque, defaultdict
INF = float('inf')
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(... | # coding:utf-8
import sys
INF = float('inf')
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return inpu... | p03108 |
N,M = list(map(int,input().split()))
AB = [list(map(int,input().split())) for _ in range(M)]
AB.reverse() # 逆から考える
ans_list = [-1 for i in range(M)]
ans_list[M-1] = N*(N-1)//2
par = [i for i in range(N+1)]
rank = [0 for i in range(N+1)]
cnt = [1 for i in range(N+1)]
# 木の根を求める
def root(x):
if par[... | # cnt->size
N,M = list(map(int,input().split()))
AB = [list(map(int,input().split())) for _ in range(M)]
AB.reverse() # 逆から考える
ans_list = [-1 for i in range(M)]
ans_list[M-1] = N*(N-1)//2
# parent(親)
par = [i for i in range(N+1)]
# rank(深さ)
rank = [0 for i in range(N+1)]
# 同グループの頂点数
size = [1 for i in... | p03108 |
import sys
class AlgUnionFind():
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 ... | import sys
#https://note.nkmk.me/python-union-find/
class AlgUnionFind():
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])
... | p03108 |
import sys
#https://note.nkmk.me/python-union-find/
class AlgUnionFind():
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])
... | import sys
#https://note.nkmk.me/python-union-find/
class AlgUnionFind():
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])
... | p03108 |
class UnionFind(object):
def __init__(self, n=1):
self.par = [i for i in range(n)]
self.rank = [0 for _ in range(n)]
self.size = [1 for _ in range(n)]
def find(self, x):
"""
x が属するグループを探索
"""
# xが根だったら自分自身をreturnする
if self.par[x] == x:
... | 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):
... | p03108 |
# -*- coding: utf-8 -*-
"""
D - Decayed Bridges
https://atcoder.jp/contests/abc120/tasks/abc120_d#
"""
import sys
class DisjointSet(object):
def __init__(self, n):
self.rank = []
self.p = []
self.size = []
for i in range(n+1):
self.makeSet(i)
def ma... | # -*- coding: utf-8 -*-
"""
D - Decayed Bridges
https://atcoder.jp/contests/abc120/tasks/abc120_d#
AC
"""
import sys
class DisjointSet(object):
def __init__(self, n):
self.rank = [0] * n
self.p = list(range(n))
self.size = [1] * n
def same(self, x, y):
return sel... | p03108 |
class UnionFind:
def __init__(self, n):
self.n = n
self.parent = [i for i in range(n)] # 親
self.rank = [1] * n # 木の高さ
self.size = [1] * n # size[i] は i を根とするグループのサイズ
def find(self, x): # x の根を返す
if self.parent[x] == x:
return x
else:
... | class UnionFind:
def __init__(self, n):
self.n = n
self.parent = [i for i in range(n)] # 親
self.rank = [1] * n # 木の高さ
self.size = [1] * n # size[i] は i を根とするグループのサイズ
def find(self, x): # x の根を返す
if self.parent[x] == x:
return x
else:
... | p03108 |
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
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:
s... | import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
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:
s... | p03108 |
class unionfind:
def __init__(self, n):
self.parent = list(range(n))
def find(self, i):
if self.parent[i] != i: self.parent[i] = self.find(self.parent[i])
return self.parent[i]
def unite(self, i, j):
i = self.find(i)
j = self.find(j)
if i != j: self.parent[i] = j
def issame(self, i, j):
ret... | #学習用
def root(x):
# xが根であれば
if x == p[x]:
# xを返す
return x
# xが根でなければp[x]の根をp[x]に設定する
p[x] = y = root(p[x])
# p[x]の根を返却する
return y
def unite(x, y):
# xとyの根を取得する
px = root(x); py = root(y)
# xとyの根が同じならば
if px == py:
# 根が同じならば、0を返す
ret... | p03108 |
import collections
class UnionFind:
def __init__(self, n):
self.par =[i for i in range(n+1)]
self.rank = [0] * (n+1)
def find(self, x):
if self.par[x] == x:
return x
else:
self.par[x] = self.find(self.par[x])
return self.par[x]
d... | class UnionFind():
def __init__(self, n):
self.n = n
self.parents = [-1] * (n+1)
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 uni... | p03108 |
N,M=list(map(int, input().split()))
#島、橋
class UnionFind():
# 作りたい要素数nで初期化
# 使用するインスタンス変数の初期化
def __init__(self, n):
self.n = n
# root[x]<0ならそのノードが根かつその値が木の要素数
# rootノードでその木の要素数を記録する
self.root = [-1]*(n+1)
# 木をくっつける時にアンバランスにならないように調整する
self.rnk = [0]*(n+1)
# ノードxのrootノードを... | N,M=list(map(int, input().split()))
B=[]
class UnionFind():
# 作りたい要素数nで初期化
# 使用するインスタンス変数の初期化
def __init__(self, n):
self.n = n
# root[x]<0ならそのノードが根かつその値が木の要素数
# rootノードでその木の要素数を記録する
self.root = [-1]*(n+1)
# 木をくっつける時にアンバランスにならないように調整する
self.rnk = [0]*(n+1)
# ノードxのrootノードを... | p03108 |
N,M=list(map(int,input().split()))
path=[]
for i in range(M):
a,b=list(map(int,input().split()))
path.append([a-1,b-1])
class UnionFind():
#負の値はルートで集合の個数
#正の値は次の要素を返す
def __init__(self,size):
self.table = [-1 for _ in range(size)]
self.member_num = [1 for _ in rang... |
N,M=list(map(int,input().split()))
path=[]
for i in range(M):
a,b=list(map(int,input().split()))
path.append([a-1,b-1])
class UnionFind():
#負の値はルートで集合の個数
#正の値は次の要素を返す
def __init__(self,size):
self.table = [-1 for _ in range(size)]
self.member_num = [1 for _ in ra... | p03108 |
from math import factorial
def combination(n, r): # n個からr個選ぶ場合の数
if n < r:
return 0
else:
return factorial(n)//(factorial(r) * factorial(n-r))
class UnionFind():
def __init__(self, n):
self.nodes = [-1] * n # nodes[x]: 負なら、絶対値が木の要素数
def get_root(self, x):
... | from math import factorial
def combination(n, r): # n個からr個選ぶ場合の数
if n < r:
return 0
else:
return factorial(n)//(factorial(r) * factorial(n-r))
class UnionFind():
def __init__(self, n):
self.nodes = [-1] * n # nodes[x]: 負なら、絶対値が木の要素数
def get_root(self, x):
... | p03108 |
n,m=list(map(int,input().split()))
ab=[[0,0] for i in range(m)]
for i in range(m):
ab[i][0],ab[i][1]=list(map(int,input().split()))
import sys
sys.setrecursionlimit(10**9) #再帰の上限をあげる
root=[-1 for i in range(n+1)] #自分が親なら自身の番号を、そうでないなら(元)親の番号を示す
def r(x): #親は誰?
if root[x]<0:
return x
... | import sys
input=sys.stdin.readline #文字列入力はするな!!
n,m=list(map(int,input().split()))
ab=[[0,0] for i in range(m)]
for i in range(m):
ab[i][0],ab[i][1]=list(map(int,input().split()))
sys.setrecursionlimit(10**9) #再帰の上限をあげる
root=[-1 for i in range(n+1)] #自分が親なら自身の番号を、そうでないなら(元)親の番号を示す
def r(x): #... | p03108 |
N, M = list(map(int, input().split()))
L = [list(map(int, input().split())) for _ in range(M)]
X = [set([i]) for i in range(1,N+1)]
C = [0]
for a, b in L[::-1]:
xa = [x for x in X if a in x][0]
xb = [x for x in X if b in x][0]
if(len(xa ^ xb)!=0):
C.append(C[-1]+len(xa)*len(xb))
... | N, M = list(map(int, input().split()))
L = [list(map(int, input().split())) for _ in range(M)]
X = [set([i]) for i in range(1,N+1)]
par = list(range(N+1))
rank = [0] * (N+1)
size = [1] * (N+1)
def find(x):
if par[x] == x:
return x
else:
par[x] = find(par[x])
return par[x... | p03108 |
class uf_tree:
def __init__(self, n):
self._size = n
self.sizes = [1] * n
self.par = list(range(n))
def find(self, x):
if x == self.par[x]:
return x
self.par[x] = self.find(self.par[x])
return self.par[x]
def unite(self, x, y):
... | import sys
input = sys.stdin.readline
class uf_tree:
def __init__(self, n):
self._size = n
self.sizes = [1] * n
self.par = list(range(n))
def find(self, x):
if x == self.par[x]:
return x
self.par[x] = self.find(self.par[x])
return self... | p03108 |
#%%
class Graph:
def __init__(self,n):
'''Generate graph. n is the number of islands in the world.'''
self.numIslands = n
self.union = {}
self.nextUnionKey = 1
self.isolatedPath = int(n * (n-1) / 2)
def getUnionId(self,island):
'''returns union ID of th... | N, M = list(map(int, input().split()))
AB = []
for _ in range(M):
a, b = list(map(int, input().split()))
AB.append((a-1, b-1))
class UnionFind():
def __init__(self, n):
# negative if parent
# if parent, absolute value represents the number of nodes in the union
# if chil... | p03108 |
n, m = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(m)]
group = [i for i in range(n)]
islands = {i: [i] for i in range(n)}
ret = [0] * m + [n * (n - 1) // 2]
for i in range(m - 1, -1, -1):
a, b = ab[i]
ga, gb = group[a - 1], group[b - 1]
if ga == gb:
... | n, m = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(m)]
group = [i for i in range(n)]
islands = {i: [i] for i in range(n)}
ret = [0] * m + [n * (n - 1) // 2]
for i in range(m - 1, -1, -1):
a, b = ab[i]
ga, gb = group[a - 1], group[b - 1]
if ga == gb:
... | p03108 |
n, m = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(m)]
group = [i for i in range(n)]
islands = {i: [i] for i in range(n)}
ret = [0] * m + [n * (n - 1) // 2]
for i in range(m - 1, -1, -1):
a, b = ab[i]
ga, gb = group[a - 1], group[b - 1]
if ga == gb:
... | import sys
sys.setrecursionlimit(100000)
n, m = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(m)]
parent = [i for i in range(n)]
count = [1 for i in range(n)]
def root(i):
if i == parent[i]:
return i
parent[i] = root(parent[i])
return parent[i]
... | p03108 |
def getN():
return int(eval(input()))
def getMN():
a = input().split()
b = [int(i) for i in a]
return b[0],b[1]
def getlist():
a = input().split()
b = [int(i) for i in a]
return b
from collections import Counter
class UnionFind:
def __init__(self, n):
self.par ... | def getN():
return int(eval(input()))
def getMN():
a = input().split()
b = [int(i) for i in a]
return b[0],b[1]
def getlist():
a = input().split()
b = [int(i) for i in a]
return b
from collections import Counter
class UnionFind:
def __init__(self, n):
self.par ... | p03108 |
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdi... | import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdi... | p03108 |
N, M = [int(_) for _ in input().split()]
AB = [[int(_) for _ in input().split()] for _ in range(M)]
UF = list(range(N + 1))
SIZE = [0] + [1] * N # 1-indexed
def find(x):
if UF[x] != x:
UF[x] = find(UF[x])
return UF[x]
def unite(x, y):
m = min(find(x), find(y))
SIZE[m] = SIZE... | N, M = [int(_) for _ in input().split()]
AB = [[int(_) for _ in input().split()] for _ in range(M)]
UF = list(range(N + 1))
SIZE = [0] + [1] * N # 1-indexed
def find(x):
if UF[x] != x:
UF[x] = find(UF[x])
return UF[x]
def unite(x, y):
X, Y = find(x), find(y)
SX,SY=SIZE[X],SI... | p03108 |
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
N,M=list(map(int, input().split()))
B=[tuple(map(int, input().split())) for _ in range(M)]
P=list(range(N+1))
C=[1 for _ in range(N+1)]
C[0] = 0
def root(x):
if P[x]==x: return x
P[x] = root(P[x])
return P[x]
def unite(a,... | import sys
input = sys.stdin.readline
N,M=list(map(int, input().split()))
B=[tuple(map(int, input().split())) for _ in range(M)]
P=list(range(N+1))
C=[1 for _ in range(N+1)]
C[0] = 0
def root(x):
t = x
stack = []
while P[t] != t:
stack.append(t)
t = P[t]
for c in stack... | p03108 |
'''
https://atcoder.jp/contests/abc120/tasks/abc120_d
'''
def main():
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10000000)
from collections import Counter, deque
#from collections import defaultdict
from itertools import combinations, permutations
#from itertools i... | '''
https://atcoder.jp/contests/abc120/tasks/abc120_d
'''
def main():
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10000000)
from collections import Counter, deque
#from collections import defaultdict
from itertools import combinations, permutations
#from itertools i... | p03108 |
N, M = list(map(int, input().split()))
A, B = [], []
g = {}
for i in range(1, N+1):
g[i] = []
for _ in range(M):
a, b = list(map(int, input().split()))
A.append(a)
B.append(b)
g[a].append(b)
g[b].append(a)
def dfs(graph, start):
visited = []
stack = []
stack.append(... | class UnionFind():
# 初期化
# 全てのノードの親は -1 とする
def __init__(self, n):
self.par = [-1] * n
# xがどのグループに属しているか調べる
def root(self, x):
# xが親のとき
if self.par[x] < 0:
return x
# xの親がさらに親をもつとき、それをxの親とする
else:
self.par[x] = self.root(s... | p03108 |
from sys import setrecursionlimit
#setrecursionlimit(10**5)
class UnionFind:
def __init__(self, n):
# 親要素のノード番号を格納
# par[x]=xの時そのノードは根
self.par = [i for i in range(n + 1)]
# rank
self.rank = [0] * (n + 1)
self.size = [1] * (n + 1)
def find(self, x):
... | from sys import setrecursionlimit
#setrecursionlimit(10**5)
class UnionFind:
def __init__(self, n):
# 親要素のノード番号を格納
# par[x]=xの時そのノードは根
self.par = [i for i in range(n + 1)]
# rank
self.rank = [0] * (n + 1)
self.size = [1] * (n + 1)
def find(self, x):
... | p03108 |
class UnionFind():
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
'''
要素xが属するグループの根を返す
'''
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
r... | class UnionFind():
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
'''
要素xが属するグループの根を返す
'''
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
r... | p03108 |
class UnionFind:
def __init__(self, size):
self.parent = [-1 for i in range(size)]#非負なら親ノード,負ならグループの要素数
def root(self, x): #root(x): xの根ノードを返す.
if self.parent[x] < 0:
return x
else:
while self.parent[x] >= 0:
if self.parent[self.parent[x]... | class UnionFind:
def __init__(self, size):
self.parent = [-1 for i in range(size)]#非負なら親ノード,負ならグループの要素数
def root(self, x): #root(x): xの根ノードを返す.
if self.parent[x] < 0:
return x
else:
while self.parent[x] >= 0 and self.parent[self.parent[x]] >= 0:
... | p03108 |
class UnionFind:
def __init__(self, size):
self.parent = [-1 for i in range(size)]#非負なら親ノード,負ならグループの要素数
def root(self, x): #root(x): xの根ノードを返す.
if self.parent[x] < 0:
return x
else:
while self.parent[x] >= 0 and self.parent[self.parent[x]] >= 0:
... | import sys
input = sys.stdin.readline
class UnionFind:
def __init__(self, size):
self.parent = [-1 for i in range(size)]#非負なら親ノード,負ならグループの要素数
def root(self, x): #root(x): xの根ノードを返す.
if self.parent[x] < 0:
return x
elif self.parent[self.parent[x]] < 0:
... | p03108 |
import sys
input = sys.stdin.readline
class UnionFind:
def __init__(self, size):
self.parent = [-1 for i in range(size)]#非負なら親ノード,負ならグループの要素数
def root(self, x): #root(x): xの根ノードを返す.
if self.parent[x] < 0:
return x
elif self.parent[self.parent[x]] < 0:
... | import sys
input = sys.stdin.readline
class UnionFind:
def __init__(self, size):
self.parent = [-1 for i in range(size)]#非負なら親ノード,負ならグループの要素数
def root(self, x): #root(x): xの根ノードを返す.
px=self.parent[x]
if px < 0:
return x
ppx=self.parent[px]
if ppx... | p03108 |
n,m=list(map(int,input().split()))
list_p=[]
for i in range(0,m):
a,b=list(map(int,input().split()))
list_p.append([a,b])
list_p.reverse()
group=[]
count=[]
group.append(list_p[0])
count.append(0)
for i in range(1,m):
number=0
index=0
ifa=0
ifb=0
for j in group:
numbe... | class UnionFind:
def __init__(self, n):
self.par = [i for i in range(n+1)]
self.rank = [0] * (n+1)
self.size=[1]*(n+1)
# 検索
def find(self, x):
if self.par[x] == x:
return x
else:
self.par[x] = self.find(self.par[x])
ret... | p03108 |
class Unionfind():
def __init__(self, progsize):
self.table = [-1 for _ in range(progsize)]
self.size = [1 for _ in range(progsize)]
def find(self, x):
while(self.table[x] >= 0):
x = self.table[x]
return x
def union(self, x, y):
xl = self.find... | class Unionfind():
def __init__(self, progsize):
self.table = [-1 for _ in range(progsize)]
self.size = [1 for _ in range(progsize)]
def find(self, x):
while(self.table[x] >= 0):
x = self.table[x]
return x
def union(self, x, y):
xl = self.find... | p03108 |
class DisjointSet():
def __init__(self, n):
self._p = [x for x in range(n)]
self._size = [-1 for x in range(n)]
def find(self, p):
while p != self._p[p]:
p = self._p[p]
return p
def union(self, p, q):
pp, qq = self.find(p), self.find(q)
... | class DisjointSet():
def __init__(self, n):
self._p = [x for x in range(n)]
self._size = [-1 for x in range(n)]
self._rank = [0 for x in range(n)]
def find(self, p):
while p != self._p[p]:
p = self._p[p]
return p
def union(self, p, q):
... | p03108 |
import sys
# import math
# import bisect
# import copy
# import heapq
# from collections import deque
# import decimal
# sys.setrecursionlimit(100001)
# INF = sys.maxsize
# MOD = 10 ** 9 + 7
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda... | import sys
# import math
# import bisect
# import copy
# import heapq
# from collections import deque
# import decimal
# sys.setrecursionlimit(100001)
# INF = sys.maxsize
# MOD = 10 ** 9 + 7
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda... | p03108 |
import sys
readline = sys.stdin.readline
N, M = map(int, input().split())
ab = [tuple(map(lambda x: int(x) - 1, readline().split())) for _ in range(M)]
class UnionFind:
def __init__(self, size):
self.rank = [-1] * size
self.number = [1] * size
def find(self, x):
while self.rank[... | import sys
readline = sys.stdin.readline
N, M = map(int, input().split())
ab = [tuple(map(int, readline().split())) for _ in range(M)]
class UnionFind:
def __init__(self, size):
self.rank = [-1] * (size + 1)
self.number = [1] * (size + 1)
def find(self, x):
while self.rank[x] >=... | p03108 |
n, m = map(int, input().split())
ls = [list(map(int, input().split())) for _ in range(m)]
nodes = [[i, [i], 1] for i in range(n + 1)]
res = [n * (n - 1) // 2]
for a, b in reversed(ls):
x = nodes[a][0]
y = nodes[b][0]
if x == y:
res.append(res[-1])
else:
if x > y:
x... | n, m = map(int, input().split())
ls = [list(map(int, input().split())) for _ in range(m)]
parents = list(range(n + 1))
ranks = [0 for _ in range(n + 1)]
def find(x):
if parents[x] == x:
return x
parents[x] = find(parents[x])
return parents[x]
def unite(x, y):
x = find(x)
... | p03108 |
n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]
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)... | n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]
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)... | p03108 |
(N,M) = list(map(int,input().split()))
A = [0]*M
B = [0]*M
for i in range(M):
(A[i], B[i]) = list(map(int,input().split()))
A[i] -= 1
B[i] -= 1
parent = [-1]*N
depth = [0] * N
def boss(n):
d = 0
while parent[n] >= 0:
n = parent[n]
d += 1
return n,d
P = [1]*N
... | (N,M) = list(map(int,input().split()))
A = [0]*M
B = [0]*M
for i in range(M):
(A[i], B[i]) = list(map(int,input().split()))
A[i] -= 1
B[i] -= 1
parent = [-1]*N
def boss(n):
if parent[n] < 0:
return n
else:
b = boss(parent[n])
parent[n] = b
return b
... | p03108 |
N,M = [int(x) for x in input().split()]
map = [[0 for j in range(N+1)] for i in range(N+1)]
li_e = []
def dfs(v,li_gr,flg):
if(not flg[v]):
flg[v] = True
li_gr.append(v)
for u,exist in enumerate(map[v]):
if(exist == 1 and not flg[u]):
dfs(u,li_gr,flg)
... | import sys
#sys.setrecursionlimit(1000000)
class UnionFind():
def __init__(self,n):
self.parent = [-1 for _ in range(n)]
def size(self,x):
return -self.parent[self.find(x)]
def find(self,x):
if(self.parent[x]<0):
return x
else:
self.pa... | p03108 |
class UnionFind:
def __init__(self, n):
self.data=[-1 for i in range(n)]
def root(self,x):
if self.data[x]<0:
return x
else:
self.data[x]=self.root(self.data[x])
return self.data[x]
def uni(self,x,y):
x=self.root(x)
y=s... | import sys
sys.setrecursionlimit(100000)
input=lambda : sys.stdin.readline().rstrip('\n')
class UnionFind:
def __init__(self, n):
self.data=[-1 for i in range(n)]
def root(self,x):
if self.data[x]<0:
return x
else:
self.data[x]=self.root(self.data[x... | p03108 |
N,M=map(int,input().split())
#Union-Find
par=[i for i in range(N)]
siz=[1 for _ in range(N)]
rank=[0 for _ in range(N)]
root=set()
def find(x):
if(par[x]==x):
return x
else:
par[x]=find(par[x])
return par[x]
def union(a,b):
a=find(a)
b=find(b)
if(a==b):... | N,M=map(int,input().split())
#Union-Find
par=[i for i in range(N)]
siz=[1 for _ in range(N)]
rank=[0 for _ in range(N)]
root=set()
def find(x):
if(par[x]==x):
return x
else:
par[x]=find(par[x])
return par[x]
def union(a,b):
a=find(a)
b=find(b)
if(a==b):... | p03108 |
import sys
from itertools import combinations
input = sys.stdin.readline
class UnionFind:
"""Union Find class.
"Path compression" and "Union by rank" are used.
References:
<https://en.wikipedia.org/wiki/Disjoint-set_data_structure>
"""
def __init__(self, N):
sel... | import sys
from itertools import combinations
input = sys.stdin.readline
class UnionFind:
"""Union Find class.
"Path compression" and "Union by rank" are used.
References:
<https://en.wikipedia.org/wiki/Disjoint-set_data_structure>
"""
def __init__(self, N):
sel... | p03108 |
# 逆順に見ていく
# M本の橋が全て崩落した状態=全ての島が行き来できない=不便さ:N*(N-1)//2
# 橋ができたら、UnionFindで一つに島グループとしてつなぐ
# 新たにできた橋が、今まで別の島グループだったときは
# 二つの島グループの間の組み合わせの数だけ不便さが減る
# 島グループ1の数 * 島グループ2の数だけ不便さが減る
# 新たにできた島グループが同じ島グループだったときは何も変わらない
import sys
readline=sys.stdin.readline
# 0-indexed
class UnionFind:
N=0
parent=None
siz... | import sys
readline = sys.stdin.readline
class UnionFind:
N=0
parent=None
size=None
def __init__(self,N):
self.N=N
self.parent=[i for i in range(self.N)]
self.size=[1]*self.N
def root(self,x):
while x!=self.parent[x]:
self.parent[x]=self.parent[self.parent[x]]
... | p03108 |
import sys
sys.setrecursionlimit(65536)
N,M=list(map(int, input().split()))
bridge=[[int(x) for x in input().split()] for _ in range(M)]
uf=[i for i in range(N+1)]
nc=[1 for i in range(N+1)]
def find(i):
if uf[i] == i:
return i
else:
uf[i] = find(uf[i])
return uf[i]
def union(i, j):
gi, gj... | import sys
sys.setrecursionlimit(65536)
N,M=list(map(int, input().split()))
bridge=[[int(x) for x in input().split()] for _ in range(M)]
uf=[i for i in range(N+1)]
nc=[1 for i in range(N+1)]
def find(i):
if uf[i] == i:
return i
else:
uf[i] = find(uf[i])
return uf[i]
def union(i, j):
gi, gj... | p03108 |
n,m = list(map(int,input().split()))
a = [0]*m
b = [0]*m
for i in range(m):
a[i], b[i] = list(map(int,input().split()))
d = [0]*(n+1)
for i in range(1,n+1):
d[i] = i
s = [0] + [1]*n
g=[ [0] ]*(n+1)
for i in range(1,n+1):
g[i] = [i]
g[0] = []
#print(g)
#quit()
c = n*(n-1)//2
ans = []
ans... | n,m = list(map(int,input().split()))
a = [0]*m
b = [0]*m
for i in range(m):
a[i], b[i] = list(map(int,input().split()))
d = [0]*(n+1)
for i in range(1,n+1):
d[i] = i
g=[ [0] ]*(n+1)
for i in range(1,n+1):
g[i] = [i]
g[0] = []
#print(g)
#quit()
c = n*(n-1)//2
ans = []
ans.append(c)
for... | p03108 |
import queue
n, m = list(map(int, input().split()))
bridges = [list(map(int, input().split())) for _ in range(m)]
nodes = [[] for _ in range(n)] # いける島のリスト
ans = n*(n-1)//2
ans_list = []
q = queue.Queue()
for i in range(m):
ans_list.append(ans)
# 順番に橋をかけていく
a, b = bridges[-(i+1)]
if not a i... | class UnionFind():
# 作りたい要素数nで初期化
# 使用するインスタンス変数の初期化
def __init__(self, n):
self.n = n
# root[x]<0ならそのノードが根かつその値が木の要素数
# rootノードでその木の要素数を記録する
self.root = [-1]*(n+1)
# 木をくっつける時にアンバランスにならないように調整する
self.rnk = [0]*(n+1)
# ノードxのrootノードを見つける
def... | p03108 |
mod = 10 ** 9 + 7
mod2 = 2 ** 61 + 1
from collections import deque
import heapq
from bisect import bisect_left, insort_left, bisect_right
_NUMINT_ALL = list(range(10))
def main():
ans = solve()
if ans is True or ans is False:
YesNo(ans)
elif ans is not None:
print(ans)
... | mod = 10 ** 9 + 7
mod2 = 2 ** 61 + 1
from collections import deque
import heapq
from bisect import bisect_left, insort_left, bisect_right
_NUMINT_ALL = list(range(10))
def main():
ans = solve()
if ans is True or ans is False:
YesNo(ans)
elif ans is not None:
print(ans)
... | p03108 |
mod = 10 ** 9 + 7
mod2 = 2 ** 61 + 1
from collections import deque
import heapq
from bisect import bisect_left, insort_left, bisect_right
_NUMINT_ALL = list(range(10))
def main():
ans = solve()
if ans is True or ans is False:
YesNo(ans)
elif ans is not None:
print(ans)
... | mod = 10 ** 9 + 7
mod2 = 2 ** 61 + 1
from collections import deque
import heapq
from bisect import bisect_left, insort_left, bisect_right
_NUMINT_ALL = list(range(10))
def main():
ans = solve()
if ans is True or ans is False:
YesNo(ans)
elif ans is not None:
print(ans)
... | p03108 |
mod = 10 ** 9 + 7
mod2 = 2 ** 61 + 1
from collections import deque
import heapq
from bisect import bisect_left, insort_left, bisect_right
_NUMINT_ALL = list(range(10))
def main():
ans = solve()
if ans is True or ans is False:
YesNo(ans)
elif ans is not None:
print(ans)
... | mod = 10 ** 9 + 7
mod2 = 2 ** 61 + 1
from collections import deque
import heapq
import time
from bisect import bisect_left, insort_left, bisect_right
import sys
input = sys.stdin.readline
_NUMINT_ALL = list(range(10))
def main():
ans = solve()
if ans is True or ans is False:
YesNo(an... | p03108 |
mod = 10 ** 9 + 7
mod2 = 2 ** 61 + 1
from collections import deque
import heapq
import time
from bisect import bisect_left, insort_left, bisect_right
import sys
input = sys.stdin.readline
_NUMINT_ALL = list(range(10))
def main():
ans = solve()
if ans is True or ans is False:
YesNo(an... | mod = 10 ** 9 + 7
mod2 = 2 ** 61 + 1
from collections import deque
import heapq
import time
from bisect import bisect_left, insort_left, bisect_right
import sys
input = sys.stdin.readline
_NUMINT_ALL = list(range(10))
def main():
ans = solve()
if ans is True or ans is False:
YesNo(an... | p03108 |
N, M = [int(x) for x in input().split()]
bridge = [[int(x) for x in input().split()] for x in range(M)]
par = [int(x) for x in range(N+1)] # 所属グループ
gn = [1 for _ in range(N+1)]
def group(i):
if par[i] == i:
return i
else:
return group(par[i])
def group_num(i): #... | N, M = [int(x) for x in input().split()]
bridge = [[int(x) for x in input().split()] for x in range(M)]
par = [int(x) for x in range(N+1)] # 所属グループ
gn = [1 for _ in range(N+1)] # 各グループサイズ
def group(i):
if par[i] == i:
return i
else:
par[i] = group(par[i])
return pa... | p03108 |
n,m = list(map(int,input().split()))
ab = [list(map(int,input().split())) for i in range(m)]
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] ... | 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):
... | p03108 |
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
class UnionFind:
# Reference: https://note.nkmk.me/python-union-find/
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def f... | import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
class UnionFind:
# Reference: https://note.nkmk.me/python-union-find/
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def f... | p03108 |
n,m=map(int,input().split())
A=[]
par=[-1]*n
def find(x):
if par[x]<0:
return x
else:
par[x]=find(par[x])
return par[x]
def unite(x,y):
x=find(x)
y=find(y)
if x==y:
return False
else:
if par[x]>par[y]:
x,y=y,x
par[x]+=par[y]
par[y]=x
return True
de... | import sys
input=lambda: sys.stdin.readline().rstrip()
n,m=list(map(int,input().split()))
par=[-1]*n
def find(x):
if par[x]<0:
return x
else:
par[x]=find(par[x])
return par[x]
def unite(x,y):
x=find(x)
y=find(y)
if x==y:
return False
else:
if par[x]>par[y]:
x,y=y... | p03108 |
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):
x = self.find(x)
y = self... | 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):
... | p03108 |
# ユニオンファインド
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(... | 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])
... | p03108 |
class UnionFind:
def __init__(self, n):
self.n = n
self.par = [-1 for _ in range(n)]
#マイナスが根を表す
#数字はグループのノード数(サイズ)を表す
#初期状態では、全てのノードが根で、サイズは1
def same(self, x, y):
return self.root(x)==self.root(y)
def root(self, x):
if self.par[x]<0:
... | n, m = list(map(int, input().split()))
AB = [[int(i) for i in input().split()] for i in range(m)]
class UnionFind:
def __init__(self, n):
self.n = n
self.par = [-1 for _ in range(n)]
def same(self, x, y):
return self.root(x)==self.root(y)
def root(self, x):
i... | p03108 |
N,M=list(map(int,input().split()))
AB=[[0,0] for i in range(M)]
for i in range(M):
AB[i][0],AB[i][1]=list(map(int,input().split()))
par=[i for i in range(N+1)]
rank=[1 for i in range(N+1)]
def Union_Find_find(x):
global par
if par[x]==x:
return x
else:
return Union_Find_find(p... | N,M=list(map(int,input().split()))
AB=[[0,0] for i in range(M)]
for i in range(M):
AB[i][0],AB[i][1]=list(map(int,input().split()))
par=[i for i in range(N+1)]
rank=[1 for i in range(N+1)]
hight=[1 for i in range(N+1)]
def Union_Find_find(x):
global par,rank,hight
if par[x]==x:
return x
... | p03108 |
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):
... | p03108 |
import sys
sys.setrecursionlimit(99999)
def find_index_by_value(a, val):
for i, items in enumerate(a):
if val in items:
return i
return -1
def num_reachable(n):
return n * (n - 1) // 2
n, m = list(map(int, input().split()))
edges = []
for _ in range(m):
a, b =... | def find_index_by_value(a, val):
for i, items in enumerate(a):
if val in items:
return i
return -1
def num_reachable(n):
return n * (n - 1) // 2
n, m = list(map(int, input().split()))
edges = []
for _ in range(m):
a, b = list(map(int, input().split()))
edges.... | p03108 |
import sys
input = sys.stdin.readline
def comb(n,r):
r = min(r,n-r)
result = 1
for i in range(n-r+1,n+1):
result *= i
for i in range(1,r+1):
result //= i
return result
class Unionfind:
__slots__ = ['nodes']
def __init__(self, n):
self.nodes = [-1]... | import sys
input = sys.stdin.readline
# 組み合わせ n_C_r
def comb(n,r):
r = min(r,n-r)
result = 1
for i in range(n-r+1,n+1):
result *= i
for i in range(1,r+1):
result //= i
return result
class Unionfind:
__slots__ = ['nodes','size']
def __init__(self, n):
... | p03108 |
from sys import stdin
def input():
return stdin.readline()[:-1]
#おまじない。
n,m=list(map(int,input().split()))
AB=[list(map(int,input().split())) for i in range(m)]
from itertools import combinations
#union-find召喚
par=[-1]*n
def find(x):
if par[x]<0:
return x
else:
par[x]=find(par... | n, m = map(int, input().split())
par = [-1] * (n + 1)
def find(x):
if par[x] < 0:
return x
else:
par[x] = find(par[x])
return par[x]
def size(x):
X = find(x)
return -par[X]
def unite(x, y):
if find(x) == find(y):
return False
X = find(x... | p03108 |
class UF_tree:
def __init__(self, n):
self.root = [-1] * (n + 1) # -1ならそのノードが根,で絶対値が木の要素数
self.rank = [0] * (n + 1)
def find(self, x): # xの根となる要素番号を返す
if self.root[x] < 0:
return x
else:
self.root[x] = self.find(self.root[x])
retur... | import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
class UF_tree:
def __init__(self, n):
self.root = [-1] * (n + 1) # -1ならそのノードが根,で絶対値が木の要素数
self.rank = [0] * (n + 1)
def find(self, x): # xの根となる要素番号を返す
if self.root[x] < 0:
return x
... | p03108 |
S = input()[:: -1]
mod = 10 ** 9 + 7
dp = [[0 for _ in range(13)] for _ in range(len(S) + 1)]
if S[0] == "?":
for k in range(10):
dp[1][k % 13] += 1
else:
dp[1][int(S[0]) % 13] += 1
tmp = 1
for i in range(2, len(S) + 1):
tmp *= 10
for j in range(13):
if dp[i - 1][j] > 0:... | S = input()[:: -1]
mod = 10 ** 9 + 7
dp = [[0 for _ in range(13)] for _ in range(len(S) + 1)]
if S[0] == "?":
for k in range(10):
dp[1][k % 13] += 1
else:
dp[1][int(S[0]) % 13] += 1
tmp = 1
for i in range(2, len(S) + 1):
tmp *= 10
tmp %= 13
for j in range(13):
if dp[... | p02960 |
import sys
mod = 10**9 + 7
S = sys.stdin.readline().strip()
S = S[::-1]
ls = len(S)
dp = [0] * 13
dp[0] = 1
for i in range(ls):
ndp = [0] * 13
# print("S", S[i])
for j in range(13):
if S[i] == "?":
for k in range(10):
r = k * pow(10, i, 13) + j
... | import sys
mod = 10**9 + 7
S = sys.stdin.readline().strip()
S = S[::-1]
ls = len(S)
dp = [0] * 13
dp[0] = 1
for i in range(ls):
ndp = [0] * 13
# print("S", S[i])
for j in range(13):
if dp[j] == 0:
continue
digit = pow(10, i, 13)
if S[i] == "?":
... | p02960 |
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
#from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
f... | import sys,math,copy
def main():
# import numpy as np
input = sys.stdin.readline
S = ''.join(reversed(input().strip()))
# dp = np.zeros((13), dtype=int)
# dp_ = np.zeros((13), dtype=int)
dp = [0] * 13
dp_ = [0] * 13
mod13_list = [i % 13 for i in range(12 ** 2 + 1)]
list... | p02960 |
import sys
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = True
MOD = 10**9 + 7
class ModInt:
def __init__(self,... | import sys
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = True
MOD = 10**9 + 7
class ModInt:
def __init__(self,... | p02960 |
import sys
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = True
MOD = 10**9 + 7
class ModInt:
def __init__(self,... | import sys
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = True
MOD = 10**9 + 7
class ModInt:
def __init__(self,... | p02960 |
import sys
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = True
MOD = 10**9 + 7
class ModInt:
def __init__(self,... | import sys
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = True
MOD = 10**9 + 7
def main():
S = input()
N = l... | p02960 |
mod = 10 ** 9 + 7
S = input()[::-1]
W = [1, 10, 9, 12, 3, 4]
dp = [[0] * 13 for _ in range(len(S))]
for i, s in enumerate(S):
if i == 0:
if s == "?":
for j in range(10):
dp[0][j] = 1
else:
dp[0][int(s)] = 1
continue
if s == "?":... |
mod = 10 ** 9 + 7
S = input()[::-1]
W = [1, 10, 9, 12, 3, 4]
dp = [[0] * 13 for _ in range(len(S))]
for i, s in enumerate(S):
if i == 0:
if s == "?":
for j in range(10):
dp[0][j] = 1
else:
dp[0][int(s)] = 1
continue
w = W[i % 6]... | p02960 |
# import sys
# fin = sys.stdin.readline
MOD = 10 ** 9 + 7
S = input()[::-1]
dp = [[0] * 13 for _ in range(len(S) + 1)]
dp[0][0] = 1
for i, c in enumerate(S, start=1):
base = 10 ** (i - 1) % 13
if c != '?':
d = int(c) * base % 13
for r in range(13):
c = (d + r) % 13
... | # import sys
# fin = sys.stdin.readline
MOD = 10 ** 9 + 7
S = input()[::-1]
dp = [[0] * 13 for _ in range(len(S) + 1)]
dp[0][0] = 1
for i, c in enumerate(S, start=1):
base = pow(10, i - 1, 13)
if c != '?':
d = int(c) * base % 13
for r in range(13):
c = (d + r) % 13
... | p02960 |
# import sys
# fin = sys.stdin.readline
MOD = 10 ** 9 + 7
S = input()[::-1]
dp = [[0] * 13 for _ in range(len(S) + 1)]
dp[0][0] = 1
base = 1
for i, c in enumerate(S, start=1):
if c != '?':
d = int(c) * base % 13
for r in range(13):
c = (d + r) % 13
dp[i][c] +=... | import sys
fin = sys.stdin.readline
MOD = 10 ** 9 + 7
S = fin().rstrip("\n")[::-1]
dp = [[0] * 13 for _ in range(len(S) + 1)]
dp[0][0] = 1
base = 1
for i, c in enumerate(S, start=1):
if c != '?':
d = int(c) * base % 13
for r in range(13):
c = (d + r) % 13
dp[i... | p02960 |
# standart input
# n = int(input())
# a = list(map(int, input().split()))
N = 13
MOD = 10 ** 9 + 7
s = input()[::-1]
dp = [0 for i in range(N)]
nextdp = [0 for i in range(N)]
zeros = [0 for i in range(N)]
dp[0] = 1
for i, si in enumerate(s):
nextdp = zeros[:]
r = 10 ** i % N
for j in ... | # standart input
# n = int(input())
# a = list(map(int, input().split()))
N = 13
MOD = 10 ** 9 + 7
s = input()[::-1]
dp = [0 for i in range(N)]
nextdp = [0 for i in range(N)]
zeros = [0 for i in range(N)]
dp[0] = 1
r = 1
for i, si in enumerate(s):
nextdp = zeros[:]
for j in range(10):
... | p02960 |
import sys
input = sys.stdin.readline
def main():
S = input().strip()
N = len(S)
A = []
for c in S:
A.append(-1 if c == "?" else int(c))
dp = [[0]*13 for _ in range(N+1)]
dp[0][0] = 1
for n in range(N):
c = A[N-n-1]
for i in range(10):
m =... | import sys
input = sys.stdin.readline
def main():
S = input().strip()
N = len(S)
dp = [[0]*13 for _ in range(N+1)]
dp[0][0] = 1
m = 1
for n in range(N):
c = S[N-n-1]
if c == "?":
for i in range(10):
for j in range(13):
... | p02960 |
import sys
input = sys.stdin.readline
def main():
mod = 10**9+7
S = input().strip()
N = len(S)
dp = [[0]*13 for _ in range(N+1)]
dp[0][0] = 1
for n in range(N):
c = S[N-n-1]
m = 10**n % 13
for i in range(10):
if c == "?" or int(c) == i:
... | import sys
input = sys.stdin.readline
def main():
mod = 10**9+7
S = input().strip()
N = len(S)
dp = [[0]*13 for _ in range(N+1)]
dp[0][0] = 1
m = 1
for n in range(N):
c = S[N-n-1]
for i in range(10):
if c == "?" or int(c) == i:
for... | p02960 |
from heapq import heappush, heappop
import re
from collections import deque
import sys
import math
input = sys.stdin.readline
def int_raw():
return int(eval(input()))
def ss_raw():
return input().split()
def ints_raw():
return tuple(map(int, ss_raw()))
DIV = 10**9+7
def mod_... | from heapq import heappush, heappop
import re
from collections import deque
import sys
import math
input = sys.stdin.readline
def int_raw():
return int(eval(input()))
def ss_raw():
return input().split()
def ints_raw():
return tuple(map(int, ss_raw()))
DIV = 10**9+7
def mod_... | p02960 |
from collections import defaultdict
s = input()[::-1]
mod = 13
p = 10 ** 9 + 7
dic = defaultdict(int)
dic[0] = 1
for i in range(len(s)):
tmp = defaultdict(int)
if s[i] == "?":
for j in range(10):
d = j * 10 ** i % mod
for k, v in list(dic.items()):
t... | from collections import defaultdict
s = input()[::-1]
mod = 13
p = 10 ** 9 + 7
dic = defaultdict(int)
dic[0] = 1
for i in range(len(s)):
tmp = defaultdict(int)
if s[i] == "?":
for j in range(10):
d = j * pow(10, i, mod)
for k, v in list(dic.items()):
... | p02960 |
from collections import defaultdict
s = input()[::-1]
mod = 13
p = 10 ** 9 + 7
dic = defaultdict(int)
dic[0] = 1
for i in range(len(s)):
tmp = defaultdict(int)
if s[i] == "?":
for j in range(10):
d = j * pow(10, i, mod)
for k, v in list(dic.items()):
... | s = input()[::-1]
mod = 13
p = 10 ** 9 + 7
dp = [[0] * 13 for _ in range(len(s) + 1)]
dp[0][0] = 1
for i in range(len(s)):
if s[i] == "?":
for j in range(10):
d = j * pow(10, i, mod)
for k in range(13):
dp[i + 1][(k + d) % mod] += dp[i][k]
... | p02960 |
s = input()[::-1]
mod = 13
p = 10 ** 9 + 7
dp = [[0] * 13 for _ in range(len(s) + 1)]
dp[0][0] = 1
for i in range(len(s)):
if s[i] == "?":
for j in range(10):
d = j * pow(10, i, mod)
for k in range(13):
dp[i + 1][(k + d) % mod] += dp[i][k]
... | p = 10 ** 9 + 7
def main():
s = input()[::-1]
mod = 13
dp = [[0] * 13 for _ in range(len(s) + 1)]
dp[0][0] = 1
for i in range(len(s)):
if s[i] == "?":
for j in range(10):
d = j * pow(10, i, mod)
for k in range(13):
... | p02960 |
p = 10 ** 9 + 7
def main():
s = input()[::-1]
mod = 13
dp = [[0] * 13 for _ in range(len(s) + 1)]
dp[0][0] = 1
for i in range(len(s)):
if s[i] == "?":
for j in range(10):
d = j * pow(10, i, mod)
for k in range(13):
... | def main():
s = input()[::-1]
mod = 13
p = 10 ** 9 + 7
dp = [0] * mod
dp[0] = 1
for i, ss in enumerate(s):
tmp = [0] * mod
if ss == "?":
for j in range(10):
d = j * pow(10, i, mod)
for k in range(mod):
... | p02960 |
#!/usr/bin/python3
# -*- coding:utf-8 -*-
MAX = 10**9 + 7
def main():
s = input().strip()
p = 13
dp = [[0]*p for _ in range(len(s))]
if s[-1] == '?':
dp[0][:10] = [1] * 10
else:
dp[0][int(s[-1])] = 1
for i, c in enumerate(s[::-1][1:], 1):
if c == '?':
for c in range(10):
... | #!/usr/bin/python3
# -*- coding:utf-8 -*-
MAX = 10**9 + 7
def main():
s = input().strip()
p = 13
dp = [[0]*p for _ in range(len(s))]
remains = [[0]*p for _ in range(6)]
for i in range(6):
for j in range(p):
remains[i][j] = ((10 ** i) * j) % p
if s[-1] == '?':
dp[0][:10] =... | p02960 |
def main():
S = {i:s for i, s in enumerate(input()[::-1])}
MOD = 10**9 + 7
DP_table = {(i, j):0 for i in range(1, len(S)+1) for j in range(13)}
digit = S[0]
if digit == '?':
for i in range(10):
DP_table[(1, i)] = 1
else:
#DP_table[(1, int(digit))] = 1... | def main():
S = {i:s for i, s in enumerate(input()[::-1])}
MOD = 10**9 + 7
DP_table = [[0 for i in range(13)] for j in range(len(S))]
digit = S[0]
if digit == '?':
for i in range(10):
DP_table[0][i] = 1
else:
DP_table[0][int(digit)] = 1
mul... | p02960 |
# Python3 (3.4.3)
import sys
input = sys.stdin.readline
# -------------------------------------------------------------
# function
# -------------------------------------------------------------
# -------------------------------------------------------------
# main
# --------------------------------------... | # PyPy3 (2.4.0)
import sys
input = sys.stdin.readline
# -------------------------------------------------------------
# function
# -------------------------------------------------------------
# -------------------------------------------------------------
# main
# ----------------------------------------... | p02960 |
# 解説AC
MOD = 10**9 + 7
def main():
S = [s for s in input()[::-1]]
N = len(S)
dp = [[0]*13 for i in range(N+1)]
dp[0][0] = 1
for i in range(N):
for j in range(13):
if S[i] != "?":
a = int(S[i])
ne = ((a*pow(10, i, 13))+j) % 13
... | # 解説AC
MOD = 10**9 + 7
def main():
S = [s for s in input()[::-1]]
N = len(S)
dp = [[0]*13 for i in range(N+1)]
dp[0][0] = 1
d = 1
for i in range(N):
for j in range(13):
if S[i] != "?":
a = int(S[i])
ne = ((a*d)+j) % 13
... | p02960 |
d=[0]*13
d[0]=1
mul=1
mulmod=1
t=input()[::-1]
mod=10**9+7
zero=[0]*13
for c in t:
e=zero[:]
if c=="?":
for i in range(10):
for j in range(13):
k=(i*mul+j)%13
e[k]+=d[j]
e[k]%=mod
else:
for j in range(13):
... | d=[0]*13
d[0]=1
m=1
mmod=1
t=input()[::-1]
mod=10**9+7
zero=[0]*13
for c in t:
e=zero[:]
if c=="?":
for i in range(10):
for j in range(13):
k=(i*m+j)%13
e[k]+=d[j]
e[k]%=mod
else:
for j in range(13):
k... | p02960 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.