input
stringlengths
20
127k
target
stringlengths
20
119k
problem_id
stringlengths
6
6
import collections def friend_candidates(i, friends, visited=None): visited = set([i]) stack = list(friends[i]) while stack: v = stack.pop() if v in visited: continue stack.extend(friends[v]) visited.add(v) return visited N, M, K = lis...
def dfs(graph, start): visited = set() stack = [start] while stack: v = stack.pop() if v not in visited: stack.extend(graph[v]) visited.add(v) yield v def read_graph(nodes, edges): graph = [[] for _ in range(nodes)] for _ in range(...
p02762
import collections def dfs(graph, start): visited = set() stack = [start] while stack: v = stack.pop() if v not in visited: stack.extend(graph[v]) visited.add(v) yield v def read_graph(nodes, edges): graph = [set() for _ in range(no...
import collections def dfs(graph, start): visited = set() stack = [start] while stack: v = stack.pop() if v not in visited: stack.extend(graph[v]) visited.add(v) yield v def read_graph(nodes, edges): graph = [set() for _ in range(no...
p02762
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...
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...
p02762
N, M, K = list(map(int, input().split())) AB = [list(map(int, input().split())) for _ in range(M)] CD = [list(map(int, input().split())) for _ in range(K)] class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: ...
N, M, K = list(map(int, input().split())) AB = [list(map(int, input().split())) for _ in range(M)] CD = [list(map(int, input().split())) for _ in range(K)] class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: ...
p02762
#https://www.slideshare.net/chokudai/union-find-49066733 class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * self.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] ...
import sys input = lambda: sys.stdin.readline().rstrip() #https://www.slideshare.net/chokudai/union-find-49066733 class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * self.n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = s...
p02762
import sys input = lambda: sys.stdin.readline().rstrip() #https://www.slideshare.net/chokudai/union-find-49066733 class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * self.n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = s...
import sys input = lambda: sys.stdin.readline() #https://www.slideshare.net/chokudai/union-find-49066733 class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * self.n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(...
p02762
import sys input = lambda: sys.stdin.readline() #https://www.slideshare.net/chokudai/union-find-49066733 class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * self.n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(...
import sys input = lambda: sys.stdin.readline().rstrip() #https://www.slideshare.net/chokudai/union-find-49066733 class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * self.n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = s...
p02762
from collections import defaultdict n, m, k = list(map(int, input().split())) ns = defaultdict(set) bs = defaultdict(set) for i in range(m): a,b = list(map(int, input().split())) ns[a-1].add(b-1) ns[b-1].add(a-1) for i in range(k): c,d = list(map(int, input().split())) bs[c-1].add(d-1)...
import sys input = lambda : sys.stdin.readline().rstrip() sys.setrecursionlimit(max(1000, 10**9)) write = lambda x: sys.stdout.write(x+"\n") n,m,k = list(map(int, input().split())) class UnionFindTree: def __init__(self, n): self.n = n self.parent = list(range(n)) self.size = [1...
p02762
N,M,K=list(map(int, input().split())) par=list(range(N)) nums=[0 for _ in range(N)] rank=[0 for _ in range(N)] #実装 def find(x): if par[x]==x: 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 if ...
N,M,K=list(map(int, input().split())) par=[-1 for _ in range(N)] nums=[-1 for _ in range(N)] #実装 def find(x): if par[x]<0: return x else: par[x] = find(par[x]) return par[x] def size(x): return -par[find(x)] def unite(x,y): x,y=find(x),find(y) if x==y: ...
p02762
class UnionFind: def __init__(self, n): self.root = list(range(n)) self.size = [1] * (n) def find_root(self, x): root = self.root while root[x] != x: root[x] = root[root[x]] x = root[x] return x """ def find_root(self, x): ...
class UnionFind: def __init__(self, n): self.root = list(range(n)) self.size = [1] * (n) def find_root(self, x): root = self.root while root[x] != x: root[x] = root[root[x]] x = root[x] return x """ def find_root(self, x): ...
p02762
# input N, M, K = list(map(int, input().split())) Frends = [[] for _ in range(N)] Fs = [[] for _ in range(N)] for _ in range(M): a, b = list(map(int, input().split())) a -= 1 b -= 1 Frends[a].append(b) Frends[b].append(a) if a < b: Fs[a].append(b) else: Fs[b].a...
# input N, M, K = list(map(int, input().split())) Friends = [set() for _ in range(N)] for _ in range(M): a, b = list(map(int, input().split())) a -= 1 b -= 1 Friends[a].add(b) Friends[b].add(a) Blocked = [set() for _ in range(N)] for _ in range(K): c, d = list(map(int, input().spl...
p02762
from collections import defaultdict, deque n, m, k = list(map(int, input().split())) friends = defaultdict(list) block = defaultdict(list) for _ in range(m): a, b = list(map(int, input().split())) friends[a].append(b) friends[b].append(a) for _ in range(k): c, d = list(map(int, input().split())...
from collections import defaultdict, deque n, m, k = list(map(int, input().split())) friends = defaultdict(set) block = defaultdict(set) for _ in range(m): a, b = list(map(int, input().split())) friends[a].add(b) friends[b].add(a) for _ in range(k): c, d = list(map(int, input().split())) b...
p02762
#n: #m: #k: n,m,k=list(map(int,input().split())) friend={} block={} result=[] for i in range(1,n+1): friend[i]=set([]) block[i]=set([]) result.append(0) for _ in range(m): a,b=list(map(int,input().split())) friend[a].add(b) friend[b].add(a) for _ in range(k): a,b=list(map...
#n: #m: #k: n,m,k=list(map(int,input().split())) friend={} block={} result=[] friendGroup=[] for i in range(1,n+1): friend[i]=set([]) block[i]=set([]) result.append(0) friendGroup.append(0) for _ in range(m): a,b=list(map(int,input().split())) friend[a].add(b) friend[b].add...
p02762
#n: #m: #k: n,m,k=list(map(int,input().split())) friend={} block={} result=[] friendGroup=[] for i in range(1,n+1): friend[i]=set([]) block[i]=set([]) result.append(0) friendGroup.append(0) for _ in range(m): a,b=list(map(int,input().split())) friend[a].add(b) friend[b].add...
#n: #m: #k: n,m,k=list(map(int,input().split())) friend={} block={} #resultは、ある人が何人の友達候補を持つかを保持する。 result=[] #friendGroupは、ある人が何グループに所属するかを保持する。 friendGroup=[] #初期化 for i in range(1,n+1): friend[i]=set([]) block[i]=set([]) result.append(0) friendGroup.append(0) #友達関係を作る for _ in r...
p02762
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]) ret...
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]) ret...
p02762
from collections import defaultdict def answer(): n, m, k = list(map(int, input().split())) friend = defaultdict(set) block = defaultdict(set) group = defaultdict(set) cand = defaultdict(set) uft = defaultdict(int) for i in range(m): a, b = list(map(int, input().split())) ...
from collections import defaultdict import sys sys.setrecursionlimit(100000) def root(uft, x): if uft[x] == x: return x uft[x] = root(uft, uft[x]) return uft[x] def union(uft, x, y): x = root(uft, x) y = root(uft, y) if x == y: return uft[x] = y def answer(): ...
p02762
def warshall_floyd(d): #d[i][j]: iからjへの最短距離 for k in range(n): for i in range(n): for j in range(n): d[i][j] = min(d[i][j],d[i][k] + d[k][j]) return d INF = 999999 n, m, k = list(map(int, input().split())) ab = [[INF]*n for _ in range(n)] for _ in range(m): ...
n, m, k = list(map(int, input().split())) friend = [{i+1} for i in range(n)] exclude = [set() for _ in range(n)] # print(exclude) for _ in range(m): a,b = list(map(int, input().split())) exclude[a-1].add(b) exclude[b-1].add(a) ai, bi = -1 , -1 for i, f in enumerate(friend): if a in f: ai = i if ...
p02762
from collections import defaultdict 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[x]) return self.parents[x] def un...
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[x]) return self.parents[x] def union(self, x, y): x = self.find...
p02762
import sys read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines class UnionFind_size: def __init__(self, n): # 初期状態は全要素が根なので、親は自分自身 self.par = [i for i in range(n+1)] # 集団の要素数(size)を格納する(初期は全て1) self.size = [1] * (n+1) # 根を検索する関数 ...
import sys read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines def main(): N,M,K = list(map(int, readline().split())) F = [[] for _ in range(N+1)] for _ in range(M): a,b = list(map(int, readline().split())) F[a].append(b) F[b].append(a)...
p02762
N, M, K = 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 s...
N, M, K = 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 s...
p02762
N, M, K = 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 s...
N, M, K = 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...
p02762
from heapq import heappush, heappop, heapify from collections import deque, defaultdict, Counter import itertools from itertools import permutations, combinations, accumulate import sys import bisect import string import math import time def I(): return int(input()) def MI(): return map(int, input().s...
from heapq import heappush, heappop, heapify from collections import deque, defaultdict, Counter import itertools from itertools import permutations, combinations, accumulate import sys import bisect import string import math import time def I(): return int(input()) def MI(): return map(int, input().s...
p02762
from collections import deque q = deque([]) def bfs(i): t = [] q.append(i) c = [0] * n u = q.popleft() if G[u] != 0: for j in G[u]: if not c[j] != 0: q.append(j) c[u] = 1 while len(q) != 0: u = q.popleft() c[u] = 1 ...
def get_root(s): if s != root[s]: root[s] = get_root(root[s]) return root[s] return s def unite(s, t): root_s = get_root(s) root_t = get_root(t) if not root_s == root_t: if rank[s] == rank[t]: root[root_t] = root_s rank[root_s] += 1 ...
p02762
class union_find(): def __init__(self, n): self.par = [i for i in range(n + 1)] self.rank = [1 for i in range(n + 1)] self.size = [1 for i in range(n + 1)] def find(self, c): if self.par[c] == c: return c ret = self.find(self.par[c]) self....
class union_find(): __slots__ = ["par", "rank", "size"] def __init__(self, n): self.par = [i for i in range(n + 1)] self.rank = [1 for i in range(n + 1)] self.size = [1 for i in range(n + 1)] def find(self, c): if self.par[c] == c: return c re...
p02762
class UnionFind(): def __init__(self, n): self.par = [i for i in range(n)] self.size = [1 for _ in range(n)] def find(self, x): if self.par[x] != x: self.par[x] = self.find(self.par[x]) return self.par[x] def unite(self, x, y): xroot = sel...
class UnionFind(): def __init__(self, n): self.par = [i for i in range(n)] self.size = [1 for _ in range(n)] def find(self, x): if self.par[x] != x: self.par[x] = self.find(self.par[x]) return self.par[x] def unite(self, x, y): xroot = sel...
p02762
import sys sys.setrecursionlimit(10**5) def bfs(ind): kouho=friends[ind] for i in kouho: if sans[i]!=-1: continue sans[i]=sans[ind] l[sans[ind]].append(i) bfs(i) n,m,k=list(map(int,input().split())) f=[list(map(int,input().split())) for _ in range(m)] frie...
import sys sys.setrecursionlimit(10**5) total=0 def bfs(ind,num): kouho=friends[ind] for i in kouho: if sans[i]!=-1: continue num+=1 sans[i]=sans[ind] l[sans[ind]].add(i) num+=bfs(i,0) return num n,m,k=list(map(int,input().split())) f=[...
p02762
import sys line = sys.stdin.readline().strip() n,m,k = list(map(int, line.split())) roots = list(range(n)) linked = [0]*n for i in range(m): line = sys.stdin.readline().strip() a,b = list(map(int, line.split())) c, d = a-1,b-1 linked[c] += 1 linked[d] += 1 while c != roots[c]: ...
import sys line = sys.stdin.readline().strip() n,m,k = list(map(int, line.split())) roots = [-1]*n linked = [0]*n def find_root(x): if roots[x] < 0: return x else: roots[x] = find_root(roots[x]) return roots[x] def unite(x, y): x = find_root(x) y = find_root(y) ...
p02762
from collections import * from itertools import * from bisect import * from heapq import * import copy #import math #N=int(input()) #X,Y=map(int,input().split()) #S=list(map(int,input().split())) #S=[list(map(int,input().split())) for i in range(N)] class UnionFind(): def __init__(self, n): se...
from collections import * from itertools import * from bisect import * from heapq import * import copy #import math #N=int(input()) #X,Y=map(int,input().split()) #S=list(map(int,input().split())) #S=[list(map(int,input().split())) for i in range(N)] class UnionFind(): def __init__(self, n): se...
p02762
class Union_Find(): 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 Union_Find(): 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):...
p02762
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): ...
p02762
import sys input = sys.stdin.readline sys.setrecursionlimit(10**9) n, m, k = map(int, input().split()) f = [[] for _ in range(n)] b = [[] for _ in range(n)] for _ in range(m): a, _b = map(int, input().split()) f[a-1].append(_b-1) f[_b-1].append(a-1) for _ in range(k): a, _b = map(int, input...
import sys input = sys.stdin.readline sys.setrecursionlimit(10**9) #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: return False else: ...
p02762
class UnionFind: def __init__(self, n): self.parent = [ -1 for _ in range(n) ] self._size = n def unite(self, x, y): x, y = self.root(x), self.root(y) if x != y: if self.parent[y] < self.parent[x]: x, y = y, x self.parent[x] += s...
class UnionFind: def __init__(self, n): self.parent = [ -1 for _ in range(n) ] self._size = n def unite(self, x, y): x, y = self.root(x), self.root(y) if x != y: if self.parent[y] < self.parent[x]: x, y = y, x self.parent[x] += s...
p02762
class UnionFind(): ''' UnionFindでグラフの状態を管理する ''' def __init__(self, n): self.n = n self.root = [-1]*(n) self.rnk = [0]*(n) def find_root(self, x): ''' ルートのノードを見つける ''' if(self.root[x] < 0): return x else: ...
class UnionFind(): ''' UnionFindでグラフの状態を管理する ''' def __init__(self, n): self.n = n self.root = [-1]*(n) self.rnk = [0]*(n) def find_root(self, x): ''' ルートのノードを見つける ''' if(self.root[x] < 0): return x else: ...
p02762
N, M ,K= list(map(int, input().split())) a=[0]*M b=[0]*M c=[0]*K d=[0]*K for i in range(M): a[i], b[i] = list(map(int, input().split())) for i in range(K): c[i], d[i] = list(map(int, input().split())) l=list(range(N+1)) r=[0]*(N+1) la=N di=[[]for _ in range(N+1)] def find(x): if l[x]==x: ...
N, M ,K= list(map(int, input().split())) a=[0]*M b=[0]*M c=[0]*K d=[0]*K for i in range(M): a[i], b[i] = list(map(int, input().split())) for i in range(K): c[i], d[i] = list(map(int, input().split())) l=list(range(N+1)) r=[0]*(N+1) size=[1]*(N+1) la=N di=[[]for _ in range(N+1)] def find(x): ...
p02762
N, M, K=list(map(int, input().split())) class UnionFindTree: def __init__(self, n): self.parent = [-1 for _ in range(n)] def root(self, x): p, seq = self.parent[x], list() while p >= 0: seq.append(x) x, p = p, self.parent[p] for c in seq: self...
def main(): N, M, K=list(map(int, input().split())) friend_or_block = [0 for _ in range(N)] friends_chain = UnionFindTree(N) for _ in range(M): a, b = list(map(int, input().split())) a, b = a-1, b-1 friends_chain.unite(a, b) friend_or_block[a] += 1 frie...
p02762
def main(): N, M, K=list(map(int, input().split())) friend_or_block = [0 for _ in range(N)] friends_chain = UnionFindTree(N) for _ in range(M): a, b = list(map(int, input().split())) a, b = a-1, b-1 friends_chain.unite(a, b) friend_or_block[a] += 1 frie...
def main(): N, M, K=list(map(int, input().split())) friend_or_block = [0] * N friends_chain = UnionFindTree(N) for _ in range(M): a, b = list(map(int, input().split())) a, b = a-1, b-1 friends_chain.unite(a, b) friend_or_block[a] += 1 friend_or_block[b]...
p02762
import collections n,m,k = list(map(int,input().split(' '))) friendships = [list(map(int, input().split(' '))) for _ in range(m)] disses = [list(map(int, input().split(' '))) for _ in range(k)] parent = {i:i for i in range(0, n+1)} sizes = {i:1 for i in range(0, n+1)} hd = collections.Counter() adj = c...
import collections n,m,k = list(map(int,input().split(' '))) friendships = [list(map(int, input().split(' '))) for _ in range(m)] disses = [list(map(int, input().split(' '))) for _ in range(k)] parent = [i for i in range(0, n+1)] sizes = [1 for i in range(0, n+1)] hd = collections.Counter() adj = collect...
p02762
import collections n,m,k = list(map(int,input().split(' '))) friendships = [list(map(int, input().split(' '))) for _ in range(m)] disses = [list(map(int, input().split(' '))) for _ in range(k)] parent = [i for i in range(0, n+1)] sizes = [1 for i in range(0, n+1)] hd = [1 for i in range(0, n + 1)] def ...
import collections n,m,k = list(map(int,input().split(' '))) parent = list(range(0, n+1)) sizes = [1] * (n + 1) hd = [1] * (n + 1) def f(parent, u): if parent[u] != u: parent[u] = f(parent, parent[u]) return parent[u] for a,b in [list(map(int, input().split(' '))) for _ in range(m)]: pa,pb = f...
p02762
import collections import sys n,m,k = list(map(int,sys.stdin.readline().split())) parent = list(range(0, n+1)) sizes = [1] * (n + 1) hd = [1] * (n + 1) def f(parent, u): if parent[u] != u: parent[u] = f(parent, parent[u]) return parent[u] for a,b in [list(map(int,sys.stdin.readline().split())) for _ in...
import collections import sys n,m,k = list(map(int,sys.stdin.readline().split())) parent = list(range(0, n+1)) sizes = [1] * (n + 1) hd = [1] * (n + 1) def f(parent, u): if parent[u] != u: parent[u] = f(parent, parent[u]) return parent[u] for _ in range(m): a,b = list(map(int,sys.stdin.readline().spl...
p02762
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): ...
p02762
n,m,k=map(int,input().split()) par=[-1]*n # this par & rank are initialized lists def find(x): global par if par[x]<0: return x else: par[x]=find(par[x]) return par[x] def unite(x,y): x,y=find(x),find(y) global par if x==y:return False else: ...
n,m,k=map(int,input().split()) par=[-1]*n # this par & rank are initialized lists def find(x): global par if par[x]<0: return x else: par[x]=find(par[x]) return par[x] def unite(x,y): x,y=find(x),find(y) global par if x==y:return False else: ...
p02762
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): ...
def find(x): if par[x]<0: return x else: par[x]=find(par[x]) return par[x] def unite(x,y): x,y=find(x),find(y) if x!=y: if x>y: x,y=y,x par[x]+=par[y] par[y]=x def same(x,y): return find(x)==find(y) def size(x): return-par[find(x)] ...
p02762
import sys input = sys.stdin.buffer.readline class UnionFind: __slots__ = ["data"] def __init__(self, n=0): self.data = [-1]*(n+1) def root(self, x): if self.data[x] < 0: return x self.data[x] = self.root(self.data[x]) return self.data[x] ...
import sys input = sys.stdin.buffer.readline class UnionFind: __slots__ = ["data"] def __init__(self, n=0): self.data = [-1]*(n+1) def root(self, x): if self.data[x] < 0: return x self.data[x] = self.root(self.data[x]) return self.data[x] ...
p02762
import sys input = sys.stdin.buffer.readline class UnionFind: __slots__ = ["data"] def __init__(self, n=0): self.data = [-1]*(n+1) def root(self, x): if self.data[x] < 0: return x self.data[x] = self.root(self.data[x]) return self.data[x] ...
import sys input = sys.stdin.buffer.readline class UnionFind: __slots__ = ["data"] def __init__(self, n=0): self.data = [-1]*(n+1) def root(self, x): if self.data[x] < 0: return x self.data[x] = self.root(self.data[x]) return self.data[x] ...
p02762
from collections import deque n,m,k=list(map(int,input().split())) flag=[0]*(n+1) friend=[[] for i in range(n+1)] block=[[] for i in range(n+1)] for i in range(m): a,b=list(map(int, input().split())) friend[a].append(b) friend[b].append(a) for i in range(k): a, b = list(map(int, input().spli...
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): ...
p02762
def main(): N,M,K=list(map(int,input().split())) G=[[] for i in range(N)] for i in range(M): a,b=list(map(int,input().split())) G[a-1].append(b-1) G[b-1].append(a-1) C=[] D=[] for i in range(K): c,d=list(map(int,input().split())) C.append(...
def main(): N,M,K=list(map(int,input().split())) G=[[] for i in range(N)] for i in range(M): a,b=list(map(int,input().split())) G[a-1].append(b-1) G[b-1].append(a-1) B=[[] for i in range(N)] for i in range(K): c,d=list(map(int,input().split())) ...
p02762
import sys from collections import defaultdict, Counter class UnionFind(object): 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 from collections import defaultdict, Counter class UnionFind(object): 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]...
p02762
def main(): 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]) ...
def main(): 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]) ...
p02762
import bisect import sys sys.setrecursionlimit(10 ** 6) # from decorator import stop_watch # # # @stop_watch def solve(N, M, K, ABs, CDs): friend_map = [[] for _ in range(N + 1)] for a, b in ABs: friend_map[a].append(b) friend_map[b].append(a) block_map = [[] for _ in range(N +...
import sys sys.setrecursionlimit(10 ** 6) # from decorator import stop_watch # # # @stop_watch def solve(N, M, K, ABs, CDs): friend_map = [[] for _ in range(N + 1)] for a, b in ABs: friend_map[a].append(b) friend_map[b].append(a) block_map = [[] for _ in range(N + 1)] for c...
p02762
from collections import defaultdict class UnionFind: def __init__(self, nodes): self.sets = [None for n in nodes] self.sizes = [1 for n in nodes] def find(self, a): a -= 1 curr = a path = [] while self.sets[curr] is not None: path.appen...
class UnionFind: def __init__(self, nodes): self.sets = [None] * len(nodes) self.sizes = [1] * len(nodes) def find(self, a): curr = a path = [] while self.sets[curr] is not None: path.append(curr) curr = self.sets[curr] for no...
p02762
import sys #input=sys.stdin.readline class UnionFind(): def __init__(self,n): self.n=n self.parent=list(range(n)) self.size=[1]*n #print(self.size, end=" self.size \n") def find(self,x): if self.parent[x]==x: return x else: self.parent[x]=self.find(self.parent[x]) ...
import sys input=sys.stdin.readline class UnionFind(): def __init__(self,n): self.n=n self.parent=list(range(n)) self.size=[1]*n #print(self.size, end=" self.size \n") def find(self,x): if self.parent[x]==x: return x else: self.parent[x]=self.find(self.parent[x]) ...
p02762
from collections import deque n, m, k = list(map(int, input().split())) f = [set() for _ in range(n)] l = [set() for _ in range(n)] for _ in range(m): a, b = list(map(int, input().split())) a, b = a-1, b-1 f[a].add(b) f[b].add(a) for _ in range(k): c, d = list(map(int, input().split())) ...
class UnionFind(): def __init__(self, n): self.parents = [-1] * n def root(self, x): if self.parents[x] < 0: return x else: return self.root(self.parents[x]) def unite(self, x, y): x, y = self.root(x), self.root(y) if x ==...
p02762
import sys def input(): return sys.stdin.readline().strip() sys.setrecursionlimit(10 ** 9) class UnionFind: # 初期化 def __init__(self, n_nodes): self.n_nodes = n_nodes # 親要素のノード番号を格納する.初めは全て親ノード. # self.parent[x] == x の時,そのノードは根. self.parent = [i for i in ...
import sys def input(): return sys.stdin.readline().strip() sys.setrecursionlimit(10 ** 9) class UnionFind: # 初期化 def __init__(self, n_nodes): self.n_nodes = n_nodes # 親要素のノード番号を格納する.初めは全て親ノード. # self.parent[x] == x の時,そのノードは根. self.parent = [i for i in ...
p02762
from collections import deque from copy import deepcopy N, M, K = list(map(int, input().split())) SNS = [[0 for _ in range(N+1)] for __ in range(N+1)] Follow = [deque() for __ in range(N+1)] for _ in range(M): a, b = list(map(int, input().split())) SNS[a][b] = 1 SNS[b][a] = 1 Follow[a].append(b) Fol...
from collections import deque from copy import deepcopy 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]) ...
p02762
while True: n=eval(input()) if n==0: break dp=[[0 for i in range(n)]for j in range(n)] flag0=0 for i in range(n): j=0 for c in input(): if c == ".": flag0=1 dp[i][j]=1 j+=1 if flag0==0: print(0)...
while True: n=eval(input()) if n==0: break dp=[[0 for i in range(n)]for j in range(n)] flag0=0 for i in range(n): j=0 for c in input(): if c == ".": flag0=1 dp[i][j]=1 j+=1 if flag0==0: print(0)...
p00092
while True: n=eval(input()) if n==0: break dp=[[0 for i in range(n)]for j in range(n)] flag0=0 for i in range(n): j=0 for c in input(): if c == ".": dp[i][j]=1 flag0=1 j+=1 if flag0==0: print(0)...
while True: n=eval(input()) if n==0: break dp=[[0 for i in range(n)]for j in range(n)] for i in range(n): j=0 for c in input(): if c == ".": dp[i][j]=1 j+=1 for i in range(1,n): for j in range(1,n): if d...
p00092
while True: n=eval(input()) if n==0: break dp=[[0 for i in range(n)]for j in range(n)] for i in range(n): j=0 for c in input(): if c == ".": dp[i][j]=1 j+=1 for i in range(1,n): for j in range(1,n): if d...
while True: n=eval(input()) if n==0: break dp=[[0 for i in range(n+1)]for j in range(n+1)] for i in range(1,n+1): j=0 line=" "+input() for c in line: if c == ".": dp[i][j]=min(dp[i-1][j],dp[i][j-1],dp[i-1][j-1])+1 j+=1 ...
p00092
while 1: n = int(input()) if n == 0: break dp = [[0] * n for i in range(n)] r = 0 for i in range(n): for j, ch in enumerate(input()): if ch == '*': continue dp[i][j] = 1 if i >= 1 or j >= 1: for k in rang...
while 1: n = int(input()) if n == 0: break dp = [[0] * n for i in range(n)] r = 0 for i in range(n): for j, ch in enumerate(input()): if ch == '*': continue dp[i][j] = 1 if i >= 1 or j >= 1: dp[i][j] += m...
p00092
while True: n = int(input()) if n == 0: break field = [input() for i in range(n)] flag = 0 for len in range(n,-1,-1): for row in range(n-len+1): for sp in range(n-len+1): if field[row][sp:sp+len] == "."*len: for i in range(1,len): if field[row+i][sp:sp+len] != "."*len: if len ...
while True: n = int(input()) if n == 0: break field = [input() for i in range(n)] large = [[0] * (n + 1) for i in range(n + 1)] mx = 0 for i in range(n): for j in range(n): if field[i][j] == '.': large[i][j] = min(large[i][j - 1], large[i - 1][j], lar...
p00092
#!/usr/bin/env python from sys import stdin, exit def main(readline=stdin.readline): while 1: n = int(readline()) if not n: exit() pre = [0] * (n+1) now = [0] * (n+1) maximum = 0 for _ in range(n): for x, c in enumerate(rea...
#!/usr/bin/env python from sys import stdin, exit def main(readline=stdin.readline): while 1: n = int(readline()) if not n: exit() pre = [0] * (n+1) now = [0] * (n+1) maximum = 0 for _ in range(n): for x, c in enumerate(rea...
p00092
import sys while 1: n=eval(input()) if n==0:break A=[input() for i in range(n)] B=[] c=0 for i in range(n): if c==0 and '1' in A[i]: c=1 B.append(int(A[i].replace('.','1').replace('*','0'),2)) C=B[:] for i in range(1,n): for j in range(n-i): B[j]&=B[j+1] if '1'*(i...
import sys while 1: n=eval(input()) if n==0:break A=[input() for i in range(n)] B=[] c=0 for i in range(n): if c==0 and '1' in A[i]: c=1 B.append(int(A[i].replace('.','1').replace('*','0'),2)) C=B[:] for i in range(1,n): for j in range(n-i): B[j]&=B[j+1] if '1'*(i...
p00092
import sys while 1: n=eval(input()) if n==0:break A=[input() for i in range(n)] B=[] c=0 for i in range(n): if c==0 and '1' in A[i]: c=1 B.append(int(A[i].replace('.','1').replace('*','0'),2)) C=B[:] for i in range(1,n): for j in range(n-i): B[j]&=B[j+1] if '1'*(i...
import sys while 1: n=eval(input()) if n==0:break A=[' '*(n+1)]+[' '+input() for i in range(n)] M=[[0]*(n+1) for i in range(n+1)] c=0 for i in range(1,n+1): for j in range(1,n+1): if A[i][j]=='.': M[i][j]=min(M[i-1][j-1],M[i-1][j],M[i][j-1])+1 c=max(c,max(M[i])) print(c)
p00092
import sys while 1: n=eval(input()) if n==0:break A=[' '*(n+1)]+[' '+input() for i in range(n)] M=[[0]*(n+1) for i in range(n+1)] c=0 for i in range(1,n+1): for j in range(1,n+1): if A[i][j]=='.': M[i][j]=min(M[i-1][j-1],M[i-1][j],M[i][j-1])+1 c=max(c,max(M[i])) print(c)
import sys while 1: n=eval(input())+1 if n==1:break A=[''*n]+[' '+input() for i in range(1,n)] M=[[0]*n for i in range(n)] for i in range(1,n): for j in range(1,n): if A[i][j]=='.': M[i][j]=min(M[i-1][j-1],M[i-1][j],M[i][j-1])+1 print(max(list(map(max,M))))
p00092
import sys while 1: n=eval(input())+1 if n==1:break A=[''*n]+[' '+input() for i in range(1,n)] M=[[0]*n for i in range(n)] for i in range(1,n): for j in range(1,n): if A[i][j]=='.': M[i][j]=min(M[i-1][j-1],M[i-1][j],M[i][j-1])+1 print(max(list(map(max,M))))
import sys while 1: n=eval(input())+1 if n==1:break N=list(range(n)) N1=N[1:] A=[''*n]+[' '+input() for i in N1] M=[[0]*n for i in N] y=M[0] for i in N1: x=[0]*n B=A[i] for j in N1: if B[j]=='.': x[j]=min(y[j-1],y[j],x[j-1])+1 M[i]=x y=x print(max(list(map(m...
p00092
import sys while 1: n = eval(input()) + 1 if n==1: break N = list(range(n)) M = [[0] * n for i in N] y = M[0] for i in N[1:]: x = [0] * n A = ' ' + input() for j in N[1:]: if A[j] == '.': x[j] = min(y[j-1],y[j],x[j-1]) + 1 M[i] = x y = x print(max(list(map(max,M))...
import sys while 1: n = eval(input()) + 1 if n==1: break N = list(range(n)) M = [0] * n y = [0] * n for i in N[1:]: x = [0] * n A = ' ' + input() for j in N[1:]: if A[j] == '.': x[j] = min(y[j-1],y[j],x[j-1]) + 1 M[i]=max(x) y = x print(max(M))
p00092
while True: try: hand = list(map(int, input().split(","))) kind = list(set(hand)) rank =[] for card in kind: rank.append(hand.count(card)) rank.sort() rank.reverse() if rank[0] == 4: print("four card") elif ra...
while True: try: hand = sorted(map(int, input().split(","))) kind = len(set(hand)) ma = max([hand.count(i) for i in hand]) if kind == 4: print("one pair") elif kind == 3: if ma == 2: pr...
p00038
def readdata(): x = [] try: while True: x.append(list(map(int,input().split(",")))) except: return x def checkhand2(hand): tmp = [(e+11)%13 for e in hand] x1=sorted(list(set(tmp))) x2=[] for e in x1: x2.append(tmp.count(e)) return x1,x2 def is...
prize=["null","one pair","two pair","three card", "straight","full house","four card"] try: while True: hand = list(map(int,input().split(","))) x = sorted(list(set(hand))) a = len(x) b = max([hand.count(e) for e in x]) if b==4: p=6 elif b==3: ...
p00038
import sys input=sys.stdin.readline def main(): N = int(eval(input())) S = input().strip() E = [0]*N W = [0]*N for i in range(1,N): E[i] = E[i-1] if S[i-1] == "W": E[i] += 1 for i in range(N-2,-1,-1): W[i] = W[i+1] if S[i+1] == "E"...
import sys input=sys.stdin.readline def main(): N = int(eval(input())) S = input().strip() E = [0]*N e = 0 for i in range(0,N): E[i] = e if S[i] == "W": e += 1 W = [0]*N w = 0 for i in range(N-1,-1,-1): W[i] = w if S[i] =...
p03339
N = int(eval(input())) S = list(input().strip()) ans = N for i in range(N): cnt = len([1 for d in S[: max(i-1, 0)] if not d == 'E']) + len([1 for d in S[min(i+1, N-1):] if not d == 'W']) if ans > cnt: ans = cnt print(ans)
N = int(eval(input())) S = list(input().strip()) ans = len([1 for d in S[1:] if d == 'E']) cnt = ans for i in range(1, N): if S[i] == 'E': cnt -= 1 if S[i-1] == 'W': cnt += 1 if ans > cnt: ans = cnt print(ans)
p03339
def main(): n = int(input().strip()) s = input().strip() m = 300000 for i in range(0, len(s)): c = len([x for x in s[0:i] if x == 'W']) c += len([x for x in s[i+1:] if x == 'E']) m = min(m, c) print(m) if __name__ == '__main__': main()
def main(): n = int(input().strip()) s = input().strip() right_e = len([x for x in s if x == 'E']) left_w = 0 m = right_e - 1 if s[0] == 'E' else right_e for i in range(1, len(s)): if s[i-1] == 'E': right_e -= 1 else: left_w += 1 ...
p03339
N = int(input()) S = list(input()) ans = N for i in range(N): left = S[:i].count('W') right = S[i:].count('E') ans = min(ans, left+right) print(ans)
N = int(input()) S = list(input()) ans = N min_ans = N for i in range(N): if i == 0: ans = S[1:].count('E') min_ans = ans else: if S[i-1] == 'W': ans += 1 if S[i] == 'E': ans -= 1 min_ans = min(min_ans, ans) print(min_ans)
p03339
# author: kagemeka # created: 2019-11-07 23:33:56(JST) ## internal modules import sys import collections # import math # import string # import bisect # import re # import itertools # import statistics # import functools # import operator ## external module...
import sys n = int(sys.stdin.readline().rstrip()) s = sys.stdin.readline().rstrip() def main(): l = 0 r = s.count('E') res = r for i in range(n): cur = s[i] if cur == 'E': r -= 1 res = min(res, l + r) if cur == 'W': l += 1 ret...
p03339
import sys n = int(sys.stdin.readline().rstrip()) s = sys.stdin.readline().rstrip() def main(): l = 0 r = s.count('E') res = r for i in range(n): cur = s[i] if cur == 'E': r -= 1 res = min(res, l + r) if cur == 'W': l += 1 ret...
import sys n, s = sys.stdin.read().split() def main(): cnt = s.count('E') cur = 'E' res = [cnt] for c in s: if cur == 'W': cnt += 1 if c == 'E': cnt -= 1 res.append(cnt) cur = c print((min(res))) if __name__ == '__main__': main()
p03339
import sys input = sys.stdin.readline #a = input().rstrip() 文字の時rstrip()で改行削除 N = int(eval(input())) S = input().rstrip() min = float("inf") for i in range(N): cnt = 0 for j in range(i): if S[j] != "E": cnt += 1 for j in range(i+1, N): if S[j] != "W": ...
import sys input = sys.stdin.readline N = int(eval(input())) S = input().rstrip() W = [0] * N E = [0] * N cnt = 0 for i in range(1, N): if S[i-1] == "W": cnt += 1 W[i] = cnt else: W[i] = cnt cnt = 0 for i in reversed(list(range(N-1))): if S[i+1] == "E": cn...
p03339
#!/usr/bin/env python import collections E = "E" # -> W = "W" # <- n = int(input().rstrip()) line = input().rstrip() min_change_count = float('inf') for i in range(n): w_line = line[0:i] e_line = line[i+1:] if i < n else [] change_count = 0 change_count += collections.Counter(...
E = "E" # -> W = "W" # <- n = int(input().rstrip()) line = input().rstrip() bef_change_count = line[1:].count(E) min_change_count = bef_change_count for leader_i in range(1, n): change_count = bef_change_count if line[leader_i] == E and line[leader_i - 1] == E: change_count -= 1 ...
p03339
# coding: utf-8 def main(): N = int(eval(input())) S = input()[:N] min_count = N for i in range(N): count = S[:i].count('W') + S[i+1:].count('E') if count < min_count: min_count = count print(min_count) if __name__ == '__main__': main()
# coding: utf-8 def main(): N = int(eval(input())) S = input()[:N] min_count = count = S[1:].count('E') for i in range(1, N): if S[i - 1] == 'W': count += 1 if S[i] == 'E': count -= 1 if count < min_count: min_count =...
p03339
# -*- coding: utf-8 -*- #---------- N = int(input().strip()) A_list = list(input().strip()) #---------- count = N for i_leader in range(len(A_list)): ahead_list = A_list[:i_leader] behind_list = A_list[i_leader+1:] turn_W_to_E = len( [ i for i in ahead_list if i == "W"] ) turn_E_to_W...
# -*- coding: utf-8 -*- #---------- N = int(input().strip()) A_list = list(input().strip()) #---------- W_member_sum_0_to_N = [0]*len(A_list) E_member_sum_N_to_0 = [0]*len(A_list) cnt=0 for i in range(len(A_list)): if A_list[i] == "W": cnt += 1 W_member_sum_0_to_N[i] += cnt cnt=0 for ...
p03339
n = eval(input()) l = list(input()) ans = n for i in range(0, n): tmp = l[:i].count("W") + l[i+1:].count("E") if tmp < ans: ans = tmp print(ans)
n = eval(input()) l = list(input()) left = 0 right = l.count("E") count = [] for i in range(0, n): if l[i] == "E": right -= 1 count += [left + right] if l[i] == "W": left += 1 print(min(count))
p03339
N = int(eval(input())) S = list(input().strip()) Is = [1 if s == 'W' else 0 for s in S] res = N for i in range(0,N): left = Is[:max(i - 1,0)] right = Is[min(i + 1,N - 1):] tmp = sum(left) + (len(right) - sum(right)) res = min(tmp,res) print(res)
N = int(eval(input())) S = list(input().strip()) cnt = S[1:].count('E') res = cnt for i in range(1,N): if S[i] == 'E': cnt -= 1 if S[i - 1] == "W": cnt += 1 res = min(res,cnt) print(res)
p03339
# -*- coding: utf-8 -*- import sys # ---------------------------------------------------------------- # Use Solve Function def solve(lines): N = int(lines.pop(0)) S = lines.pop(0) RS = S[::-1] S = 'x' + S RS = 'x' + RS slist = [] rslist = [] ss = 0 rss = 0 for i i...
# -*- coding: utf-8 -*- import sys # ---------------------------------------------------------------- # Use Solve Function def solve(lines): N = int(lines.pop(0)) S = lines.pop(0) RS = S[::-1] S = 'x' + S RS = 'x' + RS slist = [] rslist = [] ss = 0 rss = 0 for i i...
p03339
from sys import stdin def main(): N = int(stdin.readline().rstrip()) S = stdin.readline() count = 0 s1 = [] for i, x in enumerate(S): if x == 'W': count += 1 s1.append(count) count = 0 s2 = [] for i, x in enumerate(S[::-1]): if x == 'E...
from sys import stdin def main(): N = int(stdin.readline().rstrip()) S = stdin.readline().rstrip() spam1 = [0] for x in S[:-1]: spam1 += [spam1[-1] + 1 if x == 'W' else spam1[-1]] spam2 = [0] for x in S[::-1][:-1]: spam2 += [spam2[-1] + 1 if x == 'E' else spam2[-1]] ...
p03339
from sys import stdin import collections N = int(stdin.readline().rstrip()) S = stdin.readline().rstrip() people = [] for i in range(N): people.append(S[i]) tmp = collections.Counter(people[1:]) tmp2 = tmp['E'] tmp3 = collections.Counter(people[:N-1]) tmp4 = tmp['W'] minv = min(tmp2, tmp4) for...
from sys import stdin import collections N = int(stdin.readline().rstrip()) S = stdin.readline().rstrip() people = [] for i in range(N): people.append(S[i]) tmp = collections.Counter(people[1:]) num_return = tmp['E'] minv = num_return for i in range(1, N): if people[i] == 'E': num_...
p03339
N = int(input()) S = str(input()) opt_cnt = N for i in range(N): first = S[:i] second = S[i+1:] cnt = first.count('W') + second.count('E') if opt_cnt > cnt: opt_cnt = cnt print(opt_cnt)
def opt(N, S): if N == 1: return 0 else: split = int(N/2) return min(opt(split,S[:split])+S[split:].count('E'),S[:split].count('W')+opt(N - split,S[split:])) Num = int(input()) Str = str(input()) print((opt(Num, Str)))
p03339
N = int(input()) S = input() score_list = [] for i in range(N): E_num = S[i+1:].count('E') W_num = S[:i].count('W') score_list.append(E_num+W_num) print(min(score_list))
N = int(input()) S = input() num = S[1:].count('E') score_list = [] score_list.append(num) for i in range(N-1): if S[i]=='W': num+=1 if S[i+1]=='E': num-=1 score_list.append(num) print(min(score_list))
p03339
def main(): N, S = open(0).read().split() pre = S.count("E") ans = pre for Si in S: if pre < ans: ans = pre if Si == "E": pre -= 1 else: pre += 1 print((pre if pre < ans else ans)) return main()
def main(): N, S = open(0).read().split() pre = S.count("E") cur = pre for Si in S: if pre < cur: cur = pre if Si == "E": pre -= 1 else: pre += 1 print((pre if pre < cur else cur)) return main()
p03339
def 解(): iN = int(eval(input())) sS = input().rstrip() aE= [0]*iN #東向かされる人 aW= [0]*iN #西向かされる人 for i in range(1,iN): if sS[i-1] == "W": aE[i] = aE[i-1]+1 else: aE[i] = aE[i-1] iMinCost = aE[iN-1] for i in range(iN-2,-1,-1): if sS[...
def 解(): iN = int(eval(input())) sS = input().rstrip() aE= [0]*iN #東向かされる人 for i in range(1,iN): if sS[i-1] == "W": aE[i] = aE[i-1]+1 else: aE[i] = aE[i-1] iMinCost = aE[iN-1] iW = 0 #西向かされる人 for i in range(iN-2,-1,-1): if sS[i+1]...
p03339
#abc098c n=int(input()) s=input() res=float('inf') for i in range(len(s)): s1=s[:i] s2=s[i+1:] res=min(s1.count('W')+s2.count('E'),res) print(res)
#abc098c n=int(input()) s=input() cnt=s[1:].count('E') res=cnt for i in range(1,n): if s[i-1]=='W': cnt+=1 if s[i]=='E': cnt-=1 res=min(cnt,res) print(res)
p03339
def solve(): N = int(input()) S = input() # str min_need_change = N for i in range(N): count = 0 for j in range(0, i): if S[j] == 'W': count += 1 for j in range(i+1, N): if S[j] == 'E': count += 1 if coun...
def solve(): N = int(input()) S = input() # str count = 0 for i in range(1, N): # count for leader is the 1st person case if S[i] == 'E': count += 1 min_need_change = count for i in range(1,N): # count for leader is the 2nd, ... (N + 1)th person cases if S[i]...
p03339
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from heapq import heappush, heappop Q = int(readline()) query = (tuple(map(int,line.split())) for line in readlines()) # 適当に番兵を入れておく INF = 10**12 L = [INF] # -1倍していれる R = [INF] SL = 0; SR...
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from heapq import heappush, heappop Q = int(readline()) query = (tuple(map(int,line.split())) for line in readlines()) # 適当に番兵を入れておく INF = 10**12 L = [INF] # -1倍していれる R = [INF] SL = 0; SR...
p03040
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from heapq import heappush, heappop Q = int(readline()) query = (tuple(map(int,line.split())) for line in readlines()) # 適当に番兵を入れておく INF = 10**12 L = [INF] # -1倍していれる R = [INF] SL = 0; SR...
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from heapq import heappush, heappop Q = int(readline()) query = (tuple(map(int,line.split())) for line in readlines()) # 適当に番兵を入れておく INF = 10**12 L = [INF] # -1倍していれる R = [INF] sumL = 0; ...
p03040
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools import random sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return list(map(int, s...
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools import random sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return list(map(int, s...
p03040
import queue que_small=queue.PriorityQueue() small_sum=0 que_s_size=0 que_b_size=0 que_big=queue.PriorityQueue() big_sum=0 Q=int(eval(input())) ans = [] b_sum=0 for i in range(Q): q=input().split() if len(q)>1: a=int(q[1]) if que_s_size==0: que_small.put(-a) ...
import heapq spq = [] bpq = [] #que_small=queue.PriorityQueue() small_sum=0 que_s_size=0 que_b_size=0 #que_big=queue.PriorityQueue() big_sum=0 Q=int(eval(input())) ans = [] b_sum=0 for i in range(Q): q=input().split() if len(q)>1: a=int(q[1]) if que_s_size==0: ...
p03040
import bisect as bis Q = int(eval(input())) minval = 0 argminval = -(10**9) L, R = [-(10**9)], [10**9] lenL, lenR = 0, 0 def listupdate(items, new): ind = bis.bisect_left(items, new) ret = items[:ind]+[new]+items[ind:] return ret for _ in range(Q): qi = list(map(int, input().split())) ...
import heapq as hq Q = int(eval(input())) minval = 0 argminval = -(10**9) #aを大きい方と小さい方に分けて最小ヒープで管理(Lは最大が欲しいので-1掛けておく) L, R = [10**9], [10**9] lenL, lenR = 0, 0 for _ in range(Q): qi = list(map(int, input().split())) if qi[0] == 1: ai, bi = qi[1], qi[2] LC, RC = -1*L[0], R[0] #左右それぞれ...
p03040
def gcd(a, b): while b: a, b = b, a % b return a def prime_decomposition(n): i = 2 d = {} while i * i <= n: while n % i == 0: n //= i if i not in d: d[i] = 0 d[i] += 1 i += 1 if n > 1: if n not in d: d[n] = 1 return d def eratosthenes(n...
def gcd(a, b): while b: a, b = b, a % b return a def prime_decomposition(n): i = 2 d = {} while i * i <= n: while n % i == 0: n //= i if i not in d: d[i] = 0 d[i] += 1 i += 1 if n > 1: if n not in d: d[n] = 1 return d def eratosthenes(n):...
p03065
def factors(z): ret = [] for i in range(2, int(z**(1/2))+1): if z%i == 0: ret.append(i) while z%i == 0: z //= i if z != 1: ret.append(z) return ret def eratosththenes(N): if N == 0: return [0] work = [True] * (N+1) ...
def factors(z): ret = [] for i in range(2, int(z**(1/2))+1): if z%i == 0: ret.append(i) while z%i == 0: z //= i if z != 1: ret.append(z) return ret def eratosththenes(N): if N == 0: return [0] work = [True] * (N+1) ...
p03065
def gcd(a, b): while a > 0 and b > 0: a, b = b, a % b return a + b def st(a, b): if b == 0: return 1 if b % 2 == 0: q = st(a, b // 2) return q * q return a * st(a, b - 1) n = int(eval(input())) a = [] for i in range(n + 1): x = int(eval(input())) a.append(x) ans = 0 for x in range(1,...
def gcd(a, b): while a > 0 and b > 0: a, b = b, a % b return a + b def st(a, b): if b == 0: return 1 if b % 2 == 0: q = st(a, b // 2) return q * q return a * st(a, b - 1) n = int(eval(input())) a = [] for i in range(n + 1): x = int(eval(input())) a.append(x) ans = 0 for x in range(1,...
p03065
n = int(eval(input())) a = list(reversed([int(eval(input())) for i in range(n+1) ])) prime = [2,3] for i in range(5,100000): f = True for p in prime: if(p*p > i):break if i % p == 0 : f = False break if f :prime.append(i) def gcd(x,y): if(x < y):...
n = int(eval(input())) a = list(reversed([int(eval(input())) for i in range(n+1) ])) prime = [2,3] for i in range(5,100000): f = True for p in prime: if(p*p > i):break if i % p == 0 : f = False break if f :prime.append(i) def gcd(x,y): if(x < y):...
p03065
from functools import reduce import random import time MOD = 5949067957999863586856402224250344565968032244067932793830970391614040563838343474483322055545213055814077289404616970074718640994131755066582161947914009874328833510627285653397871976967779473728741946189992245661425682670972713351588558187861484629825142...
from functools import reduce import random MOD = 59490679579998635868564022242503445659680322440679327938309703916140405638383434744833220555452130558140772894046169700747186409941317550665821619479140098743288335106272856533978719769677794737287419461899922456614256826709727133515885581878614846298251428096512495590...
p03065
import sys input=sys.stdin.readline import collections def main(): S = input().strip() mi = len(S) for c in list(collections.Counter(S).keys()): s = list(S) n = 0 while len(list(collections.Counter(s).keys())) > 1: for i in range(len(s)-1): if...
import sys input=sys.stdin.readline def main(): S = input().strip() mi = len(S) for c in set(S): s = list(S) n = 0 while len(set(s)) > 1: for i in range(len(s)-1): if s[i+1] == c: s[i] = c s.pop() ...
p03687
def main(): s = input().strip() N = len(s) check = [False] * N ans = N for i in range(N): now = s[i] # now: 検索対象 # print("="*10) # print(now) max_ = N-1-i count = 0 for j in range(i+1): if s[j] != now: count += ...
def main(): s = input().strip() N = len(s) check = [False] * N ans = N for i in range(N): if not check[i]: now = s[i] check[i] = True max_ = N-1-i count = 0 for j in range(i+1): if s[j] != now: ...
p03687