input stringlengths 20 127k | target stringlengths 20 119k | problem_id stringlengths 6 6 |
|---|---|---|
N = int(eval(input()))
A = [int(i)-1 for i in input().split()]
ans = 0
for i,a in enumerate(A):
if i == A[a] and i < a:
ans += 1
print(ans) | N = int(eval(input()))
A = [int(i) - 1 for i in input().split()]
print((len([1 for i, a in enumerate(A) if i == A[a] and i < a]))) | p03993 |
N = int(eval(input()))
A = [int(i) for i in input().split()]
arr = [[False for i in range(N)] for j in range(N)]
cnt = 0
for ind, val in enumerate(A):
arr[ind][val-1] = True
if arr[val-1][ind] == True:
cnt += 1
print(cnt) | N = int(eval(input()))
A = [int(i) for i in input().split()]
cnt = 0
for i, a in enumerate(A):
if A[a-1]-1 == i:
cnt += 1
print((cnt//2)) | p03993 |
N = int(eval(input()))
A = [int(e)-1 for e in input().split()]
ad = [set() for _ in range(N)]
for i, a in enumerate(A):
ad[a].add(i)
ans = 0
for i, a in enumerate(A):
if a in ad[i]:
ans += 1
print((ans//2)) | N = int(eval(input()))
A = [int(e)-1 for e in input().split()]
ans = 0
for i, a in enumerate(A):
if A[a] == i:
ans += 1
ans //= 2
print(ans) | p03993 |
N = int(eval(input()))
li = [int(i) for i in input().split()]
# タプルを作って裏返したペアが存在するかどうか
tupple = []
for i in range(N) :
s = str(i+1) + ' ' + str(li[i])
tupple.append(s)
c = 0
for tup in tupple :
t = tup.split()
needle = t[1] + ' ' + t[0]
if needle in tupple :
c += 1
print((c//2)) | N = int(eval(input()))
li = [int(i) for i in input().split()]
c = 0
for i in range(1,N+1) :
# iを基準に行って帰ってこれればOK
if (i == li[li[i-1]-1]) :
c += 1
print((c//2)) | p03993 |
a,p=list(map(int,input().split()))
pye=int((a*3+p)/2)
print(pye) | a,p=list(map(int,input().split()))
print((int((a*3+p)/2))) | p03029 |
A, P = list(map(int, input().split()))
P = A * 3 + P
print((P // 2)) | A, P = list(map(int, input().split()))
print(((A * 3 + P) // 2)) | p03029 |
a,p = list(map(int,input().split()))
print(((3*a+p)//2)) | a,b = list(map(int,input().split()))
print(((3*a+b)//2)) | p03029 |
import math
i=list(map(int,input().split()))
a=i[0]*3+i[1]
print((math.floor(a/2))) | a,p=list(map(int,input().split()))
print(((a*3+p)//2)) | p03029 |
a,p = list(map(int, input().split()))
n = 3*a
ans = (n+p) // 2
print(ans) | a,p = list(map(int, input().split()))
num =(3*a + p)//2
print(num) | p03029 |
A, P = list(map(int, input().split()))
print(((A * 3 + P) // 2)) | A, P = list(map(int, input().split()))
P += A * 3
print((P // 2)) | p03029 |
A,P = list(map(int,input().split()))
print(((A*3+P)//2)) | a, p = list(map(int, input().split()))
print(((a * 3 + p) // 2))
| p03029 |
A,P = list(map(int,input().split()))
a=((P + A * 3) // 2)
print(a) | A,P = list(map(int,input().split()))
print(((P + A * 3) // 2))
| p03029 |
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
l = [None for i in range(n)]
for i in range(n):l[i] = I()
return l
def LIR(n):
l = [None for i in range(n)]
for i in range(n):l[i] = LI()
return l
def SR(n):
l = [None for i in range(n)]
for i in range(n):l[i] = S()
return l
def LSR(n):
l = [None for i in range(n)]
for i in range(n):l[i] = LS()
return l
sys.setrecursionlimit(1000000)
mod = 1000000007
#A
def A():
a,p = LI()
print(((a*3+p)//2))
return
#B
def B():
n = I()
return
#C
def C():
n = I()
return
#D
def D():
n = I()
return
#E
def E():
n = I()
return
#F
def F():
n = I()
return
#Solve
if __name__ == "__main__":
A()
| #!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
res = list(sys.stdin.readline())
if res[-1] == "\n":
return res[:-1]
return res
def IR(n):
return [I() for i in range(n)]
def LIR(n):
return [LI() for i in range(n)]
def SR(n):
return [S() for i in range(n)]
def LSR(n):
return [LS() for i in range(n)]
sys.setrecursionlimit(1000000)
mod = 1000000007
def solve():
a,p = LI()
p += 3*a
print((p>>1))
return
#Solve
if __name__ == "__main__":
solve()
| p03029 |
A,P = list(map(int,input().split()))
n = 3*A+P
print((n//2)) | A,P = list(map(int,input().split()))
print(((3*A+P)//2)) | p03029 |
A, P = list(map(int, input().split()))
P += A * 3
pie = P // 2
print(pie)
| def resolve():
a, p = list(map(int, input().split()))
print(((a*3+p) // 2))
return
if __name__ == "__main__":
resolve()
| p03029 |
A,P = list(map(int,input().split()))
print(((3*A+P)//2)) | print((eval(input().replace(" ","*3+"))//2)) | p03029 |
A, P = list(map(int, input().split()))
P += 3 * A
print((P//2)) | a,p=list(map(int,input().split()))
print((int((3 * a + p) / 2)))
| p03029 |
A, P = list(map(int, input().split()))
x = (A * 3) + P
if x >= 2:
y = x / 2
else:
y = 0
print((int(y))) | A, P = list(map(int, input().split()))
x = A*3 + P
print((x//2)) | p03029 |
A,P = list(map(int,input().split()))
P = P + A*3
print((P//2)) | A,P = list(map(int,input().split()))
P += A*3
print((P//2)) | p03029 |
A, P = list(map(int, input().split()))
print(((A * 3 + P) // 2)) | a, p = list(map(int, input().split()))
print(((a*3 + p) // 2)) | p03029 |
#coding:utf-8
N,Q = list(map(int,input().split()))
rank = [0 for i in range(N)]
class Tree:
def __init__(self,par,key):
self.par = par
self.key = key
List = [Tree(i,i) for i in range(N)]
for i in range(Q):
com, x,y = list(map(int,input().split()))
while type(x) != int or List[x].par != x:
x = List[x].par.key
a = rank[x]
while type(y) != int or List[y].par != y:
y = List[y].par.key
b = rank[y]
if com == 0:
if a <= b:
if a == b:
rank[x] += 1
List[y].par = List[x]
else:
List[x].par = List[y]
else:
if x == y:
print("1")
else:
print("0")
| #coding:utf-8
import time
N,Q = list(map(int,input().split()))
rank = [0 for i in range(N)]
class Tree:
def __init__(self,par,key):
self.par = par
self.key = key
List = [Tree(i,i) for i in range(N)]
for i in range(Q):
com, x,y = list(map(int,input().split()))
x1,y1 = x,y
while List[x1].par != x1:
x1 = List[x1].par
a = rank[x1]
while List[y1].par != y1:
y1 = List[y1].par
b = rank[y1]
if com == 0:
if a <= b:
if a == b:
rank[x1] += 1
List[y1].par = x1
else:
List[x1].par = y1
else:
if x1 == y1:
print("1")
else:
print("0")
| p02343 |
#coding:utf-8
import time
N,Q = list(map(int,input().split()))
rank = [0 for i in range(N)]
class Tree:
def __init__(self,par,key):
self.par = par
self.key = key
List = [Tree(i,i) for i in range(N)]
for i in range(Q):
com, x,y = list(map(int,input().split()))
x1,y1 = x,y
while List[x1].par != x1:
x1 = List[x1].par
a = rank[x1]
while List[y1].par != y1:
y1 = List[y1].par
b = rank[y1]
if com == 0:
if a <= b:
if a == b:
rank[x1] += 1
List[y1].par = x1
else:
List[x1].par = y1
else:
if x1 == y1:
print("1")
else:
print("0")
| #coding:utf-8
import time
N,Q = list(map(int,input().split()))
rank = [0 for i in range(N)]
class Tree:
def __init__(self,par,key):
self.par = par
self.key = key
List = [Tree(i,i) for i in range(N)]
def unit(x,y):
while List[x].par != x:
x = List[x].par
a = rank[x]
while List[y].par != y:
y = List[y].par
b = rank[y]
if a <= b:
if a == b:
rank[x] += 1
List[y].par = x
else:
List[x].par = y
def Find(x,y):
Q = []
P = []
while List[x].par != x:
x = List[x].par
Q.append(x)
while List[y].par != y:
y = List[y].par
P.append(y)
for xi in Q:
List[xi].par = x
rank[xi] = 1
for yi in P:
List[yi].par = y
rank[yi] = 1
if x == y:
print((1))
else:
print((0))
for i in range(Q):
com, x,y = list(map(int,input().split()))
if com == 0:
unit(x,y)
else:
Find(x,y)
| p02343 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
input:
5 12
0 1 4
0 2 3
1 1 2
1 3 4
1 1 4
1 3 2
0 1 3
1 2 4
1 3 0
0 0 4
1 0 2
1 3 0
output:
0
0
1
1
1
0
1
1
"""
import sys
class UnionFind(object):
__slots__ = ('size', 'rank', 'p_list')
def __init__(self, size):
self.size = int(size)
self.rank = [0] * self.size
self.p_list = [i for i in range(self.size)]
def find_set(self, x):
if x != self.p_list[x]:
self.p_list[x] = self.find_set(self.p_list[x])
return self.p_list[x]
def same(self, x, y):
return self.find_set(x) == self.find_set(y)
def link(self, x, y):
if self.rank[x] > self.rank[y]:
self.p_list[y] = x
else:
self.p_list[x] = y
if self.rank[x] == self.rank[y]:
self.rank[y] += 1
def unite(self, x, y):
self.link(self.find_set(x), self.find_set(y))
def action(cmd):
for _, each in enumerate(cmd):
cmd, ele_1, ele_2 = list(map(int, each))
if cmd == 0:
case.unite(ele_1, ele_2)
elif cmd == 1:
if case.same(ele_1, ele_2):
print('1')
else:
print('0')
return case
if __name__ == '__main__':
_input = sys.stdin.readlines()
ele_num, cmd_num = list(map(int, _input[0].split()))
cmd_list = [x.split() for x in _input[1:]]
case = UnionFind(size=ele_num)
ans = action(cmd_list) | #!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
input:
5 12
0 1 4
0 2 3
1 1 2
1 3 4
1 1 4
1 3 2
0 1 3
1 2 4
1 3 0
0 0 4
1 0 2
1 3 0
output:
0
0
1
1
1
0
1
1
"""
import sys
class UnionFind(object):
__slots__ = ('size', 'rank', 'p_list')
def __init__(self, size):
self.size = int(size)
self.rank = [0] * self.size
self.p_list = [i for i in range(self.size)]
def _find(self, x):
if x != self.p_list[x]:
self.p_list[x] = self._find(self.p_list[x])
return self.p_list[x]
def same(self, x, y):
return self._find(x) == self._find(y)
def link(self, x, y):
if self.rank[x] > self.rank[y]:
self.p_list[y] = x
else:
self.p_list[x] = y
if self.rank[x] == self.rank[y]:
self.rank[y] += 1
def union(self, x, y):
self.link(self._find(x), self._find(y))
def action(cmd):
for _, each in enumerate(cmd):
cmd, ele_1, ele_2 = list(map(int, each))
if cmd == 0:
case.union(ele_1, ele_2)
elif cmd == 1:
if case.same(ele_1, ele_2):
print('1')
else:
print('0')
return case
if __name__ == '__main__':
_input = sys.stdin.readlines()
ele_num, cmd_num = list(map(int, _input[0].split()))
cmd_list = [x.split() for x in _input[1:]]
case = UnionFind(size=ele_num)
ans = action(cmd_list) | p02343 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
input:
5 12
0 1 4
0 2 3
1 1 2
1 3 4
1 1 4
1 3 2
0 1 3
1 2 4
1 3 0
0 0 4
1 0 2
1 3 0
output:
0
0
1
1
1
0
1
1
"""
import sys
class UnionFind(object):
__slots__ = ('size', 'tree_height_rank_list', 'ancestor_list')
def __init__(self, size):
self.size = int(size)
self.tree_height_rank_list = [0] * self.size
self.ancestor_list = [i for i in range(self.size)]
def _find(self, x):
if x != self.ancestor_list[x]:
self.ancestor_list[x] = self._find(self.ancestor_list[x])
return self.ancestor_list[x]
def same(self, x, y):
return self._find(x) == self._find(y)
def link(self, x, y):
if self.tree_height_rank_list[x] > self.tree_height_rank_list[y]:
self.ancestor_list[y] = x
else:
self.ancestor_list[x] = y
if self.tree_height_rank_list[x] == self.tree_height_rank_list[y]:
self.tree_height_rank_list[y] += 1
def union(self, x, y):
self.link(self._find(x), self._find(y))
def action(cmd):
for _, each in enumerate(cmd):
cmd, ele_1, ele_2 = list(map(int, each))
if cmd == 0:
case.union(ele_1, ele_2)
elif cmd == 1:
if case.same(ele_1, ele_2):
print('1')
else:
print('0')
return case
if __name__ == '__main__':
_input = sys.stdin.readlines()
ele_num, cmd_num = list(map(int, _input[0].split()))
cmd_list = [x.split() for x in _input[1:]]
case = UnionFind(size=ele_num)
ans = action(cmd_list) | #!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
input:
5 12
0 1 4
0 2 3
1 1 2
1 3 4
1 1 4
1 3 2
0 1 3
1 2 4
1 3 0
0 0 4
1 0 2
1 3 0
output:
0
0
1
1
1
0
1
1
"""
import sys
class UnionFind(object):
__slots__ = ('size', 'tree_height_rank_list', 'ancestor_list')
def __init__(self, size):
self.size = int(size)
self.tree_height_rank_list = [0] * self.size
self.ancestor_list = [i for i in range(self.size)]
def _find(self, x):
if x != self.ancestor_list[x]:
self.ancestor_list[x] = self._find(self.ancestor_list[x])
return self.ancestor_list[x]
def same(self, x, y):
return self._find(x) == self._find(y)
def link(self, x, y):
if self.tree_height_rank_list[x] > self.tree_height_rank_list[y]:
self.ancestor_list[y] = x
else:
self.ancestor_list[x] = y
if self.tree_height_rank_list[x] == self.tree_height_rank_list[y]:
self.tree_height_rank_list[y] += 1
def union(self, x, y):
self.link(self._find(x), self._find(y))
def action(_cmd_list):
for _, each in enumerate(_cmd_list):
cmd, ele_1, ele_2 = list(map(int, each))
if cmd == 0:
case.union(ele_1, ele_2)
elif cmd == 1:
if case.same(ele_1, ele_2):
print('1')
else:
print('0')
return case
if __name__ == '__main__':
_input = sys.stdin.readlines()
ele_num, cmd_num = list(map(int, _input[0].split()))
cmd_list = [x.split() for x in _input[1:]]
case = UnionFind(size=ele_num)
ans = action(cmd_list) | p02343 |
n, q = list(map(int, input().split()))
s =[set([i]) for i in range(n)]
p =[i for i in range(n)]
for i in range(q):
com, x, y = list(map(int, input().split()))
if com:
if y in s[p[x]]: print((1))
else: print((0))
else:
s[p[x]] = s[p[x]] | s[p[y]]
p_x_tmp = p[x]
p_y_tmp = p[y]
for j in range(n):
if p[j] == p_y_tmp: p[j] = p_x_tmp | def find(x):
global s
if s[x] == x:
return x
s[x] = find(s[x])
return s[x]
n, q = list(map(int, input().split()))
s =[i for i in range(n)]
for i in range(q):
com, x, y = list(map(int, input().split()))
if com:
if find(x) == find(y): print((1))
else: print((0))
else:
s[find(y)] = find(x) | p02343 |
import sys
f_i = sys.stdin
n, q = list(map(int, f_i.readline().split()))
S = [[i] for i in range(n)]
def unite(sets, v1, v2):
s1 = None
s2 = None
for s in sets:
if v1 in s and v2 in s:
return None
elif v1 in s:
s1 = s
elif v2 in s:
s2 = s
if s1 and s2:
break
sets.append(s1 + s2)
sets.remove(s1)
sets.remove(s2)
def same(sets, v1, v2):
for s in sets:
if v1 in s and v2 in s:
print((1))
break
elif v1 in s or v2 in s:
print((0))
break
for l in f_i:
com, x, y = list(map(int, l.split()))
if com == 0:
unite(S, x, y)
else:
same(S, x, y) | import sys
f_i = sys.stdin
n, q = list(map(int, f_i.readline().split()))
S = set([frozenset([i]) for i in range(n)])
def unite(sets, v1, v2):
s1 = None
s2 = None
for s in sets:
if v1 in s and v2 in s:
return None
elif v1 in s:
s1 = s
elif v2 in s:
s2 = s
if s1 and s2:
break
sets.discard(s1)
sets.discard(s2)
s3 = s1.union(s2)
sets.add(s3)
def same(sets, v1, v2):
for s in sets:
if v1 in s and v2 in s:
print((1))
break
elif v1 in s or v2 in s:
print((0))
break
for l in f_i:
com, x, y = list(map(int, l.split()))
if com == 0:
unite(S, x, y)
else:
same(S, x, y) | p02343 |
import sys
f_i = sys.stdin
n, q = list(map(int, f_i.readline().split()))
S = set(frozenset([i]) for i in range(n))
def unite(sets, v1, v2):
s1 = None
s2 = None
t_s = frozenset([v1, v2])
for s in sets:
if t_s <= s:
return None
elif s1 and s2:
break
elif v1 in s:
s1 = s
elif v2 in s:
s2 = s
sets.discard(s1)
sets.discard(s2)
s3 = s1.union(s2)
sets.add(s3)
def same(sets, v1, v2):
t_s = frozenset([v1, v2])
for s in sets:
if t_s <= s:
print((1))
break
elif v1 in s or v2 in s:
print((0))
break
for l in f_i:
com, x, y = list(map(int, l.split()))
if com == 0:
unite(S, x, y)
else:
same(S, x, y) | import sys
f_i = sys.stdin
n, q = list(map(int, f_i.readline().split()))
S = set([frozenset([i]) for i in range(n)])
def unite(sets, v1, v2):
s1 = None
s2 = None
t_s = {v1, v2}
for s in sets:
if t_s <= s:
return None
elif v1 in s:
s1 = s
elif v2 in s:
s2 = s
if s1 and s2:
break
sets.discard(s1)
sets.discard(s2)
s3 = s1.union(s2)
sets.add(s3)
def same(sets, v1, v2):
for s in sets:
t_s = {v1, v2}
if t_s <= s:
print((1))
break
elif v1 in s or v2 in s:
print((0))
break
for l in f_i:
com, x, y = list(map(int, l.split()))
if com == 0:
unite(S, x, y)
else:
same(S, x, y) | p02343 |
import sys
f_i = sys.stdin
n, q = list(map(int, f_i.readline().split()))
S = set([frozenset([i]) for i in range(n)])
def unite(sets, v1, v2):
s1 = None
s2 = None
t_s = {v1, v2}
for s in sets:
if t_s <= s:
return None
elif v1 in s:
s1 = s
elif v2 in s:
s2 = s
if s1 and s2:
break
sets.discard(s1)
sets.discard(s2)
s3 = s1.union(s2)
sets.add(s3)
def same(sets, v1, v2):
for s in sets:
t_s = {v1, v2}
if t_s <= s:
print((1))
break
elif v1 in s or v2 in s:
print((0))
break
for l in f_i:
com, x, y = list(map(int, l.split()))
if com == 0:
unite(S, x, y)
else:
same(S, x, y) | import sys
f_i = sys.stdin
n, q = list(map(int, f_i.readline().split()))
S = set(frozenset([i]) for i in range(n))
def unite(sets, v1, v2):
s1 = None
s2 = None
for s in sets:
if v1 in s and v2 in s:
return None
elif v1 in s:
s1 = s
elif v2 in s:
s2 = s
if s1 and s2:
break
sets.discard(s1)
sets.discard(s2)
s3 = s1.union(s2)
sets.add(s3)
def same(sets, v1, v2):
for s in sets:
if v1 in s and v2 in s:
print((1))
break
elif v1 in s or v2 in s:
print((0))
break
for l in f_i:
com, x, y = list(map(int, l.split()))
if com == 0:
unite(S, x, y)
else:
same(S, x, y) | p02343 |
#! python3
# disjoint_set_union_find_tree.py
n, q = [int(x) for x in input().split(' ')]
sets = [set([i]) for i in range(n)]
def unite(Sx, Sy):
return Sx | Sy
def search(x):
global sets
flag = False
for i in range(len(sets)):
if x in sets[i]:
flag = True
break
if flag:
return i
else:
return None
def same(x, y):
global sets
flag = 0
for set in sets:
if x in set:
if y in set:
flag = 1
break
return flag
for i in range(q):
query, x, y = input().split(' ')
x, y = int(x), int(y)
if query == '0':
# unite
i_x = search(x)
i_y = search(y)
if i_x != None and i_y != None and i_x != i_y:
sets[i_x] = unite(sets[i_x], sets[i_y])
del sets[i_y]
elif query == '1':
# same
print((same(x, y)))
| #! python3
# disjoint_set_union_find_tree.py
class UnionFindTree():
def __init__(self, n):
self.par = [i for i in range(n+1)]
self.rank = [0] * (n+1)
def find(self, x):
if self.par[x] == x:
return x
else:
self.par[x] = self.find(self.par[x])
return self.find(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
def same_check(self, x, y):
return self.find(x) == self.find(y)
n, q = [int(x) for x in input().split(' ')]
uft = UnionFindTree(n-1)
for i in range(q):
com, x, y = [int(x) for x in input().split(' ')]
if com == 0:
uft.unite(x, y)
elif com == 1:
if uft.same_check(x, y):
print((1))
else:
print((0))
| p02343 |
n,m = list(map(int,input().split()))
arr = []
for i in range(n):
arr.append(i)
def root(i):
t = i
while arr[t] != t:
t = arr[t]
return t
for i in range(m):
query,x,y = list(map(int,input().split()))
if query == 0:
arr[root(y)] = x
else :
print(1 if root(x) == root(y) else 0)
| n,m = list(map(int,input().split()))
arr = []
for i in range(n):
arr.append(i)
def root(i):
t = i
while arr[t] != t:
t = arr[t]
return t
for i in range(m):
query,x,y = list(map(int,input().split()))
if query == 0:
if root(y) == root(x):
next
else :
arr[root(y)] = root(x)
else :
print(1 if root(x) == root(y) else 0)
| p02343 |
from sys import stdin
high = [0] * 10000
par = [i for i in range(10000)]
def findSet(x):
if par[x] == x: return x
par[x] = findSet(par[x])
return par[x]
def unite(x, y):
x = findSet(x)
y = findSet(y)
if x == y: return
if high[x] > high[y]: par[y] = x;
else:
par[x] = y
if high[x] == high[y]: high[y] += 1
def same(x, y):
return findSet(x) == findSet(y)
n, q = map(int, stdin.readline().split())
ans = []
ans_append = ans.append
for i in range(q):
t, a, b = map(int, stdin.readline().split())
if t == 0: unite(a, b);
elif t == 1: ans_append(1 if same(a, b) else 0)
print(*ans, sep='\n')
| from sys import stdin
n, q = map(int, stdin.readline().split())
ans = []
ans_append = ans.append
high = [0] * n
par = [i for i in range(n)]
def findSet(x):
if par[x] == x: return x
par[x] = findSet(par[x])
return par[x]
def unite(x, y):
x = findSet(x)
y = findSet(y)
if x == y: return
if high[x] > high[y]: par[y] = x;
else:
par[x] = y
if high[x] == high[y]: high[y] += 1
def same(x, y):
return findSet(x) == findSet(y)
for i in range(q):
t, a, b = map(int, stdin.readline().split())
if t == 0: unite(a, b);
elif t == 1: ans_append(1 if same(a, b) else 0)
print(*ans, sep='\n')
| p02343 |
# encoding: "utf-8"
class Unreachable(Exception):
pass
def main():
n, q = [int(x) for x in input().split()]
setlist = list()
for i in range(n):
setlist.append({i})
for _ in range(q):
run(setlist)
def run(setlist):
com, x, y = [int(x) for x in input().split()]
if com == 0:
# unite
new_set = setlist[x] | setlist[y]
for i in new_set:
setlist[i] = new_set
elif com == 1:
# same
if y in setlist[x]:
print((1))
else:
print((0))
else:
raise Unreachable
if __name__ == "__main__":
main() | # encoding: "utf-8"
class Unreachable(Exception):
pass
def main():
n, q = [int(x) for x in input().split()]
setlist = list()
for i in range(n):
setlist.append({i})
commands = list()
for _ in range(q):
com, x, y = [int(x) for x in input().split()]
commands.append((com, x, y))
for command in commands:
run(setlist, command)
def run(setlist, command):
com, x, y = command
if com == 0:
# unite
new_set = setlist[x] | setlist[y]
for i in new_set:
setlist[i] = new_set
elif com == 1:
# same
if y in setlist[x]:
print((1))
else:
print((0))
else:
raise Unreachable
if __name__ == "__main__":
main() | p02343 |
class DisjointSet():
def __init__(self):
self.parent = None
def ancestor(elems, x):
s = elems[x]
while s.parent is not None:
s = s.parent
return s
def unite(elems, x, y):
parent = DisjointSet()
ancestor(elems, x).parent = parent
ancestor(elems, y).parent = parent
def same(elems, x, y):
return ancestor(elems, x) is ancestor(elems, y)
def main():
n, q = [int(x) for x in input().split()]
elems = [DisjointSet() for _ in range(n)]
for _ in range(q):
com, x, y = [int(x) for x in input().split()]
if com == 0:
unite(elems, x, y)
else:
print((1 if same(elems, x, y) else 0))
if __name__ == '__main__':
main()
| class DisjointSet():
def __init__(self):
self.parent = None
def ancestor(elems, x):
s = elems[x]
while s.parent is not None:
s = s.parent
return s
def unite(elems, x, y):
ax = ancestor(elems, x)
ay = ancestor(elems, y)
if ax is not ay:
parent = DisjointSet()
ax.parent = ay.parent = parent
def same(elems, x, y):
return ancestor(elems, x) is ancestor(elems, y)
def main():
n, q = [int(x) for x in input().split()]
elems = [DisjointSet() for _ in range(n)]
for _ in range(q):
com, x, y = [int(x) for x in input().split()]
if com == 0:
unite(elems, x, y)
else:
print((1 if same(elems, x, y) else 0))
if __name__ == '__main__':
main()
| p02343 |
from sys import stdin
class DisjointSet():
def __init__(self):
self.parent = None
self.ancestor = self
def ancestor(elems, x):
s = elems[x].ancestor
while s.parent is not None:
s = s.parent
elems[x].ancestor = s
return s
def unite(elems, x, y):
ax = ancestor(elems, x)
ay = ancestor(elems, y)
if ax is not ay:
ax.parent = ay
ax.ancestor = ay.ancestor
def same(elems, x, y):
return ancestor(elems, x) is ancestor(elems, y)
def main():
n, q = [int(x) for x in input().split()]
elems = [DisjointSet() for _ in range(n)]
for _ in range(q):
com, x, y = [int(x) for x in input().split()]
if com == 0:
unite(elems, x, y)
else:
print((1 if same(elems, x, y) else 0))
if __name__ == '__main__':
main()
| class DisjointSetElement():
def __init__(self):
self.parent = None
self.anc = self
def ancestor(self):
s = self.anc
while s.parent is not None:
s = s.parent
self.anc = s
return s
def unite(self, y):
ax = self.ancestor()
ay = y.ancestor()
if ax is not ay:
ax.parent = ay
ax.anc = ay.anc
def same(self, y):
return self.ancestor() is y.ancestor()
def main():
n, q = [int(x) for x in input().split()]
elems = [DisjointSetElement() for _ in range(n)]
for _ in range(q):
com, x, y = [int(x) for x in input().split()]
if com == 0:
elems[x].unite(elems[y])
else:
print((1 if elems[x].same(elems[y]) else 0))
if __name__ == '__main__':
main()
| p02343 |
class DisjointSetElement():
def __init__(self):
self.parent = None
self.anc = self
def ancestor(self):
s = self.anc
while s.parent is not None:
s = s.parent
self.anc = s
return s
def unite(self, y):
ax = self.ancestor()
ay = y.ancestor()
if ax is not ay:
ax.parent = ay
ax.anc = ay.anc
def same(self, y):
return self.ancestor() is y.ancestor()
def main():
n, q = [int(x) for x in input().split()]
elems = [DisjointSetElement() for _ in range(n)]
for _ in range(q):
com, x, y = [int(x) for x in input().split()]
if com == 0:
elems[x].unite(elems[y])
else:
print((1 if elems[x].same(elems[y]) else 0))
if __name__ == '__main__':
main()
| class DisjointSetElement():
def __init__(self, x):
self.parent = x
self.rank = 0
class DisjointSet():
def __init__(self, n):
self.elems = [DisjointSetElement(n) for n in range(n)]
def _find_set(self, x):
if x != self.elems[x].parent:
self.elems[x].parent = self._find_set(self.elems[x].parent)
return self.elems[x].parent
def same(self, x, y):
return self._find_set(x) == self._find_set(y)
def _link(self, x, y):
if self.elems[x].rank > self.elems[y].rank:
self.elems[y].parent = x
else:
self.elems[x].parent = y
if self.elems[x].rank == self.elems[y].rank:
self.elems[y].rank += 1
def unite(self, x, y):
self._link(self._find_set(x), self._find_set(y))
def main():
n, q = [int(x) for x in input().split()]
ds = DisjointSet(n)
for _ in range(q):
com, x, y = [int(x) for x in input().split()]
if com == 0:
ds.unite(x, y)
else:
print((1 if ds.same(x, y) else 0))
if __name__ == '__main__':
main()
| p02343 |
# coding=utf-8
def unite(sets, x, y):
sx = [s for s in sets if x in s][0]
sy = [s for s in sets if y in s][0]
if sx == sy:
return sets
else:
newSets = []
newSets.append(set.union(sx, sy))
newSets += [s for s in sets if x not in s and y not in s]
return newSets
def same(sets, x, y):
for s in sets:
if x in s and y in s:
return 1
else:
return 0
def main():
elemNum, queryNum = list(map(int, input().rstrip().split()))
sets = [set([i]) for i in range(elemNum)]
for _ in range(queryNum):
qType, x, y = list(map(int, input().rstrip().split()))
if qType == 0:
sets = unite(sets, x, y)
else:
print(same(sets, x, y))
if __name__ == '__main__':
main() | # coding=utf-8
def unite(sets, x, y):
sx = [s for s in sets if x in s][0]
# print sx
if y in sx:
return sets
else:
newSets = [s for s in sets if x not in s and y not in s]
sy = [s for s in sets if y in s][0]
# print sy
newSets.append(set.union(sx, sy))
return newSets
def same(sets, x, y):
sx = [s for s in sets if x in s][0]
if y in sx:
return 1
else:
return 0
def main():
elemNum, queryNum = list(map(int, input().rstrip().split()))
sets = [set([i]) for i in range(elemNum)]
for _ in range(queryNum):
qType, x, y = list(map(int, input().rstrip().split()))
if qType:
print(same(sets, x, y))
else:
sets = unite(sets, x, y)
# print sets
if __name__ == '__main__':
main() | p02343 |
#!/usr/bin/python3
class UnionFindTree:
def __init__(self, x):
self.parent = self
self.id = x
def find(self):
if self.id == self.parent.id:
return self
else:
return self.parent.find()
# main
n, q = list(map(int, input().split()))
nodes = [UnionFindTree(i) for i in range(n)]
for i in range(q):
com, x, y = list(map(int, input().split()))
x_root = nodes[x].find()
y_root = nodes[y].find()
if com == 0:
# union
y_root.parent = x_root
else:
# same
same = 1 if x_root.id == y_root.id else 0
print(same)
| #!/usr/bin/python3
class UnionFindTree:
def __init__(self, x):
self.parent = self
self.id = x
self.rank = 0
def find(self):
if self.id == self.parent.id:
return self
else:
return self.parent.find()
# main
n, q = list(map(int, input().split()))
nodes = [UnionFindTree(i) for i in range(n)]
for i in range(q):
com, x, y = list(map(int, input().split()))
x_root = nodes[x].find()
y_root = nodes[y].find()
if com == 0:
# union
if x_root.rank > y_root.rank:
y_root.parent = x_root
elif y_root.rank > x_root.rank:
x_root.parent = y_root
elif x_root.id != y_root.id:
y_root.parent = x_root
x_root.rank += 1
else:
# same
same = 1 if x_root.id == y_root.id else 0
print(same)
| p02343 |
# -*- coding: utf-8 -*-
class DisjointSets:
'''
Implementing disjoint sets using disjoint sets forests structure.
--- methods ---
__init__(size): size should be the number of elements in this disjoint sets
unite(x, y): unite sets that contain x and y into a new set
same(x, y): check if x and y are in the same set
'''
def __init__(self, size):
self.size = size
self.ranks = [0] * self.size
self.roots = [0] * self.size
for x in range(self.size):
self._make_set(x)
def _make_set(self, x):
self.roots[x] = x
self.ranks[x] = 0
def _link(self, x, y):
if self.ranks[x] > self.ranks[y]:
self.roots[y] = x
else:
self.roots[x] = y
if self.ranks[x] == self.ranks[y]:
self.ranks[y] += 1
def _find_set(self, x):
# compression of the path
if x != self.roots[x]:
self.roots[x] = self._find_set(self.roots[x])
return self.roots[x]
def unite(self, x, y):
self._link(self._find_set(x), self._find_set(y))
def same(self, x, y):
return self._find_set(x) == self._find_set(y)
if __name__ == '__main__':
import sys
n, q = list(map(int, input().split()))
ds = DisjointSets(n)
for line in sys.stdin.readlines():
query, x, y = list(map(int, line.strip().split()))
if query == 0:
ds.unite(x, y)
elif query == 1:
if ds.same(x, y):
print((1))
else:
print((0))
| # -*- coding: utf-8 -*-
class DisjointSets:
'''
Implementing disjoint sets using disjoint sets forests structure.
--- methods ---
__init__(size): size should be the number of elements in this disjoint sets
unite(x, y): unite sets that contain x and y into a new set
same(x, y): check if x and y are in the same set
'''
def __init__(self, size):
self.size = size
self.roots = [x for x in range(self.size)]
self.ranks = [0] * self.size
def _link(self, x, y):
if self.ranks[x] > self.ranks[y]:
self.roots[y] = x
else:
self.roots[x] = y
if self.ranks[x] == self.ranks[y]:
self.ranks[y] += 1
def _find_set(self, x):
# compression of the path
if x != self.roots[x]:
self.roots[x] = self._find_set(self.roots[x])
return self.roots[x]
def unite(self, x, y):
self._link(self._find_set(x), self._find_set(y))
def same(self, x, y):
return self._find_set(x) == self._find_set(y)
if __name__ == '__main__':
'''
an example of using this function
AIZU ONLINE JUDGE - DSL_1_A
'''
import sys
n, q = list(map(int, input().split()))
ds = DisjointSets(n)
for line in sys.stdin.readlines():
query, x, y = list(map(int, line.strip().split()))
if query == 0:
ds.unite(x, y)
elif query == 1:
if ds.same(x, y):
print((1))
else:
print((0))
| p02343 |
#!/usr/bin/env python
from sys import stdin
def main():
n, q = (int(s) for s in stdin.readline().split())
s = [{i} for i in range(n)]
for _ in range(q):
com, x, y = (int(s) for s in stdin.readline().split())
if com:
for si in s:
if x in si and y in si:
print(1)
break
else:
print(0)
else:
for si in s:
if x in si:
a = si
if y in si:
b = si
if a != b:
s.append(a | b)
s.remove(a)
s.remove(b)
main() | #!/usr/bin/env python
from sys import stdin
def main():
n, q = (int(s) for s in stdin.readline().split())
s = [{i} for i in range(n)]
for _ in range(q):
com, x, y = (int(s) for s in stdin.readline().split())
if com:
if x in s[x] and y in s[x]:
print(1)
else:
print(0)
else:
a = s[x]
b = s[y]
if a is not b:
a |= b
for i in a:
s[i] = a
main() | p02343 |
def main():
n,q = list(map(int,input().split()))
ds = DisjointSet(n)
for i in range(q):
com,x,y = list(map(int,input().split()))
if com:
if ds.root[x] == ds.root[y]:
print(1)
else:
print(0)
else:
ds.unite(x,y)
class DisjointSet():
def __init__(self,n):
self.n = n
self.root = list(range(n))
self.parent = [-1]*n
self.rank = [0]*n
def findSet(self,x):
if self.parent[x] == -1:
return x
else:
p = self.findSet(self.parent[x])
self.parent[x] = p
def unite(self,x,y):
if self.rank[x] > self.rank[y]:
for z in [u for u in range(self.n) if self.root[u] == self.root[y]]:
self.parent[z] = self.root[x]
self.root[z] = self.root[x]
self.rank[z] = self.rank[x]
elif self.rank[x] < self.rank[y]:
for z in [u for u in range(self.n) if self.root[u] == self.root[x]]:
self.parent[z] = self.root[y]
self.root[z] = self.root[y]
self.rank[z] = self.rank[y]
else:
for z in [u for u in range(self.n) if self.root[u] == self.root[x]]:
self.parent[z] = self.root[y]
self.root[z] = self.root[y]
self.rank[z] = self.rank[y]+1
if __name__ == "__main__":
main() | def main():
n,q = list(map(int,input().split()))
ds = DisjointSet(n)
for i in range(q):
com,x,y = list(map(int,input().split()))
if com:
ds.findSet(x)
ds.findSet(y)
if ds.parent[x] == ds.parent[y]:
print(1)
else:
print(0)
else:
ds.findSet(x)
ds.findSet(y)
ds.unite(x,y)
class DisjointSet():
def __init__(self,n):
self.n = n
self.parent = list(range(n))
def findSet(self,x):
if self.parent[x] == x:
return x
else:
p = self.findSet(self.parent[x])
self.parent[x] = p
return p
def unite(self,x,y):
if self.parent[y] > self.parent[x]:
self.parent[self.parent[y]] = self.parent[x]
else:
self.parent[self.parent[x]] = self.parent[y]
if __name__ == "__main__":
main() | p02343 |
n, q = list(map(int, input().split()))
set_list = [{i} for i in range(n)]
set_dict = list(range(n))
while q:
op, x, y = list(map(int, input().split()))
if op:
print((1 if set_dict[x] == set_dict[y] else 0))
else:
idx_x, idx_y = set_dict[x], set_dict[y]
set_y = set_list[idx_y]
for i in set_y:
set_dict[i] = idx_x
set_list[idx_x] |= set_y
q -= 1 | def find(x):
global set_list
if set_list[x] == x:
return x
result = set_list[x] = find(set_list[x])
return result
n, q = list(map(int, input().split()))
set_list = list(range(n))
while q:
op, x, y = list(map(int, input().split()))
if op:
print((1 if find(x) == find(y) else 0))
else:
set_list[find(y)] = find(x)
q -= 1 | p02343 |
def unite(x, y, s_list):
x_list = None
y_list = None
for s in s_list:
if x in s and y in s:
x_list = s.copy()
y_list = s.copy()
del s
elif x in s:
x_list = s.copy()
del s
elif y in s:
y_list = s.copy()
del s
if x_list != None and y_list != None:
s_list.append(list(set(x_list + y_list)))
elif x_list != None and y_list == None:
x_list.append(y)
s_list.append(x_list)
elif x_list == None and y_list != None:
y_list.append(x)
s_list.append(y_list)
else:
s_list.append([x, y])
def same(x, y, s_list):
x_list = None
y_list = None
for s in s_list:
if x in s and y in s:
return 1
return 0
n, q = list(map(int, input().split()))
com_list = list()
x_list = list()
y_list = list()
s_list = list()
for _ in range(q):
com, x, y = list(map(int, input().split()))
com_list.append(com)
x_list.append(x)
y_list.append(y)
for i in range(q):
com = com_list[i]
x = x_list[i]
y = y_list[i]
if com == 0:
unite(x, y, s_list)
else:
result = same(x, y, s_list)
print(result)
| def make_set(x, disjoint):
disjoint[x] = x
def find_set(x, disjoint):
if x not in list(disjoint.keys()):
make_set(x, disjoint)
if x != disjoint[x]:
disjoint[x] = find_set(disjoint[x], disjoint)
return disjoint[x]
def link(x, y, disjoint):
disjoint[y] = x
def unite(x, y, disjoint):
link(find_set(x, disjoint), find_set(y, disjoint), disjoint)
def same(x, y, disjoint):
if find_set(x, disjoint) == find_set(y, disjoint):
return 1
return 0
n, q = list(map(int, input().split()))
com_list = list()
x_list = list()
y_list = list()
for _ in range(q):
com, x, y = list(map(int, input().split()))
com_list.append(com)
x_list.append(x)
y_list.append(y)
disjoint = dict()
for i in range(q):
com = com_list[i]
x = x_list[i]
y = y_list[i]
if com == 0:
unite(x, y, disjoint)
else:
print((same(x, y, disjoint))) | p02343 |
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10000)
N, Q = tuple(map(int, input().split()))
tree = [[-1, 1] for _ in range(N)] # [next, rank]
def find(i):
if tree[i][0] == -1:
group = i
else:
group = find(tree[i][0])
return group
def unite(x, y):
px = find(x)
py = find(y)
if tree[px][1] == tree[py][1]: # rank is same
tree[py][0] = px
tree[px][1] += 1
else:
if tree[px][1] < tree[py][1]:
px, py = py, px
tree[py][0] = px
for _ in range(Q):
com, x, y = tuple(map(int, input().split()))
if com: # same
print((int(find(x) == find(y))))
else: # unite
if not int(find(x) == find(y)):
unite(x, y) | # -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10000)
N, Q = tuple(map(int, input().split()))
tree = [[-1, 1] for _ in range(N)] # [next, rank]
def find(i):
if tree[i][0] == -1:
group = i
else:
group = find(tree[i][0])
tree[i][0] = group
return group
def unite(x, y):
px = find(x)
py = find(y)
if tree[px][1] == tree[py][1]: # rank is same
tree[py][0] = px
tree[px][1] += 1
else:
if tree[px][1] < tree[py][1]:
px, py = py, px
tree[py][0] = px
for _ in range(Q):
com, x, y = tuple(map(int, input().split()))
if com: # same
print((int(find(x) == find(y))))
else: # unite
if not int(find(x) == find(y)):
unite(x, y)
| p02343 |
class UnionFind:
def __init__(self,n):
self.parent = {}
for i in range(n):
self.parent[i] = i
def get_root(self,x):
r = x
depth = 0
while self.parent[r]!=r:
r = self.parent[r]
depth += 1
return r,depth
def unite(self,x,y):
rx,dx = self.get_root(x)
ry,dy = self.get_root(y)
if rx!=ry:
if dx > dy:
self.parent[ry] = rx
else:
self.parent[rx] = ry
def same(self,x,y):
rx,_ = self.get_root(x)
ry,_ = self.get_root(y)
if rx==ry: return 1
else: return 0
n,q = list(map(int,input().split(' ')))
tree = UnionFind(n)
answers = []
for _ in range(q):
com,x,y = list(map(int,input().split(' ')))
if com==0:
tree.unite(x,y)
elif com==1:
answers.append(tree.same(x,y))
for a in answers:
print(a) | class UnionFind:
def __init__(self,n):
self.parent = {}
for i in range(n):
self.parent[i] = i
def get_root(self,x):
r = x
depth = 0
path = [r]
while self.parent[r]!=r:
r = self.parent[r]
path.append(r)
depth += 1
for n in path:
self.parent[n] = r
return r,depth
def unite(self,x,y):
rx,dx = self.get_root(x)
ry,dy = self.get_root(y)
if rx!=ry:
if dx > dy:
self.parent[ry] = rx
else:
self.parent[rx] = ry
def same(self,x,y):
rx,_ = self.get_root(x)
ry,_ = self.get_root(y)
if rx==ry: return 1
else: return 0
n,q = list(map(int,input().split(' ')))
tree = UnionFind(n)
answers = []
for _ in range(q):
com,x,y = list(map(int,input().split(' ')))
if com==0:
tree.unite(x,y)
elif com==1:
answers.append(tree.same(x,y))
for a in answers:
print(a) | p02343 |
import sys
def solve():
n, q = map(int, sys.stdin.readline().split())
uf = UnionFind(n)
for lp in range(q):
c, x, y = map(int, sys.stdin.readline().split())
if c == 0:
uf.unite(x, y)
else:
print(1 if uf.same(x, y) else 0)
class UnionFind:
def __init__(self, n):
self.ds = [i for i in range(n)]
self.root = [i for i in range(n)]
self.rank = [0] * n
def find_root(self, x):
if self.root[x] == x:
return x
res = self.root[x]
updates = [x]
while res != self.root[res]:
updates.append(res)
res = self.root[res]
for u in updates:
self.root[u] = res
return res
def same(self, x, y):
return self.find_root(x) == self.find_root(y)
def unite(self, x, y):
p = self.find_root(x)
q = self.find_root(y)
if p == q:
return None
if self.rank[p] < self.rank[q]:
self.root[p] = q
elif self.rank[q] < self.rank[p]:
self.root[q] = p
else:
self.root[q] = p
self.rank[p] += 1
def debug(x, table):
for name, val in table.items():
if x is val:
print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
return None
if __name__ == '__main__':
solve()
| import sys
def solve():
n, q = map(int, sys.stdin.readline().split())
uf = UnionFind(n)
for lp in range(q):
c, x, y = map(int, sys.stdin.readline().split())
if c == 0:
uf.unite(x, y)
else:
print(1 if uf.same(x, y) else 0)
class UnionFind:
def __init__(self, n):
self.ds = [i for i in range(n)]
self.root = [i for i in range(n)]
self.rank = [0] * n
def find_root(self, x):
if x != self.root[x]:
self.root[x] = self.find_root(self.root[x])
return self.root[x]
def same(self, x, y):
return self.find_root(x) == self.find_root(y)
def unite(self, x, y):
p = self.find_root(x)
q = self.find_root(y)
if p == q:
return None
if self.rank[p] < self.rank[q]:
self.root[p] = q
elif self.rank[q] < self.rank[p]:
self.root[q] = p
else:
self.root[q] = p
self.rank[p] += 1
def debug(x, table):
for name, val in table.items():
if x is val:
print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
return None
if __name__ == '__main__':
solve()
| p02343 |
import sys
def solve():
n, q = map(int, sys.stdin.readline().split())
uf = UnionFind(n)
ans = []
for lp in range(q):
c, x, y = map(int, sys.stdin.readline().split())
if c == 0:
uf.unite(x, y)
else:
ans.append(1 if uf.is_same(x, y) else 0)
print(*ans, sep='\n')
class UnionFind:
def __init__(self, n):
self.ds = [i for i in range(n)]
self.root = [i for i in range(n)]
self.rank = [0] * n
def find_root(self, x):
if x != self.root[x]:
self.root[x] = self.find_root(self.root[x])
return self.root[x]
def is_same(self, x, y):
return self.find_root(x) == self.find_root(y)
def unite(self, x, y):
p = self.find_root(x)
q = self.find_root(y)
if p == q:
return None
if self.rank[p] < self.rank[q]:
self.root[p] = q
elif self.rank[q] < self.rank[p]:
self.root[q] = p
else:
self.root[q] = p
self.rank[p] += 1
def debug(x, table):
for name, val in table.items():
if x is val:
print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
return None
if __name__ == '__main__':
solve()
| import sys
def solve():
n, q = map(int, sys.stdin.readline().split())
ds = [[i] for i in range(n)]
n2s = [i for i in range(n)]
ans = []
for lp in range(q):
c, x, y = map(int, sys.stdin.readline().split())
if c == 0:
merge(ds, n2s, x, y)
else:
ans.append(1 if n2s[x] == n2s[y] else 0)
print(*ans, sep='\n')
def merge(ds, n2s, x, y):
u = n2s[x]
v = n2s[y]
if u == v:
return None
if len(ds[u]) < len(ds[v]):
ds[v].extend(ds[u])
for w in ds[u]:
n2s[w] = v
del ds[u][:]
else:
ds[u].extend(ds[v])
for w in ds[v]:
n2s[w] = u
del ds[v][:]
def debug(x, table):
for name, val in table.items():
if x is val:
print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
return None
if __name__ == '__main__':
solve()
| p02343 |
import sys
sys.setrecursionlimit(10**5)
def solve():
n, q = map(int, sys.stdin.readline().split())
uf = UnionFind(n)
ans = []
for lp in range(q):
c, x, y = map(int, sys.stdin.readline().split())
if c == 0:
uf.unite(x, y)
else:
ans.append(1 if uf.is_same(x, y) else 0)
print(*ans, sep='\n')
class UnionFind:
def __init__(self, n):
self.ds = [i for i in range(n)]
self.root = [i for i in range(n)]
# self.rank = [0] * n
def find_root(self, x):
if x != self.root[x]:
self.root[x] = self.find_root(self.root[x])
return self.root[x]
def is_same(self, x, y):
return self.find_root(x) == self.find_root(y)
def unite(self, x, y):
p = self.find_root(x)
q = self.find_root(y)
self.root[p] = q
def debug(x, table):
for name, val in table.items():
if x is val:
print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
return None
if __name__ == '__main__':
solve()
| import sys
def solve():
n, q = map(int, sys.stdin.readline().split())
uf = UnionFind(n)
ans = []
for lp in range(q):
c, x, y = map(int, sys.stdin.readline().split())
if c == 0:
uf.unite(x, y)
else:
ans.append(1 if uf.same(x, y) else 0)
print(*ans, sep='\n')
class UnionFind:
def __init__(self, n):
self.ds = [i for i in range(n)]
self.root = [i for i in range(n)]
self.rank = [0] * n
def find_root(self, x):
if self.root[x] == x:
return x
res = self.root[x]
while res != self.root[res]:
res = self.root[res]
return res
def same(self, x, y):
return self.find_root(x) == self.find_root(y)
def unite(self, x, y):
p = self.find_root(x)
q = self.find_root(y)
if p == q:
return None
if self.rank[p] < self.rank[q]:
self.root[p] = q
elif self.rank[q] < self.rank[p]:
self.root[q] = p
else:
self.root[q] = p
self.rank[p] += 1
def debug(x, table):
for name, val in table.items():
if x is val:
print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
return None
if __name__ == '__main__':
solve()
| p02343 |
A, B = list(map(int, input().split(' ')))
def is_palindromic(n):
sep_idx = len(n) // 2
if n[:sep_idx] == n[sep_idx+1:][::-1]:
return True
return False
count = 0
for n in range(A, B+1):
n_str = str(n)
if is_palindromic(n_str):
count += 1
print(count) | A, B = list(map(int, input().split(' ')))
def is_palindromic(n):
s, t = n % 10, (n // 10000) % 10
u, v = (n // 10) % 10, (n // 1000) % 10
if s == t and u == v:
return True
return False
count = 0
for n in range(A, B+1):
if is_palindromic(n):
count += 1
print(count) | p03416 |
A, B = list(map(int, input().split()))
print((len([1 for i in range(A,B+1) if str(i)[0:]==str(i)[::-1]]))) | a,b=list(map(int,input().split()));count=0
for i in range(a,b+1):
c=str(i)
if c==c[::-1]: count+=1
print(count) | p03416 |
import sys
def input():
return sys.stdin.readline().rstrip()
if __name__ == "__main__":
a, b = list(map(int, input().split()))
count = 0
for i in range(a, b+1):
if str(i)[0] == str(i)[-1] and str(i)[1] == str(i)[-2]:
count += 1
print(count)
| import sys
def input():
return sys.stdin.readline().rstrip()
if __name__ == "__main__":
a, b = list(map(int, input().split()))
count = 0
for i in range(a, b+1):
s = str(i)
if s[0] == s[-1] and s[1] == s[-2]:
count += 1
print(count)
| p03416 |
a, b = list(map(int, input().split()))
ans = 0
for i in range(a,b+1):
if str(i) == str(i)[::-1]:
ans += 1
print(ans) | a, b = list(map(int, input().split()))
ans = 0
for i in range(1,10):
for j in range(0,10):
for k in range(0,10):
x = 10000*i + 1000*j + 100*k + 10*j + i
if a <= x <= b:
ans += 1
print(ans) | p03416 |
a,b = list(map(int,input().split()))
c = 0
f = 1
for i in range(a,b+1):
if str(i)[0] == str(i)[4] and str(i)[1] == str(i)[3]:
c += 1
print(c)
| a,b = list(map(int,input().split()))
c = 0
for i in range(a,b+1):
if str(i) == str(i)[::-1]:
c += 1
print(c) | p03416 |
# coding: utf-8
import sys
input = sys.stdin.readline
A, B = list(map(int, input().split()))
ans = sum(1 for i in range(A, B+1) if str(i)==str(i)[::-1])
print(ans)
| # coding: utf-8
import sys
input = sys.stdin.readline
A, B = list(map(int, input().split()))
ans = 0
for i in range(A, B + 1):
i = str(i)
if i == i[::-1]:
ans += 1
print(ans)
| p03416 |
# coding: utf-8
import sys
input = sys.stdin.readline
A, B = list(map(int, input().split()))
ans = 0
for i in range(A, B + 1):
i = str(i)
if i == i[::-1]:
ans += 1
print(ans)
| # coding: utf-8
import sys
input = sys.stdin.readline
A, B = list(map(int, input().split()))
ans = 0
for i in range(1, 10):
for j in range(10):
for k in range(10):
val = 10000*i + 1000*j + 100*k + 10*j + i
if A <= val <= B:
ans+=1
print(ans)
| p03416 |
def main():
a, b = list(map(int,input().split()))
r = 0
for i in range(a, b + 1):
r += str(i) == str(i)[::-1]
print(r)
if __name__ == '__main__':
main() | import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
a, b = list(map(int, input().split()))
r = 0
for i1 in range(a, b + 1):
stri1 = str(i1)
r += stri1 == stri1[::-1]
print(r)
if __name__ == '__main__':
main()
| p03416 |
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
a, b = list(map(int, input().split()))
r = 0
for i1 in range(a, b + 1):
stri1 = str(i1)
r += stri1 == stri1[::-1]
print(r)
if __name__ == '__main__':
main()
| import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
a, b = list(map(int, input().split()))
r = 0
for i1 in range(a, b + 1):
c1 = i1 // 10000 == i1 % 10
c2 = ((i1 // 1000) % 10) == ((i1//10)%10)
r += c1 and c2
print(r)
if __name__ == '__main__':
main() | p03416 |
a,b = list(map(int, input().split()))
c = 0
for i in range(a, b+1):
s = str(i)
sr = "".join(list(reversed(s)))
if s== sr:
c+=1
print(c) | a,b = list(map(int, input().split()))
res = 0
for i in range(a, b + 1):
s1 = str(i)
s2 = s1[::-1]
if s1 == s2:
res += 1
print(res) | p03416 |
N = {} #10000以上key以下の回文数の個数
cnt = 0
for i in range(10000, 100000):
L = list(str(i))
if L == list(reversed(L)):
cnt += 1
N[i] = cnt
A, B = list(map(int, input().split()))
print((N[B]-N[A-1] if A > 10000 else N[B])) | A, B =list(map(int, input().split()))
num_palin = [0] * (B+1)
for i in range(1,B+1):
if str(i) == str(i)[::-1]:
num_palin[i] = num_palin[i-1] + 1
else:
num_palin[i] = num_palin[i-1]
print((num_palin[B]-num_palin[A-1]))
| p03416 |
# ABC090B - Palindromic Numbers
a, b = list(map(int, input().rstrip().split()))
count = 0
for i in range(a, b + 1):
lst = list(map(int, list(str(i))))
if lst[0] == lst[4] and lst[1] == lst[3]:
count += 1
print(count) | # ABC090B - Palindromic Numbers
def main():
a, b = list(map(int, input().rstrip().split()))
cnt = 0
for i in range(a, b + 1):
s = str(i)
if s == s[::-1]:
cnt += 1
print(cnt)
if __name__ == "__main__":
main() | p03416 |
a,b = list(map(int,input().split()))
count = 0
for i in range(b-a+1):
if str(a+i) == str(a+i)[::-1]:
count += 1
print(count)
| a,b=list(map(int,input().split()))
ans=0
for i in range(a,b+1):
if i==int(str(i)[::-1]):
ans+=1
print(ans)
| p03416 |
a,b=list(map(int, input().split()))
cnt=0
for i in range(a,b+1):
n=list(str(i))
if n==list(reversed(n)):
cnt+=1
print(cnt) | from sys import stdin
nii=lambda:list(map(int,stdin.readline().split()))
lnii=lambda:list(map(int,stdin.readline().split()))
a,b=nii()
ans=0
for i in range(a,b+1):
s=str(i)
if s==s[::-1]:
ans+=1
print(ans) | p03416 |
a, b = list(map(int, input().split()))
total = 0
for i in range(a, b+1):
if i == int(str(i)[::-1]):
total += 1
print(total) | a, b = list(map(int, input().split()))
total = 0
for i in range(a, b+1):
if (i%10 == i//10000%10) and (i//10%10 == i//1000%10):
total += 1
print(total) | p03416 |
A,B=list(map(int,input().split()))
cnt = 0
for i in range(A,B+1):
if str(i)[0] == str(i)[-1]:
if str(i)[1] == str(i)[-2]:
cnt += 1
print(cnt) | A,B=list(map(int,input().split()))
cnt = 0
for i in range(A,B+1):
if str(i) == str(i)[::-1]:cnt += 1
print(cnt) | p03416 |
A, B = list(map(int, input().split()))
print((sum([1 for i in range(A, B + 1) if str(i) == str(i)[::-1]]))) | A, B = list(map(int, input().split()))
print((sum([1 for i in range(A, B + 1) if str(i)[0] == str(i)[-1]
and str(i)[1] == str(i)[-2]]))) | p03416 |
a , b = input().split()
a = int(a)
b = int(b)
count=0
for i in range(a,b+1):
test = list(str(i))
if (test[0] == test[4]) and (test[1]==test[3]):
count=count+1
print(count) | A,B=list(map(int,input().split()))
ct=0
for i in range(A,B+1):
s=str(i)
if s==s[::-1]:
ct+=1
print(ct) | p03416 |
a,b = list(map(int,input().split()))
ans = 0
for i in range(a,b+1):
s = list(str(i))
if s == s[::-1]:
ans += 1
print(ans) | a,b=list(map(int,input().split()))
print((sum(str(i)==str(i)[::-1] for i in range(a,b+1)))) | p03416 |
A,B = (int(i) for i in input().split())
count = 0
for i in range(B-A+1):
number = A+i
charnum = str(number)
if(charnum[0]==charnum[4] and charnum[1]==charnum[3]):
count = count + 1
print(count) | A,B = (int(i) for i in input().split())
count = 0
for i in range(A,B+1):
s = str(i)
if s[0]==s[4] and s[1]==s[3]:
count += 1
print(count) | p03416 |
a, b = list(map(int, input().split()))
count = 0
for i in range(a, b+1):
target = str(i)
if target == target[::-1]:
count += 1
print(count) | a, b = list(map(int, input().split()))
count = 0
for i in range(a, b+1):
if i//10000 == (i//1) % 10 and (i//1000) % 10 == (i//10) % 10:
count += 1
print(count) | p03416 |
a, b = list(map(int, input().split()))
nums = [x for x in range(a, b+1) if str(x)[0] == str(x)[-1] and str(x)[1] == str(x)[-2]]
print((len(nums))) | a, b = list(map(int, input().split()))
nums = []
for i in range(a, b+1):
str_i = str(i)
if str_i[0] == str_i[-1] and str_i[1] == str_i[-2]:
nums.append(i)
print((len(nums))) | p03416 |
A, B = list(map(int,input().split()))
a = ""
c = 0
for i in range(A, B+1):
a = str(i)
if a[0] == a[-1] and a[1] == a[-2]:
c += 1
print(c)
| A, B = list(map(int, input().split()))
ans = 0
for i in range(A, B + 1):
I = str(i)
if I[0] == I[4] and I[1] == I[3]:
ans += 1
print(ans)
| p03416 |
A, B = list(map(int, input().split()))
count = 0
for i in range(A, B+1):
if(str(i) == str(i)[::-1]):
count += 1
print(count)
| A, B = list(map(int, input().split()))
count = 0
for i in range(10):
for j in range(10):
for k in range(10):
if(A <= 10000*i + 1000*j + 100*k + 10*j +i <= B):
count += 1
print(count) | p03416 |
def test():
a,b = list(map(int,input().split()))
count = 0
for i in range(a,b+1):
j = list(map(int,list(str(i))))
if j[0] == j[4] and j[1] == j[3]:
count += 1
print(count)
if __name__ == "__main__":
test()
| def test():
a,b = list(map(int,input().split()))
count = 0
for i in range(a,b+1):
if str(i)[0] == str(i)[4] and str(i)[1] == str(i)[3]:
count += 1
print(count)
if __name__ == "__main__":
test()
| p03416 |
a, b = list(map(int, input().split()))
ans = 0
for i in range(a, b+1):
if str(i)[0] == str(i)[4] and str(i)[1] == str(i)[3]:
ans += 1
print(ans)
| a, b = list(map(int, input().split()))
ans = 0
for i in range(a, b+1):
s = str(i)
if s[0] == s[4] and s[1] == s[3]:
ans += 1
print(ans)
| p03416 |
a, b = list(map(int, input().split()))
count = 0
for i in range(a, b + 1):
x = list(str(i))
y = x[::-1]
if x == y:
count += 1
print(count)
| a, b = list(map(int, input().split()))
count = 0
for i in range(a, b + 1):
x = str(i)
y = x[::-1]
if x == y:
count += 1
print(count)
| p03416 |
A, B = list(map(int, input().split()))
res = 0
for i in range(A, B+1):
if str(i) == str(i)[::-1]:
res += 1
print(res) | A, B = list(map(int, input().split()))
res = 0
for i in range(10):
for j in range(10):
for k in range(10):
cand = int(''.join([str(i), str(j), str(k), str(j), str(i)]))
if A <= cand <= B:
res += 1
print(res) | p03416 |
# Problem:
# Python Try
import sys
# from collections import defaultdict
# import heapq,copy
# from collections import deque
def II(): return int(sys.stdin.readline())
def MI(): return list(map(int, sys.stdin.readline().split()))
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def palindromic(Num):
result = False
strNum = str(Num)
strRevNum = strNum[::-1]
checkDigit = 0
for j in range(0, len(strNum)):
if strNum[j] == strRevNum[j]:
checkDigit = checkDigit + 1
if checkDigit == len(strNum):
result = True
return result
def solver(fromNum, ToNum):
result = 0
for j in range(fromNum, ToNum+1, +1):
if palindromic(j):
result = result + 1
return result
if __name__ == "__main__":
A, B = MI()
print(("{}".format(solver(A, B))))
| # Problem: https://atcoder.jp/contests/abc090/tasks/abc090_b
# Python 2nd Try
import sys
# from collections import defaultdict
# import heapq,copy
# from collections import deque
def II(): return int(sys.stdin.readline())
def MI(): return list(map(int, sys.stdin.readline().split()))
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def solver(fromNum, toNum):
result = 0
# algorithm
for number in range(fromNum, toNum+1):
strNum = str(number)
if strNum[0] == strNum[4]:
if strNum[1] == strNum[3]:
result = result + 1
return result
if __name__ == "__main__":
A, B = MI()
print(("{}".format(solver(A, B))))
| p03416 |
A,B=list(map(int,input().split()))
ans=0
for i in range(A,B+1):
s=str(i)
l=len(s)
if all( s[j]==s[l-1-j] for j in range(l) ): ans+=1
print(ans) | A,B=list(map(int,input().split()))
ans=0
for i in range(A,B+1):
s=str(i)
if all( s[j]==s[4-j] for j in range(3) ): ans+=1
print(ans) | p03416 |
def sumNum(num, sums):
if num == 0:
return sums
sums += num % 10
num = num // 10
return sumNum(num, sums)
A, B = [int(i) for i in input().rstrip().split(' ')]
cnt = 0
for i in range(A, B+1):
arr = [i for i in list(str(i))]
if not arr[0] == arr[-1]:
continue
asc = ''.join(arr)
desc = ''.join(list(reversed(arr)))
if asc == desc:
cnt += 1
print(cnt)
| A, B = [int(i) for i in input().rstrip().split(' ')]
cnt = 0
for i in range(A, B+1):
arr = [i for i in list(str(i))]
if not arr[0] == arr[-1]:
continue
asc = ''.join(arr)
desc = ''.join(list(reversed(arr)))
if asc == desc:
cnt += 1
print(cnt) | p03416 |
a,b=list(map(int,input().split()))
count=0
for i in range(a,b+1):
l1=list(str(i))
l2=l1[::-1]
if l1==l2 :
count+=1
print(count) | #n=int(input())
a,b=list(map(int,input().split()))
#l=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]
count=0
for i in range(a,b+1):
istr=str(i)
if istr[0]==istr[4] and istr[1]==istr[3]:
count+=1
print(count)
| p03416 |
a,b = list(map(int, input().split()))
cnt = 0
for i in range(a,b+1):
tmp = list(map(int,list(str(i))))
if tmp==list(reversed(tmp)):
cnt+=1
print(cnt) | a,b = list(map(int, input().split()))
cnt = 0
for i in range(a,b+1):
tmp = list(str(i))
if tmp==list(reversed(tmp)):
cnt+=1
print(cnt) | p03416 |
a,b = list(map(int,input().split()))
cnt = 0
for i in range(a,b+1):
i = list(str(i))
if i == i[::-1]:
cnt += 1
print(cnt) | n,m = list(map(int,input().split()))
cnt = 0
for i in range(n,m+1):
s = str(i)
if s == s[::-1]:
cnt +=1
print(cnt) | p03416 |
# author: kagemeka
# created: 2019-11-06 21:30:22(JST)
import sys
# import collections
# import math
# import string
# import bisect
# import re
# import itertools
# import statistics
# import functools
# import operator
def main():
a, b = (int(x) for x in sys.stdin.read().split())
count = 0
for n in range(a, b+1):
n = str(n)
if n == n[::-1]:
count += 1
print(count)
if __name__ == "__main__":
# execute only if run as a script
main()
| import sys
from bisect import bisect_left as bi_l, bisect_right as bi_r
a, b = list(map(int, sys.stdin.readline().split()))
def main():
palindromic_nums = []
for i in range(1, 10):
for j in range(10):
for k in range(10):
x = [i, j, k, j, i]
palindromic_nums.append(int(''.join(list(map(str, x)))))
return bi_r(palindromic_nums, b) - bi_l(palindromic_nums, a)
if __name__ == '__main__':
ans = main()
print(ans) | p03416 |
import sys
from bisect import bisect_left as bi_l, bisect_right as bi_r
a, b = list(map(int, sys.stdin.readline().split()))
def main():
palindromic_nums = []
for i in range(1, 10):
for j in range(10):
for k in range(10):
x = [i, j, k, j, i]
palindromic_nums.append(int(''.join(list(map(str, x)))))
return bi_r(palindromic_nums, b) - bi_l(palindromic_nums, a)
if __name__ == '__main__':
ans = main()
print(ans) | import sys
def cnt(n):
m = str(n)
l = len(m)
if l == 1: return n + 1
tot = 0
tot += pow(10, (l - 1) // 2) * (int(m[0]) - 1)
tot += pow(10, l // 2) - 1 - 9 * pow(10, l // 2 - 1) * (l & 1 ^ 1)
while l >= 2:
l -= 2
if l == 0: tot += m[1] >= m[0]
elif l == 1: tot += int(m[1]) + 1 - (m[-1] < m[0])
else:
m = str(int(m[1:-1]) - (m[-1] < m[0]))
m = '0' * (l - len(m)) + m
tot += int(m[0]) * pow(10, (l - 1) // 2)
return tot
a, b = list(map(int, sys.stdin.readline().split()))
def main():
print((cnt(b) - cnt(a-1)))
if __name__ == '__main__':
main() | p03416 |
A,B = list(map(int, input().split()))
AB = list(range(A, B+1))
N = B-A+1
count = 0
for i in range(N):
count += (1 if str(AB[i]) == str(AB[i])[::-1] else 0)
print(count) | A,B = list(map(int, input().split()))
count = 0
for i in range(A, B+1):
count += (1 if str(i) == str(i)[::-1] else 0)
print(count) | p03416 |
a, b = list(map(int, input().split()))
count = 0
for i in range(a,b+1):
b1 = i%10
s = i//10
b2 = s%10
t = s//10
u = t//10
a2 = u%10
v = u//10
a1 = v%10
if a1 == b1 and a2 == b2:
count += 1
print(count) | A, B = list(map(int, input().split()))
count = 0
for n in range(A, B+1):
if n//10000 == n%10 and (n//1000)%10 == (n//10)%10:
count += 1
print(count) | p03416 |
A, B = list(map(int, input().split()))
cnt = 0
for i in range(A, B+1):
l = list(str(i))
if l == list(reversed(l)):
cnt += 1
print(cnt)
| A, B = list(map(int, input().split()))
cnt = 0
for i in range(A, B+1):
l = str(i)
if l == l[::-1]:
cnt += 1
print(cnt)
| p03416 |
# ABC 090: B – Palindromic Numbers
a, b = [int(s) for s in input().split()]
number_of_parindromics = 0
for i in range(a, b + 1):
if str(i) == str(i)[::-1]:
number_of_parindromics += 1
print(number_of_parindromics) | # ABC 090: B – Palindromic Numbers
a, b = list(map(int, input().split()))
nums = 0
for i in range(a, b + 1):
i_str = str(i)
nums = nums + 1 if i_str == i_str[::-1] else nums
print(nums) | p03416 |
a, b = list(map(int, input().split()))
cnt = 0
for i in range(a, b+1):
i = str(i)
if i[0] == i[-1] and i[1] == i[-2]:
cnt += 1
print(cnt)
| a, b = list(map(int, input().split()))
cnt = 0
for i in range(10):
for j in range(10):
for k in range(10):
c = 10000*i+1000*j+100*k+10*j+i
if a <= c <= b:
cnt += 1
print(cnt)
| p03416 |
A,B = list(map(int,input().split()))
n=0
for i in range(A,B+1):
if i == int(str(i)[::-1]):
n+=1
print(n) | A,B = list(map(int,input().split()))
ans = 0
for i in range(A,B+1):
s = str(i)
if s == s[::-1]:
ans += 1
print(ans)
| p03416 |
a,b = list(map(int,input().split()))
count = 0
for i in range(a,b+1):
s = str(i)
l = len(s)
if all(s[j] == s[l-j-1] for j in range(l)):
count += 1
print(count) | a,b = list(map(int,input().split()))
count = 0
for i in range(a,b+1):
s = str(i)
if s == s[::-1]:
count += 1
print(count) | p03416 |
a,b = list(map(int, input().split()))
cnt = 0
itr = b - a
for i in range(a,b+1):
m = str(i)
if m[0] == m[4] and m[1] == m[3]:
cnt += 1
print(cnt)
| a, b = list(map(int, input().split()))
cnt = 0
for i in range(a, b+1):
si = str(i)
if si[0] == si[4] and si[1] == si[3]:
cnt += 1
print(cnt)
| p03416 |
a, b = list(map(int, input().split()))
sum_n = 0
for i in range(a, b + 1):
if str(i)[0] == str(i)[-1] and str(i)[1] == str(i)[-2]:
sum_n += 1
print(sum_n) | a, b = list(map(int, input().split()))
sum_s = 0
for i in range(a, b + 1):
s = str(i)
if s[0] == s[-1] and s[1] == s[-2]:
sum_s += 1
print(sum_s) | p03416 |
a,b = list(map(int,input().split()))
c = 0
for i in range(a,b+1):
new = list(reversed(list(str(i))))
if new == list(str(i)):
c += 1
print(c) | a,b = list(map(int,input().split()))
c = 0
for i in range(a,b+1):
if list(str(i)) == list(str(i))[::-1]:
c += 1
print(c) | p03416 |
a,b = list(map(int,input().split()))
c = 0
for i in range(a,b+1):
if list(str(i)) == list(str(i))[::-1]:
c += 1
print(c) | a,b = list(map(int,input().split()))
c = 0
for i in range(a,b+1):
if str(i) == str(i)[::-1]:
c += 1
print(c) | p03416 |
a,b = list(map(int,input().split()))
ans = 0
for i in range(a,b+1):
x = str(i)
flag = 1
for j in range(len(x) // 2):
if x[j] != x[-(j+1)]:
flag = 0
ans += flag
print(ans) | def is_palindrome(s):
for i in range(len(s) // 2 + 1):
if s[i] != s[-i-1]:
return False
return True
a,b = list(map(int, input().split()))
ans = 0
for i in range(a,b+1):
if is_palindrome(str(i)):
ans += 1
print(ans) | p03416 |
a, b = list(map(int, input().split()))
count = 0
for i in range(a, b+1):
t_list = list(str(i))
if t_list[0] == t_list[4] and t_list[1] == t_list[3]:
count += 1
print(count) | a, b = list(map(int, input().split()))
c = 0
for i in range(a, b+1):
s = str(i)
t = s[4] + s[3]
if s[0] + s[1] == t :
c += 1
print(c) | p03416 |
a,b=list(map(int,input().split()))
count=0
for i in range(a,b+1):
if str(i)==str(i)[::-1]:
count+=1
print(count) | a,b=list(map(int,input().split()))
print((sum(i==i[::-1]for i in map(str,list(range(a,b+1)))))) | p03416 |
a, b = list(map(int, input().split()))
count = 0
for i in range(a, b+1):
if str(i) == str(i)[::-1]:
count += 1
print(count)
| a, b = list(map(int, input().split()))
ans = 0
for num in range(a, b+1):
s = str(num)
if s[0] == s[4] and s[1] == s[3]:
ans += 1
print(ans) | p03416 |
A, B = list(map(int, input().split()))
ans = 0
for i in range(A, B + 1):
num = str(i)
rev = num[::-1]
if num == rev:
ans += 1
print(ans) | A, B = list(map(int, input().split()))
ans = 0
for n in range(A, B + 1):
num = str(n)
if num == num[::-1]:
ans += 1
print(ans) | p03416 |
a, b = list(map(int, input().split()))
tmp=str(a)
c=0
for i in range(a,b+1):
tmp=str(i)
tmp2=""
for s in range(len(tmp)):
tmp2=tmp2+tmp[len(tmp)-1-s]
if tmp==tmp2:
c=c+1
print(c) | def main():
#1. input
a, b = list(map(int, input().split()))
ans = 0
#2. from A to B
for i in range(a, b+1):
#2.1. judge if the number is palindromic
if (str(i)[0]==str(i)[4]) and (str(i)[1]==str(i)[3]):
ans = ans+1
#3. output
print(ans)
if __name__ == '__main__':
main() | p03416 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.