output_description stringlengths 15 956 | submission_id stringlengths 10 10 | status stringclasses 3 values | problem_id stringlengths 6 6 | input_description stringlengths 9 2.55k | attempt stringlengths 1 13.7k | problem_description stringlengths 7 5.24k | samples stringlengths 2 2.72k |
|---|---|---|---|---|---|---|---|
If A is a divisor of B, print A + B; otherwise, print B - A.
* * * | s156532692 | Runtime Error | p03125 | Input is given from Standard Input in the following format:
A B | N, M = map(int, input().split())
A = list(map(int, input().split()))
honsuu = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
aval = []
for a in A:
aval += [honsuu[a]]
aval.sort()
dp = [0]
# i 本のマッチで作れる最高の桁数
for i in range(1, N + 1):
for b in aval:
if i < b:
continue
else:
dp += [dp[i - b] + 1]
break
else:
dp += [0]
# 作れる桁数はdp[-1]=dp[N] で, 最高桁にi が使えるかどうかの条件は,
# a in A かつ dp[N- honsuu[a]] = dp[N] -1
A.sort(reverse=True)
ans = []
for i in range(dp[N], 1, -1):
for a in A:
if N - honsuu[a] < 0:
continue
if dp[N - honsuu[a]] == dp[N] - 1:
N -= honsuu[a]
ans += [a]
break
final = 0
for i in range(10):
if i in A and honsuu[i] == N:
final = i
break
ans += [final]
print("".join(map(str, ans)))
| Statement
You are given positive integers A and B.
If A is a divisor of B, print A + B; otherwise, print B - A. | [{"input": "4 12", "output": "16\n \n\nAs 4 is a divisor of 12, 4 + 12 = 16 should be printed.\n\n* * *"}, {"input": "8 20", "output": "12\n \n\n* * *"}, {"input": "1 1", "output": "2\n \n\n1 is a divisor of 1."}] |
Print the number of the edges that are bridges among the M edges.
* * * | s454372761 | Wrong Answer | p03575 | Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M | import copy
N, M = map(int, input().split())
count = 0
path = [[] for j in range(N + 1)]
for n in range(M):
a, b = map(int, input().split())
path[a].append(b)
path[b].append(a)
visited = []
road = []
def solve(now, visited, road):
ans = []
if now in visited:
return visited
visited.append(now)
for i in range(len(road[now])):
ans.extend(solve(road[now][i], visited[:], road[:]))
return ans
PATH = copy.deepcopy(path)
check = [[0 for C in range(N + 1)] for c in range(N + 1)]
Ans = [[] for c in range(N + 1)]
for x in range(1, N + 1):
Q = []
for y in range(len(PATH[x])):
check[x][path[x][y]] = 1
if check[path[x][y]][x] != 1:
PATH = copy.deepcopy(path)
PATH[x].pop(y)
Q = copy.deepcopy(solve(x, [], PATH[:]))
Q = list(set(Q))
Ans[x].append(len(Q))
for i in range(N + 1):
for s in range(len(Ans[i])):
if Ans[i][s] != N:
count += 1
break
print(count)
| Statement
You are given an undirected connected graph with N vertices and M edges that
does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a _bridge_.
Find the number of the edges that are bridges among the M edges. | [{"input": "7 7\n 1 3\n 2 7\n 3 4\n 4 5\n 4 6\n 5 6\n 6 7", "output": "4\n \n\nThe figure below shows the given graph:\n\n\n\nThe edges shown in red are bridges. There are four of them.\n\n* * *"}, {"input": "3 3\n 1 2\n 1 3\n 2 3", "output": "0\n \n\nIt is possible that there is no bridge.\n\n* * *"}, {"input": "6 5\n 1 2\n 2 3\n 3 4\n 4 5\n 5 6", "output": "5\n \n\nIt is possible that every edge is a bridge."}] |
Print the number of the edges that are bridges among the M edges.
* * * | s525042036 | Runtime Error | p03575 | Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M | class UnionFind:
def __init__(self,n):
#par[x]==xならxというノードが根。xに親がいない
self.par = [i for i in range(n+1)]
self.rank = [0]*(n+1)
def find(self,x): #xの根を検索
#xが根ならxを返す
if self.par[x] == x:
return x
else:
self.par[x] = self.find(self.par[x]) #パス圧縮
return self.par[x]
def unite(self,x,y):
#根を探す
x = self.find(x)
y = self.find(y)
#木の深さを比較し、小さい方を大きい方につなげる
if self.rank[x] < self.rank[y]:
self.par[x] = y
else:
self.par[y] = x
if self.rank[x] == self.rank[y]:
self.rank[x] += 1 #rankが同じ木を繋げたとき根のランクを一つ上げる
def same_check(self,x,y): #xとyが同じ集合に属しているか
return self.find(x) == self.find(y)
n, m = map(int,input().split())
A=[list(map(int,input().split())) for i in range(m)]
ans = 0
for i in range(m):
uf = UnionFind(n) #
for j in range(m):
if i != j: #
uf.unite(A[j][0],A[j][1])
f = 1
g = uf.find(1) #
for j in range(1,n+1):
if g != uf.find(j): #1~n(iを除く)がすべて同じ集合に属しているか
f = 0
break
if not f:
ans += 1
print(ans) | Statement
You are given an undirected connected graph with N vertices and M edges that
does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a _bridge_.
Find the number of the edges that are bridges among the M edges. | [{"input": "7 7\n 1 3\n 2 7\n 3 4\n 4 5\n 4 6\n 5 6\n 6 7", "output": "4\n \n\nThe figure below shows the given graph:\n\n\n\nThe edges shown in red are bridges. There are four of them.\n\n* * *"}, {"input": "3 3\n 1 2\n 1 3\n 2 3", "output": "0\n \n\nIt is possible that there is no bridge.\n\n* * *"}, {"input": "6 5\n 1 2\n 2 3\n 3 4\n 4 5\n 5 6", "output": "5\n \n\nIt is possible that every edge is a bridge."}] |
Print the number of the edges that are bridges among the M edges.
* * * | s546111643 | Runtime Error | p03575 | Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M | import sys
if sys.platform =='ios':
sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]
def dfs(G, v):
seen[v] += 1
#print('v :', v)
for nv in G[v]:
if seen[nv]: continue
#print('v nv :', v, nv, 'Recursion')
dfs(G, nv)
N, M = MAP()
A = [MAP() for _ in range(M)]
#print(A)
ans = 0
for i in range(M):
G = [[] for _ in range(N)]
for j, m in enumerate(A):
# i!=jのとき辺を追加しない
if i != j:
a, b = m
G[a-1].append(b-1)
G[b-1].append(a-1)
seen = [0] * N
dfs(G, 1)
#print(i, j, seen)
if sum(seen) != N: ans += 1
print(ans)import sys
if sys.platform =='ios':
sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]
def dfs(G, v):
seen[v] += 1
#print('v :', v)
for nv in G[v]:
if seen[nv]: continue
#print('v nv :', v, nv, 'Recursion')
dfs(G, nv)
N, M = MAP()
A = [MAP() for _ in range(M)]
#print(A)
ans = 0
for i in range(M):
G = [[] for _ in range(N)]
for j, m in enumerate(A):
# i!=jのとき辺を追加しない
if i != j:
a, b = m
G[a-1].append(b-1)
G[b-1].append(a-1)
seen = [0] * N
dfs(G, 1)
#print(i, j, seen)
if sum(seen) != N: ans += 1
print(ans) | Statement
You are given an undirected connected graph with N vertices and M edges that
does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a _bridge_.
Find the number of the edges that are bridges among the M edges. | [{"input": "7 7\n 1 3\n 2 7\n 3 4\n 4 5\n 4 6\n 5 6\n 6 7", "output": "4\n \n\nThe figure below shows the given graph:\n\n\n\nThe edges shown in red are bridges. There are four of them.\n\n* * *"}, {"input": "3 3\n 1 2\n 1 3\n 2 3", "output": "0\n \n\nIt is possible that there is no bridge.\n\n* * *"}, {"input": "6 5\n 1 2\n 2 3\n 3 4\n 4 5\n 5 6", "output": "5\n \n\nIt is possible that every edge is a bridge."}] |
Print the number of the edges that are bridges among the M edges.
* * * | s533593376 | Runtime Error | p03575 | Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_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] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x):
return -self.parents[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
def members(self, x):
root = self.find(x)
return [i for i in range(self.n) if self.find(i) == root]
def roots(self):
return [i for i, x in enumerate(self.parents) if x < 0]
def group_count(self):
return len(self.roots())
def all_group_members(self):
return {r: self.members(r) for r in self.roots()}
def __str__(self):
return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
N,M = list(map(int,input().split()))
AB = []
for i in range(M):
AB.append(list(map(int,input().split())))
out = 0
for break in range(M):
Network = UnionFind(N)
for j in range(M):
if j!=break:
A,B=AB[j][0],AB[j][1]
Network.union(A-1, B-1)
if Network.group_count()==2:
out+=1
print(out) | Statement
You are given an undirected connected graph with N vertices and M edges that
does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a _bridge_.
Find the number of the edges that are bridges among the M edges. | [{"input": "7 7\n 1 3\n 2 7\n 3 4\n 4 5\n 4 6\n 5 6\n 6 7", "output": "4\n \n\nThe figure below shows the given graph:\n\n\n\nThe edges shown in red are bridges. There are four of them.\n\n* * *"}, {"input": "3 3\n 1 2\n 1 3\n 2 3", "output": "0\n \n\nIt is possible that there is no bridge.\n\n* * *"}, {"input": "6 5\n 1 2\n 2 3\n 3 4\n 4 5\n 5 6", "output": "5\n \n\nIt is possible that every edge is a bridge."}] |
Print the number of the edges that are bridges among the M edges.
* * * | s328290601 | Accepted | p03575 | Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M | import copy
n, m = map(int, input().split())
node = [[0 for _ in range(n)] for _ in range(n)]
aL = [0 for _ in range(m)]
bL = [0 for _ in range(m)]
for i in range(m):
a, b = map(int, input().split())
node[a - 1][b - 1] = 1
node[b - 1][a - 1] = 1
aL[i] = a
bL[i] = b
tmp1 = copy.deepcopy(node)
chk = [0 for _ in range(n)]
def DFS(x):
chk[x] = 1
for i in range(n):
if chk[i] != 1 and tmp1[x][i] == 1:
tmp1[x][i] = 0
tmp1[i][x] = 0
chk[i] = 1
DFS(i)
return chk
cnt = 0
for j in range(m):
tmp1[aL[j] - 1][bL[j] - 1] = 0
tmp1[bL[j] - 1][aL[j] - 1] = 0
if sum(DFS(0)) != n:
cnt += 1
tmp1 = copy.deepcopy(node)
chk = [0 for _ in range(n)]
print(cnt)
| Statement
You are given an undirected connected graph with N vertices and M edges that
does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a _bridge_.
Find the number of the edges that are bridges among the M edges. | [{"input": "7 7\n 1 3\n 2 7\n 3 4\n 4 5\n 4 6\n 5 6\n 6 7", "output": "4\n \n\nThe figure below shows the given graph:\n\n\n\nThe edges shown in red are bridges. There are four of them.\n\n* * *"}, {"input": "3 3\n 1 2\n 1 3\n 2 3", "output": "0\n \n\nIt is possible that there is no bridge.\n\n* * *"}, {"input": "6 5\n 1 2\n 2 3\n 3 4\n 4 5\n 5 6", "output": "5\n \n\nIt is possible that every edge is a bridge."}] |
Print the number of the edges that are bridges among the M edges.
* * * | s910618144 | Runtime Error | p03575 | Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M | from itertools import permutations
def solve():
n, k = map(int, input().split())
xs, ys = [], []
for _ in range(n):
x, y = map(int, input().split())
xs.append(x)
ys.append(y)
ans = ((10**9) * (10**9)) ** 2
for x1 in xs:
for x2 in xs:
for y1 in ys:
for y2 in ys:
minX, maxX = sorted([x1, x2])
minY, maxY = sorted([y1, y2])
cnt = 0
for i in range(n):
if minX <= xs[i] <= maxX and minY <= ys[i] <= maxY:
cnt += 1
if cnt >= k:
ans = min(ans, (maxX - minX) * (maxY - minY))
print(ans)
if __name__ == "__main__":
solve()
| Statement
You are given an undirected connected graph with N vertices and M edges that
does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a _bridge_.
Find the number of the edges that are bridges among the M edges. | [{"input": "7 7\n 1 3\n 2 7\n 3 4\n 4 5\n 4 6\n 5 6\n 6 7", "output": "4\n \n\nThe figure below shows the given graph:\n\n\n\nThe edges shown in red are bridges. There are four of them.\n\n* * *"}, {"input": "3 3\n 1 2\n 1 3\n 2 3", "output": "0\n \n\nIt is possible that there is no bridge.\n\n* * *"}, {"input": "6 5\n 1 2\n 2 3\n 3 4\n 4 5\n 5 6", "output": "5\n \n\nIt is possible that every edge is a bridge."}] |
Print the number of the edges that are bridges among the M edges.
* * * | s552627072 | Wrong Answer | p03575 | Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M | from queue import LifoQueue
from collections import defaultdict
def solve(n, m, AB):
g = defaultdict(lambda: [])
for a, b in AB:
g[a].append(b)
g[b].append(a)
q = LifoQueue(maxsize=m)
q.put((0, 1, 0))
s = {1}
total = m
ng_count = 0
while not q.empty():
p, c, l = q.get()
cands = [n for n in g[c] if n != p]
size = len(cands)
count = 0
for n in cands:
if n not in s:
nl = l
if l > 0 and size == 1:
# 分岐後の一本道の場合は長さを+1
nl += 1
if size >= 2:
# 分岐の場合は長さをリセット
nl = 1
q.put((c, n, nl))
s.add(n)
else:
# 既出の点が発見されたらそこまでの長さを引く
ng_count += 1
total -= l + 1
count += 1
if count > 0:
# 同じ辺を複数削除してしまった場合に追加
total += (count - 1) * l
return total + ng_count // 2 # 重複削除のために追加
_n, _m = map(int, input().split())
_AB = [map(int, input().split()) for _ in range(_m)]
print(solve(_n, _m, _AB))
| Statement
You are given an undirected connected graph with N vertices and M edges that
does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a _bridge_.
Find the number of the edges that are bridges among the M edges. | [{"input": "7 7\n 1 3\n 2 7\n 3 4\n 4 5\n 4 6\n 5 6\n 6 7", "output": "4\n \n\nThe figure below shows the given graph:\n\n\n\nThe edges shown in red are bridges. There are four of them.\n\n* * *"}, {"input": "3 3\n 1 2\n 1 3\n 2 3", "output": "0\n \n\nIt is possible that there is no bridge.\n\n* * *"}, {"input": "6 5\n 1 2\n 2 3\n 3 4\n 4 5\n 5 6", "output": "5\n \n\nIt is possible that every edge is a bridge."}] |
Print the number of the edges that are bridges among the M edges.
* * * | s467308804 | Runtime Error | p03575 | Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M | import times, strutils, sequtils, math, algorithm, tables, sets, lists, intsets
import critbits, future, strformat, deques
template `max=`(x,y) = x = max(x,y)
template `min=`(x,y) = x = min(x,y)
let read* = iterator: string {.closure.} =
while true: (for s in stdin.readLine.split: yield s)
proc scan(): int = read().parseInt
proc toInt(c:char): int =
return int(c) - int('0')
var par:seq[int]
proc root(par:var seq[int], x:int):int=
if par[x] == x:
return x
else:
par[x] = root(par, par[x])
result = par[x]
proc unite(par:var seq[int], x,y :int)=
var
rx = root(par,x)
ry = root(par,y)
if rx == ry:
return
else:
par[rx] = ry
proc same(par:var seq[int], x,y:int):bool=
return root(par,x) == root(par,y)
proc solve():int=
var
(n,m) = (scan(),scan())
usebridges = newseqwith(n,newseq[int](0))
bridges = newseq[(int,int)](0)
for i in 0..<m:
bridges.add( (scan()-1,scan()-1))
for i in 0..<m:
par = newseq[int](n)
for j in 0..<n:
par[j] = j
for j,b in bridges:
if i!=j:
par.unite(b[0],b[1])
block check:
for left in 0..<n:
for right in 0..<n:
if not par.same(left,right):
result+=1
break check
echo solve() | Statement
You are given an undirected connected graph with N vertices and M edges that
does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a _bridge_.
Find the number of the edges that are bridges among the M edges. | [{"input": "7 7\n 1 3\n 2 7\n 3 4\n 4 5\n 4 6\n 5 6\n 6 7", "output": "4\n \n\nThe figure below shows the given graph:\n\n\n\nThe edges shown in red are bridges. There are four of them.\n\n* * *"}, {"input": "3 3\n 1 2\n 1 3\n 2 3", "output": "0\n \n\nIt is possible that there is no bridge.\n\n* * *"}, {"input": "6 5\n 1 2\n 2 3\n 3 4\n 4 5\n 5 6", "output": "5\n \n\nIt is possible that every edge is a bridge."}] |
Print the number of the edges that are bridges among the M edges.
* * * | s397944082 | Accepted | p03575 | Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M | def c_bridge(N, M, Edges):
def dfs(v):
is_visited[v] = True # 現在の頂点を訪問済にする
for v2 in range(N):
if not graph[v][v2] or is_visited[v2]:
continue # 頂点v2に行けないか、行く必要がない
dfs(v2)
graph = [[False for _ in range(N)] for _ in range(N)]
for a, b in Edges:
graph[a - 1][b - 1] = True
graph[b - 1][a - 1] = True
# 各辺は橋か? グラフからその辺を取り除いて、グラフが連結かを判定
ans = 0
for a, b in Edges:
# 辺を取り除く
graph[a - 1][b - 1] = False
graph[b - 1][a - 1] = False
# すべての辺を未訪問として初期化
is_visited = [False for _ in range(N)]
# 全頂点に到達できなければ、除いた辺は橋である
dfs(0)
ans += 1 if not all(is_visited) else 0
# 辺を復元
graph[a - 1][b - 1] = True
graph[b - 1][a - 1] = True
return ans
N, M = [int(i) for i in input().split()]
Edges = [[int(i) for i in input().split()] for j in range(M)]
print(c_bridge(N, M, Edges))
| Statement
You are given an undirected connected graph with N vertices and M edges that
does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a _bridge_.
Find the number of the edges that are bridges among the M edges. | [{"input": "7 7\n 1 3\n 2 7\n 3 4\n 4 5\n 4 6\n 5 6\n 6 7", "output": "4\n \n\nThe figure below shows the given graph:\n\n\n\nThe edges shown in red are bridges. There are four of them.\n\n* * *"}, {"input": "3 3\n 1 2\n 1 3\n 2 3", "output": "0\n \n\nIt is possible that there is no bridge.\n\n* * *"}, {"input": "6 5\n 1 2\n 2 3\n 3 4\n 4 5\n 5 6", "output": "5\n \n\nIt is possible that every edge is a bridge."}] |
Print the number of the edges that are bridges among the M edges.
* * * | s422115893 | Accepted | p03575 | Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M | class UnionFind:
def __init__(self, size):
self.parent = list(range(size))
self.height = [0] * size
self.size = [1] * size
self.component = size
def root(self, index):
if self.parent[index] == index: # 根の場合
return index
rootIndex = self.root(self.parent[index]) # 葉の場合親の根を取得
self.parent[index] = rootIndex # 親の付け直し
return rootIndex
def union(self, index1, index2): # 結合
root1 = self.root(index1)
root2 = self.root(index2)
if root1 == root2: # 連結されている場合
return
self.component -= 1 # 連結成分を減らす
if self.height[root1] < self.height[root2]:
self.parent[root1] = root2 # root2に結合
self.size[root2] += self.size[root1]
else:
self.parent[root2] = root1 # root1に結合
self.size[root1] += self.size[root2]
if self.height[root1] == self.height[root2]:
self.height[root1] += 1
return
def isSameRoot(self, index1, index2):
return self.root(index1) == self.root(index2)
def sizeOfSameRoot(self, index):
return self.size[self.root(index)]
def getComponent(self):
return self.component
N, M = map(int, input().split())
edges = [tuple(map(lambda a: int(a) - 1, input().split())) for _ in range(M)]
def isBride(i):
tree = UnionFind(N)
for j, (a, b) in enumerate(edges):
if i == j:
continue
tree.union(a, b)
return tree.getComponent() > 1
print(len([0 for i in range(M) if isBride(i)]))
| Statement
You are given an undirected connected graph with N vertices and M edges that
does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a _bridge_.
Find the number of the edges that are bridges among the M edges. | [{"input": "7 7\n 1 3\n 2 7\n 3 4\n 4 5\n 4 6\n 5 6\n 6 7", "output": "4\n \n\nThe figure below shows the given graph:\n\n\n\nThe edges shown in red are bridges. There are four of them.\n\n* * *"}, {"input": "3 3\n 1 2\n 1 3\n 2 3", "output": "0\n \n\nIt is possible that there is no bridge.\n\n* * *"}, {"input": "6 5\n 1 2\n 2 3\n 3 4\n 4 5\n 5 6", "output": "5\n \n\nIt is possible that every edge is a bridge."}] |
Print the number of the edges that are bridges among the M edges.
* * * | s939929126 | Accepted | p03575 | Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M | n, m = list(map(int, input().split()))
ab = [list(map(int, input().split())) for i in range(m)]
# 逆順にしたもの追加
ab += [[b, a] for a, b in ab]
# 辺id付きで、idがfrom,abi[from]=[to1,to2,...] のリスト
abi = [[] for _ in range(n + 1)]
for i, (a, b) in enumerate(ab):
abi[a].append(b)
abi[b].append(a)
# 再起のlimitを上げる
import sys
sys.setrecursionlimit(4100000)
from collections import deque
def dfs(i):
visitted.append(i)
flg = False
for x in abi[i]:
if x not in visitted and (i, x) != blocked and (x, i) != blocked:
dfs(x)
flg = True
return len(visitted) == n
ret = 0
for i in range(m):
blocked = (ab[i][0], ab[i][1])
visitted = deque([])
if not dfs(1):
ret += 1
print(ret)
| Statement
You are given an undirected connected graph with N vertices and M edges that
does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a _bridge_.
Find the number of the edges that are bridges among the M edges. | [{"input": "7 7\n 1 3\n 2 7\n 3 4\n 4 5\n 4 6\n 5 6\n 6 7", "output": "4\n \n\nThe figure below shows the given graph:\n\n\n\nThe edges shown in red are bridges. There are four of them.\n\n* * *"}, {"input": "3 3\n 1 2\n 1 3\n 2 3", "output": "0\n \n\nIt is possible that there is no bridge.\n\n* * *"}, {"input": "6 5\n 1 2\n 2 3\n 3 4\n 4 5\n 5 6", "output": "5\n \n\nIt is possible that every edge is a bridge."}] |
Print the number of the edges that are bridges among the M edges.
* * * | s145984964 | Accepted | p03575 | Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M | from collections import deque
def BFS(N, M, List):
Edge = [[] for TN in range(N + 1)]
for TM in range(0, M):
Edge[List[TM][0]].append(List[TM][1])
Edge[List[TM][1]].append(List[TM][0])
Distance = [-1] * (N + 1)
Distance[0] = 0
Distance[1] = 0
From = [0] * (N + 1)
From[1] = 1
Deque = deque()
Deque.append(1)
while Deque:
Now = Deque.popleft()
for Con in Edge[Now]:
if Distance[Con] == -1:
Distance[Con] = Distance[Now] + 1
Deque.append(Con)
From[Con] = Now
return Distance[1:], From[1:]
import copy
N, M = (int(T) for T in input().split())
List = [[] for TM in range(0, M)]
for TM in range(0, M):
List[TM] = [int(T) for T in input().split()]
Count = 0
for TM in range(0, M):
ListCopy = copy.deepcopy(List)
del ListCopy[TM]
Distance, From = BFS(N, M - 1, ListCopy)
if -1 in Distance:
Count += 1
print(Count)
| Statement
You are given an undirected connected graph with N vertices and M edges that
does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a _bridge_.
Find the number of the edges that are bridges among the M edges. | [{"input": "7 7\n 1 3\n 2 7\n 3 4\n 4 5\n 4 6\n 5 6\n 6 7", "output": "4\n \n\nThe figure below shows the given graph:\n\n\n\nThe edges shown in red are bridges. There are four of them.\n\n* * *"}, {"input": "3 3\n 1 2\n 1 3\n 2 3", "output": "0\n \n\nIt is possible that there is no bridge.\n\n* * *"}, {"input": "6 5\n 1 2\n 2 3\n 3 4\n 4 5\n 5 6", "output": "5\n \n\nIt is possible that every edge is a bridge."}] |
Print the number of the edges that are bridges among the M edges.
* * * | s516357845 | Accepted | p03575 | Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M | int1 = lambda x: int(x) - 1
N, M = map(int, input().split())
G = [list() for _ in range(N)]
edges = [tuple(map(int1, input().split())) for _ in range(M)]
for u, v in edges:
G[u].append(v)
G[v].append(u)
pre = [-1] * N
low = [-1] * N
def detect_bridge(v, p=None, c=0):
ret = list()
pre[v] = low[v] = c
c += 1
for x in G[v]:
if x == p:
continue
if pre[x] < 0:
br, c = detect_bridge(x, v, c)
ret.extend(br)
if low[v] > low[x]:
low[v] = low[x]
if pre[v] == low[v] > 0:
ret.append((p, v))
return ret, c
bridges, _ = detect_bridge(0)
print(len(bridges))
| Statement
You are given an undirected connected graph with N vertices and M edges that
does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a _bridge_.
Find the number of the edges that are bridges among the M edges. | [{"input": "7 7\n 1 3\n 2 7\n 3 4\n 4 5\n 4 6\n 5 6\n 6 7", "output": "4\n \n\nThe figure below shows the given graph:\n\n\n\nThe edges shown in red are bridges. There are four of them.\n\n* * *"}, {"input": "3 3\n 1 2\n 1 3\n 2 3", "output": "0\n \n\nIt is possible that there is no bridge.\n\n* * *"}, {"input": "6 5\n 1 2\n 2 3\n 3 4\n 4 5\n 5 6", "output": "5\n \n\nIt is possible that every edge is a bridge."}] |
Print the number of the edges that are bridges among the M edges.
* * * | s159727811 | Runtime Error | p03575 | Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M | import sys
import copy
N, M = map(int, input().split())
V = []
E = []
for i in range(N):
V.append(set())
for i in range(M):
*l, = map(int, input().split())
V[l[0]-1].add(l[1]-1)
V[l[1]-1].add(l[0]-1)
E.append([l[0], l[1]])
count = 0
E_copy = copy.deepcopy(E)
V_copy = copy.deepcopy(V)
for i in range(M):
e = [E[i][0], E[i][1]]
V[e[0]].remove(e[1])
V[e[1]].remove(e[0])
#辺が存在しないノードがあるかをチェックする。
for v in V:
if len(v)==0:
count = count + 1
break
E = copy.deepcopy(E_copy)
V = copy.deepcopy(V_copy)
print(count)
| Statement
You are given an undirected connected graph with N vertices and M edges that
does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a _bridge_.
Find the number of the edges that are bridges among the M edges. | [{"input": "7 7\n 1 3\n 2 7\n 3 4\n 4 5\n 4 6\n 5 6\n 6 7", "output": "4\n \n\nThe figure below shows the given graph:\n\n\n\nThe edges shown in red are bridges. There are four of them.\n\n* * *"}, {"input": "3 3\n 1 2\n 1 3\n 2 3", "output": "0\n \n\nIt is possible that there is no bridge.\n\n* * *"}, {"input": "6 5\n 1 2\n 2 3\n 3 4\n 4 5\n 5 6", "output": "5\n \n\nIt is possible that every edge is a bridge."}] |
Print the number of the edges that are bridges among the M edges.
* * * | s652559499 | Runtime Error | p03575 | Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M | const int limit = 50;
int n, m;
int a[limit], b[limit];
bool graph[limit][limit];
bool visited[limit];
void dfs(int v){
visited[v] = true;
for (int v2=0; v2<n;++v2){
if (graph[v][v2]==false) continue;
if (visited[v2]==true) continue;
dfs(v2);
}
}
int main(void){
cin >> n >> m;
for (int i=0; i<m; ++i){
graph[a[i]][b[i]] = graph[b[i]][a[i]] = false;
for (int j=0; j<n; ++j) visited[j] = false;
dfs(0);
bool bridge = false;
for (j=0; j<n; ++j) if (visited[j] == false) bridge = true;
if (bridge) ans += 1;
graph[a[i]][b[i]] = graph[b[i]][a[i]] = true;
}
cout << ans << endl;
return 0;
} | Statement
You are given an undirected connected graph with N vertices and M edges that
does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a _bridge_.
Find the number of the edges that are bridges among the M edges. | [{"input": "7 7\n 1 3\n 2 7\n 3 4\n 4 5\n 4 6\n 5 6\n 6 7", "output": "4\n \n\nThe figure below shows the given graph:\n\n\n\nThe edges shown in red are bridges. There are four of them.\n\n* * *"}, {"input": "3 3\n 1 2\n 1 3\n 2 3", "output": "0\n \n\nIt is possible that there is no bridge.\n\n* * *"}, {"input": "6 5\n 1 2\n 2 3\n 3 4\n 4 5\n 5 6", "output": "5\n \n\nIt is possible that every edge is a bridge."}] |
Print the number of the edges that are bridges among the M edges.
* * * | s058786252 | Accepted | p03575 | Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M | import sys
class Node:
def __init__(self, node_num):
self.node_num = node_num
self.connect = []
def add_node(self, node):
self.connect.append(node)
def delete_node(self, node):
self.connect.remove(node)
def is_connect(node_list):
check_list = [0]
stack = [node_list[0]]
while len(stack) != 0:
if len(check_list) == N:
return True
current = stack.pop()
for ii in current.connect:
if ii not in check_list:
check_list.append(ii)
stack.append(node_list[ii])
return len(check_list) == N
N, M = list(map(int, sys.stdin.readline().strip().split(" ")))
node_list = [Node(i) for i in range(N)]
edge_list = []
for i in range(M):
n, m = list(map(int, sys.stdin.readline().strip().split(" ")))
node_list[n - 1].add_node(m - 1)
node_list[m - 1].add_node(n - 1)
edge_list.append((n - 1, m - 1))
res = 0
prev_edge = None
for e in edge_list:
if prev_edge is not None:
node_list[prev_edge[0]].add_node(prev_edge[1])
node_list[prev_edge[1]].add_node(prev_edge[0])
prev_edge = e
node_list[e[0]].delete_node(e[1])
node_list[e[1]].delete_node(e[0])
if not is_connect(node_list):
res += 1
print(str(res))
| Statement
You are given an undirected connected graph with N vertices and M edges that
does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a _bridge_.
Find the number of the edges that are bridges among the M edges. | [{"input": "7 7\n 1 3\n 2 7\n 3 4\n 4 5\n 4 6\n 5 6\n 6 7", "output": "4\n \n\nThe figure below shows the given graph:\n\n\n\nThe edges shown in red are bridges. There are four of them.\n\n* * *"}, {"input": "3 3\n 1 2\n 1 3\n 2 3", "output": "0\n \n\nIt is possible that there is no bridge.\n\n* * *"}, {"input": "6 5\n 1 2\n 2 3\n 3 4\n 4 5\n 5 6", "output": "5\n \n\nIt is possible that every edge is a bridge."}] |
Print the number of the edges that are bridges among the M edges.
* * * | s877503322 | Wrong Answer | p03575 | Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M | # -*- coding: utf-8 -*-
"""
Created on Thu Sep 12 10:38:04 2019
C - Bridge
実行時間制限: 2 sec / メモリ制限: 256 MB
配点 :
300
点
@author: justwah
use union find
"""
import sys
read = sys.stdin.readline
n, m = map(int, input().split()) # single line use regular input() is faster
l = [
list(tuple(map(int, read().split()))) for i in range(m)
] # multiple lines use sys.stdin.readline faster
def root(x): # recursively find root for a node
if parent[x] < 0: # if at root, return self
return x
else:
parent[x] = root(
parent[x]
) # while searching rejoint queried node to a common parent for hierarchical tree
return parent[x]
def merge(a, b):
a = root(a)
b = root(b)
if rank[a] < rank[b]: # joint lower rank node to a higher rank one
parent[a] = b
else:
parent[b] = a
if rank[a] == rank[b]: # rank only change if two same rank node joined
rank[a] += 1
return
score = 0
for i in range(m):
parent = [-1] * (n + 1)
rank = [0] * (n + 1)
for j, (a, b) in enumerate(
l[i:]
): # iteraete from different starting point - test different "last bridge"
a = root(a)
b = root(b)
if a != b:
merge(a, b)
else:
if j == (len(l) - 1): # if the last bridge is redundant, +1
score += 1
print(m - score)
| Statement
You are given an undirected connected graph with N vertices and M edges that
does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a _bridge_.
Find the number of the edges that are bridges among the M edges. | [{"input": "7 7\n 1 3\n 2 7\n 3 4\n 4 5\n 4 6\n 5 6\n 6 7", "output": "4\n \n\nThe figure below shows the given graph:\n\n\n\nThe edges shown in red are bridges. There are four of them.\n\n* * *"}, {"input": "3 3\n 1 2\n 1 3\n 2 3", "output": "0\n \n\nIt is possible that there is no bridge.\n\n* * *"}, {"input": "6 5\n 1 2\n 2 3\n 3 4\n 4 5\n 5 6", "output": "5\n \n\nIt is possible that every edge is a bridge."}] |
Print the number of the edges that are bridges among the M edges.
* * * | s055378940 | Runtime Error | p03575 | Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M | #include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <map>
#include <queue>
#include <list>
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,a,b) for(int i=int(a);i<int(b);++i)
#define crep(i) for(char i='a';i<='z';i++)
#define psortsecond(A,N) sort(A,A+N,[](const pii &a, const pii &b){return a.second<b.second;});
#define psortfirst(A,N) sort(A,A+N,[](const pii &a, const pii &b){return a.first<b.first;});
#define ALL(x) (x).begin(),(x).end()
int ctoi(const char c){
if('0' <= c && c <= '9') return (c-'0');
return -1;
}
using namespace std;
using pii = pair<int,int>;
long long gcd(long long a, long long b){return (b == 0 ? a : gcd(b, a%b));}
long long lcm(long long a, long long b){return a*b/gcd(a,b);}
typedef long long ll;
#define MOD 1000000007
#define EPS 10e-8
ll N,M,A[3000],B[3000],C=0;
vector<vector<ll>> TEN(57);
ll touri[57];
void kentou(ll a,ll b,ll t){
t++;
touri[a]=1;
if(a==b){
C++;
return;
}
rep(i,TEN[a].size()){
if(touri[TEN[a][i]]==0 && (t!=1 || TEN[a][i]!=b)){
kentou(TEN[a][i],b,t);
}
}
}
int main(){
cin >> N >> M;
rep(i,M){
cin >> A[i] >> B[i];
TEN[A[i]].push_back(B[i]);
TEN[B[i]].push_back(A[i]);
}
rep(i,M){
kentou(A[i],B[i],0);
rep(j,57){
touri[j]=0;
}
}
cout << M-C << endl;
} | Statement
You are given an undirected connected graph with N vertices and M edges that
does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a _bridge_.
Find the number of the edges that are bridges among the M edges. | [{"input": "7 7\n 1 3\n 2 7\n 3 4\n 4 5\n 4 6\n 5 6\n 6 7", "output": "4\n \n\nThe figure below shows the given graph:\n\n\n\nThe edges shown in red are bridges. There are four of them.\n\n* * *"}, {"input": "3 3\n 1 2\n 1 3\n 2 3", "output": "0\n \n\nIt is possible that there is no bridge.\n\n* * *"}, {"input": "6 5\n 1 2\n 2 3\n 3 4\n 4 5\n 5 6", "output": "5\n \n\nIt is possible that every edge is a bridge."}] |
Print the number of the edges that are bridges among the M edges.
* * * | s747004382 | Runtime Error | p03575 | Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M |
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,b) for(ll i=a;i>=b;i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define veci vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vec2(a,b) vector<vec>(a,vec(b))
#define vec2ll(a,b) vector<vec>(a,vecll(b))
#define vec3(a,b,c) vector<vector<vec>>(a,vec2(b,c))
#define vec3ll(a,b,c) vector<vector<vecll>>(a,vec2ll(b,c))
#define fi first
#define se second
#define all(c) begin(c),end(c)
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))
using namespace std;
int in() {int x;cin>>x;return x;}
ll lin() {ll x;cin>>x;return x;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T> inline void print(pair<T,T> p){cout<<"("<<p.first<<","<<p.second<<") ";}
//template<class T> inline void print(vector<pair<T,T>> v){for(auto e:v)print(e); cout<<endl;}
//template<class T> inline void print(T v){for(auto e:v)cout<<e<<" ";cout<<endl;}
template<typename T>
istream& operator >> (istream& is, vector<T>& vec){
for(T& x:vec) is >> x;
return is;
}
const ll INF=1e10+100;
class DisjointSet{
public:
vector<ll> rank,p;
DisjointSet(){}
DisjointSet(ll size){
rank.resize(size,0);
p.resize(size,0);
rep(i,size){
makeSet(i);
}
}
void makeSet(ll x){
p[x]=x;
rank[x]=0;
}
bool same(ll x,ll y){
return findSet(x)==findSet(y);
}
void unite(ll x,ll y){
link(findSet(x),findSet(y));
}
void link(ll x, ll y){
if(rank[x]>rank[y]){
p[y]=x;
}
else{
p[x]=y;
if(rank[x]==rank[y]){
rank[y]++;
}
}
}
ll findSet(ll x){
if(p[x]!=x){
p[x]=findSet(p[x]);
}
return p[x];
}
};
int main()
{
ll n,m;
cin >> n >> m;
vector<ll> a(m);
vector<ll> b(m);
rep(i,m){
ll s,t;
cin >> s >> t;
s--;
t--;
a[i]=s;
b[i]=t;
}
ll ans=0;
rep(i,m){ //i番目の辺を使わない
DisjointSet dis=DisjointSet(n);
bool flag=true; //trueならばグラフは連結
rep(j,m){
if(j==i) continue;
dis.unite(a[j],b[j]);
}
rep(p,n){
for(ll q=p+1;q<n;q++){
if(!dis.same(p,q)){
flag=false;
break;
}
}
}
if(!flag) ans++;
}
cout << ans << endl;
return 0;
} | Statement
You are given an undirected connected graph with N vertices and M edges that
does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a _bridge_.
Find the number of the edges that are bridges among the M edges. | [{"input": "7 7\n 1 3\n 2 7\n 3 4\n 4 5\n 4 6\n 5 6\n 6 7", "output": "4\n \n\nThe figure below shows the given graph:\n\n\n\nThe edges shown in red are bridges. There are four of them.\n\n* * *"}, {"input": "3 3\n 1 2\n 1 3\n 2 3", "output": "0\n \n\nIt is possible that there is no bridge.\n\n* * *"}, {"input": "6 5\n 1 2\n 2 3\n 3 4\n 4 5\n 5 6", "output": "5\n \n\nIt is possible that every edge is a bridge."}] |
Print the number of the edges that are bridges among the M edges.
* * * | s417155919 | Runtime Error | p03575 | Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M | #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
struct Edge{
int from,to;
};
int V,E;
int boss[55],height[55];
Edge edge[55];
int get_boss(int id){
if(boss[id] == id)return id; //自分が代表なら、自分の値を返す
else{
return boss[id] = get_boss(boss[id]); //代表でないなら、自分が所属する組織の代表を返しつつ、経路圧縮
}
}
int is_same(int x,int y){
return get_boss(x) == get_boss(y);
}
void unite(int x,int y){
int boss_x = get_boss(x);
int boss_y = get_boss(y);
//既に同じグループなら何もしない
if(boss_x == boss_y)return;
//高さが高い方に吸収する
if(height[x] > height[y]){
boss[boss_y] = boss_x;
}else if(height[x] < height[y]){
boss[boss_x] = boss_y;
}else{ //height[x] == height[y]
boss[boss_y] = boss_x;
height[x]++;
}
}
void init(){
for(int i = 0; i < V; i++){
boss[i] = i;
height[i] = 0;
}
}
int main(){
scanf("%d %d",&V,&E);
for(int i = 0; i < E; i++){
scanf("%d %d",&edge[i].from,&edge[i].to);
edge[i].from--;
edge[i].to--;
}
int ans = 0,num_group;
for(int i = 0; i < E; i++){
init();
for(int k = 0; k < E; k++){
if(k == i)continue;
unite(edge[k].from,edge[k].to);
}
num_group = 0;
for(int k = 0; k < V; k++){
if(k == get_boss(k)){
num_group++;
}
}
if(num_group != 1)ans++;
}
printf("%d\n",ans);
return 0;
}
| Statement
You are given an undirected connected graph with N vertices and M edges that
does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a _bridge_.
Find the number of the edges that are bridges among the M edges. | [{"input": "7 7\n 1 3\n 2 7\n 3 4\n 4 5\n 4 6\n 5 6\n 6 7", "output": "4\n \n\nThe figure below shows the given graph:\n\n\n\nThe edges shown in red are bridges. There are four of them.\n\n* * *"}, {"input": "3 3\n 1 2\n 1 3\n 2 3", "output": "0\n \n\nIt is possible that there is no bridge.\n\n* * *"}, {"input": "6 5\n 1 2\n 2 3\n 3 4\n 4 5\n 5 6", "output": "5\n \n\nIt is possible that every edge is a bridge."}] |
Print the number of the edges that are bridges among the M edges.
* * * | s405038544 | Accepted | p03575 | Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M | n, m = map(int, input().split(" "))
a = [list(map(int, input().split(" "))) for i in range(m)]
a_connects = [[] for i in range(n)]
for x, y in a:
a_connects[x - 1].append(y - 1)
a_connects[y - 1].append(x - 1)
# print(a_connects)
a_connect_sums = [len(connect) for connect in a_connects]
# print(a_connect_sums)
bridge_count = 0
while 1 in a_connect_sums:
x = a_connect_sums.index(1)
y = a_connects[x][0]
a_connects[x].remove(y)
a_connects[y].remove(x)
a_connect_sums[x] -= 1
a_connect_sums[y] -= 1
bridge_count += 1
# print('')
# print(a_connects)
# print(a_connect_sums)
print(bridge_count)
| Statement
You are given an undirected connected graph with N vertices and M edges that
does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a _bridge_.
Find the number of the edges that are bridges among the M edges. | [{"input": "7 7\n 1 3\n 2 7\n 3 4\n 4 5\n 4 6\n 5 6\n 6 7", "output": "4\n \n\nThe figure below shows the given graph:\n\n\n\nThe edges shown in red are bridges. There are four of them.\n\n* * *"}, {"input": "3 3\n 1 2\n 1 3\n 2 3", "output": "0\n \n\nIt is possible that there is no bridge.\n\n* * *"}, {"input": "6 5\n 1 2\n 2 3\n 3 4\n 4 5\n 5 6", "output": "5\n \n\nIt is possible that every edge is a bridge."}] |
Print the number of the edges that are bridges among the M edges.
* * * | s993088253 | Runtime Error | p03575 | Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M | a
| Statement
You are given an undirected connected graph with N vertices and M edges that
does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a _bridge_.
Find the number of the edges that are bridges among the M edges. | [{"input": "7 7\n 1 3\n 2 7\n 3 4\n 4 5\n 4 6\n 5 6\n 6 7", "output": "4\n \n\nThe figure below shows the given graph:\n\n\n\nThe edges shown in red are bridges. There are four of them.\n\n* * *"}, {"input": "3 3\n 1 2\n 1 3\n 2 3", "output": "0\n \n\nIt is possible that there is no bridge.\n\n* * *"}, {"input": "6 5\n 1 2\n 2 3\n 3 4\n 4 5\n 5 6", "output": "5\n \n\nIt is possible that every edge is a bridge."}] |
Print the number of the edges that are bridges among the M edges.
* * * | s531384434 | Runtime Error | p03575 | Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M | atodeyaruyo
| Statement
You are given an undirected connected graph with N vertices and M edges that
does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a _bridge_.
Find the number of the edges that are bridges among the M edges. | [{"input": "7 7\n 1 3\n 2 7\n 3 4\n 4 5\n 4 6\n 5 6\n 6 7", "output": "4\n \n\nThe figure below shows the given graph:\n\n\n\nThe edges shown in red are bridges. There are four of them.\n\n* * *"}, {"input": "3 3\n 1 2\n 1 3\n 2 3", "output": "0\n \n\nIt is possible that there is no bridge.\n\n* * *"}, {"input": "6 5\n 1 2\n 2 3\n 3 4\n 4 5\n 5 6", "output": "5\n \n\nIt is possible that every edge is a bridge."}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s974365554 | Accepted | p03826 | The input is given from Standard Input in the following format:
A B C D | import functools
import os
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
def inp():
return int(input())
def inpf():
return float(input())
def inps():
return input()
def inl():
return list(map(int, input().split()))
def inlf():
return list(map(float, input().split()))
def inls():
return input().split()
def inpm(line):
return [inp() for _ in range(line)]
def inpfm(line):
return [inpf() for _ in range(line)]
def inpsm(line):
return [inps() for _ in range(line)]
def inlm(line):
return [inl() for _ in range(line)]
def inlfm(line):
return [inlf() for _ in range(line)]
def inlsm(line):
return [inls() for _ in range(line)]
def Yesif(cond):
print("Yes" if cond else "No")
def YESIF(cond):
print("YES" if cond else "NO")
def yesif(cond):
print("yes" if cond else "no")
a, b, c, d = inl()
print(max(a * b, c * d))
| Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s636237933 | Accepted | p03826 | The input is given from Standard Input in the following format:
A B C D | #
# ⋀_⋀
# (・ω・)
# ./ U ∽ U\
# │* 合 *│
# │* 格 *│
# │* 祈 *│
# │* 願 *│
# │* *│
#  ̄
#
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline
from math import floor, ceil, sqrt, factorial, log # log2ないyp
from heapq import heappop, heappush, heappushpop
from collections import Counter, defaultdict, deque
from itertools import (
accumulate,
permutations,
combinations,
product,
combinations_with_replacement,
)
from bisect import bisect_left, bisect_right
from copy import deepcopy
inf = float("inf")
mod = 10**9 + 7
def pprint(*A):
for a in A:
print(*a, sep="\n")
def INT_(n):
return int(n) - 1
def MI():
return map(int, input().split())
def MF():
return map(float, input().split())
def MI_():
return map(INT_, input().split())
def LI():
return list(MI())
def LI_():
return [int(x) - 1 for x in input().split()]
def LF():
return list(MF())
def LIN(n: int):
return [I() for _ in range(n)]
def LLIN(n: int):
return [LI() for _ in range(n)]
def LLIN_(n: int):
return [LI_() for _ in range(n)]
def LLI():
return [list(map(int, l.split())) for l in input()]
def I():
return int(input())
def F():
return float(input())
def ST():
return input().replace("\n", "")
def main():
a, b, c, d = MI()
print(max(a * b, c * d))
if __name__ == "__main__":
main()
| Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s256689203 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | a,b,c,b = map(int,input().split())
print( max(a*b, c*d) | Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s808079377 | Accepted | p03826 | The input is given from Standard Input in the following format:
A B C D | val = list(map(int, input().split()))
print(max(val[0] * val[1], val[2] * val[3]))
| Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s189711413 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | a, ,b, c, d = map(int, input().split())
print(max(a*b, c*d)) | Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s098464992 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | , b, c, d = map(int, input().split())
print(max(a*b, c*d)) | Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s386899003 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | # -*- coding:utf-8 -*-
import math
import sys
def mark(s, x):
for i in range(x + x, len(s), x):
s[i] = False
def sieve(n):
s = [True] * n
for x in range(2, int(n**0.5) + 1):
if s[x]: mark(s, x)
return [i for i in range(0,n) if s[i] and i > 1]
n = int(input())
p = sieve(n)
arr = [0]*len(p)
n = math.factorial(n)
for i in range(len(p)):
flag = 1
k = p[i]
while flag:
if n%k == 0:
k = k*p[i]
arr[i] += 1
else:
flag = 0
x = 1
for i in range(len(arr)):
if arr[i] != 0:
x *= (arr[i]+1)
if ( n = 6):
print(4)
else:
print(x%(10**9+7)) | Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s973022091 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | a,b,c,d = list(map(int,input().split()))
sum1=a*b
sum2=c*d
if sum1 > sum2:
print(sum1)
elif sum1 < sum2:
print(sum2)
else sum1 == sum2:
print(sum1)
| Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s737920140 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | n, s = int(input()), input()
print(max([s[0:i].count("I") - s[0:i].count("D") for i in range(n)]))
| Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s131912887 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | in = [int(i) for i in input().split()]
print(max(in[0] * in[1], in[2] * in[3]])) | Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s582376972 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | in = [int(i) for i in input()]
print(max(in[0] * in[1], in[2] * in[3]])) | Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s452118808 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | a,b,c,d=[int(i)for i in input().split()]
print(max(a*b,c*d) | Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s992790391 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | input()
tmp = x = 0
for a in input():
x += int(a.replace('I','1').replace('D','-1'))
if tmp < x:
tmp = x
print(max([,tmp]))
| Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s187252147 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | a,b,c,d = map(int(),input().split())
if a*b == c*d:
print(a*b)
if a*b =< c*d:
print(c*d)
if a*b >= c*d:
print(a*b)
| Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s890570532 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | a = input().spulit()
A = int(a[0])
B = int(a[1])
C = int(a[2])
D = int(a[3])
q = A * B
w = C * D
if q <= w:
print(w)
else:
print(q)
| Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s425241313 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | a,b,x=input().split()
a=int()
b=int()
x=a*b
c,d,y=input().split()
c=int()
d=int()
y=b*c
if x>y:
print(x)
elif:y>x
print(y)
elif:x=y
print(x) | Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s314226402 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | A,B,C,D=map(int,input().split())
sone=A*B
stwo=C*D
if sone>stwo:
print(sone)
elif sone<stwo:
print(stwo):
elif sone==stwo:
print(sone) | Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s102260188 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | a,b=input().split()
a=int()
b=int()
x=a*b
b,c=input().split()
b=int()
c=int()
y=b*c
if x>y:
print(x)
elif: y>x
print(y)
elif:x=y
print(x)
| Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s062035579 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | a,b,c,d=map(int,input().split())
print(max(a*b,c*d) | Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s083836846 | Wrong Answer | p03826 | The input is given from Standard Input in the following format:
A B C D | z = list(map(int, input().split()))
min(z[0] * z[1], z[2] * z[3])
| Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s183883631 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | a,b,c,d=,ap(int,input().split())
print(max(a*b, c*d)) | Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s036796894 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | a, b, c, d = map(int, input().split())
x = a * b, y = c * d
if x >= y:
print(x)
else:
print(y)
| Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s176781547 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | a, b, c, d = map(int, input()split())
s1 = a*b
s2 = c*d
if s1 > s2:
print(s1)
else:
print(s2) | Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s799190785 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | A B C D = input()
if A*B>C*D:
print(A*B)
elif C*D>A*B:
print(C*D)
elif A*B==C*D:
print(A*B) | Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s896531535 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | a,b,c,d = map(int, input().split())
s = a*b
t = c*d
if s > t = print(s)
else: print(t) | Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s133613268 | Accepted | p03826 | The input is given from Standard Input in the following format:
A B C D | print(max(x * y for x, y in zip(*[iter(map(int, input().split()))] * 2)))
| Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s849615363 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | l=list(map(int, input().split())
print(max(l[0]*l[1],l[2]*l[3]) | Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s410883937 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | A,B,C,D=(int (i) for i in input() split())
print(max(A*B,C*D)) | Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s432252594 | Accepted | p03826 | The input is given from Standard Input in the following format:
A B C D | value = input()
values = value.split()
# print(values)
if (int(values[0]) * int(values[1])) > (int(values[2]) * int(values[3])):
print(int(values[0]) * int(values[1]))
else:
print(int(values[2]) * int(values[3]))
| Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s562051518 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | import math
prime = {}
N = int(input())
if N == 1:
print(1)
exit()
isPrime = [True] * (N + 1)
isPrime[0] = False
isPrime[1] = False
for i in range(2, int(math.sqrt(N)) + 2):
if isPrime[i]:
n = 2 * i
while n <= N:
isPrime[n] = False
n += i
for i in range(N + 1):
if isPrime[i]:
prime[i] = 0
key_list = list(prime.keys())
for num in range(2, N + 1):
res = num
idx = 0
while res > 1:
k = key_list[idx]
if res % k == 0:
prime[k] += 1
res = res // k
else:
idx += 1
ans = 1
for v in prime.values():
ans *= v + 1
ans = ans % (10**9 + 7)
if ans == 0:
ans = 1
print(ans)
| Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s504611420 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | n, a, b = [int(x) for x in input().split(" ")]
dis = [a * int(x) for x in input().split(" ")]
dist = [dis[x + 1] - dis[x] for x in range(n - 1)]
out = 0
for j in dist:
if j > b:
out += b
else:
out += j
print(out)
| Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s199155115 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | print(max(int(input()) * int(input()) for i in [1, 1]))
| Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s570980246 | Accepted | p03826 | The input is given from Standard Input in the following format:
A B C D | nums = list(map(int, input().split()))
squares = [nums[0] * nums[1], nums[2] * nums[3]]
print(max(squares))
| Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s223572143 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | A,B,C,D=map(int,input().split().split().split())
x=(A*B)
y=(C*D)
if x>=y:
print(x)
else:
print(y) | Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s558640214 | Accepted | p03826 | The input is given from Standard Input in the following format:
A B C D | (*a,) = map(int, input().split())
print(max(a[0] * a[1], a[2] * a[3]))
| Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area.
* * * | s293609477 | Runtime Error | p03826 | The input is given from Standard Input in the following format:
A B C D | a,b,c,d = map(int, input().split())
print(a*b if a*b > d*c els | Statement
There are two rectangles. The lengths of the vertical sides of the first
rectangle are A, and the lengths of the horizontal sides of the first
rectangle are B. The lengths of the vertical sides of the second rectangle are
C, and the lengths of the horizontal sides of the second rectangle are D.
Print the area of the rectangle with the larger area. If the two rectangles
have equal areas, print that area. | [{"input": "3 5 2 7", "output": "15\n \n\nThe first rectangle has an area of 3\u00d75=15, and the second rectangle has an\narea of 2\u00d77=14. Thus, the output should be 15, the larger area.\n\n* * *"}, {"input": "100 600 200 300", "output": "60000"}] |
Print the sum of the weights of the Minimum Spanning Tree. | s414566189 | Wrong Answer | p02364 | |V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1
, where |V| is the number of vertices and |E| is the number of edges in the
graph. The graph vertices are named with the numbers 0, 1,..., |V|-1
respectively.
si and ti represent source and target verticess of i-th edge (undirected) and
wi represents the weight of the i-th edge. | Minimum Spanning Tree
Find the sum of weights of edges of the Minimum Spanning Tree for a given
weighted undirected graph G = (V, E). | [{"input": "4 6\n 0 1 2\n 1 2 1\n 2 3 1\n 3 0 1\n 0 2 3\n 1 3 5", "output": "3"}, {"input": "6 9\n 0 1 1\n 0 2 3\n 1 2 1\n 1 3 7\n 2 4 1\n 1 4 3\n 3 4 1\n 3 5 1\n 4 5 6", "output": "5"}] | |
Print the sum of the weights of the Minimum Spanning Tree. | s047730740 | Runtime Error | p02364 | |V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1
, where |V| is the number of vertices and |E| is the number of edges in the
graph. The graph vertices are named with the numbers 0, 1,..., |V|-1
respectively.
si and ti represent source and target verticess of i-th edge (undirected) and
wi represents the weight of the i-th edge. | n = int(input())
m = [
[-1 for i in range(n)] for j in range(n)
] # お隣さんへのコストを保存(この中から最小を探す)
v = set()
v.add(0)
for i in range(n): # 隣接の辺のコストを入力
nums = list(map(int, input().split()))
for j in range(n):
m[i][j] = nums[j]
def prim_mst(n):
isVisited = [False] * n
d = [0] + [2001] * (n - 1) # TとV-Tのを繋ぐ最小辺の重みを格納する
while True:
mincost = 2001
for i in range(n):
if not (isVisited[i]) and d[i] < mincost:
mincost = d[i]
u = i
isVisited[u] = True
if mincost == 2001: # mincostが2001になったら繰り返し作業終了
break
for v in range(n):
if (not isVisited[v]) and (m[u][v] != -1):
if m[u][v] < d[v]:
d[v] = m[u][v]
print(sum(d))
prim_mst(n)
| Minimum Spanning Tree
Find the sum of weights of edges of the Minimum Spanning Tree for a given
weighted undirected graph G = (V, E). | [{"input": "4 6\n 0 1 2\n 1 2 1\n 2 3 1\n 3 0 1\n 0 2 3\n 1 3 5", "output": "3"}, {"input": "6 9\n 0 1 1\n 0 2 3\n 1 2 1\n 1 3 7\n 2 4 1\n 1 4 3\n 3 4 1\n 3 5 1\n 4 5 6", "output": "5"}] |
Print the sum of the weights of the Minimum Spanning Tree. | s387905917 | Accepted | p02364 | |V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1
, where |V| is the number of vertices and |E| is the number of edges in the
graph. The graph vertices are named with the numbers 0, 1,..., |V|-1
respectively.
si and ti represent source and target verticess of i-th edge (undirected) and
wi represents the weight of the i-th edge. | #!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
input:
6 9
0 1 1
0 2 3
1 2 1
1 3 7
2 4 1
1 4 3
3 4 1
3 5 1
4 5 6
output:
5
"""
import sys
import heapq as hp
WHITE, GRAY, BLACK = 0, 1, 2
def generate_adj_table(v_table):
for each in v_table:
v_from, v_to, edge_weight = map(int, each)
init_adj_table[v_from][v_to] = edge_weight
init_adj_table[v_to][v_from] = edge_weight
return init_adj_table
def prim_span_tree():
distance[init_v] = 0
distance_heap = []
hp.heappush(distance_heap, (0, init_v))
while distance_heap:
current_vertex = hp.heappop(distance_heap)[1]
color[current_vertex] = BLACK
for adj_vertex, adj_weight in adj_table[current_vertex].items():
if color[adj_vertex] is not BLACK:
if adj_weight < distance[adj_vertex]:
distance[adj_vertex] = adj_weight
parent[adj_vertex] = current_vertex
color[adj_vertex] = GRAY
hp.heappush(distance_heap, (adj_weight, adj_vertex))
return distance
if __name__ == "__main__":
_input = sys.stdin.readlines()
vertices, edges = map(int, _input[0].split())
v_info = map(lambda x: x.split(), _input[1:])
parent = [-1] * vertices
distance = [float("inf")] * vertices
color = [WHITE] * vertices
init_v = 0
init_adj_table = tuple(dict() for _ in range(vertices))
adj_table = generate_adj_table(v_info)
print(sum(prim_span_tree()))
| Minimum Spanning Tree
Find the sum of weights of edges of the Minimum Spanning Tree for a given
weighted undirected graph G = (V, E). | [{"input": "4 6\n 0 1 2\n 1 2 1\n 2 3 1\n 3 0 1\n 0 2 3\n 1 3 5", "output": "3"}, {"input": "6 9\n 0 1 1\n 0 2 3\n 1 2 1\n 1 3 7\n 2 4 1\n 1 4 3\n 3 4 1\n 3 5 1\n 4 5 6", "output": "5"}] |
Print the sum of the weights of the Minimum Spanning Tree. | s464833890 | Accepted | p02364 | |V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1
, where |V| is the number of vertices and |E| is the number of edges in the
graph. The graph vertices are named with the numbers 0, 1,..., |V|-1
respectively.
si and ti represent source and target verticess of i-th edge (undirected) and
wi represents the weight of the i-th edge. | import sys
class UnionFind:
def __init__(self, node_size):
self._node = node_size
self.par = [i for i in range(self._node)]
self.rank = [0] * self._node
def find(self, ver):
if self.par[ver] == ver:
return ver
else:
self.par[ver] = self.find(self.par[ver])
return self.par[ver]
def unite(self, ver1, ver2):
ver1, ver2 = self.find(ver1), self.find(ver2)
if ver1 == ver2:
return
if self.rank[ver1] < self.rank[ver2]:
ver1, ver2 = ver2, ver1
self.par[ver2] = ver1
if self.rank[ver1] == self.rank[ver2]:
self.rank[ver1] += 1
def same(self, ver1, ver2):
return self.find(ver1) == self.find(ver2)
class Kruskal:
class Edge:
def __init__(self, u, v, cost):
self.u, self.v, self.cost = u, v, cost
def __lt__(self, another):
return self.cost < another.cost
def __init__(self, node_size):
self._node = node_size
self._edge_list = []
def add_edge(self, u, v, cost):
self._edge_list.append(self.Edge(u, v, cost))
def solve(self):
uf = UnionFind(self._node)
res = 0
edge_count = 0
sorted_edge_list = sorted(self._edge_list)
for e in sorted_edge_list:
if not uf.same(e.u, e.v):
uf.unite(e.u, e.v)
res += e.cost
edge_count += 1
if edge_count == self._node - 1:
break
return res
if __name__ == "__main__":
n, m = map(int, sys.stdin.readline().split())
kr = Kruskal(n)
for _ in range(m):
s, t, w = map(int, sys.stdin.readline().split())
kr.add_edge(s, t, w)
print(kr.solve())
| Minimum Spanning Tree
Find the sum of weights of edges of the Minimum Spanning Tree for a given
weighted undirected graph G = (V, E). | [{"input": "4 6\n 0 1 2\n 1 2 1\n 2 3 1\n 3 0 1\n 0 2 3\n 1 3 5", "output": "3"}, {"input": "6 9\n 0 1 1\n 0 2 3\n 1 2 1\n 1 3 7\n 2 4 1\n 1 4 3\n 3 4 1\n 3 5 1\n 4 5 6", "output": "5"}] |
Print the sum of the weights of the Minimum Spanning Tree. | s829248836 | Accepted | p02364 | |V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1
, where |V| is the number of vertices and |E| is the number of edges in the
graph. The graph vertices are named with the numbers 0, 1,..., |V|-1
respectively.
si and ti represent source and target verticess of i-th edge (undirected) and
wi represents the weight of the i-th edge. | class UnionFind:
def __init__(self, num):
self.parent = [i for i in range(num + 1)]
def find(self, node):
if self.parent[node] == node:
return node
self.parent[node] = self.find(self.parent[node])
return self.parent[node]
def union(self, node1, node2):
node1 = self.find(node1)
node2 = self.find(node2)
if node1 == node2:
return
if self.parent[node1] > self.parent[node2]:
node1, node2 = node2, node1
self.parent[node2] = node1
return
def same(self, node1, node2):
return self.find(node1) == self.find(node2)
# edges is array which consists of [cost, frm, to] elements
def kruskal(vertex_num, edges):
ans = 0
uf = UnionFind(vertex_num)
# sort edges in cost ascending order
edges.sort()
for edge in edges:
cost, frm, to = edge
if uf.same(frm, to) is False:
uf.union(frm, to)
ans += cost
return ans
v, e = map(int, input().split())
edges = []
for _ in range(e):
s, t, w = map(int, input().split())
edges.append([w, s, t])
print(kruskal(v, edges))
| Minimum Spanning Tree
Find the sum of weights of edges of the Minimum Spanning Tree for a given
weighted undirected graph G = (V, E). | [{"input": "4 6\n 0 1 2\n 1 2 1\n 2 3 1\n 3 0 1\n 0 2 3\n 1 3 5", "output": "3"}, {"input": "6 9\n 0 1 1\n 0 2 3\n 1 2 1\n 1 3 7\n 2 4 1\n 1 4 3\n 3 4 1\n 3 5 1\n 4 5 6", "output": "5"}] |
Print the sum of the weights of the Minimum Spanning Tree. | s159108841 | Accepted | p02364 | |V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1
, where |V| is the number of vertices and |E| is the number of edges in the
graph. The graph vertices are named with the numbers 0, 1,..., |V|-1
respectively.
si and ti represent source and target verticess of i-th edge (undirected) and
wi represents the weight of the i-th edge. | class UnionFind(object):
def __init__(self, n):
self.parent = {i: i for i in range(1, n + 1)}
self.size = {i: 1 for i in range(1, n + 1)}
def find(self, a):
if self.parent[a] != a:
self.parent[a] = self.find(self.parent[a])
return self.parent[a]
def getsize(self, a):
return self.size[self.find(a)]
def unite(self, a, b):
a = self.find(a)
b = self.find(b)
if a == b:
return
if self.size[a] > self.size[b]:
self.size[a] += self.size[b]
self.parent[b] = a
else:
self.size[b] += self.size[a]
self.parent[a] = b
def isunited(self, a, b):
return self.find(a) == self.find(b)
V, E = map(int, input().split())
uft = UnionFind(V)
l = [tuple(map(int, input().split())) for _ in range(E)]
l.sort(key=lambda x: x[2])
ans = 0
for s, t, w in l:
s += 1
t += 1
if not uft.isunited(s, t):
ans += w
uft.unite(s, t)
print(ans)
| Minimum Spanning Tree
Find the sum of weights of edges of the Minimum Spanning Tree for a given
weighted undirected graph G = (V, E). | [{"input": "4 6\n 0 1 2\n 1 2 1\n 2 3 1\n 3 0 1\n 0 2 3\n 1 3 5", "output": "3"}, {"input": "6 9\n 0 1 1\n 0 2 3\n 1 2 1\n 1 3 7\n 2 4 1\n 1 4 3\n 3 4 1\n 3 5 1\n 4 5 6", "output": "5"}] |
Print the sum of the weights of the Minimum Spanning Tree. | s198476562 | Accepted | p02364 | |V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1
, where |V| is the number of vertices and |E| is the number of edges in the
graph. The graph vertices are named with the numbers 0, 1,..., |V|-1
respectively.
si and ti represent source and target verticess of i-th edge (undirected) and
wi represents the weight of the i-th edge. | # https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/all/GRL_2_A
# 最小全域木、またお前か(プリムのアルゴリズムでできるやつ)
# ここではクラスカルのアルゴリズムを用いて解く。
# アイデアとしては辺の重みの小さい順にgreedyにノード同士をつないでいけば、必ず最小全域木になるでしょうというもの
# ただし木とならないような辺は省かなければ行けない。
# 辺を追加する際に木であることを崩さないように効率よく判定するのにunion-find木を使っている
class UnionFind:
def __init__(self, N):
self.N = N # ノード数
# 親ノードをしめす。負は自身が親ということ。
self.parent = [-1] * N # idxが各ノードに対応。
# 本で言うrankはこの実装では扱っていない。
def root(self, A): # 本で言うfindset
# print(A)
# ノード番号を受け取って一番上の親ノードの番号を帰す
if self.parent[A] < 0:
return A
self.parent[A] = self.root(self.parent[A]) # 経由したノードすべての親を上書き
return self.parent[A]
def size(self, A):
# ノード番号を受け取って、そのノードが含まれている集合のサイズを返す。
return -self.parent[self.root(A)]
def unite(self, A, B):
# ノード番号を2つ受け取って、そのノード同士をつなげる処理を行う。
# 引数のノードを直接つなぐ代わりに、親同士を連結する処理にする。
A = self.root(A)
B = self.root(B)
# すでにくっついている場合
if A == B:
return False
# 大きい方に小さい方をくっつけたほうが処理が軽いので大小比較
if self.size(A) < self.size(B):
A, B = B, A
# くっつける
self.parent[A] += self.parent[B] # sizeの更新
self.parent[B] = (
A # self.rootが呼び出されればBにくっついてるノードもすべて親がAだと上書きされる
)
return True
def is_in_same(self, A, B):
return self.root(A) == self.root(B)
Edges = []
# load data
n_V, n_E = list(map(int, input().split()))
for _ in range(n_E):
s, t, w = list(map(int, input().split()))
Edges.append((w, s, t))
def kruskal(N, Edges):
"""
Nは頂点数、Ndgesは各要素が(w,s,t)を前提としたlist
"""
edges = sorted(Edges)
ret = 0
union = UnionFind(N)
n_edges = 0
for w, s, t in edges:
if n_edges == N - 1:
# 全域木になったら早期終了可
break
if union.is_in_same(s, t):
continue
union.unite(s, t)
ret += w
n_edges += 1
return ret
print(kruskal(n_V, Edges))
| Minimum Spanning Tree
Find the sum of weights of edges of the Minimum Spanning Tree for a given
weighted undirected graph G = (V, E). | [{"input": "4 6\n 0 1 2\n 1 2 1\n 2 3 1\n 3 0 1\n 0 2 3\n 1 3 5", "output": "3"}, {"input": "6 9\n 0 1 1\n 0 2 3\n 1 2 1\n 1 3 7\n 2 4 1\n 1 4 3\n 3 4 1\n 3 5 1\n 4 5 6", "output": "5"}] |
For each dataset, print the day (please see the following words) in a line.
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday | s510459460 | Accepted | p00027 | The input is a sequence of datasets. The end of the input is indicated by a
line containing one zero. Each dataset consists of two integers m and d
separated by a single space in a line. These integers respectively represent
the month and the day.
The number of datasets is less than or equal to 50. | week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
days = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
while 1:
key = 2
data = list(map(int, input().split()))
if data[0] == 0 and data[1] == 0:
break
month = data[0]
day = data[1]
for i in range(month - 1):
key += days[i]
key += day
print(week[key % 7])
| What day is today?
Your task is to write a program which reads a date (from 2004/1/1 to
2004/12/31) and prints the day of the date. Jan. 1, 2004, is Thursday. Note
that 2004 is a leap year and we have Feb. 29. | [{"input": "1\n 2 29\n 0 0", "output": "Thursday\n Sunday"}] |
For each dataset, print the day (please see the following words) in a line.
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday | s395343386 | Runtime Error | p00027 | The input is a sequence of datasets. The end of the input is indicated by a
line containing one zero. Each dataset consists of two integers m and d
separated by a single space in a line. These integers respectively represent
the month and the day.
The number of datasets is less than or equal to 50. | week = {
"1": "Thursday",
"2": "Friday",
"3": "Saturday",
"4": "Sunday",
"5": "Monday",
"6": "Tuesday",
"7": "Wednesday",
}
month = {
"1": 0,
"2": 31,
"3": 60,
"4": 91,
"5": 121,
"6": 152,
"7": 182,
"8": 213,
"9": 244,
"10": 274,
"11": 305,
"12": 335,
}
m, d = input().split()
while m != "0":
days = month[m] + int(d)
print(week[str(days % 7)])
m, d = input().split()
| What day is today?
Your task is to write a program which reads a date (from 2004/1/1 to
2004/12/31) and prints the day of the date. Jan. 1, 2004, is Thursday. Note
that 2004 is a leap year and we have Feb. 29. | [{"input": "1\n 2 29\n 0 0", "output": "Thursday\n Sunday"}] |
Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
* * * | s884490515 | Runtime Error | p02919 | Input is given from Standard Input in the following format:
N
P_1 P_2 \ldots P_N | class MinSegmentTree:
def __init__(self, n):
self.n = n
self.INF = n - 1
self.size = 1
while self.size < n:
self.size *= 2
self.node = [self.INF] * (2 * self.size - 1)
def update(self, i, val):
i += self.size - 1
self.node[i] = val
while i > 0:
i = (i - 1) // 2
self.node[i] = min(self.node[2 * i + 1], self.node[2 * i + 2])
def get_min(self, begin, end):
begin += self.size - 1
end += self.size - 1
s = self.INF
while begin < end:
if (end - 1) & 1:
end -= 1
s = min(s, self.node[end])
if (begin - 1) & 1:
s = min(s, self.node[begin])
begin += 1
begin = (begin - 1) // 2
end = (end - 1) // 2
return s
class MaxSegmentTree:
def __init__(self, n):
self.n = n
self.INF = 0
self.size = 1
while self.size < n:
self.size *= 2
self.node = [self.INF] * (2 * self.size - 1)
def update(self, i, val):
i += self.size - 1
self.node[i] = val
while i > 0:
i = (i - 1) // 2
self.node[i] = max(self.node[2 * i + 1], self.node[2 * i + 2])
def get_max(self, begin, end):
begin += self.size - 1
end += self.size - 1
s = self.INF
while begin < end:
if (end - 1) & 1:
end -= 1
s = max(s, self.node[end])
if (begin - 1) & 1:
s = max(s, self.node[begin])
begin += 1
begin = (begin - 1) // 2
end = (end - 1) // 2
return s
n = int(input())
a = list(map(int, input().split()))
ind = {}
for i in range(n):
ind[a[i]] = i
smax = MaxSegmentTree(n + 2)
smin = MinSegmentTree(n + 2)
ans = 0
for i in range(n):
pos = ind[n - i] + 1
l2 = smax.get_max(0, pos)
l1 = smax.get_max(0, l2)
r1 = smin.get_min(pos + 1, n + 3)
r2 = smin.get_min(r1 + 1, n + 3)
smin.update(pos, pos)
smax.update(pos, pos)
ans += ((l2 - l1) * (r1 - pos) + (r2 - r1) * (pos - l2)) * (n - i)
print(ans)
| Statement
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest
value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. | [{"input": "3\n 2 3 1", "output": "5\n \n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.\n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "30\n \n\n* * *"}, {"input": "8\n 8 2 7 3 4 5 6 1", "output": "136"}] |
Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
* * * | s601234571 | Accepted | p02919 | Input is given from Standard Input in the following format:
N
P_1 P_2 \ldots P_N | def make_tree(n):
i = 2
while True:
if i >= n * 2:
tree = [-1] * i
break
else:
i *= 2
return tree
def initialization(tree, a):
l = len(tree) // 2
for i in range(l, l + len(a)):
tree[i] = a[i - l]
for i in range(l - 1, 0, -1):
tree[i] = max(tree[2 * i], tree[2 * i + 1])
return
def update(tree, i, x):
i += len(tree) // 2
tree[i] = x
i //= 2
while True:
if i == 0:
break
tree[i] = max(tree[2 * i], tree[2 * i + 1])
i //= 2
return
def find(tree, s, t):
s += len(tree) // 2
t += len(tree) // 2
ans = -1
while True:
if s > t:
break
if s % 2 == 0:
s //= 2
else:
ans = max(ans, tree[s])
s = (s + 1) // 2
if t % 2 == 1:
t //= 2
else:
ans = max(ans, tree[t])
t = (t - 1) // 2
return ans
n = int(input())
p = list(map(int, input().split()))
q = [0] * n
tree = make_tree(n)
a = [[-1, -1] for _ in range(n)]
b = [[-1, -1] for _ in range(n)]
update(tree, p[0] - 1, 0)
for i in range(1, n):
a[i][0] = find(tree, p[i], n - 1)
if not a[i][0] == -1:
update(tree, p[a[i][0]] - 1, -1)
a[i][1] = find(tree, p[i], n - 1)
update(tree, p[a[i][0]] - 1, a[i][0])
update(tree, p[i] - 1, i)
# print(a)
initialization(tree, [-1] * n)
update(tree, p[-1] - 1, 0)
for i in range(1, n):
j = n - i - 1
b[j][0] = find(tree, p[j], n - 1)
if not b[j][0] == -1:
update(tree, p[n - b[j][0] - 1] - 1, -1)
b[j][1] = find(tree, p[j], n - 1)
update(tree, p[n - b[j][0] - 1] - 1, b[j][0])
update(tree, p[j] - 1, i)
if not b[j][0] == -1:
b[j][0] = n - b[j][0] - 1
if not b[j][1] == -1:
b[j][1] = n - b[j][1] - 1
# print(b)
ans = 0
for i in range(n):
if p[i] == n:
continue
if b[i][0] == -1:
if not a[i][1] == -1:
x = (n - i) * (a[i][0] - a[i][1])
elif not a[i][0] == -1:
x = (n - i) * (a[i][0] + 1)
elif a[i][0] == -1:
if not b[i][1] == -1:
x = (i + 1) * (b[i][1] - b[i][0])
elif not b[i][0] == -1:
x = (i + 1) * (n - b[i][0])
elif a[i][1] == b[i][1] == -1:
x = (i - a[i][0]) * (n - b[i][0]) + (b[i][0] - i) * (a[i][0] + 1)
elif b[i][1] == -1:
x = (b[i][0] - i) * (a[i][0] - a[i][1]) + (i - a[i][0]) * (n - b[i][0])
elif a[i][1] == -1:
x = (i - a[i][0]) * (b[i][1] - b[i][0]) + (b[i][0] - i) * (a[i][0] + 1)
else:
x = (i - a[i][0]) * (b[i][1] - b[i][0]) + (b[i][0] - i) * (a[i][0] - a[i][1])
ans += p[i] * x
# print(ans)
print(ans)
| Statement
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest
value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. | [{"input": "3\n 2 3 1", "output": "5\n \n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.\n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "30\n \n\n* * *"}, {"input": "8\n 8 2 7 3 4 5 6 1", "output": "136"}] |
Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
* * * | s795153758 | Accepted | p02919 | Input is given from Standard Input in the following format:
N
P_1 P_2 \ldots P_N | n = int(input())
p = list(map(int, input().split()))
p_ind = [(p[i], i + 1) for i in range(n)]
p_ind.sort(reverse=True)
# 平方分割します。
max_n = 10**5
div_n = int(max_n ** (1 / 2)) + 1
a = [[] for _ in range(div_n)]
a[0] = [0, 0]
a[(n + 1) // div_n].append(n + 1)
a[(n + 1) // div_n].append(n + 1)
a_count = [0] * (div_n)
a_count[0] += 2
a_count[(n + 1) // div_n] += 2
import bisect
ans = 0
for tmp in p_ind:
val, i = tmp
group = i // div_n
j = bisect.bisect(a[group], i)
# left
if j > 1:
left1 = a[group][j - 1]
left2 = a[group][j - 2]
elif j == 1:
left1 = a[group][j - 1]
lg = group - 1
while True:
if a_count[lg] > 0:
break
lg -= 1
left2 = a[lg][-1]
else:
lg = group - 1
while True:
if a_count[lg] > 0:
break
lg -= 1
if a_count[lg] >= 2:
left1 = a[lg][-1]
left2 = a[lg][-2]
else:
left1 = a[lg][-1]
while True:
lg -= 1
if a_count[lg] > 0:
break
left2 = a[lg][-1]
# right
if a_count[group] >= j + 2:
right1 = a[group][j]
right2 = a[group][j + 1]
elif a_count[group] >= j + 1:
right1 = a[group][j]
rg = group
while True:
rg += 1
if a_count[rg] > 0:
break
right2 = a[rg][0]
else:
rg = group
while True:
rg += 1
if a_count[rg] > 0:
break
if a_count[rg] >= 2:
right1 = a[rg][0]
right2 = a[rg][1]
else:
right1 = a[rg][0]
while True:
rg += 1
if a_count[rg] > 0:
break
right2 = a[rg][0]
ans += ((right2 - right1) * (i - left1) + (right1 - i) * (left1 - left2)) * val
a[group].insert(j, i)
a_count[group] += 1
print(ans)
| Statement
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest
value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. | [{"input": "3\n 2 3 1", "output": "5\n \n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.\n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "30\n \n\n* * *"}, {"input": "8\n 8 2 7 3 4 5 6 1", "output": "136"}] |
Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
* * * | s069203648 | Accepted | p02919 | Input is given from Standard Input in the following format:
N
P_1 P_2 \ldots P_N | n = int(input())
p = list(map(int, input().split()))
r = list(range(n))
l = list(range(n))
I = [-1] * (n + 1)
for i, P in enumerate(p):
I[P] = i
# I[1~Nまでの数字] = 0,1,...
# index を求めている
ans = 0
for N, index in enumerate(I[1:], 1): # 1からスタート
L = index - 1
if L >= 0:
L = l[L]
R = index + 1
if R < n:
R = r[R]
l[R - 1] = L
r[L + 1] = R
# 0 <= L , R < n
if L >= 0:
L2 = L - 1
if L2 >= 0:
L2 = l[L2]
ans += N * (L - L2) * (R - index)
if R < n:
R2 = R + 1
if R2 < n:
R2 = r[R2]
ans += N * (index - L) * (R2 - R)
print(ans)
| Statement
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest
value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. | [{"input": "3\n 2 3 1", "output": "5\n \n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.\n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "30\n \n\n* * *"}, {"input": "8\n 8 2 7 3 4 5 6 1", "output": "136"}] |
Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
* * * | s915593394 | Runtime Error | p02919 | Input is given from Standard Input in the following format:
N
P_1 P_2 \ldots P_N | import numpy as np
N = int(input())
P = np.array(input().split(), dtype=np.int64)
A = np.zeros((N - 1, N - 1))
B = np.zeros((N - 1, N - 1))
for i in range(N - 1):
A[i, i] = min(P[i], P[i + 1])
B[i, i] = max(P[i], P[i + 1])
for i in np.arange(N - 2)[::-1]:
for j in np.arange(i + 1, N - 1):
if A[i, j - 1] == A[i + 1, j]:
if B[i, j - 1] == B[i + 1, j]:
A[i, j] = A[i + 1, j]
B[i, j] = B[i + 1, j]
else:
A[i, j] = min(B[i, j - 1], B[i + 1, j])
B[i, j] = max(B[i, j - 1], B[i + 1, j])
elif B[i, j - 1] == B[i + 1, j]:
A[i, j] = max(A[i, j - 1], A[i + 1, j])
B[i, j] = B[i, j - 1]
elif B[i, j - 1] == A[i + 1, j]:
A[i, j] = A[i + 1, j]
B[i, j] = B[i + 1, j]
else:
A[i, j] = A[i, j - 1]
B[i, j] = B[i, j - 1]
print(int(np.sum(A)))
| Statement
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest
value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. | [{"input": "3\n 2 3 1", "output": "5\n \n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.\n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "30\n \n\n* * *"}, {"input": "8\n 8 2 7 3 4 5 6 1", "output": "136"}] |
Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
* * * | s432874707 | Wrong Answer | p02919 | Input is given from Standard Input in the following format:
N
P_1 P_2 \ldots P_N | from collections import Counter
from heapq import heappush, heappop
def solve(n, sss):
keys = [-1] + sorted(sss.keys())
m = keys.pop()
if sss[m] != 1:
return False
k = keys[-1]
q = [(-n, -m)]
while k != -1:
r, s = heappop(q)
r = -r
s = -s
if r == 0:
continue
if k >= s:
tmp = set()
while q:
tmp.add((-r, -s))
r, s = heappop(q)
r, s = -r, -s
if k < s:
for item in tmp:
heappush(q, item)
break
else:
return False
heappush(q, (-(r - 1), -k))
heappush(q, (-(r - 1), -s))
sss[k] -= 1
if sss[k] == 0:
keys.pop()
k = keys[-1]
# print(s, r, q, keys, sss)
return True
n = int(input())
sss = Counter(map(int, input().split()))
print("Yes" if solve(n, sss) else "No")
| Statement
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest
value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. | [{"input": "3\n 2 3 1", "output": "5\n \n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.\n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "30\n \n\n* * *"}, {"input": "8\n 8 2 7 3 4 5 6 1", "output": "136"}] |
Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
* * * | s730438560 | Accepted | p02919 | Input is given from Standard Input in the following format:
N
P_1 P_2 \ldots P_N | class BalancingTree:
class node:
def __init__(self, v, p):
self.value = v
self.pivot = p
self.left = None
self.right = None
def __init__(self, n):
self.N = n
self.root = self.node(1 << n, 1 << n)
def debug(self):
def debug_info(nd_):
return (
nd_.value - 1,
nd_.pivot - 1,
nd_.left.value - 1 if nd_.left else -1,
nd_.right.value - 1 if nd_.right else -1,
)
def debug_node(nd):
re = []
if nd.left:
re += debug_node(nd.left)
if nd.value:
re.append(debug_info(nd))
if nd.right:
re += debug_node(nd.right)
return re
print("Debug - root =", self.root.value - 1, debug_node(self.root)[:50])
def append(self, v): # v を追加(その時点で v はない前提)
v += 1
nd = self.root
while True:
if v == nd.value:
# v がすでに存在する場合に何か処理が必要ならここに書く
return 0
else:
mi, ma = min(v, nd.value), max(v, nd.value)
if mi < nd.pivot:
nd.value = ma
if nd.left:
nd = nd.left
v = mi
else:
p = nd.pivot
nd.left = self.node(mi, p - (p & -p) // 2)
break
else:
nd.value = mi
if nd.right:
nd = nd.right
v = ma
else:
p = nd.pivot
nd.right = self.node(ma, p + (p & -p) // 2)
break
def leftmost(self, nd):
if nd.left:
return self.leftmost(nd.left)
return nd
def rightmost(self, nd):
if nd.right:
return self.rightmost(nd.right)
return nd
def find_l(self, v): # vより真に小さいやつの中での最大値(なければ-1)
v += 1
nd = self.root
prev = 0
if nd.value < v:
prev = nd.value
while True:
if v <= nd.value:
if nd.left:
nd = nd.left
else:
return prev - 1
else:
prev = nd.value
if nd.right:
nd = nd.right
else:
return prev - 1
def find_r(self, v): # vより真に大きいやつの中での最小値(なければRoot)
v += 1
nd = self.root
prev = 0
if nd.value > v:
prev = nd.value
while True:
if v < nd.value:
prev = nd.value
if nd.left:
nd = nd.left
else:
if prev == 2**self.N:
return None
else:
return prev - 1
else:
if nd.right:
nd = nd.right
else:
if prev == 2**self.N:
return None
else:
return prev - 1
@property
def find_max(self):
return self.find_l((1 << self.N) - 1)
@property
def find_min(self):
return self.find_r(-1)
def delete(
self, v, nd=None, prev=None
): # 値がvのノードがあれば削除(なければ何もしない)
v += 1
if not nd:
nd = self.root
if not prev:
prev = nd
while v != nd.value:
prev = nd
if v <= nd.value:
if nd.left:
nd = nd.left
else:
return
else:
if nd.right:
nd = nd.right
else:
return
if (not nd.left) and (not nd.right):
if nd.value < prev.value:
prev.left = None
else:
prev.right = None
elif not nd.left:
if nd.value < prev.value:
prev.left = nd.right
else:
prev.right = nd.right
elif not nd.right:
if nd.value < prev.value:
prev.left = nd.left
else:
prev.right = nd.left
else:
nd.value = self.leftmost(nd.right).value
self.delete(nd.value - 1, nd.right, nd)
def __contains__(self, v: int) -> bool:
return self.find_r(v - 1) == v
def resolve():
N = int(input())
P = list(map(int, input().split()))
A = []
for i in range(N):
A.append((P[i], i))
A.sort(reverse=True)
BT = BalancingTree(17)
ans = 0
for i in range(N):
p, idx = A[i]
BT.append(idx)
l1 = BT.find_l(idx) #
l2 = BT.find_l(l1)
r1 = BT.find_r(idx)
if r1 is None:
r1 = N
r2 = BT.find_r(r1)
if r2 is None:
r2 = N
cmb = (l1 - l2) * (r1 - idx) + (r2 - r1) * (idx - l1)
ans += p * cmb
print(ans)
if __name__ == "__main__":
resolve()
| Statement
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest
value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. | [{"input": "3\n 2 3 1", "output": "5\n \n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.\n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "30\n \n\n* * *"}, {"input": "8\n 8 2 7 3 4 5 6 1", "output": "136"}] |
Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
* * * | s044063359 | Accepted | p02919 | Input is given from Standard Input in the following format:
N
P_1 P_2 \ldots P_N | class BIT:
def __init__(self, n):
self.n = n
self.data = [0] * (n + 1)
def ope(self, x, y):
return x + y
def update(self, i, v):
while i <= self.n:
self.data[i] = self.ope(self.data[i], v)
i += i & -i
def query(self, i):
ret = 0
while 0 < i:
ret = self.ope(self.data[i], ret)
i &= i - 1
return ret
def lowerBound(self, w):
if w <= 0:
return 0
x, k = 0, 2 ** self.n.bit_length()
while k:
if x + k <= self.n and self.data[x + k] < w:
w -= self.data[x + k]
x += k
k >>= 1
return x + 1
n = int(input())
p = list(map(int, input().split()))
pos = [0] * n
for i, pi in enumerate(p):
pos[pi - 1] = i
bit1 = BIT(n)
bit2 = BIT(n)
ans = 0
for pi in range(n, 0, -1):
i = pos[pi - 1]
cur = bit1.query(i + 1)
if cur == 0:
left_0 = i + 1
left_1 = 0
else:
tmp = bit1.lowerBound(cur)
tmp2 = bit1.lowerBound(cur - 1)
left_0 = i + 1 - tmp
left_1 = tmp - tmp2
# 更新
bit1.update(i + 1, 1)
j = n - i - 1
cur = bit2.query(j + 1)
if cur == 0:
right_0 = j + 1
right_1 = 0
else:
tmp = bit2.lowerBound(cur)
tmp2 = bit2.lowerBound(cur - 1)
right_0 = j + 1 - tmp
right_1 = tmp - tmp2
# 更新
bit2.update(j + 1, 1)
ans += pi * (left_0 * right_1 + left_1 * right_0)
print(ans)
| Statement
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest
value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. | [{"input": "3\n 2 3 1", "output": "5\n \n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.\n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "30\n \n\n* * *"}, {"input": "8\n 8 2 7 3 4 5 6 1", "output": "136"}] |
Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
* * * | s001394960 | Accepted | p02919 | Input is given from Standard Input in the following format:
N
P_1 P_2 \ldots P_N | from operator import itemgetter
def solve(n, ppp):
qqq = sorted(enumerate(ppp), key=itemgetter(1))
left_next_index = list(range(-1, n - 1)) + [-1, -1]
right_next_index = list(range(1, n + 1)) + [n, n]
ans = 0
for i, p in qqq:
l2 = left_next_index[i]
l1 = left_next_index[l2]
r1 = right_next_index[i]
r2 = right_next_index[r1]
ans += p * ((l2 - l1) * (r1 - i) + (r2 - r1) * (i - l2))
left_next_index[r1] = l2
right_next_index[l2] = r1
return ans
n = int(input())
ppp = list(map(int, input().split()))
print(solve(n, ppp))
| Statement
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest
value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. | [{"input": "3\n 2 3 1", "output": "5\n \n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.\n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "30\n \n\n* * *"}, {"input": "8\n 8 2 7 3 4 5 6 1", "output": "136"}] |
Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
* * * | s410086333 | Wrong Answer | p02919 | Input is given from Standard Input in the following format:
N
P_1 P_2 \ldots P_N | n, ans, sum = int(input()), 0, 0
P = list(map(int, input().split()))
first, second = P[n - 1], P[n - 1]
first_pos, second_pos = n - 1, n - 1
li = 0
count = 1
for i in range(n - 2, -1, -1):
x = P[i]
if x > first:
second = first
first = x
second_pos = first_pos
first_pos = i
sum = li + (n - second_pos) * second
li = 0
elif second < x and x < first:
second, second_pos = x, i
sum = second * (n - i - 1)
count += 1
li += x
else:
sum += x
count += 1
li += x
ans += sum
print(ans)
| Statement
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest
value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. | [{"input": "3\n 2 3 1", "output": "5\n \n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.\n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "30\n \n\n* * *"}, {"input": "8\n 8 2 7 3 4 5 6 1", "output": "136"}] |
Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
* * * | s456489326 | Runtime Error | p02919 | Input is given from Standard Input in the following format:
N
P_1 P_2 \ldots P_N | # -*- coding: utf-8 -*-
import sys
import bisect
from collections import deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
# template
def solve():
n = int(readline())
p = tuple(map(int, readline().split()))
# print(p)
idx = [0] * (n)
for i in range(n):
idx[p[i] - 1] = i
perm = deque([-1, idx[-1], n])
# print(perm)
count = 0
for i in range(n - 2, -1, -1):
id = bisect.bisect_left(perm, idx[i])
if id != 1:
count += (i + 1) * ((perm[id - 1] - perm[id - 2]) * (perm[id] - idx[i]))
if id != n - i:
count += (i + 1) * ((perm[id + 1] - perm[id]) * (idx[i] - perm[id - 1]))
perm.insert(id, idx[i])
print(count)
return
if __name__ == "__main__":
solve()
| Statement
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest
value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. | [{"input": "3\n 2 3 1", "output": "5\n \n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.\n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "30\n \n\n* * *"}, {"input": "8\n 8 2 7 3 4 5 6 1", "output": "136"}] |
Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
* * * | s079695099 | Wrong Answer | p02919 | Input is given from Standard Input in the following format:
N
P_1 P_2 \ldots P_N | N = int(input())
X = list(map(int, input().split()))
index = [[i, X[i]] for i in range(N)]
Xi = list(sorted(index, key=lambda x: x[1], reverse=True))
# print(Xi)
result = 0
for i, j in Xi:
if N != j:
# print((N-i-1)*j)
result += (N - i - 1) * j
print(result + N * (N - 1) * X[-1] - (N - 1))
| Statement
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest
value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. | [{"input": "3\n 2 3 1", "output": "5\n \n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.\n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "30\n \n\n* * *"}, {"input": "8\n 8 2 7 3 4 5 6 1", "output": "136"}] |
Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
* * * | s237583219 | Runtime Error | p02919 | Input is given from Standard Input in the following format:
N
P_1 P_2 \ldots P_N | import sys
import random
class RandomizedBinarySearchTree:
def __init__(self, n):
n += 10
self.n = n
self.children = [[0] * n, [0] * n]
self.values = [0] * n
self.counts = [0] * n
self.indices = list(range(n - 2, -1, -1))
self.root = -1
def merge(self, left_root, right_root):
children = self.children
counts = self.counts
li = left_root
ri = right_root
stack = []
while li != -1 and ri != -1:
if random.randrange(counts[li] + counts[ri]) < counts[li]:
stack.append((li, 1))
li = children[1][li]
else:
stack.append((ri, 0))
ri = children[0][ri]
i = li if li != -1 else ri
while stack:
pi, is_right = stack.pop()
children[is_right][pi] = i
counts[pi] = counts[children[0][pi]] + counts[children[1][pi]] + 1
i = pi
return i
def split(self, root, x):
i = root
lefts, rights = self.children
values = self.values
counts = self.counts
l_stack = []
r_stack = []
while i != -1:
if x < values[i]:
r_stack.append(i)
i = lefts[i]
else:
l_stack.append(i)
i = rights[i]
li, ri = -1, -1
while l_stack:
pi = l_stack.pop()
rights[pi] = li
counts[pi] = counts[lefts[pi]] + counts[li] + 1
li = pi
while r_stack:
pi = r_stack.pop()
lefts[pi] = ri
counts[pi] = counts[ri] + counts[rights[pi]] + 1
ri = pi
return li, ri
def insert(self, x):
ni = self.indices.pop()
self.children[0][ni] = self.children[1][ni] = -1
self.values[ni] = x
self.counts[ni] = 1
li, ri = self.split(self.root, x)
self.root = self.merge(self.merge(li, ni), ri)
def delete(self, x):
li, mri = self.split(self.root, x - 1)
mi, ri = self.split(mri, x)
if mi == -1:
self.root = self.merge(li, ri)
return
self.indices.append(mi)
self.root = self.merge(li, ri)
return
def upper_bound(self, x, default=-1):
i = self.root
lefts, rights = self.children
values = self.values
counts = self.counts
y = default
c = counts[i]
j = 0
while i != -1:
if x < values[i]:
y = values[i]
c = j + counts[lefts[i]]
i = lefts[i]
else:
j += counts[lefts[i]] + 1
i = rights[i]
return y, c
def lower_bound(self, x, default=-1):
i = self.root
lefts, rights = self.children
values = self.values
counts = self.counts
y = default
c = counts[i]
j = 0
while i != -1:
if x <= values[i]:
y = values[i]
c = j + counts[lefts[i]]
i = lefts[i]
else:
j += counts[lefts[i]] + 1
i = rights[i]
return y, c
def get_k_th(self, k, default=-1):
i = self.root
children = self.children
values = self.values
counts = self.counts
if counts[i] <= k:
return default
j = k
while i != -1:
left_count = counts[children[0][i]]
if left_count == j:
return values[i]
elif left_count > j:
i = children[0][i]
else:
j -= left_count + 1
i = children[1][i]
return default
def debug_print(self):
print("Lefts ", self.children[0])
print("Rights", self.children[1])
print("Values", self.values)
print("Counts", self.counts)
self._debug_print(self.root, 0)
def _debug_print(self, i, depth):
if i != -1:
self._debug_print(self.children[0][i], depth + 1)
print(" " * depth, self.values[i], self.counts[i])
self._debug_print(self.children[1][i], depth + 1)
q = int(input())
rbst = RandomizedBinarySearchTree(q)
for line in sys.stdin:
t, x = map(int, line.split())
if t == 1:
rbst.insert(x)
else:
y = rbst.get_k_th(x - 1)
print(y)
rbst.delete(y)
| Statement
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest
value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. | [{"input": "3\n 2 3 1", "output": "5\n \n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.\n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "30\n \n\n* * *"}, {"input": "8\n 8 2 7 3 4 5 6 1", "output": "136"}] |
Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
* * * | s246780522 | Accepted | p02919 | Input is given from Standard Input in the following format:
N
P_1 P_2 \ldots P_N | N = int(input())
P_id = [0] * (N + 1) # P_id[i]は順列Pに置けるiのindex, P[0]は無視
for index, p in enumerate(map(int, input().split())):
P_id[p] = index
left_next_index = list(range(-1, N - 1)) + [
"うんこ",
-1,
] # + [- 1]ではないことに注意, left_next_index[r1] = l1においてr1 = Nの場合があるから
right_next_index = list(range(1, N + 1)) + [N, "うんこ"]
res = 0
for p in range(1, N):
l1 = left_next_index[P_id[p]]
l2 = left_next_index[l1]
r1 = right_next_index[P_id[p]]
r2 = right_next_index[r1]
res += p * ((l1 - l2) * (r1 - P_id[p]) + (P_id[p] - l1) * (r2 - r1))
left_next_index[r1] = l1
right_next_index[l1] = r1
print(res)
| Statement
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest
value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. | [{"input": "3\n 2 3 1", "output": "5\n \n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.\n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "30\n \n\n* * *"}, {"input": "8\n 8 2 7 3 4 5 6 1", "output": "136"}] |
Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
* * * | s445425579 | Wrong Answer | p02919 | Input is given from Standard Input in the following format:
N
P_1 P_2 \ldots P_N | n = int(input())
pn = list(map(int, input().split()[:n]))
pn.sort()
k = n - 1
print(k * (k + 1) * (2 * k + 1) // 6)
| Statement
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest
value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. | [{"input": "3\n 2 3 1", "output": "5\n \n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.\n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "30\n \n\n* * *"}, {"input": "8\n 8 2 7 3 4 5 6 1", "output": "136"}] |
Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
* * * | s747378921 | Wrong Answer | p02919 | Input is given from Standard Input in the following format:
N
P_1 P_2 \ldots P_N | N = int(input())
ls = [int(s) for s in input().split()]
count = 0
def plus(ls):
L = len(ls)
if L > 1:
val_ls = ls
a = max(val_ls)
i1 = ls.index(a)
val_ls[i1] = 0
b = max(val_ls)
i2 = ls.index(b)
val_ls[i1] = a
i3 = max([i1, i2])
i4 = min([i1, i2])
# print(a,b,i4,i3)
return [(i4 + 1) * (L - i3) * b, i4, i3]
else:
return 0
S = [ls]
T = []
ans = 0
sign = 1
while S != []:
while S != []:
Ls = S[0]
e = plus(Ls)
if e == 0:
S.pop(0)
else:
# print(Ls,e)
ans += e[0] * sign
S.pop(0)
S.append(Ls[: e[2]])
S.append(Ls[e[1] + 1 :])
T.append(Ls[e[1] + 1 : e[2]])
S = T
sign *= -1
# print(S,T,ans)
print(ans)
| Statement
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest
value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. | [{"input": "3\n 2 3 1", "output": "5\n \n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.\n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "30\n \n\n* * *"}, {"input": "8\n 8 2 7 3 4 5 6 1", "output": "136"}] |
Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
* * * | s616367115 | Wrong Answer | p02919 | Input is given from Standard Input in the following format:
N
P_1 P_2 \ldots P_N | n = int(input())
p = list(map(int, input().split()))
x = [[() for i in range(n)] for j in range(n - 1)]
total = 0
for i in range(n - 1):
x[i][i + 1] = (min(p[i], p[i + 1]), max(p[i], p[i + 1]))
total += x[i][i + 1][0]
for j in range(1, n - 1):
for i in range(n - j - 1):
if x[i][i + j][1] == x[i + 1][i + j + 1][1]:
x[i][i + j + 1] = (
max(x[i][i + j][0], x[i + 1][i + j + 1][0]),
x[i][i + j][1],
)
elif x[i][i + j][1] == x[i + 1][i + j + 1][0]:
x[i][i + j + 1] = x[i + 1][i + j + 1]
elif x[i][i + j][0] == x[i + 1][i + j + 1][1]:
x[i][i + j + 1] = x[i][i + j]
else:
x[i][i + j + 1] = (
min(x[i][i + j][1], x[i + 1][i + j + 1][1]),
max(x[i][i + j][1], x[i + 1][i + j + 1][1]),
)
total += x[i][i + j + 1][0]
| Statement
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest
value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. | [{"input": "3\n 2 3 1", "output": "5\n \n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.\n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "30\n \n\n* * *"}, {"input": "8\n 8 2 7 3 4 5 6 1", "output": "136"}] |
Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
* * * | s280110099 | Runtime Error | p02919 | Input is given from Standard Input in the following format:
N
P_1 P_2 \ldots P_N | N = int(input())
c = list(map(int, input().split(" ")))
V = []
cnt = [0]
def dfs(s):
# print(V)
if len(s) == 2:
return min(s)
b = dfs(s[:-1])
a = dfs(s[1:])
if s[:-1] not in V:
cnt[0] += b
if s[1:] not in V:
cnt[0] += a
V.append(s[1:])
V.append(s[:-1])
if a == b and a < s[0]:
return min(s[0], s[-1])
else:
return max(a, b)
t = dfs(c)
print(cnt[0] + t)
| Statement
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest
value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. | [{"input": "3\n 2 3 1", "output": "5\n \n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.\n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "30\n \n\n* * *"}, {"input": "8\n 8 2 7 3 4 5 6 1", "output": "136"}] |
Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
* * * | s569213004 | Accepted | p02919 | Input is given from Standard Input in the following format:
N
P_1 P_2 \ldots P_N | import heapq
from collections import defaultdict
N = int(input())
P = list(map(int, input().split()))
d_r = defaultdict(lambda: N + 1)
h_r = []
d_r2 = defaultdict(lambda: N + 1)
h_r2 = []
for i in range(N):
p = P[i]
while h_r2:
q = heapq.heappop(h_r2)
if q < p:
d_r2[q] = p
else:
heapq.heappush(h_r2, q)
break
while h_r:
q = heapq.heappop(h_r)
if q < p:
d_r[q] = p
heapq.heappush(h_r2, q)
else:
heapq.heappush(h_r, q)
break
heapq.heappush(h_r, p)
d_l = defaultdict(lambda: 0)
h_l = []
d_l2 = defaultdict(lambda: 0)
h_l2 = []
for i in range(N - 1, -1, -1):
p = P[i]
while h_l2:
q = heapq.heappop(h_l2)
if q < p:
d_l2[q] = p
else:
heapq.heappush(h_l2, q)
break
while h_l:
q = heapq.heappop(h_l)
if q < p:
d_l[q] = p
heapq.heappush(h_l2, q)
else:
heapq.heappush(h_l, q)
break
heapq.heappush(h_l, p)
d = {}
for i in range(N):
d[P[i]] = i
d[N + 1] = N
d[0] = -1
ans = 0
for i in range(N):
x = d[d_l2[P[i]]]
y = d[d_l[P[i]]]
z = d[d_r[P[i]]]
w = d[d_r2[P[i]]]
ans += P[i] * ((y - x) * (z - i) + (w - z) * (i - y))
print(ans)
| Statement
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest
value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. | [{"input": "3\n 2 3 1", "output": "5\n \n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.\n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "30\n \n\n* * *"}, {"input": "8\n 8 2 7 3 4 5 6 1", "output": "136"}] |
Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
* * * | s717835622 | Wrong Answer | p02919 | Input is given from Standard Input in the following format:
N
P_1 P_2 \ldots P_N | INF = 10 * 6
class SegTree:
def __init__(self, A):
n_ = len(A)
self.n = 1
while self.n < n_:
self.n *= 2
self.tree = [INF for _ in range(2 * self.n - 1)]
for k, a in enumerate(A):
self.update(k, a)
def update(self, k, a):
k += self.n - 1
self.tree[k] = a
while k > 0:
k = (k - 1) // 2
self.tree[k] = min(self.tree[k * 2 + 1], self.tree[k * 2 + 2])
def query(self, l, r):
l += self.n - 1
r += self.n - 2
res = INF
while r - l > 1:
if l & 1 == 0:
res = min(res, self.tree[l])
if r & 1 == 1:
res = min(res, self.tree[r])
r -= 1
l = l // 2
r = (r - 1) // 2
if l == r:
res = min(res, self.tree[l])
else:
res = min(min(res, self.tree[l]), self.tree[r])
return res
n = int(input())
p = list(map(int, input().split()))
pii = []
for i, pi in enumerate(p):
pii.append([pi, i])
pii.sort(reverse=True)
st_r = SegTree([n] * (n + 1))
st_l = SegTree([1] * (n + 1))
ans = 0
for pi, i in pii:
# print("###")
# print(pi, i)
# print(st_r.tree)
if pi < n:
l = -st_l.query(0, i)
r = st_r.query(i + 1, n)
# print(l, r)
if l != -1:
l2 = l - 1
if l > 0:
l2 = -st_l.query(0, l)
ans += (l - l2) * (r - i) * pi
if r != n:
r2 = r + 1
if r < n - 1:
r2 = st_r.query(r + 1, n)
ans += (i - l) * (r2 - r) * pi
st_r.update(i, i)
st_l.update(i, -i)
print(ans)
| Statement
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest
value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. | [{"input": "3\n 2 3 1", "output": "5\n \n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.\n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "30\n \n\n* * *"}, {"input": "8\n 8 2 7 3 4 5 6 1", "output": "136"}] |
Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
* * * | s892755634 | Wrong Answer | p02919 | Input is given from Standard Input in the following format:
N
P_1 P_2 \ldots P_N | class SegmentTree:
def __init__(self, L, op, ide):
self.op = op
self.ide = ide
self.sz = len(L)
self.n = 1
self.s = 1
for i in range(1000):
self.n *= 2
self.s += 1
if self.n >= self.sz:
break
self.node = [self.ide] * (2 * self.n - 1)
for i in range(self.sz):
self.node[i + self.n - 1] = L[i]
for i in range(self.n - 2, -1, -1):
self.node[i] = self.op(self.node[i * 2 + 1], self.node[i * 2 + 2])
def add(self, a, x):
k = a + self.n - 1
self.node[k] += x
for i in range(1000):
k = (k - 1) // 2
self.node[k] = self.op(self.node[2 * k + 1], self.node[2 * k + 2])
if k <= 0:
break
def substitute(self, a, x):
k = a + self.n - 1
self.node[k] = x
for i in range(1000):
k = (k - 1) // 2
self.node[k] = self.op(self.node[2 * k + 1], self.node[2 * k + 2])
if k <= 0:
break
def get_one(self, a):
k = a + self.n - 1
return self.node[k]
def get(self, l, r):
res = self.ide
n = self.n
if self.sz <= r or 0 > l:
print("ERROR: the indice are wrong.")
return False
for i in range(self.s):
count = 2**i - 1
a = (r + 1) // n
b = (l - 1) // n
if a - b == 3:
res = self.op(self.node[count + b + 1], res)
res = self.op(self.node[count + b + 2], res)
right = a * n
left = (b + 1) * n - 1
break
if a - b == 2:
res = self.op(self.node[count + b + 1], res)
right = a * n
left = (b + 1) * n - 1
break
n = n // 2
# left
n1 = n // 2
for j in range(i + 1, self.s):
count = 2**j - 1
a = (left + 1) // n1
b = (l - 1) // n1
if a - b == 2:
res = self.op(self.node[count + b + 1], res)
left = (b + 1) * n1 - 1
n1 = n1 // 2
# right
n1 = n // 2
for j in range(i + 1, self.s):
count = 2**j - 1
a = (r + 1) // n1
b = (right - 1) // n1
if a - b == 2:
res = self.op(self.node[count + b + 1], res)
right = a * n1
n1 = n1 // 2
return res
N = int(input())
P = list(map(int, input().split()))
ST = SegmentTree(P, max, -(10**27))
def bisect(i, j, ST, num, index, P, Smin, Smin_rev, left_flag):
if left_flag:
if i == j:
if P[i] > num:
return i
else:
return i - 1
if Smin[j + 1] == num:
return -1
if P[j] > num:
return j
else:
if i == j:
if P[i] > num:
return i
else:
return i + 1
if Smin_rev[i - 1] == num:
return N
if P[i] > num:
return i
if not left_flag:
while j - i > 1:
if ST.get(index + 1, (i + j) // 2) < num:
i = (i + j) // 2
else:
j = (i + j) // 2
return j
else:
while j - i > 1:
if ST.get((i + j) // 2, index - 1) < num:
j = (i + j) // 2
else:
i = (i + j) // 2
return i
Smin = []
smin = -(10**27)
for i in range(N):
smin = max(smin, P[i])
Smin.append(smin)
Smin_rev = []
smin = -(10**27)
for i in range(N):
smin = max(smin, P[N - i - 1])
Smin_rev.append(smin)
Smin_rev = Smin_rev[::-1]
score = 0
for i in range(N):
if i == 0:
right1 = bisect(i + 1, N - 1, ST, P[i], i, P, Smin, Smin_rev, False)
if right1 >= N - 1:
right2 = N
else:
right2 = bisect(
right1 + 1, N - 1, ST, P[i], right1, P, Smin, Smin_rev, False
)
right1, right2 = right1 - i, right2 - i
left1 = 1
left2 = 1
elif i == N - 1:
left1 = bisect(0, i - 1, ST, P[i], i, P, Smin, Smin_rev, True)
if left1 <= 0:
left2 = -1
else:
left2 = bisect(0, left1 - 1, ST, P[i], left1, P, Smin, Smin_rev, True)
left1, left2 = i - left1, i - left2
right1 = 1
right2 = 1
else:
right1 = bisect(i + 1, N - 1, ST, P[i], i, P, Smin, Smin_rev, False)
if right1 >= N - 1:
right2 = N
else:
right2 = bisect(
right1 + 1, N - 1, ST, P[i], right1, P, Smin, Smin_rev, False
)
right1, right2 = right1 - i, right2 - i
left1 = bisect(0, i - 1, ST, P[i], i, P, Smin, Smin_rev, True)
if left1 <= 0:
left2 = -1
else:
left2 = bisect(0, left1 - 1, ST, P[i], left1, P, Smin, Smin_rev, True)
# print("a",left1,left2)
left1, left2 = i - left1, i - left2
score += (left1 * (right2 - right1) + (left2 - left1) * right1) * P[i]
# print(left1,left2,right1,right2)
print(score)
| Statement
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest
value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. | [{"input": "3\n 2 3 1", "output": "5\n \n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.\n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "30\n \n\n* * *"}, {"input": "8\n 8 2 7 3 4 5 6 1", "output": "136"}] |
Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
* * * | s120926351 | Wrong Answer | p02919 | Input is given from Standard Input in the following format:
N
P_1 P_2 \ldots P_N | N = int(input())
P = list(map(int, input().split()))
ans = list()
for x in range(N - 1):
l = list()
l.append(P[x])
l.append(P[x + 1])
if len(l) == 2:
ans.append(sorted(l)[-2])
for y in range(N - 1 - x):
if P[x + 1 + y] > ans[-1] and P[x + 1 + y] < max(l):
ans.append(P[x + 1 + y])
else:
ans.append(ans[-1])
print(sum(ans))
| Statement
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest
value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. | [{"input": "3\n 2 3 1", "output": "5\n \n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.\n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "30\n \n\n* * *"}, {"input": "8\n 8 2 7 3 4 5 6 1", "output": "136"}] |
Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
* * * | s351289412 | Wrong Answer | p02919 | Input is given from Standard Input in the following format:
N
P_1 P_2 \ldots P_N | n = int(input())
ListP = [int(val) for val in input().split()]
l = 0
r = 2
xlr = 0
tempMax = tempMax2 = max2 = 0
while l < n - 1:
tempMax2 = min(ListP[l : l + 2])
xlr += tempMax2
tempMax = max(ListP[l : l + 2])
while r < n:
if tempMax2 == max2:
xlr += (n - 1 - r) * max2
break
if tempMax2 < ListP[r]:
if tempMax < ListP[r]:
tempMax2 = tempMax
tempMax = ListP[r]
else:
tempMax2 = ListP[r]
xlr += tempMax2
r += 1
if l == 0:
max2 = tempMax2
l += 1
r = l + 2
print(xlr)
| Statement
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest
value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. | [{"input": "3\n 2 3 1", "output": "5\n \n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.\n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "30\n \n\n* * *"}, {"input": "8\n 8 2 7 3 4 5 6 1", "output": "136"}] |
Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
* * * | s151534882 | Wrong Answer | p02919 | Input is given from Standard Input in the following format:
N
P_1 P_2 \ldots P_N | n = int(input())
p = list(map(int, input().split()))
x = 0
for l in range(n - 1):
for i in range(2, n - l):
if p[l] <= p[l + 1]:
first = p[l + 1]
second = p[l]
else:
first = p[l]
second = p[l + 1]
if i != 2:
for k in range(1, i - 1):
if p[l + k + 1] >= first:
second = first
first = p[l + k + 1]
elif p[l + k + 1] > second:
second = p[l + k + 1]
x += second
else:
x += second
print(int(x))
| Statement
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest
value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. | [{"input": "3\n 2 3 1", "output": "5\n \n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.\n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "30\n \n\n* * *"}, {"input": "8\n 8 2 7 3 4 5 6 1", "output": "136"}] |
Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
* * * | s322527136 | Runtime Error | p02919 | Input is given from Standard Input in the following format:
N
P_1 P_2 \ldots P_N | n = int(input())
p = [int(i) for i in input().split()]
ans = 0
for i in range(n): # 左端から
if i == 0: # 一番左なら
if p[i] == n:
ans += 0
elif p[i] == n - 1:
idx = p.index(n)
ans += p[i] * (n - idx)
else:
idx = []
for j in reversed(range(p[i] + 1, n + 1)):
idx.append(p.index(j))
idx.sort()
temp = idx[1] - idx[0]
ans += p[i] * temp
elif i == n - 1: # 右端なら
if p[i] == n:
ans += 0
elif p[i] == n - 1:
idx = p.index(n)
ans += p[i] * (idx + 1)
else:
idx = []
for j in reversed(range(p[i] + 1, n + 1)):
idx.append(p.index(j))
idx.sort()
temp = idx[-1] - idx[-2]
ans += p[i] * temp
else: # 真ん中なら
if p[i] == n:
ans += 0
elif p[i] == n - 1:
idx = p.index(n)
chi = min(i, idx)
ook = max(i, idx)
temp = (chi + 1) * (n - ook)
ans += p[i] * temp
else: # 「大」が2つ以上あるとき
left = p[:i]
right = p[i + 1 :]
if max(left) < p[i]: # 小●大
idx = []
for j in range(i + 1, n):
if p[j] > p[i] and (len(idx) < 2):
idx.append(j)
elif len(idx) == 2:
break
else:
idx = idx
temp = (i + 1) * (idx[1] - idx[0])
ans += p[i] * temp
elif max(right) > p[i]: # 大●小
idx = []
for j in reversed(range(i)):
if p[j] > p[i] and (len(idx) < 2):
idx.append(j)
elif len(idx) == 2:
break
else:
idx = idx
temp = (n - i) * (idx[0] - idx[1])
ans += temp * p[i]
else:
idx_left = []
for k in reversed(range(i)):
if p[k] > p[i] and (len(idx_left) < 2):
idx_left.append(k)
elif len(idx_left) == 2:
break
else:
idx_left = idx_left
if len(idx_left) < 2:
idx_left.append(-1)
idx_right = []
for l in range(i + 1, n):
if p[l] > p[i] and (len(idx_right) < 2):
idx_right.append(l)
elif len(idx_right) == 2:
break
else:
idx_right = idx_right
if len(idx_right) < 2:
idx_right.append(n)
temp_left = (idx_right[0] - i) * (idx_left[0] - idx_left[1])
temp_right = (idx_right[1] - idx_right[0]) * (i - idx_left[0])
temp = temp_left + temp_right
ans += p[i] * temp
print(ans)
| Statement
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest
value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}. | [{"input": "3\n 2 3 1", "output": "5\n \n\nX_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.\n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "30\n \n\n* * *"}, {"input": "8\n 8 2 7 3 4 5 6 1", "output": "136"}] |
Print the number of ID cards that allow us to pass all the gates alone.
* * * | s645509876 | Accepted | p03037 | Input is given from Standard Input in the following format:
N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M | (*t,) = map(int, open(0).read().split())
print(max(0, min(t[3::2]) - max(t[2::2]) + 1))
| Statement
We have N ID cards, and there are M gates.
We can pass the i-th gate if we have one of the following ID cards: the L_i-
th, (L_i+1)-th, ..., and R_i-th ID cards.
How many of the ID cards allow us to pass all the gates alone? | [{"input": "4 2\n 1 3\n 2 4", "output": "2\n \n\nTwo ID cards allow us to pass all the gates alone, as follows:\n\n * The first ID card does not allow us to pass the second gate.\n * The second ID card allows us to pass all the gates.\n * The third ID card allows us to pass all the gates.\n * The fourth ID card does not allow us to pass the first gate.\n\n* * *"}, {"input": "10 3\n 3 6\n 5 7\n 6 9", "output": "1\n \n\n* * *"}, {"input": "100000 1\n 1 100000", "output": "100000"}] |
Print the number of ID cards that allow us to pass all the gates alone.
* * * | s982334396 | Wrong Answer | p03037 | Input is given from Standard Input in the following format:
N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M | _, __, *d = map(int, open(0).read().split())
print(min(d[1::2]) - max(d[::2]) + 1)
| Statement
We have N ID cards, and there are M gates.
We can pass the i-th gate if we have one of the following ID cards: the L_i-
th, (L_i+1)-th, ..., and R_i-th ID cards.
How many of the ID cards allow us to pass all the gates alone? | [{"input": "4 2\n 1 3\n 2 4", "output": "2\n \n\nTwo ID cards allow us to pass all the gates alone, as follows:\n\n * The first ID card does not allow us to pass the second gate.\n * The second ID card allows us to pass all the gates.\n * The third ID card allows us to pass all the gates.\n * The fourth ID card does not allow us to pass the first gate.\n\n* * *"}, {"input": "10 3\n 3 6\n 5 7\n 6 9", "output": "1\n \n\n* * *"}, {"input": "100000 1\n 1 100000", "output": "100000"}] |
Print the number of ID cards that allow us to pass all the gates alone.
* * * | s702715600 | Wrong Answer | p03037 | Input is given from Standard Input in the following format:
N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M | ss = input().split(" ")
N = int(ss[0])
M = int(ss[1])
x = 0
y = N
for i in range(M):
tt = input().split(" ")
if int(tt[0]) > x:
x = int(tt[0])
if int(tt[1]) < y:
y = int(tt[1])
print(int(y - x + 1))
| Statement
We have N ID cards, and there are M gates.
We can pass the i-th gate if we have one of the following ID cards: the L_i-
th, (L_i+1)-th, ..., and R_i-th ID cards.
How many of the ID cards allow us to pass all the gates alone? | [{"input": "4 2\n 1 3\n 2 4", "output": "2\n \n\nTwo ID cards allow us to pass all the gates alone, as follows:\n\n * The first ID card does not allow us to pass the second gate.\n * The second ID card allows us to pass all the gates.\n * The third ID card allows us to pass all the gates.\n * The fourth ID card does not allow us to pass the first gate.\n\n* * *"}, {"input": "10 3\n 3 6\n 5 7\n 6 9", "output": "1\n \n\n* * *"}, {"input": "100000 1\n 1 100000", "output": "100000"}] |
Print the number of ID cards that allow us to pass all the gates alone.
* * * | s533127667 | Wrong Answer | p03037 | Input is given from Standard Input in the following format:
N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M | data = input().split()
N = int(data[0])
M = int(data[1])
data_gate = [input().split() for i in range(M)]
data_min = []
data_max = []
for i in range(M):
data_min.append(int(data_gate[i][0]))
data_max.append(int(data_gate[i][1]))
m = max(data_min)
M = min(data_max)
ans = M - m + 1
print(ans)
| Statement
We have N ID cards, and there are M gates.
We can pass the i-th gate if we have one of the following ID cards: the L_i-
th, (L_i+1)-th, ..., and R_i-th ID cards.
How many of the ID cards allow us to pass all the gates alone? | [{"input": "4 2\n 1 3\n 2 4", "output": "2\n \n\nTwo ID cards allow us to pass all the gates alone, as follows:\n\n * The first ID card does not allow us to pass the second gate.\n * The second ID card allows us to pass all the gates.\n * The third ID card allows us to pass all the gates.\n * The fourth ID card does not allow us to pass the first gate.\n\n* * *"}, {"input": "10 3\n 3 6\n 5 7\n 6 9", "output": "1\n \n\n* * *"}, {"input": "100000 1\n 1 100000", "output": "100000"}] |
Print the number of ID cards that allow us to pass all the gates alone.
* * * | s714107802 | Accepted | p03037 | Input is given from Standard Input in the following format:
N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M | n, m, *a = map(int, open(0).read().split())
b = min(a[1::2]) - max(a[::2]) + 1
print(b if b > 0 else 0)
| Statement
We have N ID cards, and there are M gates.
We can pass the i-th gate if we have one of the following ID cards: the L_i-
th, (L_i+1)-th, ..., and R_i-th ID cards.
How many of the ID cards allow us to pass all the gates alone? | [{"input": "4 2\n 1 3\n 2 4", "output": "2\n \n\nTwo ID cards allow us to pass all the gates alone, as follows:\n\n * The first ID card does not allow us to pass the second gate.\n * The second ID card allows us to pass all the gates.\n * The third ID card allows us to pass all the gates.\n * The fourth ID card does not allow us to pass the first gate.\n\n* * *"}, {"input": "10 3\n 3 6\n 5 7\n 6 9", "output": "1\n \n\n* * *"}, {"input": "100000 1\n 1 100000", "output": "100000"}] |
Print the number of ID cards that allow us to pass all the gates alone.
* * * | s941831020 | Accepted | p03037 | Input is given from Standard Input in the following format:
N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M | k, g = list(map(int, input().split()))
id = [list(map(int, input().split())) for i in range(g)]
l, r = list(zip(*id))
print(min(r) - max(l) + 1) if min(r) - max(l) >= 0 else print(0)
| Statement
We have N ID cards, and there are M gates.
We can pass the i-th gate if we have one of the following ID cards: the L_i-
th, (L_i+1)-th, ..., and R_i-th ID cards.
How many of the ID cards allow us to pass all the gates alone? | [{"input": "4 2\n 1 3\n 2 4", "output": "2\n \n\nTwo ID cards allow us to pass all the gates alone, as follows:\n\n * The first ID card does not allow us to pass the second gate.\n * The second ID card allows us to pass all the gates.\n * The third ID card allows us to pass all the gates.\n * The fourth ID card does not allow us to pass the first gate.\n\n* * *"}, {"input": "10 3\n 3 6\n 5 7\n 6 9", "output": "1\n \n\n* * *"}, {"input": "100000 1\n 1 100000", "output": "100000"}] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.