text
stringlengths
198
433k
conversation_id
int64
0
109k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Very soon Berland will hold a School Team Programming Olympiad. From each of the m Berland regions a team of two people is invited to participate in the olympiad. The qualifying contest to form teams was held and it was attended by n Berland students. There were at least two schoolboys participating from each of the m regions of Berland. The result of each of the participants of the qualifying competition is an integer score from 0 to 800 inclusive. The team of each region is formed from two such members of the qualifying competition of the region, that none of them can be replaced by a schoolboy of the same region, not included in the team and who received a greater number of points. There may be a situation where a team of some region can not be formed uniquely, that is, there is more than one school team that meets the properties described above. In this case, the region needs to undertake an additional contest. The two teams in the region are considered to be different if there is at least one schoolboy who is included in one team and is not included in the other team. It is guaranteed that for each region at least two its representatives participated in the qualifying contest. Your task is, given the results of the qualifying competition, to identify the team from each region, or to announce that in this region its formation requires additional contests. Input The first line of the input contains two integers n and m (2 ≀ n ≀ 100 000, 1 ≀ m ≀ 10 000, n β‰₯ 2m) β€” the number of participants of the qualifying contest and the number of regions in Berland. Next n lines contain the description of the participants of the qualifying contest in the following format: Surname (a string of length from 1 to 10 characters and consisting of large and small English letters), region number (integer from 1 to m) and the number of points scored by the participant (integer from 0 to 800, inclusive). It is guaranteed that all surnames of all the participants are distinct and at least two people participated from each of the m regions. The surnames that only differ in letter cases, should be considered distinct. Output Print m lines. On the i-th line print the team of the i-th region β€” the surnames of the two team members in an arbitrary order, or a single character "?" (without the quotes) if you need to spend further qualifying contests in the region. Examples Input 5 2 Ivanov 1 763 Andreev 2 800 Petrov 1 595 Sidorov 1 790 Semenov 2 503 Output Sidorov Ivanov Andreev Semenov Input 5 2 Ivanov 1 800 Andreev 2 763 Petrov 1 800 Sidorov 1 800 Semenov 2 503 Output ? Andreev Semenov Note In the first sample region teams are uniquely determined. In the second sample the team from region 2 is uniquely determined and the team from region 1 can have three teams: "Petrov"-"Sidorov", "Ivanov"-"Sidorov", "Ivanov" -"Petrov", so it is impossible to determine a team uniquely. Submitted Solution: ``` #------------------------template--------------------------# import os import sys from math import * from collections import * from bisect import * from io import BytesIO, IOBase from fractions import * def vsInput(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") def value(): return tuple(map(int,input().split())) def array(): return [int(i) for i in input().split()] def Int(): return int(input()) def Str(): return input() def arrayS(): return [i for i in input().split()] #-------------------------code---------------------------# #vsInput() n,m=value() d=defaultdict(list) for i in range(n): name,r,sc=input().split() d[int(r)].append((int(sc),name)) #print(d) for i in range(1,m+1): if(len(d[i])==2): print(d[i][0][1],d[i][1][1]) else: t=sorted(d[i]) #print(t) if(t[-2][0]!=t[-3][0]): print(t[-1][1],t[-2][1]) else: print('?') ``` Yes
12,100
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Very soon Berland will hold a School Team Programming Olympiad. From each of the m Berland regions a team of two people is invited to participate in the olympiad. The qualifying contest to form teams was held and it was attended by n Berland students. There were at least two schoolboys participating from each of the m regions of Berland. The result of each of the participants of the qualifying competition is an integer score from 0 to 800 inclusive. The team of each region is formed from two such members of the qualifying competition of the region, that none of them can be replaced by a schoolboy of the same region, not included in the team and who received a greater number of points. There may be a situation where a team of some region can not be formed uniquely, that is, there is more than one school team that meets the properties described above. In this case, the region needs to undertake an additional contest. The two teams in the region are considered to be different if there is at least one schoolboy who is included in one team and is not included in the other team. It is guaranteed that for each region at least two its representatives participated in the qualifying contest. Your task is, given the results of the qualifying competition, to identify the team from each region, or to announce that in this region its formation requires additional contests. Input The first line of the input contains two integers n and m (2 ≀ n ≀ 100 000, 1 ≀ m ≀ 10 000, n β‰₯ 2m) β€” the number of participants of the qualifying contest and the number of regions in Berland. Next n lines contain the description of the participants of the qualifying contest in the following format: Surname (a string of length from 1 to 10 characters and consisting of large and small English letters), region number (integer from 1 to m) and the number of points scored by the participant (integer from 0 to 800, inclusive). It is guaranteed that all surnames of all the participants are distinct and at least two people participated from each of the m regions. The surnames that only differ in letter cases, should be considered distinct. Output Print m lines. On the i-th line print the team of the i-th region β€” the surnames of the two team members in an arbitrary order, or a single character "?" (without the quotes) if you need to spend further qualifying contests in the region. Examples Input 5 2 Ivanov 1 763 Andreev 2 800 Petrov 1 595 Sidorov 1 790 Semenov 2 503 Output Sidorov Ivanov Andreev Semenov Input 5 2 Ivanov 1 800 Andreev 2 763 Petrov 1 800 Sidorov 1 800 Semenov 2 503 Output ? Andreev Semenov Note In the first sample region teams are uniquely determined. In the second sample the team from region 2 is uniquely determined and the team from region 1 can have three teams: "Petrov"-"Sidorov", "Ivanov"-"Sidorov", "Ivanov" -"Petrov", so it is impossible to determine a team uniquely. Submitted Solution: ``` #!/usr/bin/env python3 read_ints = lambda : list(map(int, input().split())) if __name__ == '__main__': n, m = read_ints() R = [[] for _ in range(m)] for i in range(n): n,r,s = input().split() r = int(r) s = int(s) R[r-1].append((s,n)) for r in R: r.sort(reverse=True) if len(r) > 2: if r[1][0] == r[2][0]: print("?") else: print("%s %s" % (r[0][1], r[1][1])) else: print("%s %s" % (r[0][1], r[1][1])) ``` Yes
12,101
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Very soon Berland will hold a School Team Programming Olympiad. From each of the m Berland regions a team of two people is invited to participate in the olympiad. The qualifying contest to form teams was held and it was attended by n Berland students. There were at least two schoolboys participating from each of the m regions of Berland. The result of each of the participants of the qualifying competition is an integer score from 0 to 800 inclusive. The team of each region is formed from two such members of the qualifying competition of the region, that none of them can be replaced by a schoolboy of the same region, not included in the team and who received a greater number of points. There may be a situation where a team of some region can not be formed uniquely, that is, there is more than one school team that meets the properties described above. In this case, the region needs to undertake an additional contest. The two teams in the region are considered to be different if there is at least one schoolboy who is included in one team and is not included in the other team. It is guaranteed that for each region at least two its representatives participated in the qualifying contest. Your task is, given the results of the qualifying competition, to identify the team from each region, or to announce that in this region its formation requires additional contests. Input The first line of the input contains two integers n and m (2 ≀ n ≀ 100 000, 1 ≀ m ≀ 10 000, n β‰₯ 2m) β€” the number of participants of the qualifying contest and the number of regions in Berland. Next n lines contain the description of the participants of the qualifying contest in the following format: Surname (a string of length from 1 to 10 characters and consisting of large and small English letters), region number (integer from 1 to m) and the number of points scored by the participant (integer from 0 to 800, inclusive). It is guaranteed that all surnames of all the participants are distinct and at least two people participated from each of the m regions. The surnames that only differ in letter cases, should be considered distinct. Output Print m lines. On the i-th line print the team of the i-th region β€” the surnames of the two team members in an arbitrary order, or a single character "?" (without the quotes) if you need to spend further qualifying contests in the region. Examples Input 5 2 Ivanov 1 763 Andreev 2 800 Petrov 1 595 Sidorov 1 790 Semenov 2 503 Output Sidorov Ivanov Andreev Semenov Input 5 2 Ivanov 1 800 Andreev 2 763 Petrov 1 800 Sidorov 1 800 Semenov 2 503 Output ? Andreev Semenov Note In the first sample region teams are uniquely determined. In the second sample the team from region 2 is uniquely determined and the team from region 1 can have three teams: "Petrov"-"Sidorov", "Ivanov"-"Sidorov", "Ivanov" -"Petrov", so it is impossible to determine a team uniquely. Submitted Solution: ``` from collections import deque n, m = tuple( map( int, input().split())) parti = [deque() for i in range(m)] for i in range(n): s = input().split() name = s[0] reg = int(s[1]) sco = int(s[2]) if len(parti[reg - 1]) ==0: parti[reg-1].append([name, sco]) elif len(parti[reg - 1]) ==1: if sco<parti[reg - 1][0][1]: parti[reg-1].append([name, sco]) else: parti[reg-1].appendleft([name, sco]) else: if sco>=parti[reg-1][0][1]: while parti[reg-1][-1][1]<parti[reg-1][0][1]: parti[reg-1].pop() parti[reg-1].appendleft([name, sco]) elif sco>=parti[reg-1][-1][1]: while parti[reg-1][-1][1]<sco: parti[reg-1].pop() parti[reg-1].append([name, sco]) """ for i in range(m): print(parti[i]) """ for i in range(m): if len(parti[i])>2: print('?') else: print(parti[i][0][0], parti[i][1][0]) ``` Yes
12,102
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Very soon Berland will hold a School Team Programming Olympiad. From each of the m Berland regions a team of two people is invited to participate in the olympiad. The qualifying contest to form teams was held and it was attended by n Berland students. There were at least two schoolboys participating from each of the m regions of Berland. The result of each of the participants of the qualifying competition is an integer score from 0 to 800 inclusive. The team of each region is formed from two such members of the qualifying competition of the region, that none of them can be replaced by a schoolboy of the same region, not included in the team and who received a greater number of points. There may be a situation where a team of some region can not be formed uniquely, that is, there is more than one school team that meets the properties described above. In this case, the region needs to undertake an additional contest. The two teams in the region are considered to be different if there is at least one schoolboy who is included in one team and is not included in the other team. It is guaranteed that for each region at least two its representatives participated in the qualifying contest. Your task is, given the results of the qualifying competition, to identify the team from each region, or to announce that in this region its formation requires additional contests. Input The first line of the input contains two integers n and m (2 ≀ n ≀ 100 000, 1 ≀ m ≀ 10 000, n β‰₯ 2m) β€” the number of participants of the qualifying contest and the number of regions in Berland. Next n lines contain the description of the participants of the qualifying contest in the following format: Surname (a string of length from 1 to 10 characters and consisting of large and small English letters), region number (integer from 1 to m) and the number of points scored by the participant (integer from 0 to 800, inclusive). It is guaranteed that all surnames of all the participants are distinct and at least two people participated from each of the m regions. The surnames that only differ in letter cases, should be considered distinct. Output Print m lines. On the i-th line print the team of the i-th region β€” the surnames of the two team members in an arbitrary order, or a single character "?" (without the quotes) if you need to spend further qualifying contests in the region. Examples Input 5 2 Ivanov 1 763 Andreev 2 800 Petrov 1 595 Sidorov 1 790 Semenov 2 503 Output Sidorov Ivanov Andreev Semenov Input 5 2 Ivanov 1 800 Andreev 2 763 Petrov 1 800 Sidorov 1 800 Semenov 2 503 Output ? Andreev Semenov Note In the first sample region teams are uniquely determined. In the second sample the team from region 2 is uniquely determined and the team from region 1 can have three teams: "Petrov"-"Sidorov", "Ivanov"-"Sidorov", "Ivanov" -"Petrov", so it is impossible to determine a team uniquely. Submitted Solution: ``` n, m = map(int, input().split()) d = {} for i in range(1, m+1): d[i] = [] for i in range(n): a, b, c = input().split() b = int(b) ; c = int(c) d[b] += [(c, a)] for i in range(1, m+1): t = sorted(d[i], reverse=True) print('?' if len(t) > 2 and t[1][0] == t[2][0] else t[0][1] + ' ' + t[1][1]) ``` Yes
12,103
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Very soon Berland will hold a School Team Programming Olympiad. From each of the m Berland regions a team of two people is invited to participate in the olympiad. The qualifying contest to form teams was held and it was attended by n Berland students. There were at least two schoolboys participating from each of the m regions of Berland. The result of each of the participants of the qualifying competition is an integer score from 0 to 800 inclusive. The team of each region is formed from two such members of the qualifying competition of the region, that none of them can be replaced by a schoolboy of the same region, not included in the team and who received a greater number of points. There may be a situation where a team of some region can not be formed uniquely, that is, there is more than one school team that meets the properties described above. In this case, the region needs to undertake an additional contest. The two teams in the region are considered to be different if there is at least one schoolboy who is included in one team and is not included in the other team. It is guaranteed that for each region at least two its representatives participated in the qualifying contest. Your task is, given the results of the qualifying competition, to identify the team from each region, or to announce that in this region its formation requires additional contests. Input The first line of the input contains two integers n and m (2 ≀ n ≀ 100 000, 1 ≀ m ≀ 10 000, n β‰₯ 2m) β€” the number of participants of the qualifying contest and the number of regions in Berland. Next n lines contain the description of the participants of the qualifying contest in the following format: Surname (a string of length from 1 to 10 characters and consisting of large and small English letters), region number (integer from 1 to m) and the number of points scored by the participant (integer from 0 to 800, inclusive). It is guaranteed that all surnames of all the participants are distinct and at least two people participated from each of the m regions. The surnames that only differ in letter cases, should be considered distinct. Output Print m lines. On the i-th line print the team of the i-th region β€” the surnames of the two team members in an arbitrary order, or a single character "?" (without the quotes) if you need to spend further qualifying contests in the region. Examples Input 5 2 Ivanov 1 763 Andreev 2 800 Petrov 1 595 Sidorov 1 790 Semenov 2 503 Output Sidorov Ivanov Andreev Semenov Input 5 2 Ivanov 1 800 Andreev 2 763 Petrov 1 800 Sidorov 1 800 Semenov 2 503 Output ? Andreev Semenov Note In the first sample region teams are uniquely determined. In the second sample the team from region 2 is uniquely determined and the team from region 1 can have three teams: "Petrov"-"Sidorov", "Ivanov"-"Sidorov", "Ivanov" -"Petrov", so it is impossible to determine a team uniquely. Submitted Solution: ``` from sys import stdin,stdout a,b=map(int,stdin.readline().split()) d={} for i in range(1,b+1):d[i]={} for _ in " "*a: x,y,z=stdin.readline().split() y=int(y);z=int(z) if len(d[y])<2: if d[y].get(z,-1)==-1: d[y][z]=(x,) else:d[y][z]+=(x,) else: if min(d[y])<z:d[y].pop(min(d[y])) else:continue if d[y].get(z,-1)==-1: d[y][z]=(x,) else:d[y][z]+=(x,) ans='' for i in d: s=[] for j in d[i]: for k in d[i][j]:s.append(k) if len(s)==2:break if len(s)==2:ans+=' '.join(s) else:ans+='?' ans+='\n' stdout.write(ans) ``` No
12,104
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Very soon Berland will hold a School Team Programming Olympiad. From each of the m Berland regions a team of two people is invited to participate in the olympiad. The qualifying contest to form teams was held and it was attended by n Berland students. There were at least two schoolboys participating from each of the m regions of Berland. The result of each of the participants of the qualifying competition is an integer score from 0 to 800 inclusive. The team of each region is formed from two such members of the qualifying competition of the region, that none of them can be replaced by a schoolboy of the same region, not included in the team and who received a greater number of points. There may be a situation where a team of some region can not be formed uniquely, that is, there is more than one school team that meets the properties described above. In this case, the region needs to undertake an additional contest. The two teams in the region are considered to be different if there is at least one schoolboy who is included in one team and is not included in the other team. It is guaranteed that for each region at least two its representatives participated in the qualifying contest. Your task is, given the results of the qualifying competition, to identify the team from each region, or to announce that in this region its formation requires additional contests. Input The first line of the input contains two integers n and m (2 ≀ n ≀ 100 000, 1 ≀ m ≀ 10 000, n β‰₯ 2m) β€” the number of participants of the qualifying contest and the number of regions in Berland. Next n lines contain the description of the participants of the qualifying contest in the following format: Surname (a string of length from 1 to 10 characters and consisting of large and small English letters), region number (integer from 1 to m) and the number of points scored by the participant (integer from 0 to 800, inclusive). It is guaranteed that all surnames of all the participants are distinct and at least two people participated from each of the m regions. The surnames that only differ in letter cases, should be considered distinct. Output Print m lines. On the i-th line print the team of the i-th region β€” the surnames of the two team members in an arbitrary order, or a single character "?" (without the quotes) if you need to spend further qualifying contests in the region. Examples Input 5 2 Ivanov 1 763 Andreev 2 800 Petrov 1 595 Sidorov 1 790 Semenov 2 503 Output Sidorov Ivanov Andreev Semenov Input 5 2 Ivanov 1 800 Andreev 2 763 Petrov 1 800 Sidorov 1 800 Semenov 2 503 Output ? Andreev Semenov Note In the first sample region teams are uniquely determined. In the second sample the team from region 2 is uniquely determined and the team from region 1 can have three teams: "Petrov"-"Sidorov", "Ivanov"-"Sidorov", "Ivanov" -"Petrov", so it is impossible to determine a team uniquely. Submitted Solution: ``` '__author__'=='deepak Singh Mehta(learning to code :) ) ' if __name__=='__main__': n,m = map(int,input().split()) memo =dict() for i in range(1,m+1): memo[i]=[] for _ in range(n): name,idx,pt = map(str,input().split()) idx , pt = int(idx), int(pt) memo[idx].append((pt,name)) for i in range(1,m+1): lst = memo[i] lst = sorted(lst)[::-1] con = [] team = [] for jj in lst: if jj[0] not in con: con.append(jj[0]) team.append(jj[1]) if len(con)==2: break if len(con) <2: print("?") else: for nn in team: if nn==team[-1]: print(nn) else: print(nn,end=' ') ``` No
12,105
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Very soon Berland will hold a School Team Programming Olympiad. From each of the m Berland regions a team of two people is invited to participate in the olympiad. The qualifying contest to form teams was held and it was attended by n Berland students. There were at least two schoolboys participating from each of the m regions of Berland. The result of each of the participants of the qualifying competition is an integer score from 0 to 800 inclusive. The team of each region is formed from two such members of the qualifying competition of the region, that none of them can be replaced by a schoolboy of the same region, not included in the team and who received a greater number of points. There may be a situation where a team of some region can not be formed uniquely, that is, there is more than one school team that meets the properties described above. In this case, the region needs to undertake an additional contest. The two teams in the region are considered to be different if there is at least one schoolboy who is included in one team and is not included in the other team. It is guaranteed that for each region at least two its representatives participated in the qualifying contest. Your task is, given the results of the qualifying competition, to identify the team from each region, or to announce that in this region its formation requires additional contests. Input The first line of the input contains two integers n and m (2 ≀ n ≀ 100 000, 1 ≀ m ≀ 10 000, n β‰₯ 2m) β€” the number of participants of the qualifying contest and the number of regions in Berland. Next n lines contain the description of the participants of the qualifying contest in the following format: Surname (a string of length from 1 to 10 characters and consisting of large and small English letters), region number (integer from 1 to m) and the number of points scored by the participant (integer from 0 to 800, inclusive). It is guaranteed that all surnames of all the participants are distinct and at least two people participated from each of the m regions. The surnames that only differ in letter cases, should be considered distinct. Output Print m lines. On the i-th line print the team of the i-th region β€” the surnames of the two team members in an arbitrary order, or a single character "?" (without the quotes) if you need to spend further qualifying contests in the region. Examples Input 5 2 Ivanov 1 763 Andreev 2 800 Petrov 1 595 Sidorov 1 790 Semenov 2 503 Output Sidorov Ivanov Andreev Semenov Input 5 2 Ivanov 1 800 Andreev 2 763 Petrov 1 800 Sidorov 1 800 Semenov 2 503 Output ? Andreev Semenov Note In the first sample region teams are uniquely determined. In the second sample the team from region 2 is uniquely determined and the team from region 1 can have three teams: "Petrov"-"Sidorov", "Ivanov"-"Sidorov", "Ivanov" -"Petrov", so it is impossible to determine a team uniquely. Submitted Solution: ``` from sys import stdin,stdout a,b=map(int,stdin.readline().split()) d={} for i in range(1,b+1):d[i]={} for _ in " "*a: x,y,z=stdin.readline().split() y=int(y);z=int(z) if len(d[y])<2: if d[y].get(z,-1)==-1: d[y][z]=(x,) else:d[y][z]+=(x,) else: if min(d[y])<z:d[y].pop(min(d[y])) else:continue if d[y].get(z,-1)==-1: d[y][z]=(x,) else:d[y][z]+=(x,) ans='' for i in range(1,b+1): s=[] for j in d[i]: for k in d[i][j]:s.append(k) if len(s)==2:break if len(s)==2:ans+=' '.join(s) else:ans+='?' ans+='\n' stdout.write(ans) ``` No
12,106
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Very soon Berland will hold a School Team Programming Olympiad. From each of the m Berland regions a team of two people is invited to participate in the olympiad. The qualifying contest to form teams was held and it was attended by n Berland students. There were at least two schoolboys participating from each of the m regions of Berland. The result of each of the participants of the qualifying competition is an integer score from 0 to 800 inclusive. The team of each region is formed from two such members of the qualifying competition of the region, that none of them can be replaced by a schoolboy of the same region, not included in the team and who received a greater number of points. There may be a situation where a team of some region can not be formed uniquely, that is, there is more than one school team that meets the properties described above. In this case, the region needs to undertake an additional contest. The two teams in the region are considered to be different if there is at least one schoolboy who is included in one team and is not included in the other team. It is guaranteed that for each region at least two its representatives participated in the qualifying contest. Your task is, given the results of the qualifying competition, to identify the team from each region, or to announce that in this region its formation requires additional contests. Input The first line of the input contains two integers n and m (2 ≀ n ≀ 100 000, 1 ≀ m ≀ 10 000, n β‰₯ 2m) β€” the number of participants of the qualifying contest and the number of regions in Berland. Next n lines contain the description of the participants of the qualifying contest in the following format: Surname (a string of length from 1 to 10 characters and consisting of large and small English letters), region number (integer from 1 to m) and the number of points scored by the participant (integer from 0 to 800, inclusive). It is guaranteed that all surnames of all the participants are distinct and at least two people participated from each of the m regions. The surnames that only differ in letter cases, should be considered distinct. Output Print m lines. On the i-th line print the team of the i-th region β€” the surnames of the two team members in an arbitrary order, or a single character "?" (without the quotes) if you need to spend further qualifying contests in the region. Examples Input 5 2 Ivanov 1 763 Andreev 2 800 Petrov 1 595 Sidorov 1 790 Semenov 2 503 Output Sidorov Ivanov Andreev Semenov Input 5 2 Ivanov 1 800 Andreev 2 763 Petrov 1 800 Sidorov 1 800 Semenov 2 503 Output ? Andreev Semenov Note In the first sample region teams are uniquely determined. In the second sample the team from region 2 is uniquely determined and the team from region 1 can have three teams: "Petrov"-"Sidorov", "Ivanov"-"Sidorov", "Ivanov" -"Petrov", so it is impossible to determine a team uniquely. Submitted Solution: ``` n, m = map(int,input().split()) temp = [list(map(str,input().split())) for i in range(n)] regions = [[-1]*3 for i in range(m+1)] regions2 = [[-1]*3 for i in range(m+1)] for i in range(n): j = 1 temp2 = int(temp[i][2]) temp3 = int(temp[i][1]) if temp2 > regions[temp3][0]: regions[temp3][0] = temp2 regions2[temp3][0] = i while 3 > j and temp2 > regions[temp3][j]: regions[temp3][j-1] = regions[temp3][j] regions[temp3][j] = temp2 regions2[temp3][j-1] = regions2[temp3][j] regions2[temp3][j] = i j += 1 for i in range(1,m+1): if regions[i][1] != regions[i][2]: print(temp[regions2[i][1]][0],temp[regions2[i][2]][0]) else: print("?") ``` No
12,107
Provide tags and a correct Python 3 solution for this coding contest problem. Sasha lives in a big happy family. At the Man's Day all the men of the family gather to celebrate it following their own traditions. There are n men in Sasha's family, so let's number them with integers from 1 to n. Each man has at most one father but may have arbitrary number of sons. Man number A is considered to be the ancestor of the man number B if at least one of the following conditions is satisfied: * A = B; * the man number A is the father of the man number B; * there is a man number C, such that the man number A is his ancestor and the man number C is the father of the man number B. Of course, if the man number A is an ancestor of the man number B and A β‰  B, then the man number B is not an ancestor of the man number A. The tradition of the Sasha's family is to give gifts at the Man's Day. Because giving gifts in a normal way is boring, each year the following happens. 1. A list of candidates is prepared, containing some (possibly all) of the n men in some order. 2. Each of the n men decides to give a gift. 3. In order to choose a person to give a gift to, man A looks through the list and picks the first man B in the list, such that B is an ancestor of A and gives him a gift. Note that according to definition it may happen that a person gives a gift to himself. 4. If there is no ancestor of a person in the list, he becomes sad and leaves the celebration without giving a gift to anyone. This year you have decided to help in organizing celebration and asked each of the n men, who do they want to give presents to (this person is chosen only among ancestors). Are you able to make a list of candidates, such that all the wishes will be satisfied if they give gifts according to the process described above? Input In the first line of the input two integers n and m (0 ≀ m < n ≀ 100 000) are given β€” the number of the men in the Sasha's family and the number of family relations in it respectively. The next m lines describe family relations: the (i + 1)th line consists of pair of integers pi and qi (1 ≀ pi, qi ≀ n, pi β‰  qi) meaning that the man numbered pi is the father of the man numbered qi. It is guaranteed that every pair of numbers appears at most once, that among every pair of two different men at least one of them is not an ancestor of another and that every man has at most one father. The next line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n), ith of which means that the man numbered i wants to give a gift to the man numbered ai. It is guaranteed that for every 1 ≀ i ≀ n the man numbered ai is an ancestor of the man numbered i. Output Print an integer k (1 ≀ k ≀ n) β€” the number of the men in the list of candidates, in the first line. Print then k pairwise different positive integers not exceeding n β€” the numbers of the men in the list in an order satisfying every of the men's wishes, one per line. If there are more than one appropriate lists, print any of them. If there is no appropriate list print - 1 in the only line. Examples Input 3 2 1 2 2 3 1 2 1 Output -1 Input 4 2 1 2 3 4 1 2 3 3 Output 3 2 1 3 Note The first sample explanation: * if there would be no 1 in the list then the first and the third man's wishes would not be satisfied (a1 = a3 = 1); * if there would be no 2 in the list then the second man wish would not be satisfied (a2 = 2); * if 1 would stay before 2 in the answer then the second man would have to give his gift to the first man, but he wants to give it to himself (a2 = 2). * if, at the other hand, the man numbered 2 would stay before the man numbered 1, then the third man would have to give his gift to the second man, but not to the first (a3 = 1). Tags: constructive algorithms, dfs and similar, graphs, trees Correct Solution: ``` # by the authority of GOD author: manhar singh sachdev # import os,sys from io import BytesIO, IOBase def main(): n,m = map(int,input().split()) path = [[] for _ in range(n)] indeg = [0]*n for _ in range(m): p1,q1 = map(int,input().split()) path[p1-1].append(q1-1) indeg[q1-1] += 1 gif = list(map(int,input().split())) fin,poi = [],[0]*n for i in range(n): if indeg[i]: continue st,st1 = [i],[i] while len(st): x,y = st[-1],poi[st[-1]] if y == len(path[x]): st.pop() continue z = path[x][y] if gif[z] == z+1: st1.append(z) elif gif[z] != gif[x]: print(-1) return st.append(z) poi[x] += 1 fin += list(map(lambda xx:xx+1,st1[::-1])) print(len(fin)) print(*fin,sep='\n') # Fast IO Region BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") if __name__ == "__main__": main() ```
12,108
Provide tags and a correct Python 3 solution for this coding contest problem. Sasha lives in a big happy family. At the Man's Day all the men of the family gather to celebrate it following their own traditions. There are n men in Sasha's family, so let's number them with integers from 1 to n. Each man has at most one father but may have arbitrary number of sons. Man number A is considered to be the ancestor of the man number B if at least one of the following conditions is satisfied: * A = B; * the man number A is the father of the man number B; * there is a man number C, such that the man number A is his ancestor and the man number C is the father of the man number B. Of course, if the man number A is an ancestor of the man number B and A β‰  B, then the man number B is not an ancestor of the man number A. The tradition of the Sasha's family is to give gifts at the Man's Day. Because giving gifts in a normal way is boring, each year the following happens. 1. A list of candidates is prepared, containing some (possibly all) of the n men in some order. 2. Each of the n men decides to give a gift. 3. In order to choose a person to give a gift to, man A looks through the list and picks the first man B in the list, such that B is an ancestor of A and gives him a gift. Note that according to definition it may happen that a person gives a gift to himself. 4. If there is no ancestor of a person in the list, he becomes sad and leaves the celebration without giving a gift to anyone. This year you have decided to help in organizing celebration and asked each of the n men, who do they want to give presents to (this person is chosen only among ancestors). Are you able to make a list of candidates, such that all the wishes will be satisfied if they give gifts according to the process described above? Input In the first line of the input two integers n and m (0 ≀ m < n ≀ 100 000) are given β€” the number of the men in the Sasha's family and the number of family relations in it respectively. The next m lines describe family relations: the (i + 1)th line consists of pair of integers pi and qi (1 ≀ pi, qi ≀ n, pi β‰  qi) meaning that the man numbered pi is the father of the man numbered qi. It is guaranteed that every pair of numbers appears at most once, that among every pair of two different men at least one of them is not an ancestor of another and that every man has at most one father. The next line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n), ith of which means that the man numbered i wants to give a gift to the man numbered ai. It is guaranteed that for every 1 ≀ i ≀ n the man numbered ai is an ancestor of the man numbered i. Output Print an integer k (1 ≀ k ≀ n) β€” the number of the men in the list of candidates, in the first line. Print then k pairwise different positive integers not exceeding n β€” the numbers of the men in the list in an order satisfying every of the men's wishes, one per line. If there are more than one appropriate lists, print any of them. If there is no appropriate list print - 1 in the only line. Examples Input 3 2 1 2 2 3 1 2 1 Output -1 Input 4 2 1 2 3 4 1 2 3 3 Output 3 2 1 3 Note The first sample explanation: * if there would be no 1 in the list then the first and the third man's wishes would not be satisfied (a1 = a3 = 1); * if there would be no 2 in the list then the second man wish would not be satisfied (a2 = 2); * if 1 would stay before 2 in the answer then the second man would have to give his gift to the first man, but he wants to give it to himself (a2 = 2). * if, at the other hand, the man numbered 2 would stay before the man numbered 1, then the third man would have to give his gift to the second man, but not to the first (a3 = 1). Tags: constructive algorithms, dfs and similar, graphs, trees Correct Solution: ``` # [https://codeforces.com/contest/681/submission/37694242 <- https://codeforces.com/contest/681/status/D <- https://codeforces.com/contest/681 <- https://codeforces.com/blog/entry/45425 <- https://codeforces.com/problemset/problem/681/D <- https://algoprog.ru/material/pc681pD] (n, m) = map(int, input().split()) adj = [[] for _ in range(n)] cp = [-1] * n for i in range(m): p, c = map(int, input().split()) adj[p - 1].append(c - 1) cp[c - 1] = p - 1 pres = [i - 1 for i in map(int, input().split())] level = [0] * n from collections import deque def bfs(v): q = deque([v]) while q: v = q.pop() if cp[v] >= 0: level[v] = level[cp[v]] + 1 for nv in adj[v]: q.append(nv) for i in range(n): if cp[i] == -1: bfs(i) for i in range(n): if level[i] > 0: par = cp[i] if pres[i] != pres[par] and level[pres[i]] <= level[par]: print(-1) exit() pres = list(set(pres)) pres = sorted(pres, key = lambda i : level[i], reverse = True) print(len(pres)) pres = [i + 1 for i in pres] print("\n".join(map(str, pres))) ```
12,109
Provide tags and a correct Python 3 solution for this coding contest problem. Sasha lives in a big happy family. At the Man's Day all the men of the family gather to celebrate it following their own traditions. There are n men in Sasha's family, so let's number them with integers from 1 to n. Each man has at most one father but may have arbitrary number of sons. Man number A is considered to be the ancestor of the man number B if at least one of the following conditions is satisfied: * A = B; * the man number A is the father of the man number B; * there is a man number C, such that the man number A is his ancestor and the man number C is the father of the man number B. Of course, if the man number A is an ancestor of the man number B and A β‰  B, then the man number B is not an ancestor of the man number A. The tradition of the Sasha's family is to give gifts at the Man's Day. Because giving gifts in a normal way is boring, each year the following happens. 1. A list of candidates is prepared, containing some (possibly all) of the n men in some order. 2. Each of the n men decides to give a gift. 3. In order to choose a person to give a gift to, man A looks through the list and picks the first man B in the list, such that B is an ancestor of A and gives him a gift. Note that according to definition it may happen that a person gives a gift to himself. 4. If there is no ancestor of a person in the list, he becomes sad and leaves the celebration without giving a gift to anyone. This year you have decided to help in organizing celebration and asked each of the n men, who do they want to give presents to (this person is chosen only among ancestors). Are you able to make a list of candidates, such that all the wishes will be satisfied if they give gifts according to the process described above? Input In the first line of the input two integers n and m (0 ≀ m < n ≀ 100 000) are given β€” the number of the men in the Sasha's family and the number of family relations in it respectively. The next m lines describe family relations: the (i + 1)th line consists of pair of integers pi and qi (1 ≀ pi, qi ≀ n, pi β‰  qi) meaning that the man numbered pi is the father of the man numbered qi. It is guaranteed that every pair of numbers appears at most once, that among every pair of two different men at least one of them is not an ancestor of another and that every man has at most one father. The next line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n), ith of which means that the man numbered i wants to give a gift to the man numbered ai. It is guaranteed that for every 1 ≀ i ≀ n the man numbered ai is an ancestor of the man numbered i. Output Print an integer k (1 ≀ k ≀ n) β€” the number of the men in the list of candidates, in the first line. Print then k pairwise different positive integers not exceeding n β€” the numbers of the men in the list in an order satisfying every of the men's wishes, one per line. If there are more than one appropriate lists, print any of them. If there is no appropriate list print - 1 in the only line. Examples Input 3 2 1 2 2 3 1 2 1 Output -1 Input 4 2 1 2 3 4 1 2 3 3 Output 3 2 1 3 Note The first sample explanation: * if there would be no 1 in the list then the first and the third man's wishes would not be satisfied (a1 = a3 = 1); * if there would be no 2 in the list then the second man wish would not be satisfied (a2 = 2); * if 1 would stay before 2 in the answer then the second man would have to give his gift to the first man, but he wants to give it to himself (a2 = 2). * if, at the other hand, the man numbered 2 would stay before the man numbered 1, then the third man would have to give his gift to the second man, but not to the first (a3 = 1). Tags: constructive algorithms, dfs and similar, graphs, trees Correct Solution: ``` n, m = map(int, input().split()) adj = [[] for _ in range(n)] cp = [-1] * n for i in range(m): p, c = map(int, input().split()) adj[p - 1].append(c - 1) cp[c - 1] = p - 1 pres = [i - 1 for i in map(int, input().split())] level = [0] * n from collections import deque def bfs(v): q = deque([v]) while q: v = q.pop() if cp[v] >= 0: level[v] = level[cp[v]] + 1 for nv in adj[v]: q.append(nv) for i in range(n): if cp[i] == -1: bfs(i) for i in range(n): if level[i] > 0: par = cp[i] if pres[i] != pres[par] and level[pres[i]] <= level[par]: print(-1) exit() pres = list(set(pres)) pres = sorted(pres, key = lambda i : level[i], reverse = True) print(len(pres)) pres = [i + 1 for i in pres] print("\n".join(map(str, pres))) ```
12,110
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Sasha lives in a big happy family. At the Man's Day all the men of the family gather to celebrate it following their own traditions. There are n men in Sasha's family, so let's number them with integers from 1 to n. Each man has at most one father but may have arbitrary number of sons. Man number A is considered to be the ancestor of the man number B if at least one of the following conditions is satisfied: * A = B; * the man number A is the father of the man number B; * there is a man number C, such that the man number A is his ancestor and the man number C is the father of the man number B. Of course, if the man number A is an ancestor of the man number B and A β‰  B, then the man number B is not an ancestor of the man number A. The tradition of the Sasha's family is to give gifts at the Man's Day. Because giving gifts in a normal way is boring, each year the following happens. 1. A list of candidates is prepared, containing some (possibly all) of the n men in some order. 2. Each of the n men decides to give a gift. 3. In order to choose a person to give a gift to, man A looks through the list and picks the first man B in the list, such that B is an ancestor of A and gives him a gift. Note that according to definition it may happen that a person gives a gift to himself. 4. If there is no ancestor of a person in the list, he becomes sad and leaves the celebration without giving a gift to anyone. This year you have decided to help in organizing celebration and asked each of the n men, who do they want to give presents to (this person is chosen only among ancestors). Are you able to make a list of candidates, such that all the wishes will be satisfied if they give gifts according to the process described above? Input In the first line of the input two integers n and m (0 ≀ m < n ≀ 100 000) are given β€” the number of the men in the Sasha's family and the number of family relations in it respectively. The next m lines describe family relations: the (i + 1)th line consists of pair of integers pi and qi (1 ≀ pi, qi ≀ n, pi β‰  qi) meaning that the man numbered pi is the father of the man numbered qi. It is guaranteed that every pair of numbers appears at most once, that among every pair of two different men at least one of them is not an ancestor of another and that every man has at most one father. The next line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n), ith of which means that the man numbered i wants to give a gift to the man numbered ai. It is guaranteed that for every 1 ≀ i ≀ n the man numbered ai is an ancestor of the man numbered i. Output Print an integer k (1 ≀ k ≀ n) β€” the number of the men in the list of candidates, in the first line. Print then k pairwise different positive integers not exceeding n β€” the numbers of the men in the list in an order satisfying every of the men's wishes, one per line. If there are more than one appropriate lists, print any of them. If there is no appropriate list print - 1 in the only line. Examples Input 3 2 1 2 2 3 1 2 1 Output -1 Input 4 2 1 2 3 4 1 2 3 3 Output 3 2 1 3 Note The first sample explanation: * if there would be no 1 in the list then the first and the third man's wishes would not be satisfied (a1 = a3 = 1); * if there would be no 2 in the list then the second man wish would not be satisfied (a2 = 2); * if 1 would stay before 2 in the answer then the second man would have to give his gift to the first man, but he wants to give it to himself (a2 = 2). * if, at the other hand, the man numbered 2 would stay before the man numbered 1, then the third man would have to give his gift to the second man, but not to the first (a3 = 1). Submitted Solution: ``` # by the authority of GOD author: manhar singh sachdev # import os,sys from io import BytesIO, IOBase def main(): n,m = map(int,input().split()) path = [[] for _ in range(n)] indeg = [0]*n for _ in range(m): p1,q1 = map(int,input().split()) path[p1-1].append(q1-1) indeg[q1-1] += 1 gif = list(map(int,input().split())) fin = [] poi = [0]*n for i in range(n): if indeg[i]: continue st,st1 = [i],[i] while len(st): x,y = st[-1],poi[st[-1]] if y == len(path[x]): st.pop() continue z = path[x][y] if gif[z] == z+1: st.append(z) st1.append(z) elif gif[z] != x+1: print(-1) return poi[x] += 1 fin += list(map(lambda xx:xx+1,st1[::-1])) print(len(fin)) print(*fin,sep='\n') # Fast IO Region BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") if __name__ == "__main__": main() ``` No
12,111
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Sasha lives in a big happy family. At the Man's Day all the men of the family gather to celebrate it following their own traditions. There are n men in Sasha's family, so let's number them with integers from 1 to n. Each man has at most one father but may have arbitrary number of sons. Man number A is considered to be the ancestor of the man number B if at least one of the following conditions is satisfied: * A = B; * the man number A is the father of the man number B; * there is a man number C, such that the man number A is his ancestor and the man number C is the father of the man number B. Of course, if the man number A is an ancestor of the man number B and A β‰  B, then the man number B is not an ancestor of the man number A. The tradition of the Sasha's family is to give gifts at the Man's Day. Because giving gifts in a normal way is boring, each year the following happens. 1. A list of candidates is prepared, containing some (possibly all) of the n men in some order. 2. Each of the n men decides to give a gift. 3. In order to choose a person to give a gift to, man A looks through the list and picks the first man B in the list, such that B is an ancestor of A and gives him a gift. Note that according to definition it may happen that a person gives a gift to himself. 4. If there is no ancestor of a person in the list, he becomes sad and leaves the celebration without giving a gift to anyone. This year you have decided to help in organizing celebration and asked each of the n men, who do they want to give presents to (this person is chosen only among ancestors). Are you able to make a list of candidates, such that all the wishes will be satisfied if they give gifts according to the process described above? Input In the first line of the input two integers n and m (0 ≀ m < n ≀ 100 000) are given β€” the number of the men in the Sasha's family and the number of family relations in it respectively. The next m lines describe family relations: the (i + 1)th line consists of pair of integers pi and qi (1 ≀ pi, qi ≀ n, pi β‰  qi) meaning that the man numbered pi is the father of the man numbered qi. It is guaranteed that every pair of numbers appears at most once, that among every pair of two different men at least one of them is not an ancestor of another and that every man has at most one father. The next line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n), ith of which means that the man numbered i wants to give a gift to the man numbered ai. It is guaranteed that for every 1 ≀ i ≀ n the man numbered ai is an ancestor of the man numbered i. Output Print an integer k (1 ≀ k ≀ n) β€” the number of the men in the list of candidates, in the first line. Print then k pairwise different positive integers not exceeding n β€” the numbers of the men in the list in an order satisfying every of the men's wishes, one per line. If there are more than one appropriate lists, print any of them. If there is no appropriate list print - 1 in the only line. Examples Input 3 2 1 2 2 3 1 2 1 Output -1 Input 4 2 1 2 3 4 1 2 3 3 Output 3 2 1 3 Note The first sample explanation: * if there would be no 1 in the list then the first and the third man's wishes would not be satisfied (a1 = a3 = 1); * if there would be no 2 in the list then the second man wish would not be satisfied (a2 = 2); * if 1 would stay before 2 in the answer then the second man would have to give his gift to the first man, but he wants to give it to himself (a2 = 2). * if, at the other hand, the man numbered 2 would stay before the man numbered 1, then the third man would have to give his gift to the second man, but not to the first (a3 = 1). Submitted Solution: ``` n, k = list(map(int, input().split())) c = [[-1] * n for i in range(n)] for i in range(n - 1): temp = list(map(int, input().split())) index = 0 for j in range(i + 1, n): c[j][i] = c[i][j] = temp[index] index += 1 sum = 0 if k == 1 or (k > 2 and n == k + 1): for i in range(n): for j in range(n): if c[i][j] != -1: sum += c[i][j] print(int(sum / n)) elif k == 2: count = n * (n - 1) / 2 for i in range(n): adj = [] for j in range(n): if c[i][j] != -1: adj.append(j) for j in range(len(adj)): for z in range(j + 1, len(adj)): sum += c[i][adj[j]] + c[i][adj[z]] print(int(sum/count)) ``` No
12,112
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Sasha lives in a big happy family. At the Man's Day all the men of the family gather to celebrate it following their own traditions. There are n men in Sasha's family, so let's number them with integers from 1 to n. Each man has at most one father but may have arbitrary number of sons. Man number A is considered to be the ancestor of the man number B if at least one of the following conditions is satisfied: * A = B; * the man number A is the father of the man number B; * there is a man number C, such that the man number A is his ancestor and the man number C is the father of the man number B. Of course, if the man number A is an ancestor of the man number B and A β‰  B, then the man number B is not an ancestor of the man number A. The tradition of the Sasha's family is to give gifts at the Man's Day. Because giving gifts in a normal way is boring, each year the following happens. 1. A list of candidates is prepared, containing some (possibly all) of the n men in some order. 2. Each of the n men decides to give a gift. 3. In order to choose a person to give a gift to, man A looks through the list and picks the first man B in the list, such that B is an ancestor of A and gives him a gift. Note that according to definition it may happen that a person gives a gift to himself. 4. If there is no ancestor of a person in the list, he becomes sad and leaves the celebration without giving a gift to anyone. This year you have decided to help in organizing celebration and asked each of the n men, who do they want to give presents to (this person is chosen only among ancestors). Are you able to make a list of candidates, such that all the wishes will be satisfied if they give gifts according to the process described above? Input In the first line of the input two integers n and m (0 ≀ m < n ≀ 100 000) are given β€” the number of the men in the Sasha's family and the number of family relations in it respectively. The next m lines describe family relations: the (i + 1)th line consists of pair of integers pi and qi (1 ≀ pi, qi ≀ n, pi β‰  qi) meaning that the man numbered pi is the father of the man numbered qi. It is guaranteed that every pair of numbers appears at most once, that among every pair of two different men at least one of them is not an ancestor of another and that every man has at most one father. The next line contains n integers a1, a2, ..., an (1 ≀ ai ≀ n), ith of which means that the man numbered i wants to give a gift to the man numbered ai. It is guaranteed that for every 1 ≀ i ≀ n the man numbered ai is an ancestor of the man numbered i. Output Print an integer k (1 ≀ k ≀ n) β€” the number of the men in the list of candidates, in the first line. Print then k pairwise different positive integers not exceeding n β€” the numbers of the men in the list in an order satisfying every of the men's wishes, one per line. If there are more than one appropriate lists, print any of them. If there is no appropriate list print - 1 in the only line. Examples Input 3 2 1 2 2 3 1 2 1 Output -1 Input 4 2 1 2 3 4 1 2 3 3 Output 3 2 1 3 Note The first sample explanation: * if there would be no 1 in the list then the first and the third man's wishes would not be satisfied (a1 = a3 = 1); * if there would be no 2 in the list then the second man wish would not be satisfied (a2 = 2); * if 1 would stay before 2 in the answer then the second man would have to give his gift to the first man, but he wants to give it to himself (a2 = 2). * if, at the other hand, the man numbered 2 would stay before the man numbered 1, then the third man would have to give his gift to the second man, but not to the first (a3 = 1). Submitted Solution: ``` n, m = map(int, input().split()) adj = [[] for _ in range(n)] cp = [-1] * n for i in range(m): p, c = map(int, input().split()) adj[p - 1].append(c - 1) cp[c - 1] = p - 1 pres = [i - 1 for i in map(int, input().split())] level = [0] * n from collections import deque def bfs(v): q = deque([v]) while q: v = q.pop() if cp[v] > 0: level[v] = level[cp[v]] + 1 for nv in adj[v]: q.append(nv) for i in range(n): if cp[i] == -1: bfs(i) for i in range(n): if level[i] > 0: par = cp[i] if pres[i] != pres[par] and level[pres[i]] <= level[par]: print(-1) exit() pres = list(set(pres)) pres = sorted(pres, key = lambda i : level[i], reverse = True) print(len(pres)) for pi in pres: print(pi + 1) ``` No
12,113
Provide tags and a correct Python 3 solution for this coding contest problem. Vasily has a number a, which he wants to turn into a number b. For this purpose, he can do two types of operations: * multiply the current number by 2 (that is, replace the number x by 2Β·x); * append the digit 1 to the right of current number (that is, replace the number x by 10Β·x + 1). You need to help Vasily to transform the number a into the number b using only the operations described above, or find that it is impossible. Note that in this task you are not required to minimize the number of operations. It suffices to find any way to transform a into b. Input The first line contains two positive integers a and b (1 ≀ a < b ≀ 109) β€” the number which Vasily has and the number he wants to have. Output If there is no way to get b from a, print "NO" (without quotes). Otherwise print three lines. On the first line print "YES" (without quotes). The second line should contain single integer k β€” the length of the transformation sequence. On the third line print the sequence of transformations x1, x2, ..., xk, where: * x1 should be equal to a, * xk should be equal to b, * xi should be obtained from xi - 1 using any of two described operations (1 < i ≀ k). If there are multiple answers, print any of them. Examples Input 2 162 Output YES 5 2 4 8 81 162 Input 4 42 Output NO Input 100 40021 Output YES 5 100 200 2001 4002 40021 Tags: brute force, dfs and similar, math Correct Solution: ``` a,b=[int(i) for i in input().split()] res=[b] n=b while n>a: if n%2==0: n=n//2 res.insert(0,n) elif n%10==1: n=(n-1)//10 res.insert(0,n) else: print('NO') break else: if n==a: print('YES') print(len(res)) print(' '.join([str(i) for i in res])) else: print('NO') ```
12,114
Provide tags and a correct Python 3 solution for this coding contest problem. Vasily has a number a, which he wants to turn into a number b. For this purpose, he can do two types of operations: * multiply the current number by 2 (that is, replace the number x by 2Β·x); * append the digit 1 to the right of current number (that is, replace the number x by 10Β·x + 1). You need to help Vasily to transform the number a into the number b using only the operations described above, or find that it is impossible. Note that in this task you are not required to minimize the number of operations. It suffices to find any way to transform a into b. Input The first line contains two positive integers a and b (1 ≀ a < b ≀ 109) β€” the number which Vasily has and the number he wants to have. Output If there is no way to get b from a, print "NO" (without quotes). Otherwise print three lines. On the first line print "YES" (without quotes). The second line should contain single integer k β€” the length of the transformation sequence. On the third line print the sequence of transformations x1, x2, ..., xk, where: * x1 should be equal to a, * xk should be equal to b, * xi should be obtained from xi - 1 using any of two described operations (1 < i ≀ k). If there are multiple answers, print any of them. Examples Input 2 162 Output YES 5 2 4 8 81 162 Input 4 42 Output NO Input 100 40021 Output YES 5 100 200 2001 4002 40021 Tags: brute force, dfs and similar, math Correct Solution: ``` import math a = [] def dfs( pos, need ): x = a[pos] # print( x ) if x > need: return if x == need: print( "YES" ) print( pos + 1 ) print( *a ) # for i in a: # print( i ) exit( 0 ) a.append( x * 2 ) dfs( pos + 1, need ) a.pop() a.append( 10 * x + 1 ) dfs( pos + 1, need ) a.pop() # cin = open( "input.txt", 'r' ) # x, y = map( int, cin.read().split() ) x, y = map( int, input().split() ) a = [ x ] dfs( 0, y ) print( "NO" ) ```
12,115
Provide tags and a correct Python 3 solution for this coding contest problem. Vasily has a number a, which he wants to turn into a number b. For this purpose, he can do two types of operations: * multiply the current number by 2 (that is, replace the number x by 2Β·x); * append the digit 1 to the right of current number (that is, replace the number x by 10Β·x + 1). You need to help Vasily to transform the number a into the number b using only the operations described above, or find that it is impossible. Note that in this task you are not required to minimize the number of operations. It suffices to find any way to transform a into b. Input The first line contains two positive integers a and b (1 ≀ a < b ≀ 109) β€” the number which Vasily has and the number he wants to have. Output If there is no way to get b from a, print "NO" (without quotes). Otherwise print three lines. On the first line print "YES" (without quotes). The second line should contain single integer k β€” the length of the transformation sequence. On the third line print the sequence of transformations x1, x2, ..., xk, where: * x1 should be equal to a, * xk should be equal to b, * xi should be obtained from xi - 1 using any of two described operations (1 < i ≀ k). If there are multiple answers, print any of them. Examples Input 2 162 Output YES 5 2 4 8 81 162 Input 4 42 Output NO Input 100 40021 Output YES 5 100 200 2001 4002 40021 Tags: brute force, dfs and similar, math Correct Solution: ``` a,b=input().split(' ') a=int(a) b=int(b) c=[] d=0 flag=0 c.append(b) while True: if (b-1)%10==0: b=(b-1)/10 d+=1 flag=1 c.append(b) elif b%2==0: b=b/2 flag=1 c.append(b) d+=1 if b<=a or flag==0: break flag=0 #c=c.reverse() #print(c) m=len(c) if b==a: print("YES") print(m) for i in range(m-1,-1,-1): print(int(c[i]),end=' ') else: print("NO") ```
12,116
Provide tags and a correct Python 3 solution for this coding contest problem. Vasily has a number a, which he wants to turn into a number b. For this purpose, he can do two types of operations: * multiply the current number by 2 (that is, replace the number x by 2Β·x); * append the digit 1 to the right of current number (that is, replace the number x by 10Β·x + 1). You need to help Vasily to transform the number a into the number b using only the operations described above, or find that it is impossible. Note that in this task you are not required to minimize the number of operations. It suffices to find any way to transform a into b. Input The first line contains two positive integers a and b (1 ≀ a < b ≀ 109) β€” the number which Vasily has and the number he wants to have. Output If there is no way to get b from a, print "NO" (without quotes). Otherwise print three lines. On the first line print "YES" (without quotes). The second line should contain single integer k β€” the length of the transformation sequence. On the third line print the sequence of transformations x1, x2, ..., xk, where: * x1 should be equal to a, * xk should be equal to b, * xi should be obtained from xi - 1 using any of two described operations (1 < i ≀ k). If there are multiple answers, print any of them. Examples Input 2 162 Output YES 5 2 4 8 81 162 Input 4 42 Output NO Input 100 40021 Output YES 5 100 200 2001 4002 40021 Tags: brute force, dfs and similar, math Correct Solution: ``` a, b = map(int, input().split()) A = [b] while b > a: if b % 2 == 0: b //= 2 elif b % 10 == 1: b //= 10 else: break A.append(b) if A[-1] == a: print('YES') print(len(A)) print(*A[::-1]) else: print('NO') ```
12,117
Provide tags and a correct Python 3 solution for this coding contest problem. Vasily has a number a, which he wants to turn into a number b. For this purpose, he can do two types of operations: * multiply the current number by 2 (that is, replace the number x by 2Β·x); * append the digit 1 to the right of current number (that is, replace the number x by 10Β·x + 1). You need to help Vasily to transform the number a into the number b using only the operations described above, or find that it is impossible. Note that in this task you are not required to minimize the number of operations. It suffices to find any way to transform a into b. Input The first line contains two positive integers a and b (1 ≀ a < b ≀ 109) β€” the number which Vasily has and the number he wants to have. Output If there is no way to get b from a, print "NO" (without quotes). Otherwise print three lines. On the first line print "YES" (without quotes). The second line should contain single integer k β€” the length of the transformation sequence. On the third line print the sequence of transformations x1, x2, ..., xk, where: * x1 should be equal to a, * xk should be equal to b, * xi should be obtained from xi - 1 using any of two described operations (1 < i ≀ k). If there are multiple answers, print any of them. Examples Input 2 162 Output YES 5 2 4 8 81 162 Input 4 42 Output NO Input 100 40021 Output YES 5 100 200 2001 4002 40021 Tags: brute force, dfs and similar, math Correct Solution: ``` f, n = map(int, input().split()) flag = True lst = [n] k = '' while n > f: if n % 10 == 1: k = str(n) n = int(k[:len(k)-1]) if n < f: flag = False break elif n % 2 == 0: n //= 2 if n < f: flag = False break elif n != f: flag = False break lst.append(n) if flag: print('YES') print(len(lst)) print(*reversed(lst)) else: print('NO') ```
12,118
Provide tags and a correct Python 3 solution for this coding contest problem. Vasily has a number a, which he wants to turn into a number b. For this purpose, he can do two types of operations: * multiply the current number by 2 (that is, replace the number x by 2Β·x); * append the digit 1 to the right of current number (that is, replace the number x by 10Β·x + 1). You need to help Vasily to transform the number a into the number b using only the operations described above, or find that it is impossible. Note that in this task you are not required to minimize the number of operations. It suffices to find any way to transform a into b. Input The first line contains two positive integers a and b (1 ≀ a < b ≀ 109) β€” the number which Vasily has and the number he wants to have. Output If there is no way to get b from a, print "NO" (without quotes). Otherwise print three lines. On the first line print "YES" (without quotes). The second line should contain single integer k β€” the length of the transformation sequence. On the third line print the sequence of transformations x1, x2, ..., xk, where: * x1 should be equal to a, * xk should be equal to b, * xi should be obtained from xi - 1 using any of two described operations (1 < i ≀ k). If there are multiple answers, print any of them. Examples Input 2 162 Output YES 5 2 4 8 81 162 Input 4 42 Output NO Input 100 40021 Output YES 5 100 200 2001 4002 40021 Tags: brute force, dfs and similar, math Correct Solution: ``` num_possuido, num_desejado = map(int, input().split()) aux = [] aux.append(num_desejado) while(num_desejado > num_possuido): if (num_desejado % 10) == 1: num_desejado = num_desejado // 10 aux.append(num_desejado) elif (num_desejado % 2 == 0): num_desejado = num_desejado // 2 aux.append(num_desejado) else: print("NO") exit() tamanho_da_seq = len(aux) seq = "" aux.reverse() for i in aux: seq += str(i) + " " if(num_desejado == num_possuido): print("YES") print(tamanho_da_seq) print(seq) else: print("NO") ```
12,119
Provide tags and a correct Python 3 solution for this coding contest problem. Vasily has a number a, which he wants to turn into a number b. For this purpose, he can do two types of operations: * multiply the current number by 2 (that is, replace the number x by 2Β·x); * append the digit 1 to the right of current number (that is, replace the number x by 10Β·x + 1). You need to help Vasily to transform the number a into the number b using only the operations described above, or find that it is impossible. Note that in this task you are not required to minimize the number of operations. It suffices to find any way to transform a into b. Input The first line contains two positive integers a and b (1 ≀ a < b ≀ 109) β€” the number which Vasily has and the number he wants to have. Output If there is no way to get b from a, print "NO" (without quotes). Otherwise print three lines. On the first line print "YES" (without quotes). The second line should contain single integer k β€” the length of the transformation sequence. On the third line print the sequence of transformations x1, x2, ..., xk, where: * x1 should be equal to a, * xk should be equal to b, * xi should be obtained from xi - 1 using any of two described operations (1 < i ≀ k). If there are multiple answers, print any of them. Examples Input 2 162 Output YES 5 2 4 8 81 162 Input 4 42 Output NO Input 100 40021 Output YES 5 100 200 2001 4002 40021 Tags: brute force, dfs and similar, math Correct Solution: ``` n = [] def a_to_b(a, b): if b % 2 == 0: b = b / 2 if b >= a: n.insert(0, int(b)) a_to_b(a, b) elif b % 10 == 1: b = b // 10 if b >= a: n.insert(0, int(b)) a_to_b(a, b) a, b = map(int, input().split()) a_to_b(a, b) n.append(b) if a in n: print("YES") print(len(n)) print(" ".join(map(str, n))) else: print("NO") ```
12,120
Provide tags and a correct Python 3 solution for this coding contest problem. Vasily has a number a, which he wants to turn into a number b. For this purpose, he can do two types of operations: * multiply the current number by 2 (that is, replace the number x by 2Β·x); * append the digit 1 to the right of current number (that is, replace the number x by 10Β·x + 1). You need to help Vasily to transform the number a into the number b using only the operations described above, or find that it is impossible. Note that in this task you are not required to minimize the number of operations. It suffices to find any way to transform a into b. Input The first line contains two positive integers a and b (1 ≀ a < b ≀ 109) β€” the number which Vasily has and the number he wants to have. Output If there is no way to get b from a, print "NO" (without quotes). Otherwise print three lines. On the first line print "YES" (without quotes). The second line should contain single integer k β€” the length of the transformation sequence. On the third line print the sequence of transformations x1, x2, ..., xk, where: * x1 should be equal to a, * xk should be equal to b, * xi should be obtained from xi - 1 using any of two described operations (1 < i ≀ k). If there are multiple answers, print any of them. Examples Input 2 162 Output YES 5 2 4 8 81 162 Input 4 42 Output NO Input 100 40021 Output YES 5 100 200 2001 4002 40021 Tags: brute force, dfs and similar, math Correct Solution: ``` a,b=map(int,input("").split()) oplst=[] oplst.append(b) for i in range(b): if(b%10==1): b=b//10 oplst.append(b) if(b==a): print("YES") print(len(oplst)) oplst.reverse() print(*oplst, sep=" ") break elif(b<a): print("NO") break elif(b%2==0): b=b//2 oplst.append(b) if(b==a): print("YES") print(len(oplst)) oplst.reverse() print(*oplst, sep=" ") break elif(b<a): print("NO") break else: print("NO") break ```
12,121
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasily has a number a, which he wants to turn into a number b. For this purpose, he can do two types of operations: * multiply the current number by 2 (that is, replace the number x by 2Β·x); * append the digit 1 to the right of current number (that is, replace the number x by 10Β·x + 1). You need to help Vasily to transform the number a into the number b using only the operations described above, or find that it is impossible. Note that in this task you are not required to minimize the number of operations. It suffices to find any way to transform a into b. Input The first line contains two positive integers a and b (1 ≀ a < b ≀ 109) β€” the number which Vasily has and the number he wants to have. Output If there is no way to get b from a, print "NO" (without quotes). Otherwise print three lines. On the first line print "YES" (without quotes). The second line should contain single integer k β€” the length of the transformation sequence. On the third line print the sequence of transformations x1, x2, ..., xk, where: * x1 should be equal to a, * xk should be equal to b, * xi should be obtained from xi - 1 using any of two described operations (1 < i ≀ k). If there are multiple answers, print any of them. Examples Input 2 162 Output YES 5 2 4 8 81 162 Input 4 42 Output NO Input 100 40021 Output YES 5 100 200 2001 4002 40021 Submitted Solution: ``` a, b = map(int, input().split()) k = 1 numbers = str(b) while(a < b): if(b % 2 == 0): b = b/2 k+= 1 numbers = str(int(b)) + " " + numbers elif(b % 10 == 1): b = b//10 k+= 1 numbers = str(int(b)) + " " + numbers else: b = 0 if(a == b): print("YES") print(k) print(numbers) else: print("NO") ``` Yes
12,122
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasily has a number a, which he wants to turn into a number b. For this purpose, he can do two types of operations: * multiply the current number by 2 (that is, replace the number x by 2Β·x); * append the digit 1 to the right of current number (that is, replace the number x by 10Β·x + 1). You need to help Vasily to transform the number a into the number b using only the operations described above, or find that it is impossible. Note that in this task you are not required to minimize the number of operations. It suffices to find any way to transform a into b. Input The first line contains two positive integers a and b (1 ≀ a < b ≀ 109) β€” the number which Vasily has and the number he wants to have. Output If there is no way to get b from a, print "NO" (without quotes). Otherwise print three lines. On the first line print "YES" (without quotes). The second line should contain single integer k β€” the length of the transformation sequence. On the third line print the sequence of transformations x1, x2, ..., xk, where: * x1 should be equal to a, * xk should be equal to b, * xi should be obtained from xi - 1 using any of two described operations (1 < i ≀ k). If there are multiple answers, print any of them. Examples Input 2 162 Output YES 5 2 4 8 81 162 Input 4 42 Output NO Input 100 40021 Output YES 5 100 200 2001 4002 40021 Submitted Solution: ``` s = input() s += " " k = 0 a = "" b ="" for i in s: if k == 0: if i != ' ': a += i else: k += 1 elif k == 1: if i != ' ': b += i a1 = [] b1 = [] a1 += a b1 += b a2 = int(a) b2 = int(b) y1 = b y = [] while b2 > a2: if (b2 - 1) % 10 == 0: b2 = int((b2 - 1) / 10) if b2 < a2: print("NO") break y.append(b2) else: if b2 % 2 == 0: b2 = int(b2 / 2) if b2 < a2: print("NO") break y.append(b2) else: print("NO") break if b2 == a2: print("YES") print(len(y) + 1) y = y[::-1] for i in y: print(i, end=" ") print(y1) ``` Yes
12,123
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasily has a number a, which he wants to turn into a number b. For this purpose, he can do two types of operations: * multiply the current number by 2 (that is, replace the number x by 2Β·x); * append the digit 1 to the right of current number (that is, replace the number x by 10Β·x + 1). You need to help Vasily to transform the number a into the number b using only the operations described above, or find that it is impossible. Note that in this task you are not required to minimize the number of operations. It suffices to find any way to transform a into b. Input The first line contains two positive integers a and b (1 ≀ a < b ≀ 109) β€” the number which Vasily has and the number he wants to have. Output If there is no way to get b from a, print "NO" (without quotes). Otherwise print three lines. On the first line print "YES" (without quotes). The second line should contain single integer k β€” the length of the transformation sequence. On the third line print the sequence of transformations x1, x2, ..., xk, where: * x1 should be equal to a, * xk should be equal to b, * xi should be obtained from xi - 1 using any of two described operations (1 < i ≀ k). If there are multiple answers, print any of them. Examples Input 2 162 Output YES 5 2 4 8 81 162 Input 4 42 Output NO Input 100 40021 Output YES 5 100 200 2001 4002 40021 Submitted Solution: ``` a,b = map(int,input().split()) l = [] c = 1 l.append(b) flag = False while(b>1): if b%10 == 1: c += 1 b = b//10 l.append(b) if a == b: flag = True break if a > b: break elif b%2 == 0: c += 1 b = b//2 l.append(b) if a==b: flag = True break if a > b: break else: break if flag: print("YES") l.sort() print(c) for i in l: print(i,end=" ") else: print("NO") ``` Yes
12,124
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasily has a number a, which he wants to turn into a number b. For this purpose, he can do two types of operations: * multiply the current number by 2 (that is, replace the number x by 2Β·x); * append the digit 1 to the right of current number (that is, replace the number x by 10Β·x + 1). You need to help Vasily to transform the number a into the number b using only the operations described above, or find that it is impossible. Note that in this task you are not required to minimize the number of operations. It suffices to find any way to transform a into b. Input The first line contains two positive integers a and b (1 ≀ a < b ≀ 109) β€” the number which Vasily has and the number he wants to have. Output If there is no way to get b from a, print "NO" (without quotes). Otherwise print three lines. On the first line print "YES" (without quotes). The second line should contain single integer k β€” the length of the transformation sequence. On the third line print the sequence of transformations x1, x2, ..., xk, where: * x1 should be equal to a, * xk should be equal to b, * xi should be obtained from xi - 1 using any of two described operations (1 < i ≀ k). If there are multiple answers, print any of them. Examples Input 2 162 Output YES 5 2 4 8 81 162 Input 4 42 Output NO Input 100 40021 Output YES 5 100 200 2001 4002 40021 Submitted Solution: ``` a,b = map(int,input().split()) output = [] num = b n1,n2=0,0 output.append(num) while (num!=a): if (num<a): output.clear() print("NO") exit() elif (num%2==0): num/=2 output.append(int(num)) elif (int(num/10)%2==0): num=int(num-1)/10 output.append(int(num)) else: num=int(num-1)/10 output.append(int(num)) output.reverse() print("YES") print(len(output)) for i in output: print(i, end=" ") ``` Yes
12,125
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasily has a number a, which he wants to turn into a number b. For this purpose, he can do two types of operations: * multiply the current number by 2 (that is, replace the number x by 2Β·x); * append the digit 1 to the right of current number (that is, replace the number x by 10Β·x + 1). You need to help Vasily to transform the number a into the number b using only the operations described above, or find that it is impossible. Note that in this task you are not required to minimize the number of operations. It suffices to find any way to transform a into b. Input The first line contains two positive integers a and b (1 ≀ a < b ≀ 109) β€” the number which Vasily has and the number he wants to have. Output If there is no way to get b from a, print "NO" (without quotes). Otherwise print three lines. On the first line print "YES" (without quotes). The second line should contain single integer k β€” the length of the transformation sequence. On the third line print the sequence of transformations x1, x2, ..., xk, where: * x1 should be equal to a, * xk should be equal to b, * xi should be obtained from xi - 1 using any of two described operations (1 < i ≀ k). If there are multiple answers, print any of them. Examples Input 2 162 Output YES 5 2 4 8 81 162 Input 4 42 Output NO Input 100 40021 Output YES 5 100 200 2001 4002 40021 Submitted Solution: ``` a,b=map(int,input().split()) l=[] l.append(b) c=1 while b>a: if b%2==0: b=b//2 l.append(b) c=c+1 else: b=(b-1)//10 l.append(b) c=c+1 m=len(l) x='' for i in range (m-1,0,-1): if i==0: x=x+str(l[i]) else: x=x+str(l[i])+' ' if a==b: print('YES') print(c) print(x) else: print('NO') ``` No
12,126
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasily has a number a, which he wants to turn into a number b. For this purpose, he can do two types of operations: * multiply the current number by 2 (that is, replace the number x by 2Β·x); * append the digit 1 to the right of current number (that is, replace the number x by 10Β·x + 1). You need to help Vasily to transform the number a into the number b using only the operations described above, or find that it is impossible. Note that in this task you are not required to minimize the number of operations. It suffices to find any way to transform a into b. Input The first line contains two positive integers a and b (1 ≀ a < b ≀ 109) β€” the number which Vasily has and the number he wants to have. Output If there is no way to get b from a, print "NO" (without quotes). Otherwise print three lines. On the first line print "YES" (without quotes). The second line should contain single integer k β€” the length of the transformation sequence. On the third line print the sequence of transformations x1, x2, ..., xk, where: * x1 should be equal to a, * xk should be equal to b, * xi should be obtained from xi - 1 using any of two described operations (1 < i ≀ k). If there are multiple answers, print any of them. Examples Input 2 162 Output YES 5 2 4 8 81 162 Input 4 42 Output NO Input 100 40021 Output YES 5 100 200 2001 4002 40021 Submitted Solution: ``` a,b=map(int,input().split()) r=[b] while b>a: if b%10==1: b=b//10 r.append(b) else: b=b//2 r.append(b) if r[len(r)-1]==a: print('YES') print(len(r)) print(*r[::-1]) else: print('NO') ``` No
12,127
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasily has a number a, which he wants to turn into a number b. For this purpose, he can do two types of operations: * multiply the current number by 2 (that is, replace the number x by 2Β·x); * append the digit 1 to the right of current number (that is, replace the number x by 10Β·x + 1). You need to help Vasily to transform the number a into the number b using only the operations described above, or find that it is impossible. Note that in this task you are not required to minimize the number of operations. It suffices to find any way to transform a into b. Input The first line contains two positive integers a and b (1 ≀ a < b ≀ 109) β€” the number which Vasily has and the number he wants to have. Output If there is no way to get b from a, print "NO" (without quotes). Otherwise print three lines. On the first line print "YES" (without quotes). The second line should contain single integer k β€” the length of the transformation sequence. On the third line print the sequence of transformations x1, x2, ..., xk, where: * x1 should be equal to a, * xk should be equal to b, * xi should be obtained from xi - 1 using any of two described operations (1 < i ≀ k). If there are multiple answers, print any of them. Examples Input 2 162 Output YES 5 2 4 8 81 162 Input 4 42 Output NO Input 100 40021 Output YES 5 100 200 2001 4002 40021 Submitted Solution: ``` # @author Nayara Souza # UFCG - Universidade Federal de Campina Grande # AA - Basico a, b = list(map(int, input().split())) l = [] l.append(b) while b > a: if b%2 == 0: b = b // 2 l.append(b) else: b = (b-1)//10 l.append(b) if l[-1] == a: print('YES') l = list(map(str,l)) l.sort() print(len(l)) print(' '.join(l)) else: print('NO') ``` No
12,128
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasily has a number a, which he wants to turn into a number b. For this purpose, he can do two types of operations: * multiply the current number by 2 (that is, replace the number x by 2Β·x); * append the digit 1 to the right of current number (that is, replace the number x by 10Β·x + 1). You need to help Vasily to transform the number a into the number b using only the operations described above, or find that it is impossible. Note that in this task you are not required to minimize the number of operations. It suffices to find any way to transform a into b. Input The first line contains two positive integers a and b (1 ≀ a < b ≀ 109) β€” the number which Vasily has and the number he wants to have. Output If there is no way to get b from a, print "NO" (without quotes). Otherwise print three lines. On the first line print "YES" (without quotes). The second line should contain single integer k β€” the length of the transformation sequence. On the third line print the sequence of transformations x1, x2, ..., xk, where: * x1 should be equal to a, * xk should be equal to b, * xi should be obtained from xi - 1 using any of two described operations (1 < i ≀ k). If there are multiple answers, print any of them. Examples Input 2 162 Output YES 5 2 4 8 81 162 Input 4 42 Output NO Input 100 40021 Output YES 5 100 200 2001 4002 40021 Submitted Solution: ``` def recursion(val_desejado, val_atual , soma , history, final_history): my_history = history.copy() my_history.append(val_atual) sum_ = val_atual if(val_atual == val_desejado): final_history.append(my_history) return if(val_atual > val_desejado): return if(soma == 1): sum_ = sum_ * 10 + 1 else: sum_ = sum_ * 2 recursion(val_desejado , sum_ , 1 , my_history , final_history) recursion(val_desejado , sum_ , 2 , my_history , final_history) inp= input() values = inp.split(" ") atual = int(values[0]) desejado = int(values[1]) history = [] recursion(desejado, atual ,1,[], history) recursion(desejado, atual ,2,[], history) if(history == []): print("NO") else: exit = "" size = len(history[0]) print(size) for i in range(size): if( i == size - 1): exit += str(history[0][i]) else: exit += str(history[0][i]) + " " print(exit) ``` No
12,129
Provide tags and a correct Python 3 solution for this coding contest problem. Santa Claus decided to disassemble his keyboard to clean it. After he returned all the keys back, he suddenly realized that some pairs of keys took each other's place! That is, Santa suspects that each key is either on its place, or on the place of another key, which is located exactly where the first key should be. In order to make sure that he's right and restore the correct order of keys, Santa typed his favorite patter looking only to his keyboard. You are given the Santa's favorite patter and the string he actually typed. Determine which pairs of keys could be mixed. Each key must occur in pairs at most once. Input The input consists of only two strings s and t denoting the favorite Santa's patter and the resulting string. s and t are not empty and have the same length, which is at most 1000. Both strings consist only of lowercase English letters. Output If Santa is wrong, and there is no way to divide some of keys into pairs and swap keys in each pair so that the keyboard will be fixed, print Β«-1Β» (without quotes). Otherwise, the first line of output should contain the only integer k (k β‰₯ 0) β€” the number of pairs of keys that should be swapped. The following k lines should contain two space-separated letters each, denoting the keys which should be swapped. All printed letters must be distinct. If there are several possible answers, print any of them. You are free to choose the order of the pairs and the order of keys in a pair. Each letter must occur at most once. Santa considers the keyboard to be fixed if he can print his favorite patter without mistakes. Examples Input helloworld ehoolwlroz Output 3 h e l o d z Input hastalavistababy hastalavistababy Output 0 Input merrychristmas christmasmerry Output -1 Tags: implementation, strings Correct Solution: ``` import sys s=input() t=input() st1=[] st2=[] checked=[] right=[] for i in range(len(s)): a=[] if s[i] in checked and t[i] not in checked: print('-1') sys.exit() if s[i] not in checked and t[i] in checked: print('-1') sys.exit() if s[i] in right and t[i] not in right: print('-1') sys.exit() if s[i] not in right and t[i] in right: print('-1') sys.exit() if s[i] not in st1 and s[i] not in st2: if t[i] not in st1 and t[i] not in st2: if s[i]!=t[i]: checked.append(s[i]) checked.append(t[i]) st1.append(s[i]) st2.append(t[i]) else: right.append(s[i]) right.append(t[i]) if s[i]==t[i]: if s[i] in st1 or s[i] in st2: print('-1') sys.exit() elif t[i] in st1 or t[i] in st2: print('-1') sys.exit() if s[i] in st2: if st1[st2.index(s[i])]!=t[i]: print('-1') sys.exit() if s[i] in right or t[i] in right: if s[i]!=t[i]: print('-1') sys.exit() print(len(st1)) for i in range(len(st1)): print(st1[i],end=" ") print(st2[i]) ```
12,130
Provide tags and a correct Python 3 solution for this coding contest problem. Santa Claus decided to disassemble his keyboard to clean it. After he returned all the keys back, he suddenly realized that some pairs of keys took each other's place! That is, Santa suspects that each key is either on its place, or on the place of another key, which is located exactly where the first key should be. In order to make sure that he's right and restore the correct order of keys, Santa typed his favorite patter looking only to his keyboard. You are given the Santa's favorite patter and the string he actually typed. Determine which pairs of keys could be mixed. Each key must occur in pairs at most once. Input The input consists of only two strings s and t denoting the favorite Santa's patter and the resulting string. s and t are not empty and have the same length, which is at most 1000. Both strings consist only of lowercase English letters. Output If Santa is wrong, and there is no way to divide some of keys into pairs and swap keys in each pair so that the keyboard will be fixed, print Β«-1Β» (without quotes). Otherwise, the first line of output should contain the only integer k (k β‰₯ 0) β€” the number of pairs of keys that should be swapped. The following k lines should contain two space-separated letters each, denoting the keys which should be swapped. All printed letters must be distinct. If there are several possible answers, print any of them. You are free to choose the order of the pairs and the order of keys in a pair. Each letter must occur at most once. Santa considers the keyboard to be fixed if he can print his favorite patter without mistakes. Examples Input helloworld ehoolwlroz Output 3 h e l o d z Input hastalavistababy hastalavistababy Output 0 Input merrychristmas christmasmerry Output -1 Tags: implementation, strings Correct Solution: ``` a = input() b = input() alph = [0] * 26 for i in range(26): alph[i] = chr(97 + i) ans = [] for i in range(len(a)): if a[i] != alph[ord(b[i]) - 97]: if (alph[ord(a[i]) - 97] == a[i]) and (alph[ord(b[i]) - 97] == b[i]): alph[ord(a[i]) - 97] = b[i] alph[ord(b[i]) - 97] = a[i] ans.append((a[i], b[i])) else: print(-1) exit() for i in range(len(b)): if a[i] != alph[ord(b[i]) - 97]: print(-1) exit() print(len(ans)) for i in range(len(ans)): print(ans[i][0], ans[i][1]) ```
12,131
Provide tags and a correct Python 3 solution for this coding contest problem. Santa Claus decided to disassemble his keyboard to clean it. After he returned all the keys back, he suddenly realized that some pairs of keys took each other's place! That is, Santa suspects that each key is either on its place, or on the place of another key, which is located exactly where the first key should be. In order to make sure that he's right and restore the correct order of keys, Santa typed his favorite patter looking only to his keyboard. You are given the Santa's favorite patter and the string he actually typed. Determine which pairs of keys could be mixed. Each key must occur in pairs at most once. Input The input consists of only two strings s and t denoting the favorite Santa's patter and the resulting string. s and t are not empty and have the same length, which is at most 1000. Both strings consist only of lowercase English letters. Output If Santa is wrong, and there is no way to divide some of keys into pairs and swap keys in each pair so that the keyboard will be fixed, print Β«-1Β» (without quotes). Otherwise, the first line of output should contain the only integer k (k β‰₯ 0) β€” the number of pairs of keys that should be swapped. The following k lines should contain two space-separated letters each, denoting the keys which should be swapped. All printed letters must be distinct. If there are several possible answers, print any of them. You are free to choose the order of the pairs and the order of keys in a pair. Each letter must occur at most once. Santa considers the keyboard to be fixed if he can print his favorite patter without mistakes. Examples Input helloworld ehoolwlroz Output 3 h e l o d z Input hastalavistababy hastalavistababy Output 0 Input merrychristmas christmasmerry Output -1 Tags: implementation, strings Correct Solution: ``` def areBidirectionalDictionaries(s1, s2): d = {} other = {} perfect = set() for i, j in zip(s1, s2): if i != j: if (i in d and d[i] != j) or (j in d and d[j] != i) or (i in perfect) or (j in perfect): print('-1') return if j not in d: d[i] = j other[j] = i elif (i in d) or (i in other): print('-1') return else: perfect.add(i) if len(d) != len(set(d.values())): print('-1') return print(len(d)) for i, j in d.items(): print(str(i)+' '+str(j)) s1 = input() s2 = input() areBidirectionalDictionaries(s1, s2) ```
12,132
Provide tags and a correct Python 3 solution for this coding contest problem. Santa Claus decided to disassemble his keyboard to clean it. After he returned all the keys back, he suddenly realized that some pairs of keys took each other's place! That is, Santa suspects that each key is either on its place, or on the place of another key, which is located exactly where the first key should be. In order to make sure that he's right and restore the correct order of keys, Santa typed his favorite patter looking only to his keyboard. You are given the Santa's favorite patter and the string he actually typed. Determine which pairs of keys could be mixed. Each key must occur in pairs at most once. Input The input consists of only two strings s and t denoting the favorite Santa's patter and the resulting string. s and t are not empty and have the same length, which is at most 1000. Both strings consist only of lowercase English letters. Output If Santa is wrong, and there is no way to divide some of keys into pairs and swap keys in each pair so that the keyboard will be fixed, print Β«-1Β» (without quotes). Otherwise, the first line of output should contain the only integer k (k β‰₯ 0) β€” the number of pairs of keys that should be swapped. The following k lines should contain two space-separated letters each, denoting the keys which should be swapped. All printed letters must be distinct. If there are several possible answers, print any of them. You are free to choose the order of the pairs and the order of keys in a pair. Each letter must occur at most once. Santa considers the keyboard to be fixed if he can print his favorite patter without mistakes. Examples Input helloworld ehoolwlroz Output 3 h e l o d z Input hastalavistababy hastalavistababy Output 0 Input merrychristmas christmasmerry Output -1 Tags: implementation, strings Correct Solution: ``` s = input() t = input() used, ln = set(), 0 for i in range(len(s)): if (s[i] != t[i]) and (not (tuple(sorted([s[i], t[i]])) in used)): ln += 1 used.add(tuple(sorted([s[i], t[i]]))) a = [i[0] for i in used] b = [i[1] for i in used] if s=='bd' and t=='cc': print(-1) elif len(a) != len(set(a)) or len(b) != len(set(b)): print(-1) elif len(a) == 0: print(0) else: print(ln) for i in used: if i[0] != i[1]: print(i[0], i[1]) ```
12,133
Provide tags and a correct Python 3 solution for this coding contest problem. Santa Claus decided to disassemble his keyboard to clean it. After he returned all the keys back, he suddenly realized that some pairs of keys took each other's place! That is, Santa suspects that each key is either on its place, or on the place of another key, which is located exactly where the first key should be. In order to make sure that he's right and restore the correct order of keys, Santa typed his favorite patter looking only to his keyboard. You are given the Santa's favorite patter and the string he actually typed. Determine which pairs of keys could be mixed. Each key must occur in pairs at most once. Input The input consists of only two strings s and t denoting the favorite Santa's patter and the resulting string. s and t are not empty and have the same length, which is at most 1000. Both strings consist only of lowercase English letters. Output If Santa is wrong, and there is no way to divide some of keys into pairs and swap keys in each pair so that the keyboard will be fixed, print Β«-1Β» (without quotes). Otherwise, the first line of output should contain the only integer k (k β‰₯ 0) β€” the number of pairs of keys that should be swapped. The following k lines should contain two space-separated letters each, denoting the keys which should be swapped. All printed letters must be distinct. If there are several possible answers, print any of them. You are free to choose the order of the pairs and the order of keys in a pair. Each letter must occur at most once. Santa considers the keyboard to be fixed if he can print his favorite patter without mistakes. Examples Input helloworld ehoolwlroz Output 3 h e l o d z Input hastalavistababy hastalavistababy Output 0 Input merrychristmas christmasmerry Output -1 Tags: implementation, strings Correct Solution: ``` def ic(s,t): """is correct""" d=dict() for c in range(len(s)): if t[c] not in d or t[c] in d and s[c] not in d[t[c]]: d.setdefault(s[c],set()).add(t[c]) d.setdefault(t[c],set()).add(s[c]) #print(d) for k,v in d.items(): if len(v)>1: return 0,None d[k]=list(v)[0] #print(d) k,v=list(d.keys()),list(d.values()) #print(k,v) sk,sv=set(k),set(v) b=len(k)==len(sk) and len(v)==len(sv) rv=set() for k,v in d.items(): if k!=v and (v,k) not in rv: rv.add((k,v)) return b,list(rv) s,t=[input() for c in range(2)] b,v=ic(s,t) if b: print(len(v)) for c in v: print(c[0],c[1]) else: print(-1) ```
12,134
Provide tags and a correct Python 3 solution for this coding contest problem. Santa Claus decided to disassemble his keyboard to clean it. After he returned all the keys back, he suddenly realized that some pairs of keys took each other's place! That is, Santa suspects that each key is either on its place, or on the place of another key, which is located exactly where the first key should be. In order to make sure that he's right and restore the correct order of keys, Santa typed his favorite patter looking only to his keyboard. You are given the Santa's favorite patter and the string he actually typed. Determine which pairs of keys could be mixed. Each key must occur in pairs at most once. Input The input consists of only two strings s and t denoting the favorite Santa's patter and the resulting string. s and t are not empty and have the same length, which is at most 1000. Both strings consist only of lowercase English letters. Output If Santa is wrong, and there is no way to divide some of keys into pairs and swap keys in each pair so that the keyboard will be fixed, print Β«-1Β» (without quotes). Otherwise, the first line of output should contain the only integer k (k β‰₯ 0) β€” the number of pairs of keys that should be swapped. The following k lines should contain two space-separated letters each, denoting the keys which should be swapped. All printed letters must be distinct. If there are several possible answers, print any of them. You are free to choose the order of the pairs and the order of keys in a pair. Each letter must occur at most once. Santa considers the keyboard to be fixed if he can print his favorite patter without mistakes. Examples Input helloworld ehoolwlroz Output 3 h e l o d z Input hastalavistababy hastalavistababy Output 0 Input merrychristmas christmasmerry Output -1 Tags: implementation, strings Correct Solution: ``` def main(): d = {} for a, b in zip(input(), input()): if d.get(a, b) != b or d.get(b, a) != a: print(-1) return d[a], d[b] = b, a res = ['%s %s' % (a, b) for a, b in d.items() if a < b] print(len(res)) print('\n'.join(res)) if __name__ == '__main__': main() ```
12,135
Provide tags and a correct Python 3 solution for this coding contest problem. Santa Claus decided to disassemble his keyboard to clean it. After he returned all the keys back, he suddenly realized that some pairs of keys took each other's place! That is, Santa suspects that each key is either on its place, or on the place of another key, which is located exactly where the first key should be. In order to make sure that he's right and restore the correct order of keys, Santa typed his favorite patter looking only to his keyboard. You are given the Santa's favorite patter and the string he actually typed. Determine which pairs of keys could be mixed. Each key must occur in pairs at most once. Input The input consists of only two strings s and t denoting the favorite Santa's patter and the resulting string. s and t are not empty and have the same length, which is at most 1000. Both strings consist only of lowercase English letters. Output If Santa is wrong, and there is no way to divide some of keys into pairs and swap keys in each pair so that the keyboard will be fixed, print Β«-1Β» (without quotes). Otherwise, the first line of output should contain the only integer k (k β‰₯ 0) β€” the number of pairs of keys that should be swapped. The following k lines should contain two space-separated letters each, denoting the keys which should be swapped. All printed letters must be distinct. If there are several possible answers, print any of them. You are free to choose the order of the pairs and the order of keys in a pair. Each letter must occur at most once. Santa considers the keyboard to be fixed if he can print his favorite patter without mistakes. Examples Input helloworld ehoolwlroz Output 3 h e l o d z Input hastalavistababy hastalavistababy Output 0 Input merrychristmas christmasmerry Output -1 Tags: implementation, strings Correct Solution: ``` s1 = input() s2 = input() a = [[0]*2 for i in range(29)] used = [-1]*29 good = 1 c = 0 for i in range(len(s2)): if good == 0: break if s2[i] != s1[i]: if used[ord(s2[i])- ord('a')] == -1 and used[ord(s1[i])- ord('a')] == -1: used[ord(s2[i])- ord('a')] = ord(s1[i])- ord('a') used[ord(s1[i])- ord('a')] = ord(s2[i])- ord('a') a[c][0] = s1[i] a[c][1] = s2[i] c += 1 elif used[ord(s2[i])- ord('a')] != ord(s1[i])- ord('a'): good = 0 else: if used[ord(s2[i])- ord('a')] == -1 : used[ord(s2[i])- ord('a')] = ord(s1[i])- ord('a') elif used[ord(s2[i])- ord('a')] != ord(s1[i])- ord('a'): good = 0 if good == 0: print(-1) else: print (c) for i in range(c): print(a[i][0], a[i][1]) ```
12,136
Provide tags and a correct Python 3 solution for this coding contest problem. Santa Claus decided to disassemble his keyboard to clean it. After he returned all the keys back, he suddenly realized that some pairs of keys took each other's place! That is, Santa suspects that each key is either on its place, or on the place of another key, which is located exactly where the first key should be. In order to make sure that he's right and restore the correct order of keys, Santa typed his favorite patter looking only to his keyboard. You are given the Santa's favorite patter and the string he actually typed. Determine which pairs of keys could be mixed. Each key must occur in pairs at most once. Input The input consists of only two strings s and t denoting the favorite Santa's patter and the resulting string. s and t are not empty and have the same length, which is at most 1000. Both strings consist only of lowercase English letters. Output If Santa is wrong, and there is no way to divide some of keys into pairs and swap keys in each pair so that the keyboard will be fixed, print Β«-1Β» (without quotes). Otherwise, the first line of output should contain the only integer k (k β‰₯ 0) β€” the number of pairs of keys that should be swapped. The following k lines should contain two space-separated letters each, denoting the keys which should be swapped. All printed letters must be distinct. If there are several possible answers, print any of them. You are free to choose the order of the pairs and the order of keys in a pair. Each letter must occur at most once. Santa considers the keyboard to be fixed if he can print his favorite patter without mistakes. Examples Input helloworld ehoolwlroz Output 3 h e l o d z Input hastalavistababy hastalavistababy Output 0 Input merrychristmas christmasmerry Output -1 Tags: implementation, strings Correct Solution: ``` before = input() after = input() length = len(before) stock = {} answer = [] for l in range(length): if before[l] != after[l] and stock.get(after[l],0) == 0 and stock.get(before[l],0) == 0: answer.append(str(before[l]) + " " + str(after[l])) stock[before[l]] = after[l] stock[after[l]] = before[l] elif before[l] != after[l] and (stock.get(after[l],0) == -1 or stock.get(before[l],0) == -1): print(-1) break elif before[l] != after[l]: if stock.get(before[l],0) != after[l] or stock.get(after[l],0) != before[l]: print(-1) break else: continue elif before[l] == after[l] and (stock.get(after[l],0) not in (0,-1) or stock.get(before[l],0) not in (0,-1)): print(-1) break else: stock[before[l]] = -1 stock[after[l]] = -1 else: anslen = len(answer) print(anslen) for a in answer: print(a) ```
12,137
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Santa Claus decided to disassemble his keyboard to clean it. After he returned all the keys back, he suddenly realized that some pairs of keys took each other's place! That is, Santa suspects that each key is either on its place, or on the place of another key, which is located exactly where the first key should be. In order to make sure that he's right and restore the correct order of keys, Santa typed his favorite patter looking only to his keyboard. You are given the Santa's favorite patter and the string he actually typed. Determine which pairs of keys could be mixed. Each key must occur in pairs at most once. Input The input consists of only two strings s and t denoting the favorite Santa's patter and the resulting string. s and t are not empty and have the same length, which is at most 1000. Both strings consist only of lowercase English letters. Output If Santa is wrong, and there is no way to divide some of keys into pairs and swap keys in each pair so that the keyboard will be fixed, print Β«-1Β» (without quotes). Otherwise, the first line of output should contain the only integer k (k β‰₯ 0) β€” the number of pairs of keys that should be swapped. The following k lines should contain two space-separated letters each, denoting the keys which should be swapped. All printed letters must be distinct. If there are several possible answers, print any of them. You are free to choose the order of the pairs and the order of keys in a pair. Each letter must occur at most once. Santa considers the keyboard to be fixed if he can print his favorite patter without mistakes. Examples Input helloworld ehoolwlroz Output 3 h e l o d z Input hastalavistababy hastalavistababy Output 0 Input merrychristmas christmasmerry Output -1 Submitted Solution: ``` s = input() s1 = input() mistake = False d = dict() ans = [] for i in range(len(s)): if s[i] != s1[i]: if s[i] in d and s1[i] in d: if not (d[s[i]] == s1[i] and d[s1[i]] == s[i]): mistake = True break elif s[i] in d or s1[i] in d: mistake = True break else: d[s[i]] = s1[i] d[s1[i]] = s[i] else: if s[i] in d and d[s[i]] != s[i]: mistake = True break d[s[i]] = s[i] if mistake: print(-1) else: ans = [] last = set() for elem in d: if elem not in last and elem != d[elem]: ans.append([elem, d[elem]]) last.add(elem) last.add(d[elem]) print(len(ans)) for elem in ans: print(*elem) ``` Yes
12,138
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Santa Claus decided to disassemble his keyboard to clean it. After he returned all the keys back, he suddenly realized that some pairs of keys took each other's place! That is, Santa suspects that each key is either on its place, or on the place of another key, which is located exactly where the first key should be. In order to make sure that he's right and restore the correct order of keys, Santa typed his favorite patter looking only to his keyboard. You are given the Santa's favorite patter and the string he actually typed. Determine which pairs of keys could be mixed. Each key must occur in pairs at most once. Input The input consists of only two strings s and t denoting the favorite Santa's patter and the resulting string. s and t are not empty and have the same length, which is at most 1000. Both strings consist only of lowercase English letters. Output If Santa is wrong, and there is no way to divide some of keys into pairs and swap keys in each pair so that the keyboard will be fixed, print Β«-1Β» (without quotes). Otherwise, the first line of output should contain the only integer k (k β‰₯ 0) β€” the number of pairs of keys that should be swapped. The following k lines should contain two space-separated letters each, denoting the keys which should be swapped. All printed letters must be distinct. If there are several possible answers, print any of them. You are free to choose the order of the pairs and the order of keys in a pair. Each letter must occur at most once. Santa considers the keyboard to be fixed if he can print his favorite patter without mistakes. Examples Input helloworld ehoolwlroz Output 3 h e l o d z Input hastalavistababy hastalavistababy Output 0 Input merrychristmas christmasmerry Output -1 Submitted Solution: ``` s1, s2 = input(), input() n = len(s1) d = dict() for i in range(n): a = s1[i] b = s2[i] if b < a: a, b = b, a if a not in d and b not in d: d[a] = b d[b] = a elif ((a in d and b not in d) or (a not in d and b in d)): print(-1) break elif d[a] != b or d[b] != a: print(-1) break else: k = [] for i in d.items(): if i[0] < i[1]: k.append('{} {}'.format(i[0], i[1])) print(len(k)) print(*k, sep='\n') ``` Yes
12,139
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Santa Claus decided to disassemble his keyboard to clean it. After he returned all the keys back, he suddenly realized that some pairs of keys took each other's place! That is, Santa suspects that each key is either on its place, or on the place of another key, which is located exactly where the first key should be. In order to make sure that he's right and restore the correct order of keys, Santa typed his favorite patter looking only to his keyboard. You are given the Santa's favorite patter and the string he actually typed. Determine which pairs of keys could be mixed. Each key must occur in pairs at most once. Input The input consists of only two strings s and t denoting the favorite Santa's patter and the resulting string. s and t are not empty and have the same length, which is at most 1000. Both strings consist only of lowercase English letters. Output If Santa is wrong, and there is no way to divide some of keys into pairs and swap keys in each pair so that the keyboard will be fixed, print Β«-1Β» (without quotes). Otherwise, the first line of output should contain the only integer k (k β‰₯ 0) β€” the number of pairs of keys that should be swapped. The following k lines should contain two space-separated letters each, denoting the keys which should be swapped. All printed letters must be distinct. If there are several possible answers, print any of them. You are free to choose the order of the pairs and the order of keys in a pair. Each letter must occur at most once. Santa considers the keyboard to be fixed if he can print his favorite patter without mistakes. Examples Input helloworld ehoolwlroz Output 3 h e l o d z Input hastalavistababy hastalavistababy Output 0 Input merrychristmas christmasmerry Output -1 Submitted Solution: ``` s = input() s1 = input() q = set() z =set() flag=0 for i in range(len(s)): if(s[i]!=s1[i] and (s1[i],s[i]) not in q and (s[i],s1[i])not in q): q.add((s[i],s1[i])) if(s[i] in z or s1[i] in z): flag = 1 z.add(s[i]) z.add(s1[i]) for i in range(len(s)): if(s[i]==s1[i] and s[i] in z): flag = 1 if(flag): print(-1) else: print(len(q)) for item in q: print(item[0],item[1]) ``` Yes
12,140
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Santa Claus decided to disassemble his keyboard to clean it. After he returned all the keys back, he suddenly realized that some pairs of keys took each other's place! That is, Santa suspects that each key is either on its place, or on the place of another key, which is located exactly where the first key should be. In order to make sure that he's right and restore the correct order of keys, Santa typed his favorite patter looking only to his keyboard. You are given the Santa's favorite patter and the string he actually typed. Determine which pairs of keys could be mixed. Each key must occur in pairs at most once. Input The input consists of only two strings s and t denoting the favorite Santa's patter and the resulting string. s and t are not empty and have the same length, which is at most 1000. Both strings consist only of lowercase English letters. Output If Santa is wrong, and there is no way to divide some of keys into pairs and swap keys in each pair so that the keyboard will be fixed, print Β«-1Β» (without quotes). Otherwise, the first line of output should contain the only integer k (k β‰₯ 0) β€” the number of pairs of keys that should be swapped. The following k lines should contain two space-separated letters each, denoting the keys which should be swapped. All printed letters must be distinct. If there are several possible answers, print any of them. You are free to choose the order of the pairs and the order of keys in a pair. Each letter must occur at most once. Santa considers the keyboard to be fixed if he can print his favorite patter without mistakes. Examples Input helloworld ehoolwlroz Output 3 h e l o d z Input hastalavistababy hastalavistababy Output 0 Input merrychristmas christmasmerry Output -1 Submitted Solution: ``` #k=int(input()) #n,m=map(int,input().split()) #a=list(map(int,input().split())) #b=list(map(int,input().split())) a=input() b=input() l=len(a) d=set() d2=set() deg=dict() for i in range(l): if a[i]==b[i]: d2.add(a[i]) deg[a[i]]=0 deg[b[i]]=0 for i in range(l): if a[i]!=b[i]: if a[i] in d2 or b[i] in d2: print("-1") quit() if (not (a[i],b[i]) in d and not (b[i],a[i]) in d): d.add((a[i],b[i])) deg[a[i]]+=1 deg[b[i]]+=1 for i in d: if deg[i[0]]>1 or deg[i[1]]>1: print("-1") quit() print(len(d)) for i in d: print(str(i[0]),'',str(i[1])) ``` Yes
12,141
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Santa Claus decided to disassemble his keyboard to clean it. After he returned all the keys back, he suddenly realized that some pairs of keys took each other's place! That is, Santa suspects that each key is either on its place, or on the place of another key, which is located exactly where the first key should be. In order to make sure that he's right and restore the correct order of keys, Santa typed his favorite patter looking only to his keyboard. You are given the Santa's favorite patter and the string he actually typed. Determine which pairs of keys could be mixed. Each key must occur in pairs at most once. Input The input consists of only two strings s and t denoting the favorite Santa's patter and the resulting string. s and t are not empty and have the same length, which is at most 1000. Both strings consist only of lowercase English letters. Output If Santa is wrong, and there is no way to divide some of keys into pairs and swap keys in each pair so that the keyboard will be fixed, print Β«-1Β» (without quotes). Otherwise, the first line of output should contain the only integer k (k β‰₯ 0) β€” the number of pairs of keys that should be swapped. The following k lines should contain two space-separated letters each, denoting the keys which should be swapped. All printed letters must be distinct. If there are several possible answers, print any of them. You are free to choose the order of the pairs and the order of keys in a pair. Each letter must occur at most once. Santa considers the keyboard to be fixed if he can print his favorite patter without mistakes. Examples Input helloworld ehoolwlroz Output 3 h e l o d z Input hastalavistababy hastalavistababy Output 0 Input merrychristmas christmasmerry Output -1 Submitted Solution: ``` s=input() t=input() num=len(s) pair1=[0]*num for i in range (num): pair1[i]=[0]*2 def check(s,t,num): arr=[0]*256 count=0 countie=0 for i in range (num): if s[i] !=t[i]: if (arr[ord(s[i])]>0 or arr[ord(t[i])]>0): yy=0 for k in range (num): if (pair1[k][0]==s[i] and pair1[k][1]==t[i]) or (pair1[k][1]==s[i] and pair1[k][0]==t[i]): yy+=1 if yy==0: return (-1) else: arr[ord(s[i])]+=1 arr[ord(t[i])]+=1 pair1[count][0]=s[i] pair1[count][1]=t[i] count+=1 countie+=1 return (countie) ans=check(s,t,num) if ans==-1 or ans==0: print (ans) else: print (ans) for p in range (ans): print (pair1[p][0],pair1[p][1]) ``` No
12,142
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Santa Claus decided to disassemble his keyboard to clean it. After he returned all the keys back, he suddenly realized that some pairs of keys took each other's place! That is, Santa suspects that each key is either on its place, or on the place of another key, which is located exactly where the first key should be. In order to make sure that he's right and restore the correct order of keys, Santa typed his favorite patter looking only to his keyboard. You are given the Santa's favorite patter and the string he actually typed. Determine which pairs of keys could be mixed. Each key must occur in pairs at most once. Input The input consists of only two strings s and t denoting the favorite Santa's patter and the resulting string. s and t are not empty and have the same length, which is at most 1000. Both strings consist only of lowercase English letters. Output If Santa is wrong, and there is no way to divide some of keys into pairs and swap keys in each pair so that the keyboard will be fixed, print Β«-1Β» (without quotes). Otherwise, the first line of output should contain the only integer k (k β‰₯ 0) β€” the number of pairs of keys that should be swapped. The following k lines should contain two space-separated letters each, denoting the keys which should be swapped. All printed letters must be distinct. If there are several possible answers, print any of them. You are free to choose the order of the pairs and the order of keys in a pair. Each letter must occur at most once. Santa considers the keyboard to be fixed if he can print his favorite patter without mistakes. Examples Input helloworld ehoolwlroz Output 3 h e l o d z Input hastalavistababy hastalavistababy Output 0 Input merrychristmas christmasmerry Output -1 Submitted Solution: ``` s = input() t = input() d = {} p = {} end = 0 for i in range(len(s)) : if s[i] != t[i] : if (s[i] in p ) or (t[i] in p) : end = 1 else : test = 0 for el in d : if (el[0] == s[i] and el[1] == t[i]) or (el[1] == s[i] and el[0] == t[i]) : test = 1 break elif (s[i] == el[0] and t[i] != el[1]) or (t[i] == el[1] and s[i] != el[0]) or( (s[i] == el[1] and t[i] != el[0])) or (t[i] == el[0] and s[i] != el[1]) : test = 1 end = 1 break if test == 0 : d[(s[i],t[i])] = 1 else : p[s[i]] = 1 if end == 1 : break if end == 1 : print(-1) else : print(len(d)) for el in d : print(*[el[0],el[1]],sep =' ') ``` No
12,143
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Santa Claus decided to disassemble his keyboard to clean it. After he returned all the keys back, he suddenly realized that some pairs of keys took each other's place! That is, Santa suspects that each key is either on its place, or on the place of another key, which is located exactly where the first key should be. In order to make sure that he's right and restore the correct order of keys, Santa typed his favorite patter looking only to his keyboard. You are given the Santa's favorite patter and the string he actually typed. Determine which pairs of keys could be mixed. Each key must occur in pairs at most once. Input The input consists of only two strings s and t denoting the favorite Santa's patter and the resulting string. s and t are not empty and have the same length, which is at most 1000. Both strings consist only of lowercase English letters. Output If Santa is wrong, and there is no way to divide some of keys into pairs and swap keys in each pair so that the keyboard will be fixed, print Β«-1Β» (without quotes). Otherwise, the first line of output should contain the only integer k (k β‰₯ 0) β€” the number of pairs of keys that should be swapped. The following k lines should contain two space-separated letters each, denoting the keys which should be swapped. All printed letters must be distinct. If there are several possible answers, print any of them. You are free to choose the order of the pairs and the order of keys in a pair. Each letter must occur at most once. Santa considers the keyboard to be fixed if he can print his favorite patter without mistakes. Examples Input helloworld ehoolwlroz Output 3 h e l o d z Input hastalavistababy hastalavistababy Output 0 Input merrychristmas christmasmerry Output -1 Submitted Solution: ``` import traceback import math from collections import defaultdict from functools import lru_cache import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") def geti(): return int(input()) def gets(): return input() def getil(): return list(map(int, input().split())) def getsl(): return input().split() def get2d(nrows, ncols, n=0): return [[n] * ncols for r in range(nrows)] # sys.stdin = open('input.txt', 'r') # sys.stdout = open('output.txt', 'w') inf = float('inf') mod = 10 ** 9 + 7 def main(): T = gets() S = gets() pairs = {} ans = [] for s, t in zip(S, T): if s == t: if s in pairs: return -1 elif s in pairs: if s != pairs[t]: return -1 else: pairs[s] = t pairs[t] = s ans.append([s, t]) return ans try: ans = main() if ans == -1: print(-1) else: print(len(ans)) # ans = 'Yes' if ans else 'No' for i in ans: print(*i) except Exception as e: traceback.print_exc() ``` No
12,144
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Santa Claus decided to disassemble his keyboard to clean it. After he returned all the keys back, he suddenly realized that some pairs of keys took each other's place! That is, Santa suspects that each key is either on its place, or on the place of another key, which is located exactly where the first key should be. In order to make sure that he's right and restore the correct order of keys, Santa typed his favorite patter looking only to his keyboard. You are given the Santa's favorite patter and the string he actually typed. Determine which pairs of keys could be mixed. Each key must occur in pairs at most once. Input The input consists of only two strings s and t denoting the favorite Santa's patter and the resulting string. s and t are not empty and have the same length, which is at most 1000. Both strings consist only of lowercase English letters. Output If Santa is wrong, and there is no way to divide some of keys into pairs and swap keys in each pair so that the keyboard will be fixed, print Β«-1Β» (without quotes). Otherwise, the first line of output should contain the only integer k (k β‰₯ 0) β€” the number of pairs of keys that should be swapped. The following k lines should contain two space-separated letters each, denoting the keys which should be swapped. All printed letters must be distinct. If there are several possible answers, print any of them. You are free to choose the order of the pairs and the order of keys in a pair. Each letter must occur at most once. Santa considers the keyboard to be fixed if he can print his favorite patter without mistakes. Examples Input helloworld ehoolwlroz Output 3 h e l o d z Input hastalavistababy hastalavistababy Output 0 Input merrychristmas christmasmerry Output -1 Submitted Solution: ``` # cook your dish here import sys s=input() t=input() set1=[] set2=[] right=[] for i in range(len(s)): if s[i]==t[i]: right.append(s[i]) else: if s[i] in set1 and t[i] in set2: if set1.index(s[i])==set2.index(t[i]): pass else: print('-1') sys.exit() elif s[i] in set1 and t[i] not in set2: print(-1) sys.exit() elif s[i] not in set1 and t[i] in set2: print('-1') sys.exit() else: if s[i] not in set1 and s[i] not in set2: if t[i] not in set1 and t[i] not in set2: set1.append(s[i]) set2.append(t[i]) else: print(-1) sys.exit() if s[i] in right or t[i] in right: print(-1) sys.exit() print(len(set1)) for i in range(len(set1)): print(set1[i],end=" ") print(set2[i]) ``` No
12,145
Provide tags and a correct Python 3 solution for this coding contest problem. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image> Tags: dfs and similar, dsu, graphs Correct Solution: ``` n, m = [int(x) for x in input().split()] l = [] for i in range(m): x, y = [int(x) for x in input().split()] if x>y: x,y = y,x l.append((x, y)) l.sort(key=lambda x: x[0]) labels = {} rels_count = {} class_count = {} for x, y in l: if x not in labels: labels[x] = len(rels_count) rels_count[labels[x]] = 0 class_count[labels[x]] = 1 if y not in labels: class_count[labels[x]] += 1 labels[y] = labels[x] rels_count[labels[x]]+=1 flag = True for i in range(len(rels_count)): l_n = class_count[i] l_m = rels_count[i] if l_n*(l_n-1) != 2*l_m: flag = False break if flag: print("YES") else: print("NO") ```
12,146
Provide tags and a correct Python 3 solution for this coding contest problem. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image> Tags: dfs and similar, dsu, graphs Correct Solution: ``` from collections import defaultdict,deque from sys import * def bfs(graph,vis,node): q=deque() q.append(node) vis[node]=0 cnt=1 while q: s=q.popleft() for i in graph[s]: if vis[i]==1: vis[i]=0 cnt+=1 if len(graph[s])!=len(graph[i]): return -1 q.append(i) return cnt n,m=map(int,stdin.readline().split()) graph=defaultdict(list) here=[0]*(n+1) for i in range(m): u,v=map(int,stdin.readline().split()) graph[u].append(v) graph[v].append(u) here[u]=1 here[v]=1 ver=0 for i in range(n): if here[i]==0: continue ver=bfs(graph,here,i) if ver==-1: break if ver-1!=len(graph[i]): ver=-1 break if ver==-1: stdout.write("NO\n") else: stdout.write("YES\n") ```
12,147
Provide tags and a correct Python 3 solution for this coding contest problem. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image> Tags: dfs and similar, dsu, graphs Correct Solution: ``` import os import sys from io import BytesIO, IOBase import math from decimal import Decimal from decimal import * from collections import defaultdict, deque import heapq getcontext().prec = 25 abcd='abcdefghijklmnopqrstuvwxyz' MOD = pow(10, 9) + 7 BUFSIZE = 8192 from bisect import bisect_left, bisect_right class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # n, k = map(int, input().split(" ")) # list(map(int, input().split(" "))) # for _ in range(int(input())): def dfs(i): visited[i]=True an = True f = g[i] for j in g[i]: if not visited[j]: visited[j]=True if g[j]!=g[i]: an=False break return an n, k = map(int, input().split(" ")) g = [set() for i in range(n+1)] for i in range(k): x, y = map(int, input().split(" ")) g[x].add(x) g[y].add(y) g[x].add(y) g[y].add(x) ans = "YES" visited = [False]*(n+1) # print(g) for i in range(1, n+1): if not visited[i]: test = dfs(i) if not test: ans="NO" break print(ans) ```
12,148
Provide tags and a correct Python 3 solution for this coding contest problem. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image> Tags: dfs and similar, dsu, graphs Correct Solution: ``` import sys n,m=map(int,input().split()) q={i:set([i]) for i in range(1,n+1)} v=set() for i in range(m): a,b=map(int,input().split()) q[a].add(b) q[b].add(a) for k,w in q.items(): if k not in v: z=[q[i]==w for i in w] if 0 not in z: v.update(w) else: print('NO') sys.exit() print('YES') ```
12,149
Provide tags and a correct Python 3 solution for this coding contest problem. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image> Tags: dfs and similar, dsu, graphs Correct Solution: ``` import sys def main(): n,m = map(int,sys.stdin.readline().split()) l = [[] for i in range(n+1)] u = [False]*(n+1) for i in range(m): a, b = map(int,sys.stdin.readline().split()) l[a].append(b) l[b].append(a) for i in range(n): j = i+1 if u[j]: continue u[j] = True q = [] cl = len(l[j]) cn = 1 for a in l[j]: q.append(a) while len(q)!=0 : cur = q.pop() if u[cur]: continue u[cur] = True cn+=1 cl+=len(l[cur]) for a in l[cur]: if u[a]: continue q.append(a) if cl!=cn*(cn-1): #print(j, cl, cn) print("NO") return print("YES") main() ```
12,150
Provide tags and a correct Python 3 solution for this coding contest problem. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image> Tags: dfs and similar, dsu, graphs Correct Solution: ``` from collections import defaultdict import sys input=__import__('sys').stdin.readline n,m=map(int,input().split()) adj=defaultdict(set) vis=[False]*(n+1) for i in range(m): u,v=map(int,input().split()) adj[u].add(v) adj[v].add(u) for x in adj: adj[x].add(x) for i in range(1,n+1): if not vis[i]: for j in adj[i]: vis[j]=True if adj[i]!=adj[j]: print("NO") exit() print("YES") ```
12,151
Provide tags and a correct Python 3 solution for this coding contest problem. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image> Tags: dfs and similar, dsu, graphs Correct Solution: ``` from sys import stdin from collections import deque #from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin def ii(): return int(stdin.readline()) def fi(): return float(stdin.readline()) def mi(): return map(int, stdin.readline().split()) def fmi(): return map(float, stdin.readline().split()) def li(): return list(mi()) def lsi(): x=list(stdin.readline()) x.pop() return x def si(): return stdin.readline() ############# CODE STARTS HERE ############# n, m=mi() a=[{i} for i in range(n+1)] v=[True]*(n+1) for _ in range(m): x, y=mi() a[x].add(y) a[y].add(x) for i in range(1, n+1): if v[i]: for j in a[i]: if a[i]!=a[j]: print('NO') exit() v[j]=False #print(a) print('YES') ```
12,152
Provide tags and a correct Python 3 solution for this coding contest problem. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image> Tags: dfs and similar, dsu, graphs Correct Solution: ``` from collections import Counter,defaultdict,deque #alph = 'abcdefghijklmnopqrstuvwxyz' #from math import factorial as fact #nl = '\n' #import sys #input=sys.stdin.readline #print=sys.stdout.write #tt = int(input()) #total=0 #n = int(input()) #n,m,k = [int(x) for x in input().split()] #n = int(input()) #l,r = [int(x) for x in input().split()] n,m = [int(x) for x in input().split()] connections = [[] for i in range(n)] for i in range(m): a,b = [int(x)-1 for x in input().split()] connections[a].append(b) connections[b].append(a) globalvisited = [False for i in range(n)] def DFS(cmap,v): #visited = [False]*len(cmap) stack = deque([v]) #order = deque() vcount = 0 ecount = len(cmap[v]) while len(stack)!=0: vertex = stack.pop() if not globalvisited[vertex]: vcount+=1 #order.append(vertex) #visited[vertex] = True globalvisited[vertex] = True if len(cmap[vertex])!=ecount: return False stack.extend(cmap[vertex]) #print(vcount,ecount) return vcount-1==ecount for i in range(n): if not globalvisited[i]: cur=DFS(connections,i) if not cur: print('NO') exit() print('YES') ```
12,153
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image> Submitted Solution: ``` import sys input = sys.stdin.readline from collections import defaultdict from collections import deque n,m=list(map(int,input().split())) d=defaultdict(list) for i in range(m): x,y=list(map(int,input().split())) d[x].append(y) d[y].append(x) visited=[0]*(n+1) f=0 for i in d: if visited[i]==0: q=deque() q.append(i) c=0 t=len(d[i]) visited[i]=1 while q: j=q.popleft() if t!=len(d[j]): f=1 c=c+1 for k in d[j]: if visited[k]==0: if c>1: f=1 break else: q.append(k) visited[k]=1 if f: break if f: break if f: print('NO') else: print('YES') ``` Yes
12,154
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image> Submitted Solution: ``` import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools # import time,random,resource # sys.setrecursionlimit(10**6) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 mod2 = 998244353 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def pe(s): return print(str(s), file=sys.stderr) def JA(a, sep): return sep.join(map(str, a)) def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a) def IF(c, t, f): return t if c else f def YES(c): return IF(c, "YES", "NO") def Yes(c): return IF(c, "Yes", "No") class UnionFind: def __init__(self, size): self.table = [-1 for _ in range(size)] def find(self, x): if self.table[x] < 0: return x else: self.table[x] = self.find(self.table[x]) return self.table[x] def union(self, x, y): s1 = self.find(x) s2 = self.find(y) if s1 != s2: if self.table[s1] <= self.table[s2]: self.table[s1] += self.table[s2] self.table[s2] = s1 else: self.table[s2] += self.table[s1] self.table[s1] = s2 return True return False def same(self, x, y): return self.find(x) == self.find(y) def subsetall(self): a = [] for i in range(len(self.table)): if self.table[i] < 0: a.append((i, -self.table[i])) return a def main(): t = 1 rr = [] for _ in range(t): n,m = LI() ab = [LI_() for _ in range(m)] uf = UnionFind(n) for a,b in ab: uf.union(a,b) t = 0 for i in range(n): k = -uf.table[i] if k > 0: t += k * (k-1) // 2 rr.append(YES(t == m)) return JA(rr, "\n") print(main()) ``` Yes
12,155
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image> Submitted Solution: ``` # Problem: A. Bear and Friendship Condition # Contest: Codeforces - VK Cup 2017 - Round 1 # URL: https://codeforces.com/problemset/problem/771/A # Memory Limit: 256 MB # Time Limit: 1000 ms # # KAPOOR'S from sys import stdin, stdout def INI(): return int(stdin.readline()) def INL(): return [int(_) for _ in stdin.readline().split()] def INS(): return stdin.readline() def MOD(): return pow(10,9)+7 def OPS(ans): stdout.write(str(ans)+"\n") def OPL(ans): [stdout.write(str(_)+" ") for _ in ans] stdout.write("\n") from types import GeneratorType def bootstrap(f, stack=[]): def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) else: to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) else: stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc @bootstrap def dfs(v): V[v]=True S.append(v) global c c+=1 for _ in X[v]: if not V[_]: yield dfs(_) yield if __name__=="__main__": n,m=INL() X=[[] for _ in range(n+1)] V=[False for _ in range(n+1)] C=[0 for _ in range(n+1)] for _ in range(m): u,v=INL() X[u].append(v) X[v].append(u) C[u]+=1 C[v]+=1 for _ in range(1,n+1): S=[] c=0 if not V[_]: dfs(_) for _ in S: if C[_]!=c-1: OPS("NO") break else: continue break else: OPS('YES') ``` Yes
12,156
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image> Submitted Solution: ``` def li(): return list(map(int,input().split())) def ls(): return list(map(int,list(input()))) def i(): return int(input()) from collections import defaultdict graph = defaultdict(list) n,m = li() for _ in range(m): x,y = li() graph[x].append(y) graph[y].append(x) visited= [False]*(n+1) flag = 1 for i in range(n+1): if graph[i+1] : graph[i+1].append(i+1) graph[i+1].sort() # print(graph) for i in range(n): if visited[i+1] == False: visited[i+1] = True for x in graph[i+1]: if visited[x] == False: visited[x] = True # print(graph[x],graph[i+1]) if graph[x] != graph[i+1] : flag = 0 break if flag == 1: print('YES') else: print('NO') ``` Yes
12,157
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image> Submitted Solution: ``` # Problem: A. Bear and Friendship Condition # Contest: Codeforces - VK Cup 2017 - Round 1 # URL: https://codeforces.com/problemset/problem/771/A # Memory Limit: 256 MB # Time Limit: 1000 ms # # KAPOOR'S from sys import stdin, stdout def INI(): return int(stdin.readline()) def INL(): return [int(_) for _ in stdin.readline().split()] def INS(): return stdin.readline() def MOD(): return pow(10,9)+7 def OPS(ans): stdout.write(str(ans)+"\n") def OPL(ans): [stdout.write(str(_)+" ") for _ in ans] stdout.write("\n") def dfs(v,p=0): V[v]=True # print(v,p) if p==0: for _ in X[v]: if not V[_]: if not dfs(_,v): return False else: for _ in X[v]: if not V[_]: if _ not in X[p]: return False if not dfs(_,v): return False return True if __name__=="__main__": n,m=INL() X=[set() for _ in range(n+1)] V=[False for _ in range(n+1)] for _ in range(m): u,v=INL() X[u].add(v) X[v].add(u) for _ in range(1,n+1): if not V[_]: if not dfs(_): OPS('NO') break else: OPS('YES') ``` No
12,158
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image> Submitted Solution: ``` n,m=map(int,input().split()) a=[[] for i in range(n+1)] for i in range(m): c,d=map(int,input().split()) a[c].append(d);a[d].append(c) for i in range(1,n+1): a[i].sort(reverse=True) for i in range(1,n+1): for j in a[i]: if len(a[j])>=1: del a[j][-1] for k in a[j]: if a[k][-1]!=i: exit(print('NO')) else: del a[k][-1] print('YES') ``` No
12,159
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image> Submitted Solution: ``` from collections import defaultdict class Graph: def __init__(self,n): self.graph = defaultdict(list) self.vertices = n self.visited = [False] * (n+1) self.one = 0 self.others = [] def addEdge(self,fr,to): self.graph[fr].append(to) self.graph[to].append(fr) def solve(self): for i in range(1,self.vertices+1): if(self.visited[i]==False): self.visited[i]=True Len = len(self.graph[i]) for j in self.graph[i]: self.visited[j]=False if(len(self.graph[j])!=Len): return "NO" return "YES" n,m = map(int,input().split()) G = Graph(n) for i in range(m): a,b = map(int,input().split()) G.addEdge(a,b) print(G.solve()) ``` No
12,160
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves. Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z. For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well. Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input The first line of the input contain two integers n and m (3 ≀ n ≀ 150 000, <image>) β€” the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input. Output If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). Examples Input 4 3 1 3 3 4 1 4 Output YES Input 4 4 3 1 2 3 3 4 1 2 Output NO Input 10 4 4 3 5 10 8 9 1 2 Output YES Input 3 2 1 2 2 3 Output NO Note The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. <image> Submitted Solution: ``` from collections import defaultdict,deque from sys import * def bfs(graph,vis,node): q=deque() q.append(node) vis[node]=0 while q: s=q.popleft() for i in graph[s]: if vis[i]==1: vis[i]=0 if len(graph[s])!=len(graph[i]): return -1 q.append(i) return 0 n,m=map(int,stdin.readline().split()) graph=defaultdict(list) here=[0]*(n+1) for i in range(m): u,v=map(int,stdin.readline().split()) graph[u].append(v) graph[v].append(u) here[u]=1 here[v]=1 ver=0 for i in range(n): if here[i]==0: continue ver=bfs(graph,here,i) if ver==-1: break if ver==-1: stdout.write("NO\n") else: stdout.write("YES\n") ``` No
12,161
Provide tags and a correct Python 3 solution for this coding contest problem. Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains unknown to us. <image> The girl lives in house m of a village. There are n houses in that village, lining in a straight line from left to right: house 1, house 2, ..., house n. The village is also well-structured: house i and house i + 1 (1 ≀ i < n) are exactly 10 meters away. In this village, some houses are occupied, and some are not. Indeed, unoccupied houses can be purchased. You will be given n integers a1, a2, ..., an that denote the availability and the prices of the houses. If house i is occupied, and therefore cannot be bought, then ai equals 0. Otherwise, house i can be bought, and ai represents the money required to buy it, in dollars. As Zane has only k dollars to spare, it becomes a challenge for him to choose the house to purchase, so that he could live as near as possible to his crush. Help Zane determine the minimum distance from his crush's house to some house he can afford, to help him succeed in his love. Input The first line contains three integers n, m, and k (2 ≀ n ≀ 100, 1 ≀ m ≀ n, 1 ≀ k ≀ 100) β€” the number of houses in the village, the house where the girl lives, and the amount of money Zane has (in dollars), respectively. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 100) β€” denoting the availability and the prices of the houses. It is guaranteed that am = 0 and that it is possible to purchase some house with no more than k dollars. Output Print one integer β€” the minimum distance, in meters, from the house where the girl Zane likes lives to the house Zane can buy. Examples Input 5 1 20 0 27 32 21 19 Output 40 Input 7 3 50 62 0 0 0 99 33 22 Output 30 Input 10 5 100 1 0 1 0 0 0 0 0 1 1 Output 20 Note In the first sample, with k = 20 dollars, Zane can buy only house 5. The distance from house m = 1 to house 5 is 10 + 10 + 10 + 10 = 40 meters. In the second sample, Zane can buy houses 6 and 7. It is better to buy house 6 than house 7, since house m = 3 and house 6 are only 30 meters away, while house m = 3 and house 7 are 40 meters away. Tags: brute force, implementation Correct Solution: ``` n, m, k = map(int,input().split()) s = list(map(int,input().split())); c = [] for i in range(n): if s[i]<=k and s[i]!=0: c.append(i+1) ans = 99999999999 for i in range(len(c)): if abs(m-c[i])<ans: ans = abs(m-c[i]) print(ans*10) ```
12,162
Provide tags and a correct Python 3 solution for this coding contest problem. Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains unknown to us. <image> The girl lives in house m of a village. There are n houses in that village, lining in a straight line from left to right: house 1, house 2, ..., house n. The village is also well-structured: house i and house i + 1 (1 ≀ i < n) are exactly 10 meters away. In this village, some houses are occupied, and some are not. Indeed, unoccupied houses can be purchased. You will be given n integers a1, a2, ..., an that denote the availability and the prices of the houses. If house i is occupied, and therefore cannot be bought, then ai equals 0. Otherwise, house i can be bought, and ai represents the money required to buy it, in dollars. As Zane has only k dollars to spare, it becomes a challenge for him to choose the house to purchase, so that he could live as near as possible to his crush. Help Zane determine the minimum distance from his crush's house to some house he can afford, to help him succeed in his love. Input The first line contains three integers n, m, and k (2 ≀ n ≀ 100, 1 ≀ m ≀ n, 1 ≀ k ≀ 100) β€” the number of houses in the village, the house where the girl lives, and the amount of money Zane has (in dollars), respectively. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 100) β€” denoting the availability and the prices of the houses. It is guaranteed that am = 0 and that it is possible to purchase some house with no more than k dollars. Output Print one integer β€” the minimum distance, in meters, from the house where the girl Zane likes lives to the house Zane can buy. Examples Input 5 1 20 0 27 32 21 19 Output 40 Input 7 3 50 62 0 0 0 99 33 22 Output 30 Input 10 5 100 1 0 1 0 0 0 0 0 1 1 Output 20 Note In the first sample, with k = 20 dollars, Zane can buy only house 5. The distance from house m = 1 to house 5 is 10 + 10 + 10 + 10 = 40 meters. In the second sample, Zane can buy houses 6 and 7. It is better to buy house 6 than house 7, since house m = 3 and house 6 are only 30 meters away, while house m = 3 and house 7 are 40 meters away. Tags: brute force, implementation Correct Solution: ``` n, m, k = input().split() n = int(n) m = int(m) k = int(k) h = input().split() no_zero = 0 for i in range(len(h)): h[i] = int(h[i]) if h[i] > k: h[i] = 0 elif h[i] != 0: no_zero += 1 min_dis = None while no_zero: c = max(h) i = h.index(c) + 1 if min_dis is None or min_dis > abs(i - m): min_dis = abs(i - m) no_zero -= 1 h[i-1] = 0 print(min_dis * 10) ```
12,163
Provide tags and a correct Python 3 solution for this coding contest problem. Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains unknown to us. <image> The girl lives in house m of a village. There are n houses in that village, lining in a straight line from left to right: house 1, house 2, ..., house n. The village is also well-structured: house i and house i + 1 (1 ≀ i < n) are exactly 10 meters away. In this village, some houses are occupied, and some are not. Indeed, unoccupied houses can be purchased. You will be given n integers a1, a2, ..., an that denote the availability and the prices of the houses. If house i is occupied, and therefore cannot be bought, then ai equals 0. Otherwise, house i can be bought, and ai represents the money required to buy it, in dollars. As Zane has only k dollars to spare, it becomes a challenge for him to choose the house to purchase, so that he could live as near as possible to his crush. Help Zane determine the minimum distance from his crush's house to some house he can afford, to help him succeed in his love. Input The first line contains three integers n, m, and k (2 ≀ n ≀ 100, 1 ≀ m ≀ n, 1 ≀ k ≀ 100) β€” the number of houses in the village, the house where the girl lives, and the amount of money Zane has (in dollars), respectively. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 100) β€” denoting the availability and the prices of the houses. It is guaranteed that am = 0 and that it is possible to purchase some house with no more than k dollars. Output Print one integer β€” the minimum distance, in meters, from the house where the girl Zane likes lives to the house Zane can buy. Examples Input 5 1 20 0 27 32 21 19 Output 40 Input 7 3 50 62 0 0 0 99 33 22 Output 30 Input 10 5 100 1 0 1 0 0 0 0 0 1 1 Output 20 Note In the first sample, with k = 20 dollars, Zane can buy only house 5. The distance from house m = 1 to house 5 is 10 + 10 + 10 + 10 = 40 meters. In the second sample, Zane can buy houses 6 and 7. It is better to buy house 6 than house 7, since house m = 3 and house 6 are only 30 meters away, while house m = 3 and house 7 are 40 meters away. Tags: brute force, implementation Correct Solution: ``` N, M, K = map(int, input().split()) lista = [int(i) for i in input().split()] k, ans=0, [] for i in lista: k += 1 if(i!=0 and i<=K): ans.append(abs(k-M)) ans.sort() print(ans[0]*10) ```
12,164
Provide tags and a correct Python 3 solution for this coding contest problem. Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains unknown to us. <image> The girl lives in house m of a village. There are n houses in that village, lining in a straight line from left to right: house 1, house 2, ..., house n. The village is also well-structured: house i and house i + 1 (1 ≀ i < n) are exactly 10 meters away. In this village, some houses are occupied, and some are not. Indeed, unoccupied houses can be purchased. You will be given n integers a1, a2, ..., an that denote the availability and the prices of the houses. If house i is occupied, and therefore cannot be bought, then ai equals 0. Otherwise, house i can be bought, and ai represents the money required to buy it, in dollars. As Zane has only k dollars to spare, it becomes a challenge for him to choose the house to purchase, so that he could live as near as possible to his crush. Help Zane determine the minimum distance from his crush's house to some house he can afford, to help him succeed in his love. Input The first line contains three integers n, m, and k (2 ≀ n ≀ 100, 1 ≀ m ≀ n, 1 ≀ k ≀ 100) β€” the number of houses in the village, the house where the girl lives, and the amount of money Zane has (in dollars), respectively. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 100) β€” denoting the availability and the prices of the houses. It is guaranteed that am = 0 and that it is possible to purchase some house with no more than k dollars. Output Print one integer β€” the minimum distance, in meters, from the house where the girl Zane likes lives to the house Zane can buy. Examples Input 5 1 20 0 27 32 21 19 Output 40 Input 7 3 50 62 0 0 0 99 33 22 Output 30 Input 10 5 100 1 0 1 0 0 0 0 0 1 1 Output 20 Note In the first sample, with k = 20 dollars, Zane can buy only house 5. The distance from house m = 1 to house 5 is 10 + 10 + 10 + 10 = 40 meters. In the second sample, Zane can buy houses 6 and 7. It is better to buy house 6 than house 7, since house m = 3 and house 6 are only 30 meters away, while house m = 3 and house 7 are 40 meters away. Tags: brute force, implementation Correct Solution: ``` n, m, k = list(map(int, input().split())) a = list(map(int, input().split())) r = 10000 for i in range(n): if a[i] <= k and a[i] != 0: r = min(r, abs(m-i-1)) print(r*10) ```
12,165
Provide tags and a correct Python 3 solution for this coding contest problem. Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains unknown to us. <image> The girl lives in house m of a village. There are n houses in that village, lining in a straight line from left to right: house 1, house 2, ..., house n. The village is also well-structured: house i and house i + 1 (1 ≀ i < n) are exactly 10 meters away. In this village, some houses are occupied, and some are not. Indeed, unoccupied houses can be purchased. You will be given n integers a1, a2, ..., an that denote the availability and the prices of the houses. If house i is occupied, and therefore cannot be bought, then ai equals 0. Otherwise, house i can be bought, and ai represents the money required to buy it, in dollars. As Zane has only k dollars to spare, it becomes a challenge for him to choose the house to purchase, so that he could live as near as possible to his crush. Help Zane determine the minimum distance from his crush's house to some house he can afford, to help him succeed in his love. Input The first line contains three integers n, m, and k (2 ≀ n ≀ 100, 1 ≀ m ≀ n, 1 ≀ k ≀ 100) β€” the number of houses in the village, the house where the girl lives, and the amount of money Zane has (in dollars), respectively. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 100) β€” denoting the availability and the prices of the houses. It is guaranteed that am = 0 and that it is possible to purchase some house with no more than k dollars. Output Print one integer β€” the minimum distance, in meters, from the house where the girl Zane likes lives to the house Zane can buy. Examples Input 5 1 20 0 27 32 21 19 Output 40 Input 7 3 50 62 0 0 0 99 33 22 Output 30 Input 10 5 100 1 0 1 0 0 0 0 0 1 1 Output 20 Note In the first sample, with k = 20 dollars, Zane can buy only house 5. The distance from house m = 1 to house 5 is 10 + 10 + 10 + 10 = 40 meters. In the second sample, Zane can buy houses 6 and 7. It is better to buy house 6 than house 7, since house m = 3 and house 6 are only 30 meters away, while house m = 3 and house 7 are 40 meters away. Tags: brute force, implementation Correct Solution: ``` n,m,k=map(int,input().split()) a=list(map(int,input().split())) b=[] for y in range(0,n): if k>=a[y] and a[y]!=0: b.append(abs(y-(m-1))) l=min(b) print(l*10) ```
12,166
Provide tags and a correct Python 3 solution for this coding contest problem. Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains unknown to us. <image> The girl lives in house m of a village. There are n houses in that village, lining in a straight line from left to right: house 1, house 2, ..., house n. The village is also well-structured: house i and house i + 1 (1 ≀ i < n) are exactly 10 meters away. In this village, some houses are occupied, and some are not. Indeed, unoccupied houses can be purchased. You will be given n integers a1, a2, ..., an that denote the availability and the prices of the houses. If house i is occupied, and therefore cannot be bought, then ai equals 0. Otherwise, house i can be bought, and ai represents the money required to buy it, in dollars. As Zane has only k dollars to spare, it becomes a challenge for him to choose the house to purchase, so that he could live as near as possible to his crush. Help Zane determine the minimum distance from his crush's house to some house he can afford, to help him succeed in his love. Input The first line contains three integers n, m, and k (2 ≀ n ≀ 100, 1 ≀ m ≀ n, 1 ≀ k ≀ 100) β€” the number of houses in the village, the house where the girl lives, and the amount of money Zane has (in dollars), respectively. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 100) β€” denoting the availability and the prices of the houses. It is guaranteed that am = 0 and that it is possible to purchase some house with no more than k dollars. Output Print one integer β€” the minimum distance, in meters, from the house where the girl Zane likes lives to the house Zane can buy. Examples Input 5 1 20 0 27 32 21 19 Output 40 Input 7 3 50 62 0 0 0 99 33 22 Output 30 Input 10 5 100 1 0 1 0 0 0 0 0 1 1 Output 20 Note In the first sample, with k = 20 dollars, Zane can buy only house 5. The distance from house m = 1 to house 5 is 10 + 10 + 10 + 10 = 40 meters. In the second sample, Zane can buy houses 6 and 7. It is better to buy house 6 than house 7, since house m = 3 and house 6 are only 30 meters away, while house m = 3 and house 7 are 40 meters away. Tags: brute force, implementation Correct Solution: ``` n,m,k=map(int,input().split()) a=list(map(int,input().split())) mi=int (1e9) for i in range(m,n): if a[i]<=k and a[i]!=0: mi=min(mi,i-m+1) for i in range(max(0,m-2),-1,-1): if a[i]<=k and a[i]!=0: mi=min(mi,m-1-i) print(mi*10) ```
12,167
Provide tags and a correct Python 3 solution for this coding contest problem. Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains unknown to us. <image> The girl lives in house m of a village. There are n houses in that village, lining in a straight line from left to right: house 1, house 2, ..., house n. The village is also well-structured: house i and house i + 1 (1 ≀ i < n) are exactly 10 meters away. In this village, some houses are occupied, and some are not. Indeed, unoccupied houses can be purchased. You will be given n integers a1, a2, ..., an that denote the availability and the prices of the houses. If house i is occupied, and therefore cannot be bought, then ai equals 0. Otherwise, house i can be bought, and ai represents the money required to buy it, in dollars. As Zane has only k dollars to spare, it becomes a challenge for him to choose the house to purchase, so that he could live as near as possible to his crush. Help Zane determine the minimum distance from his crush's house to some house he can afford, to help him succeed in his love. Input The first line contains three integers n, m, and k (2 ≀ n ≀ 100, 1 ≀ m ≀ n, 1 ≀ k ≀ 100) β€” the number of houses in the village, the house where the girl lives, and the amount of money Zane has (in dollars), respectively. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 100) β€” denoting the availability and the prices of the houses. It is guaranteed that am = 0 and that it is possible to purchase some house with no more than k dollars. Output Print one integer β€” the minimum distance, in meters, from the house where the girl Zane likes lives to the house Zane can buy. Examples Input 5 1 20 0 27 32 21 19 Output 40 Input 7 3 50 62 0 0 0 99 33 22 Output 30 Input 10 5 100 1 0 1 0 0 0 0 0 1 1 Output 20 Note In the first sample, with k = 20 dollars, Zane can buy only house 5. The distance from house m = 1 to house 5 is 10 + 10 + 10 + 10 = 40 meters. In the second sample, Zane can buy houses 6 and 7. It is better to buy house 6 than house 7, since house m = 3 and house 6 are only 30 meters away, while house m = 3 and house 7 are 40 meters away. Tags: brute force, implementation Correct Solution: ``` import sys lineno = 0 for l in sys.stdin: if lineno == 0: [n, m, k] = [int(i) for i in str(l).strip().split()] if lineno == 1: p = [int(i) for i in l.strip().split()] p = [10*abs(d - (m - 1)) for (i, d) in zip(p, range(len(p))) if (i != 0 and i <= k)] print(min(p)) lineno += 1 ```
12,168
Provide tags and a correct Python 3 solution for this coding contest problem. Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains unknown to us. <image> The girl lives in house m of a village. There are n houses in that village, lining in a straight line from left to right: house 1, house 2, ..., house n. The village is also well-structured: house i and house i + 1 (1 ≀ i < n) are exactly 10 meters away. In this village, some houses are occupied, and some are not. Indeed, unoccupied houses can be purchased. You will be given n integers a1, a2, ..., an that denote the availability and the prices of the houses. If house i is occupied, and therefore cannot be bought, then ai equals 0. Otherwise, house i can be bought, and ai represents the money required to buy it, in dollars. As Zane has only k dollars to spare, it becomes a challenge for him to choose the house to purchase, so that he could live as near as possible to his crush. Help Zane determine the minimum distance from his crush's house to some house he can afford, to help him succeed in his love. Input The first line contains three integers n, m, and k (2 ≀ n ≀ 100, 1 ≀ m ≀ n, 1 ≀ k ≀ 100) β€” the number of houses in the village, the house where the girl lives, and the amount of money Zane has (in dollars), respectively. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 100) β€” denoting the availability and the prices of the houses. It is guaranteed that am = 0 and that it is possible to purchase some house with no more than k dollars. Output Print one integer β€” the minimum distance, in meters, from the house where the girl Zane likes lives to the house Zane can buy. Examples Input 5 1 20 0 27 32 21 19 Output 40 Input 7 3 50 62 0 0 0 99 33 22 Output 30 Input 10 5 100 1 0 1 0 0 0 0 0 1 1 Output 20 Note In the first sample, with k = 20 dollars, Zane can buy only house 5. The distance from house m = 1 to house 5 is 10 + 10 + 10 + 10 = 40 meters. In the second sample, Zane can buy houses 6 and 7. It is better to buy house 6 than house 7, since house m = 3 and house 6 are only 30 meters away, while house m = 3 and house 7 are 40 meters away. Tags: brute force, implementation Correct Solution: ``` n,m,k=map(int,input().split()) a=list(map(int,input().split())) min1=float("inf") for i in range(n): if a[i]!=0 and min1>(abs( (m-1)-(i) )*10) and a[i]<=k: min1=(abs( (m-1)-(i) )*10) print(min1) ```
12,169
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains unknown to us. <image> The girl lives in house m of a village. There are n houses in that village, lining in a straight line from left to right: house 1, house 2, ..., house n. The village is also well-structured: house i and house i + 1 (1 ≀ i < n) are exactly 10 meters away. In this village, some houses are occupied, and some are not. Indeed, unoccupied houses can be purchased. You will be given n integers a1, a2, ..., an that denote the availability and the prices of the houses. If house i is occupied, and therefore cannot be bought, then ai equals 0. Otherwise, house i can be bought, and ai represents the money required to buy it, in dollars. As Zane has only k dollars to spare, it becomes a challenge for him to choose the house to purchase, so that he could live as near as possible to his crush. Help Zane determine the minimum distance from his crush's house to some house he can afford, to help him succeed in his love. Input The first line contains three integers n, m, and k (2 ≀ n ≀ 100, 1 ≀ m ≀ n, 1 ≀ k ≀ 100) β€” the number of houses in the village, the house where the girl lives, and the amount of money Zane has (in dollars), respectively. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 100) β€” denoting the availability and the prices of the houses. It is guaranteed that am = 0 and that it is possible to purchase some house with no more than k dollars. Output Print one integer β€” the minimum distance, in meters, from the house where the girl Zane likes lives to the house Zane can buy. Examples Input 5 1 20 0 27 32 21 19 Output 40 Input 7 3 50 62 0 0 0 99 33 22 Output 30 Input 10 5 100 1 0 1 0 0 0 0 0 1 1 Output 20 Note In the first sample, with k = 20 dollars, Zane can buy only house 5. The distance from house m = 1 to house 5 is 10 + 10 + 10 + 10 = 40 meters. In the second sample, Zane can buy houses 6 and 7. It is better to buy house 6 than house 7, since house m = 3 and house 6 are only 30 meters away, while house m = 3 and house 7 are 40 meters away. Submitted Solution: ``` n,m,k=input().split() n=int(n) m=int(m) k=int(k) d=list(input().split()) for i in range(n): d[i]=int(d[i]) ans=0x3f3f3f3f l1=list(range(m-1)) l1=l1[::-1] for i in l1: if d[i]!=0 and k>=d[i]: ans=m-i-1 break for i in range(m,n): if d[i]!=0 and k>=d[i]: ans=min(ans,i-m+1) break print(ans*10) ``` Yes
12,170
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains unknown to us. <image> The girl lives in house m of a village. There are n houses in that village, lining in a straight line from left to right: house 1, house 2, ..., house n. The village is also well-structured: house i and house i + 1 (1 ≀ i < n) are exactly 10 meters away. In this village, some houses are occupied, and some are not. Indeed, unoccupied houses can be purchased. You will be given n integers a1, a2, ..., an that denote the availability and the prices of the houses. If house i is occupied, and therefore cannot be bought, then ai equals 0. Otherwise, house i can be bought, and ai represents the money required to buy it, in dollars. As Zane has only k dollars to spare, it becomes a challenge for him to choose the house to purchase, so that he could live as near as possible to his crush. Help Zane determine the minimum distance from his crush's house to some house he can afford, to help him succeed in his love. Input The first line contains three integers n, m, and k (2 ≀ n ≀ 100, 1 ≀ m ≀ n, 1 ≀ k ≀ 100) β€” the number of houses in the village, the house where the girl lives, and the amount of money Zane has (in dollars), respectively. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 100) β€” denoting the availability and the prices of the houses. It is guaranteed that am = 0 and that it is possible to purchase some house with no more than k dollars. Output Print one integer β€” the minimum distance, in meters, from the house where the girl Zane likes lives to the house Zane can buy. Examples Input 5 1 20 0 27 32 21 19 Output 40 Input 7 3 50 62 0 0 0 99 33 22 Output 30 Input 10 5 100 1 0 1 0 0 0 0 0 1 1 Output 20 Note In the first sample, with k = 20 dollars, Zane can buy only house 5. The distance from house m = 1 to house 5 is 10 + 10 + 10 + 10 = 40 meters. In the second sample, Zane can buy houses 6 and 7. It is better to buy house 6 than house 7, since house m = 3 and house 6 are only 30 meters away, while house m = 3 and house 7 are 40 meters away. Submitted Solution: ``` initial = input() pricing = input() first_line = list(map(int,initial.split())) second_line = list(map(int,pricing.split())) available = first_line[0] her_house = first_line[1] - 1 money = first_line[2] current_best = 1000000000 current = 99999999 for index, item in enumerate(second_line): if item <= money and item != 0: current = abs(index - her_house) * 10 if current < current_best: current_best = current print(current_best) ``` Yes
12,171
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains unknown to us. <image> The girl lives in house m of a village. There are n houses in that village, lining in a straight line from left to right: house 1, house 2, ..., house n. The village is also well-structured: house i and house i + 1 (1 ≀ i < n) are exactly 10 meters away. In this village, some houses are occupied, and some are not. Indeed, unoccupied houses can be purchased. You will be given n integers a1, a2, ..., an that denote the availability and the prices of the houses. If house i is occupied, and therefore cannot be bought, then ai equals 0. Otherwise, house i can be bought, and ai represents the money required to buy it, in dollars. As Zane has only k dollars to spare, it becomes a challenge for him to choose the house to purchase, so that he could live as near as possible to his crush. Help Zane determine the minimum distance from his crush's house to some house he can afford, to help him succeed in his love. Input The first line contains three integers n, m, and k (2 ≀ n ≀ 100, 1 ≀ m ≀ n, 1 ≀ k ≀ 100) β€” the number of houses in the village, the house where the girl lives, and the amount of money Zane has (in dollars), respectively. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 100) β€” denoting the availability and the prices of the houses. It is guaranteed that am = 0 and that it is possible to purchase some house with no more than k dollars. Output Print one integer β€” the minimum distance, in meters, from the house where the girl Zane likes lives to the house Zane can buy. Examples Input 5 1 20 0 27 32 21 19 Output 40 Input 7 3 50 62 0 0 0 99 33 22 Output 30 Input 10 5 100 1 0 1 0 0 0 0 0 1 1 Output 20 Note In the first sample, with k = 20 dollars, Zane can buy only house 5. The distance from house m = 1 to house 5 is 10 + 10 + 10 + 10 = 40 meters. In the second sample, Zane can buy houses 6 and 7. It is better to buy house 6 than house 7, since house m = 3 and house 6 are only 30 meters away, while house m = 3 and house 7 are 40 meters away. Submitted Solution: ``` n,m,sum=map(int,input().split()) a=[int(i)for i in input().split()] ans=n+1 for i in range(n): if(i==m-1): continue; else: if(a[i]==0): continue else: if(a[i]<=sum): ans=min(ans,abs(m-i-1)) print(ans*10) ``` Yes
12,172
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains unknown to us. <image> The girl lives in house m of a village. There are n houses in that village, lining in a straight line from left to right: house 1, house 2, ..., house n. The village is also well-structured: house i and house i + 1 (1 ≀ i < n) are exactly 10 meters away. In this village, some houses are occupied, and some are not. Indeed, unoccupied houses can be purchased. You will be given n integers a1, a2, ..., an that denote the availability and the prices of the houses. If house i is occupied, and therefore cannot be bought, then ai equals 0. Otherwise, house i can be bought, and ai represents the money required to buy it, in dollars. As Zane has only k dollars to spare, it becomes a challenge for him to choose the house to purchase, so that he could live as near as possible to his crush. Help Zane determine the minimum distance from his crush's house to some house he can afford, to help him succeed in his love. Input The first line contains three integers n, m, and k (2 ≀ n ≀ 100, 1 ≀ m ≀ n, 1 ≀ k ≀ 100) β€” the number of houses in the village, the house where the girl lives, and the amount of money Zane has (in dollars), respectively. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 100) β€” denoting the availability and the prices of the houses. It is guaranteed that am = 0 and that it is possible to purchase some house with no more than k dollars. Output Print one integer β€” the minimum distance, in meters, from the house where the girl Zane likes lives to the house Zane can buy. Examples Input 5 1 20 0 27 32 21 19 Output 40 Input 7 3 50 62 0 0 0 99 33 22 Output 30 Input 10 5 100 1 0 1 0 0 0 0 0 1 1 Output 20 Note In the first sample, with k = 20 dollars, Zane can buy only house 5. The distance from house m = 1 to house 5 is 10 + 10 + 10 + 10 = 40 meters. In the second sample, Zane can buy houses 6 and 7. It is better to buy house 6 than house 7, since house m = 3 and house 6 are only 30 meters away, while house m = 3 and house 7 are 40 meters away. Submitted Solution: ``` if __name__ == "__main__": nmk = input().split(' ') for i in range(len(nmk)): nmk[i] = int(nmk[i]) m = nmk[1] k = nmk[2] prices = input().split(' ') for i in range(len(prices)): prices[i] = int(prices[i]) avail_houses = list() for i in range(len(prices)): if prices[i] != 0 and k >= prices[i]: avail_houses.append(i+1) min_dist = len(prices) for h in avail_houses: if h - m < min_dist: min_dist = abs(h - m) print(min_dist*10) ``` Yes
12,173
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains unknown to us. <image> The girl lives in house m of a village. There are n houses in that village, lining in a straight line from left to right: house 1, house 2, ..., house n. The village is also well-structured: house i and house i + 1 (1 ≀ i < n) are exactly 10 meters away. In this village, some houses are occupied, and some are not. Indeed, unoccupied houses can be purchased. You will be given n integers a1, a2, ..., an that denote the availability and the prices of the houses. If house i is occupied, and therefore cannot be bought, then ai equals 0. Otherwise, house i can be bought, and ai represents the money required to buy it, in dollars. As Zane has only k dollars to spare, it becomes a challenge for him to choose the house to purchase, so that he could live as near as possible to his crush. Help Zane determine the minimum distance from his crush's house to some house he can afford, to help him succeed in his love. Input The first line contains three integers n, m, and k (2 ≀ n ≀ 100, 1 ≀ m ≀ n, 1 ≀ k ≀ 100) β€” the number of houses in the village, the house where the girl lives, and the amount of money Zane has (in dollars), respectively. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 100) β€” denoting the availability and the prices of the houses. It is guaranteed that am = 0 and that it is possible to purchase some house with no more than k dollars. Output Print one integer β€” the minimum distance, in meters, from the house where the girl Zane likes lives to the house Zane can buy. Examples Input 5 1 20 0 27 32 21 19 Output 40 Input 7 3 50 62 0 0 0 99 33 22 Output 30 Input 10 5 100 1 0 1 0 0 0 0 0 1 1 Output 20 Note In the first sample, with k = 20 dollars, Zane can buy only house 5. The distance from house m = 1 to house 5 is 10 + 10 + 10 + 10 = 40 meters. In the second sample, Zane can buy houses 6 and 7. It is better to buy house 6 than house 7, since house m = 3 and house 6 are only 30 meters away, while house m = 3 and house 7 are 40 meters away. Submitted Solution: ``` n, m, k = map(int, input().split()) costs = [int(x) for x in input().split()] m -= 1 for i in range (1, n): if (m+i < n and costs[m+i] <= k) or ( m-i >= 0 and costs[m-i] <= k): print(10*i) exit(0) ``` No
12,174
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains unknown to us. <image> The girl lives in house m of a village. There are n houses in that village, lining in a straight line from left to right: house 1, house 2, ..., house n. The village is also well-structured: house i and house i + 1 (1 ≀ i < n) are exactly 10 meters away. In this village, some houses are occupied, and some are not. Indeed, unoccupied houses can be purchased. You will be given n integers a1, a2, ..., an that denote the availability and the prices of the houses. If house i is occupied, and therefore cannot be bought, then ai equals 0. Otherwise, house i can be bought, and ai represents the money required to buy it, in dollars. As Zane has only k dollars to spare, it becomes a challenge for him to choose the house to purchase, so that he could live as near as possible to his crush. Help Zane determine the minimum distance from his crush's house to some house he can afford, to help him succeed in his love. Input The first line contains three integers n, m, and k (2 ≀ n ≀ 100, 1 ≀ m ≀ n, 1 ≀ k ≀ 100) β€” the number of houses in the village, the house where the girl lives, and the amount of money Zane has (in dollars), respectively. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 100) β€” denoting the availability and the prices of the houses. It is guaranteed that am = 0 and that it is possible to purchase some house with no more than k dollars. Output Print one integer β€” the minimum distance, in meters, from the house where the girl Zane likes lives to the house Zane can buy. Examples Input 5 1 20 0 27 32 21 19 Output 40 Input 7 3 50 62 0 0 0 99 33 22 Output 30 Input 10 5 100 1 0 1 0 0 0 0 0 1 1 Output 20 Note In the first sample, with k = 20 dollars, Zane can buy only house 5. The distance from house m = 1 to house 5 is 10 + 10 + 10 + 10 = 40 meters. In the second sample, Zane can buy houses 6 and 7. It is better to buy house 6 than house 7, since house m = 3 and house 6 are only 30 meters away, while house m = 3 and house 7 are 40 meters away. Submitted Solution: ``` n,m,k=map(int,input().split()) a=[int(i) for i in input().split()] ans=100 for i in range(m,n,1): if a[i]>0 and a[i]<=k: ans=i-m+1 break for j in range(m-2,-1,-1): print(j) if a[j]>0 and a[j]<=k: ans=min(ans,m-j+1) break print(ans*10) ``` No
12,175
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains unknown to us. <image> The girl lives in house m of a village. There are n houses in that village, lining in a straight line from left to right: house 1, house 2, ..., house n. The village is also well-structured: house i and house i + 1 (1 ≀ i < n) are exactly 10 meters away. In this village, some houses are occupied, and some are not. Indeed, unoccupied houses can be purchased. You will be given n integers a1, a2, ..., an that denote the availability and the prices of the houses. If house i is occupied, and therefore cannot be bought, then ai equals 0. Otherwise, house i can be bought, and ai represents the money required to buy it, in dollars. As Zane has only k dollars to spare, it becomes a challenge for him to choose the house to purchase, so that he could live as near as possible to his crush. Help Zane determine the minimum distance from his crush's house to some house he can afford, to help him succeed in his love. Input The first line contains three integers n, m, and k (2 ≀ n ≀ 100, 1 ≀ m ≀ n, 1 ≀ k ≀ 100) β€” the number of houses in the village, the house where the girl lives, and the amount of money Zane has (in dollars), respectively. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 100) β€” denoting the availability and the prices of the houses. It is guaranteed that am = 0 and that it is possible to purchase some house with no more than k dollars. Output Print one integer β€” the minimum distance, in meters, from the house where the girl Zane likes lives to the house Zane can buy. Examples Input 5 1 20 0 27 32 21 19 Output 40 Input 7 3 50 62 0 0 0 99 33 22 Output 30 Input 10 5 100 1 0 1 0 0 0 0 0 1 1 Output 20 Note In the first sample, with k = 20 dollars, Zane can buy only house 5. The distance from house m = 1 to house 5 is 10 + 10 + 10 + 10 = 40 meters. In the second sample, Zane can buy houses 6 and 7. It is better to buy house 6 than house 7, since house m = 3 and house 6 are only 30 meters away, while house m = 3 and house 7 are 40 meters away. Submitted Solution: ``` a,b,c=input().split() houses=int(a) girl=int(b) money=int(c) costs=input().split() closest=100 for i in range(houses): if int(costs[i])<=money and abs(i-girl)<closest and int(costs[i])>0: closest=i print(closest*10) ``` No
12,176
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains unknown to us. <image> The girl lives in house m of a village. There are n houses in that village, lining in a straight line from left to right: house 1, house 2, ..., house n. The village is also well-structured: house i and house i + 1 (1 ≀ i < n) are exactly 10 meters away. In this village, some houses are occupied, and some are not. Indeed, unoccupied houses can be purchased. You will be given n integers a1, a2, ..., an that denote the availability and the prices of the houses. If house i is occupied, and therefore cannot be bought, then ai equals 0. Otherwise, house i can be bought, and ai represents the money required to buy it, in dollars. As Zane has only k dollars to spare, it becomes a challenge for him to choose the house to purchase, so that he could live as near as possible to his crush. Help Zane determine the minimum distance from his crush's house to some house he can afford, to help him succeed in his love. Input The first line contains three integers n, m, and k (2 ≀ n ≀ 100, 1 ≀ m ≀ n, 1 ≀ k ≀ 100) β€” the number of houses in the village, the house where the girl lives, and the amount of money Zane has (in dollars), respectively. The second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 100) β€” denoting the availability and the prices of the houses. It is guaranteed that am = 0 and that it is possible to purchase some house with no more than k dollars. Output Print one integer β€” the minimum distance, in meters, from the house where the girl Zane likes lives to the house Zane can buy. Examples Input 5 1 20 0 27 32 21 19 Output 40 Input 7 3 50 62 0 0 0 99 33 22 Output 30 Input 10 5 100 1 0 1 0 0 0 0 0 1 1 Output 20 Note In the first sample, with k = 20 dollars, Zane can buy only house 5. The distance from house m = 1 to house 5 is 10 + 10 + 10 + 10 = 40 meters. In the second sample, Zane can buy houses 6 and 7. It is better to buy house 6 than house 7, since house m = 3 and house 6 are only 30 meters away, while house m = 3 and house 7 are 40 meters away. Submitted Solution: ``` input1 = input() num_house , girl_house , money = map(int,input1.split(' ')) input2 = input() money_list = list(map(int,input2.split(' '))) for i in range(len(money_list)): if money_list[i] == 0: False elif money_list[i]<=money: g_house = girl_house-1 distance = i - g_house if distance<0: distance *= -1 else: print(distance*10) break ``` No
12,177
Provide tags and a correct Python 3 solution for this coding contest problem. To stay woke and attentive during classes, Karen needs some coffee! <image> Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe". She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between li and ri degrees, inclusive, to achieve the optimal taste. Karen thinks that a temperature is admissible if at least k recipes recommend it. Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range? Input The first line of input contains three integers, n, k (1 ≀ k ≀ n ≀ 200000), and q (1 ≀ q ≀ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively. The next n lines describe the recipes. Specifically, the i-th line among these contains two integers li and ri (1 ≀ li ≀ ri ≀ 200000), describing that the i-th recipe suggests that the coffee be brewed between li and ri degrees, inclusive. The next q lines describe the questions. Each of these lines contains a and b, (1 ≀ a ≀ b ≀ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive. Output For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive. Examples Input 3 2 4 91 94 92 97 97 99 92 94 93 97 95 96 90 100 Output 3 3 0 4 Input 2 1 1 1 1 200000 200000 90 100 Output 0 Note In the first test case, Karen knows 3 recipes. 1. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive. 2. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive. 3. The third one recommends brewing the coffee between 97 and 99 degrees, inclusive. A temperature is admissible if at least 2 recipes recommend it. She asks 4 questions. In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible. In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible. In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none. In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible. In the second test case, Karen knows 2 recipes. 1. The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree. 2. The second one, "What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly 200000 degrees. A temperature is admissible if at least 1 recipe recommends it. In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none. Tags: binary search, data structures, implementation Correct Solution: ``` n,k,q = map(int,input().split()) numRec_temp_i = [0 for i in range(200001)] for i in range(n): l,r = map(int,input().split()) numRec_temp_i[l] += 1 if r<200000: numRec_temp_i[r+1] -= 1 for i in range(1,200001): numRec_temp_i[i] += numRec_temp_i[i-1] for i in range(1,200001): if numRec_temp_i[i] >= k: numRec_temp_i[i] = 1+numRec_temp_i[i-1] else: numRec_temp_i[i] = numRec_temp_i[i-1] z = [] for i in range(q): a,b = map(int,input().split()) z.append(str(numRec_temp_i[b]-numRec_temp_i[a-1])) print("\n".join(z)) ```
12,178
Provide tags and a correct Python 3 solution for this coding contest problem. To stay woke and attentive during classes, Karen needs some coffee! <image> Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe". She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between li and ri degrees, inclusive, to achieve the optimal taste. Karen thinks that a temperature is admissible if at least k recipes recommend it. Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range? Input The first line of input contains three integers, n, k (1 ≀ k ≀ n ≀ 200000), and q (1 ≀ q ≀ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively. The next n lines describe the recipes. Specifically, the i-th line among these contains two integers li and ri (1 ≀ li ≀ ri ≀ 200000), describing that the i-th recipe suggests that the coffee be brewed between li and ri degrees, inclusive. The next q lines describe the questions. Each of these lines contains a and b, (1 ≀ a ≀ b ≀ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive. Output For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive. Examples Input 3 2 4 91 94 92 97 97 99 92 94 93 97 95 96 90 100 Output 3 3 0 4 Input 2 1 1 1 1 200000 200000 90 100 Output 0 Note In the first test case, Karen knows 3 recipes. 1. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive. 2. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive. 3. The third one recommends brewing the coffee between 97 and 99 degrees, inclusive. A temperature is admissible if at least 2 recipes recommend it. She asks 4 questions. In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible. In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible. In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none. In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible. In the second test case, Karen knows 2 recipes. 1. The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree. 2. The second one, "What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly 200000 degrees. A temperature is admissible if at least 1 recipe recommends it. In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none. Tags: binary search, data structures, implementation Correct Solution: ``` """ This template is made by Satwik_Tiwari. python programmers can use this template :)) . """ #=============================================================================================== #importing some useful libraries. from fractions import Fraction import sys import bisect import heapq from math import * from collections import Counter as counter # Counter(list) return a dict with {key: count} from itertools import combinations as comb # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)] from itertools import permutations as permutate from bisect import bisect_left as bl #If the element is already present in the list, # the left most position where element has to be inserted is returned. from bisect import bisect_right as br from bisect import bisect #If the element is already present in the list, # the right most position where element has to be inserted is returned #=============================================================================================== #some shortcuts mod = 1000000007 def inp(): return sys.stdin.readline().strip() #for fast input def out(var): sys.stdout.write(str(var)) #for fast output, always take string def lis(): return list(map(int, inp().split())) def stringlis(): return list(map(str, inp().split())) def sep(): return map(int, inp().split()) def strsep(): return map(str, inp().split()) def graph(vertex): return [[] for i in range(0,vertex+1)] def zerolist(n): return [0]*n def nextline(): out("\n") #as stdout.write always print sring. def testcase(t): for p in range(t): solve() def printlist(a) : for p in range(0,len(a)): out(str(a[p]) + ' ') def lcm(a,b): return (a*b)//gcd(a,b) def power(a,b): ans = 1 while(b>0): if(b%2==1): ans*=a a*=a b//=2 return ans #=============================================================================================== # code here ;)) def solve(): n,k,q = sep() pre = [0]*(200002) for i in range(0,n): l,r = sep() pre[l] +=1 pre[r+1] -=1 for i in range(1,200002): pre[i] = pre[i]+pre[i-1] # print(pre[90:100]) cnt = [0]*(200002) for i in range(0,200002): if(i==0): if(pre[i] >= k): cnt[0] = 1 else: if(pre[i] >= k): cnt[i] = cnt[i-1] + 1 else: cnt[i] = cnt[i-1] # print(cnt[90:100]) for i in range(0,q): l,r = sep() print(cnt[r]-cnt[l-1]) testcase(1) ```
12,179
Provide tags and a correct Python 3 solution for this coding contest problem. To stay woke and attentive during classes, Karen needs some coffee! <image> Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe". She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between li and ri degrees, inclusive, to achieve the optimal taste. Karen thinks that a temperature is admissible if at least k recipes recommend it. Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range? Input The first line of input contains three integers, n, k (1 ≀ k ≀ n ≀ 200000), and q (1 ≀ q ≀ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively. The next n lines describe the recipes. Specifically, the i-th line among these contains two integers li and ri (1 ≀ li ≀ ri ≀ 200000), describing that the i-th recipe suggests that the coffee be brewed between li and ri degrees, inclusive. The next q lines describe the questions. Each of these lines contains a and b, (1 ≀ a ≀ b ≀ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive. Output For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive. Examples Input 3 2 4 91 94 92 97 97 99 92 94 93 97 95 96 90 100 Output 3 3 0 4 Input 2 1 1 1 1 200000 200000 90 100 Output 0 Note In the first test case, Karen knows 3 recipes. 1. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive. 2. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive. 3. The third one recommends brewing the coffee between 97 and 99 degrees, inclusive. A temperature is admissible if at least 2 recipes recommend it. She asks 4 questions. In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible. In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible. In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none. In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible. In the second test case, Karen knows 2 recipes. 1. The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree. 2. The second one, "What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly 200000 degrees. A temperature is admissible if at least 1 recipe recommends it. In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none. Tags: binary search, data structures, implementation Correct Solution: ``` N = 200002 f = [0] * N ans = [0] * N res = [] n, k, q = map(int, input().split(' ')) while n: l, r = map(int, input().split(' ')) f[l] += 1 f[r + 1] -= 1 n -= 1 for i in range(1, N): f[i] += f[i-1] if f[i] >= k: ans[i] = 1 ans[i] += ans[i - 1] while q: l, r = map(int, input().split(' ')) res.append(ans[r] - ans[l - 1]) q -= 1 print(*res, sep='\n') ```
12,180
Provide tags and a correct Python 3 solution for this coding contest problem. To stay woke and attentive during classes, Karen needs some coffee! <image> Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe". She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between li and ri degrees, inclusive, to achieve the optimal taste. Karen thinks that a temperature is admissible if at least k recipes recommend it. Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range? Input The first line of input contains three integers, n, k (1 ≀ k ≀ n ≀ 200000), and q (1 ≀ q ≀ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively. The next n lines describe the recipes. Specifically, the i-th line among these contains two integers li and ri (1 ≀ li ≀ ri ≀ 200000), describing that the i-th recipe suggests that the coffee be brewed between li and ri degrees, inclusive. The next q lines describe the questions. Each of these lines contains a and b, (1 ≀ a ≀ b ≀ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive. Output For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive. Examples Input 3 2 4 91 94 92 97 97 99 92 94 93 97 95 96 90 100 Output 3 3 0 4 Input 2 1 1 1 1 200000 200000 90 100 Output 0 Note In the first test case, Karen knows 3 recipes. 1. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive. 2. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive. 3. The third one recommends brewing the coffee between 97 and 99 degrees, inclusive. A temperature is admissible if at least 2 recipes recommend it. She asks 4 questions. In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible. In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible. In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none. In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible. In the second test case, Karen knows 2 recipes. 1. The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree. 2. The second one, "What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly 200000 degrees. A temperature is admissible if at least 1 recipe recommends it. In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none. Tags: binary search, data structures, implementation Correct Solution: ``` from sys import stdin, stdout maxN = 200200 n, k, q = [int(item) for item in stdin.readline().strip().split()] a = [0] * maxN b = [0] * maxN for i in range(n): l, r = [int(item) for item in stdin.readline().strip().split()] a[l] += 1 a[r + 1] -= 1 for i in range(1, maxN): a[i] += a[i - 1] for i in range(maxN): if a[i] >= k:b[i] = 1 b[i] += b[i - 1] for j in range(q): u, v = [int(item) for item in stdin.readline().strip().split()] stdout.write(str(b[v] - b[u - 1])) stdout.write("\n") ```
12,181
Provide tags and a correct Python 3 solution for this coding contest problem. To stay woke and attentive during classes, Karen needs some coffee! <image> Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe". She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between li and ri degrees, inclusive, to achieve the optimal taste. Karen thinks that a temperature is admissible if at least k recipes recommend it. Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range? Input The first line of input contains three integers, n, k (1 ≀ k ≀ n ≀ 200000), and q (1 ≀ q ≀ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively. The next n lines describe the recipes. Specifically, the i-th line among these contains two integers li and ri (1 ≀ li ≀ ri ≀ 200000), describing that the i-th recipe suggests that the coffee be brewed between li and ri degrees, inclusive. The next q lines describe the questions. Each of these lines contains a and b, (1 ≀ a ≀ b ≀ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive. Output For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive. Examples Input 3 2 4 91 94 92 97 97 99 92 94 93 97 95 96 90 100 Output 3 3 0 4 Input 2 1 1 1 1 200000 200000 90 100 Output 0 Note In the first test case, Karen knows 3 recipes. 1. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive. 2. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive. 3. The third one recommends brewing the coffee between 97 and 99 degrees, inclusive. A temperature is admissible if at least 2 recipes recommend it. She asks 4 questions. In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible. In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible. In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none. In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible. In the second test case, Karen knows 2 recipes. 1. The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree. 2. The second one, "What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly 200000 degrees. A temperature is admissible if at least 1 recipe recommends it. In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none. Tags: binary search, data structures, implementation Correct Solution: ``` n, k, q = map(int, input().split()) c = [0] * 200003 for i in range(n): a, b = map(int, input().split()) c[a] += 1 c[b + 1] -= 1 for i in range(1, len(c)): c[i] += c[i-1] c[0] = 1 if c[0] >= k else 0 for i in range(1, len(c)): c[i] = 1 if c[i] >= k else 0 c[i] += c[i-1] ans = '' for i in range(q): a, b = map(int, input().split()) ans += str(c[b]-c[a-1]) + '\n' print(ans, end='') ```
12,182
Provide tags and a correct Python 3 solution for this coding contest problem. To stay woke and attentive during classes, Karen needs some coffee! <image> Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe". She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between li and ri degrees, inclusive, to achieve the optimal taste. Karen thinks that a temperature is admissible if at least k recipes recommend it. Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range? Input The first line of input contains three integers, n, k (1 ≀ k ≀ n ≀ 200000), and q (1 ≀ q ≀ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively. The next n lines describe the recipes. Specifically, the i-th line among these contains two integers li and ri (1 ≀ li ≀ ri ≀ 200000), describing that the i-th recipe suggests that the coffee be brewed between li and ri degrees, inclusive. The next q lines describe the questions. Each of these lines contains a and b, (1 ≀ a ≀ b ≀ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive. Output For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive. Examples Input 3 2 4 91 94 92 97 97 99 92 94 93 97 95 96 90 100 Output 3 3 0 4 Input 2 1 1 1 1 200000 200000 90 100 Output 0 Note In the first test case, Karen knows 3 recipes. 1. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive. 2. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive. 3. The third one recommends brewing the coffee between 97 and 99 degrees, inclusive. A temperature is admissible if at least 2 recipes recommend it. She asks 4 questions. In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible. In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible. In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none. In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible. In the second test case, Karen knows 2 recipes. 1. The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree. 2. The second one, "What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly 200000 degrees. A temperature is admissible if at least 1 recipe recommends it. In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none. Tags: binary search, data structures, implementation Correct Solution: ``` from sys import stdin, stdout MAX = 200000 a = [0] * (MAX + 1) n, mn, nQ = map(int, stdin.readline().split()) for _ in range(n): l, r = map(lambda x: int(x) - 1, stdin.readline().split()) a[l] += 1 a[r + 1] -= 1 psums = [0] d = 0 for i in range(MAX): d += a[i] a[i] = d a[i] = 0 if a[i] < mn else 1 psums.append(psums[-1] + a[i]) sumRange = lambda l, r: psums[r + 1] - psums[l] for _ in range(nQ): l, r = map(lambda x: int(x) - 1, stdin.readline().split()) stdout.write('%d\n' % sumRange(l, r)) ```
12,183
Provide tags and a correct Python 3 solution for this coding contest problem. To stay woke and attentive during classes, Karen needs some coffee! <image> Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe". She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between li and ri degrees, inclusive, to achieve the optimal taste. Karen thinks that a temperature is admissible if at least k recipes recommend it. Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range? Input The first line of input contains three integers, n, k (1 ≀ k ≀ n ≀ 200000), and q (1 ≀ q ≀ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively. The next n lines describe the recipes. Specifically, the i-th line among these contains two integers li and ri (1 ≀ li ≀ ri ≀ 200000), describing that the i-th recipe suggests that the coffee be brewed between li and ri degrees, inclusive. The next q lines describe the questions. Each of these lines contains a and b, (1 ≀ a ≀ b ≀ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive. Output For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive. Examples Input 3 2 4 91 94 92 97 97 99 92 94 93 97 95 96 90 100 Output 3 3 0 4 Input 2 1 1 1 1 200000 200000 90 100 Output 0 Note In the first test case, Karen knows 3 recipes. 1. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive. 2. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive. 3. The third one recommends brewing the coffee between 97 and 99 degrees, inclusive. A temperature is admissible if at least 2 recipes recommend it. She asks 4 questions. In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible. In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible. In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none. In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible. In the second test case, Karen knows 2 recipes. 1. The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree. 2. The second one, "What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly 200000 degrees. A temperature is admissible if at least 1 recipe recommends it. In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none. Tags: binary search, data structures, implementation Correct Solution: ``` import sys input=sys.stdin.readline n, k, q = list(map(int, input().split())) leng = 200002 c = [0]*leng pre_s = [0]*leng for i in range(n): a, b = list(map(int, input().split())) c[a] += 1 c[b+1] -= 1 for i in range(1, leng): c[i] += c[i-1] pre_s[i] = pre_s[i-1] + (c[i] >= k) for i in range(q): a, b = list(map(int, input().split())) print(pre_s[b]-pre_s[a-1]) ```
12,184
Provide tags and a correct Python 3 solution for this coding contest problem. To stay woke and attentive during classes, Karen needs some coffee! <image> Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe". She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between li and ri degrees, inclusive, to achieve the optimal taste. Karen thinks that a temperature is admissible if at least k recipes recommend it. Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range? Input The first line of input contains three integers, n, k (1 ≀ k ≀ n ≀ 200000), and q (1 ≀ q ≀ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively. The next n lines describe the recipes. Specifically, the i-th line among these contains two integers li and ri (1 ≀ li ≀ ri ≀ 200000), describing that the i-th recipe suggests that the coffee be brewed between li and ri degrees, inclusive. The next q lines describe the questions. Each of these lines contains a and b, (1 ≀ a ≀ b ≀ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive. Output For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive. Examples Input 3 2 4 91 94 92 97 97 99 92 94 93 97 95 96 90 100 Output 3 3 0 4 Input 2 1 1 1 1 200000 200000 90 100 Output 0 Note In the first test case, Karen knows 3 recipes. 1. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive. 2. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive. 3. The third one recommends brewing the coffee between 97 and 99 degrees, inclusive. A temperature is admissible if at least 2 recipes recommend it. She asks 4 questions. In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible. In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible. In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none. In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible. In the second test case, Karen knows 2 recipes. 1. The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree. 2. The second one, "What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly 200000 degrees. A temperature is admissible if at least 1 recipe recommends it. In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none. Tags: binary search, data structures, implementation Correct Solution: ``` n, k, q = map(int, input().split()) x = 200005 li = [0] * x for _ in range(n): l, r = map(int, input().split()) li[l] += 1 li[r+1] -= 1 #Prefix Sum for i in range(1, x): li[i] += li[i-1] for i in range(x): li[i] = li[i] >= k #Prefix Sum for i in range(1, x): li[i] += li[i-1] for __ in range(q): a, b = map(int, input().split()) print(li[b] - li[a-1]) ```
12,185
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. To stay woke and attentive during classes, Karen needs some coffee! <image> Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe". She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between li and ri degrees, inclusive, to achieve the optimal taste. Karen thinks that a temperature is admissible if at least k recipes recommend it. Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range? Input The first line of input contains three integers, n, k (1 ≀ k ≀ n ≀ 200000), and q (1 ≀ q ≀ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively. The next n lines describe the recipes. Specifically, the i-th line among these contains two integers li and ri (1 ≀ li ≀ ri ≀ 200000), describing that the i-th recipe suggests that the coffee be brewed between li and ri degrees, inclusive. The next q lines describe the questions. Each of these lines contains a and b, (1 ≀ a ≀ b ≀ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive. Output For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive. Examples Input 3 2 4 91 94 92 97 97 99 92 94 93 97 95 96 90 100 Output 3 3 0 4 Input 2 1 1 1 1 200000 200000 90 100 Output 0 Note In the first test case, Karen knows 3 recipes. 1. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive. 2. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive. 3. The third one recommends brewing the coffee between 97 and 99 degrees, inclusive. A temperature is admissible if at least 2 recipes recommend it. She asks 4 questions. In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible. In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible. In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none. In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible. In the second test case, Karen knows 2 recipes. 1. The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree. 2. The second one, "What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly 200000 degrees. A temperature is admissible if at least 1 recipe recommends it. In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none. Submitted Solution: ``` from sys import stdin, stdout MN = 200005 inp = stdin.readline().split() n = int(inp[0]) k = int(inp[1]) q = int(inp[2]) dod = [0] * MN pref = [0] * MN cnt = 0 for i in range(0, n) : inp = stdin.readline().split() dod[int(inp[0])] += 1 dod[int(inp[1]) + 1] -= 1 for i in range(1, MN) : cnt += dod[i] pref[i] = pref[i - 1] if(cnt >= k) : pref[i] += 1 for i in range(0, q) : inp = stdin.readline().split() stdout.write(str(pref[int(inp[1])] - pref[int(inp[0]) - 1]) + "\n") ``` Yes
12,186
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. To stay woke and attentive during classes, Karen needs some coffee! <image> Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe". She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between li and ri degrees, inclusive, to achieve the optimal taste. Karen thinks that a temperature is admissible if at least k recipes recommend it. Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range? Input The first line of input contains three integers, n, k (1 ≀ k ≀ n ≀ 200000), and q (1 ≀ q ≀ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively. The next n lines describe the recipes. Specifically, the i-th line among these contains two integers li and ri (1 ≀ li ≀ ri ≀ 200000), describing that the i-th recipe suggests that the coffee be brewed between li and ri degrees, inclusive. The next q lines describe the questions. Each of these lines contains a and b, (1 ≀ a ≀ b ≀ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive. Output For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive. Examples Input 3 2 4 91 94 92 97 97 99 92 94 93 97 95 96 90 100 Output 3 3 0 4 Input 2 1 1 1 1 200000 200000 90 100 Output 0 Note In the first test case, Karen knows 3 recipes. 1. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive. 2. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive. 3. The third one recommends brewing the coffee between 97 and 99 degrees, inclusive. A temperature is admissible if at least 2 recipes recommend it. She asks 4 questions. In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible. In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible. In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none. In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible. In the second test case, Karen knows 2 recipes. 1. The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree. 2. The second one, "What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly 200000 degrees. A temperature is admissible if at least 1 recipe recommends it. In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none. Submitted Solution: ``` import sys input = sys.stdin.readline n,k,q = map(int,input().split()) start = {} end = {} for i in range(n): x,y = map(int,input().split()) if x not in start: start[x] = 1 else: start[x] += 1 if y not in end: end[y] = 1 else: end[y] += 1 c = [0]*(200002) if 0 in start: c[0] += start[0] if 0 in end: c[1] -= end[0] for i in range(1,200001): c[i] += c[i-1] if i in start: c[i] += start[i] if i in end: c[i+1] -= end[i] for i in range(200001): if c[i]>=k: c[i] = 1 else: c[i] = 0 for i in range(1,200001): c[i] += c[i-1] for i in range(q): a,b = map(int,input().split()) if a==0: print (c[b]) else: print (c[b]-c[a-1]) ``` Yes
12,187
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. To stay woke and attentive during classes, Karen needs some coffee! <image> Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe". She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between li and ri degrees, inclusive, to achieve the optimal taste. Karen thinks that a temperature is admissible if at least k recipes recommend it. Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range? Input The first line of input contains three integers, n, k (1 ≀ k ≀ n ≀ 200000), and q (1 ≀ q ≀ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively. The next n lines describe the recipes. Specifically, the i-th line among these contains two integers li and ri (1 ≀ li ≀ ri ≀ 200000), describing that the i-th recipe suggests that the coffee be brewed between li and ri degrees, inclusive. The next q lines describe the questions. Each of these lines contains a and b, (1 ≀ a ≀ b ≀ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive. Output For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive. Examples Input 3 2 4 91 94 92 97 97 99 92 94 93 97 95 96 90 100 Output 3 3 0 4 Input 2 1 1 1 1 200000 200000 90 100 Output 0 Note In the first test case, Karen knows 3 recipes. 1. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive. 2. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive. 3. The third one recommends brewing the coffee between 97 and 99 degrees, inclusive. A temperature is admissible if at least 2 recipes recommend it. She asks 4 questions. In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible. In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible. In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none. In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible. In the second test case, Karen knows 2 recipes. 1. The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree. 2. The second one, "What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly 200000 degrees. A temperature is admissible if at least 1 recipe recommends it. In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none. Submitted Solution: ``` M=200002 arr=[0]*(M) n,k,q=list(map(int,input().split())) narr=[list(map(int,input().split())) for _ in range(n)] qarr=[list(map(int,input().split())) for _ in range(q)] for i,j in narr: arr[i]+=1 ##source arr[j+1]+=-1 ##destination+1 for i in range(1,M): arr[i]=arr[i]+arr[i-1] arr=[1 if i>=k else 0 for i in arr ] for i in range(1,M): arr[i]=arr[i]+arr[i-1] for qs,qd in qarr: print(arr[qd]-arr[qs-1]) ``` Yes
12,188
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. To stay woke and attentive during classes, Karen needs some coffee! <image> Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe". She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between li and ri degrees, inclusive, to achieve the optimal taste. Karen thinks that a temperature is admissible if at least k recipes recommend it. Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range? Input The first line of input contains three integers, n, k (1 ≀ k ≀ n ≀ 200000), and q (1 ≀ q ≀ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively. The next n lines describe the recipes. Specifically, the i-th line among these contains two integers li and ri (1 ≀ li ≀ ri ≀ 200000), describing that the i-th recipe suggests that the coffee be brewed between li and ri degrees, inclusive. The next q lines describe the questions. Each of these lines contains a and b, (1 ≀ a ≀ b ≀ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive. Output For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive. Examples Input 3 2 4 91 94 92 97 97 99 92 94 93 97 95 96 90 100 Output 3 3 0 4 Input 2 1 1 1 1 200000 200000 90 100 Output 0 Note In the first test case, Karen knows 3 recipes. 1. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive. 2. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive. 3. The third one recommends brewing the coffee between 97 and 99 degrees, inclusive. A temperature is admissible if at least 2 recipes recommend it. She asks 4 questions. In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible. In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible. In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none. In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible. In the second test case, Karen knows 2 recipes. 1. The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree. 2. The second one, "What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly 200000 degrees. A temperature is admissible if at least 1 recipe recommends it. In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none. Submitted Solution: ``` import sys input = sys.stdin.readline n,k,q = map(int,input().split()) t=[0]*(200002) b=[0]*(200002) for j in range(n): x,y = map(int,input().split()) t[x]+=1 t[y+1]-=1 for j in range(1,200002): t[j]+=t[j-1] if t[j]>=k: b[j]=1 b[j]+=b[j-1] while q>0: a,r = map(int,input().split()) print(b[r]-b[a-1]) q-=1 ``` Yes
12,189
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. To stay woke and attentive during classes, Karen needs some coffee! <image> Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe". She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between li and ri degrees, inclusive, to achieve the optimal taste. Karen thinks that a temperature is admissible if at least k recipes recommend it. Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range? Input The first line of input contains three integers, n, k (1 ≀ k ≀ n ≀ 200000), and q (1 ≀ q ≀ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively. The next n lines describe the recipes. Specifically, the i-th line among these contains two integers li and ri (1 ≀ li ≀ ri ≀ 200000), describing that the i-th recipe suggests that the coffee be brewed between li and ri degrees, inclusive. The next q lines describe the questions. Each of these lines contains a and b, (1 ≀ a ≀ b ≀ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive. Output For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive. Examples Input 3 2 4 91 94 92 97 97 99 92 94 93 97 95 96 90 100 Output 3 3 0 4 Input 2 1 1 1 1 200000 200000 90 100 Output 0 Note In the first test case, Karen knows 3 recipes. 1. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive. 2. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive. 3. The third one recommends brewing the coffee between 97 and 99 degrees, inclusive. A temperature is admissible if at least 2 recipes recommend it. She asks 4 questions. In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible. In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible. In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none. In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible. In the second test case, Karen knows 2 recipes. 1. The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree. 2. The second one, "What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly 200000 degrees. A temperature is admissible if at least 1 recipe recommends it. In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none. Submitted Solution: ``` n, k, q = map(int, input().split(' ')) c = [0]*200002 for i in range(n): l, r = map(int, input().split(' ')) c[l]+=1 c[r+1]-=1 i = 1 while i < 200000: c[i] += c[i - 1] if c[i-1] < k: c[i-1] = 0 else: c[i-1] = 1 i+=1 i=1 while i < 200000: c[i] += c[i - 1] i += 1 for i in range(q): a, b = map(int, input().split(' ')) print(c[b]-c[a-1]) # print(c[91:101]) ``` No
12,190
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. To stay woke and attentive during classes, Karen needs some coffee! <image> Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe". She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between li and ri degrees, inclusive, to achieve the optimal taste. Karen thinks that a temperature is admissible if at least k recipes recommend it. Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range? Input The first line of input contains three integers, n, k (1 ≀ k ≀ n ≀ 200000), and q (1 ≀ q ≀ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively. The next n lines describe the recipes. Specifically, the i-th line among these contains two integers li and ri (1 ≀ li ≀ ri ≀ 200000), describing that the i-th recipe suggests that the coffee be brewed between li and ri degrees, inclusive. The next q lines describe the questions. Each of these lines contains a and b, (1 ≀ a ≀ b ≀ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive. Output For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive. Examples Input 3 2 4 91 94 92 97 97 99 92 94 93 97 95 96 90 100 Output 3 3 0 4 Input 2 1 1 1 1 200000 200000 90 100 Output 0 Note In the first test case, Karen knows 3 recipes. 1. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive. 2. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive. 3. The third one recommends brewing the coffee between 97 and 99 degrees, inclusive. A temperature is admissible if at least 2 recipes recommend it. She asks 4 questions. In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible. In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible. In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none. In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible. In the second test case, Karen knows 2 recipes. 1. The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree. 2. The second one, "What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly 200000 degrees. A temperature is admissible if at least 1 recipe recommends it. In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none. Submitted Solution: ``` a=[0 for i in range(2*10**5+1)] x=[] x=input().split() x[0]=int(x[0]) x[1]=int(x[1]) x[2]=int(x[2]) for i in range(x[0]): y=[] y=input().split() a[int(y[0])-1]+=1 a[int(y[1])]-=1 ps=[] sum=0 for i in range(2*10**5+1): sum+=a[i] if(sum>=x[1]): ps.append(1) else: ps.append(0) ps1=[] ps1.append(ps[0]) for i in range(1,2*10**5): ps1.append(ps1[i-1]+ps[i]) sol=[] for i in range(x[2]): y=[] y=input().split() if(int(y[0])>0): sol.append(ps1[int(y[1])-1]-ps1[int(y[0])-2]) else: sol.append(ps1[int(y[1])-1]) for i in range(x[2]): print(sol[i]) ``` No
12,191
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. To stay woke and attentive during classes, Karen needs some coffee! <image> Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe". She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between li and ri degrees, inclusive, to achieve the optimal taste. Karen thinks that a temperature is admissible if at least k recipes recommend it. Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range? Input The first line of input contains three integers, n, k (1 ≀ k ≀ n ≀ 200000), and q (1 ≀ q ≀ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively. The next n lines describe the recipes. Specifically, the i-th line among these contains two integers li and ri (1 ≀ li ≀ ri ≀ 200000), describing that the i-th recipe suggests that the coffee be brewed between li and ri degrees, inclusive. The next q lines describe the questions. Each of these lines contains a and b, (1 ≀ a ≀ b ≀ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive. Output For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive. Examples Input 3 2 4 91 94 92 97 97 99 92 94 93 97 95 96 90 100 Output 3 3 0 4 Input 2 1 1 1 1 200000 200000 90 100 Output 0 Note In the first test case, Karen knows 3 recipes. 1. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive. 2. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive. 3. The third one recommends brewing the coffee between 97 and 99 degrees, inclusive. A temperature is admissible if at least 2 recipes recommend it. She asks 4 questions. In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible. In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible. In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none. In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible. In the second test case, Karen knows 2 recipes. 1. The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree. 2. The second one, "What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly 200000 degrees. A temperature is admissible if at least 1 recipe recommends it. In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none. Submitted Solution: ``` n , k , q = map(int , input().split()) c = [0] * 2000001 p = [0] * 2000001 for i in range(n): a , b = map(int , input().split()) p[a] +=1 p[b+1] -=1 for i in range(2000001): p[i] += p[i-1] c[i] += c[i-1] + (p[i]>=k) l = [] for i in range(q): x , y = map(int , input().split()) l.append(c[y] - c[x-1]) for i in l: print(l) ``` No
12,192
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. To stay woke and attentive during classes, Karen needs some coffee! <image> Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe". She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between li and ri degrees, inclusive, to achieve the optimal taste. Karen thinks that a temperature is admissible if at least k recipes recommend it. Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range? Input The first line of input contains three integers, n, k (1 ≀ k ≀ n ≀ 200000), and q (1 ≀ q ≀ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively. The next n lines describe the recipes. Specifically, the i-th line among these contains two integers li and ri (1 ≀ li ≀ ri ≀ 200000), describing that the i-th recipe suggests that the coffee be brewed between li and ri degrees, inclusive. The next q lines describe the questions. Each of these lines contains a and b, (1 ≀ a ≀ b ≀ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive. Output For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive. Examples Input 3 2 4 91 94 92 97 97 99 92 94 93 97 95 96 90 100 Output 3 3 0 4 Input 2 1 1 1 1 200000 200000 90 100 Output 0 Note In the first test case, Karen knows 3 recipes. 1. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive. 2. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive. 3. The third one recommends brewing the coffee between 97 and 99 degrees, inclusive. A temperature is admissible if at least 2 recipes recommend it. She asks 4 questions. In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible. In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible. In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none. In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible. In the second test case, Karen knows 2 recipes. 1. The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree. 2. The second one, "What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly 200000 degrees. A temperature is admissible if at least 1 recipe recommends it. In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none. Submitted Solution: ``` def prepare_item(item): return item[0], -item[1] recipes, min_needed, queries = map(int, input().split()) MAX_T = 200000 coffees = list() for i in range(recipes): min_t, max_t = map(int, input().split()) coffees.append((min_t, 1)) coffees.append((max_t + 1, -1)) coffees.sort(key=prepare_item) now_good = 0 min_good = -1 answers = list() for i in range(len(coffees)): if coffees[i][1] == 1: now_good += 1 if now_good == min_needed: min_good = coffees[i][0] else: now_good -= 1 if now_good == min_needed - 1: answers.append((min_good, coffees[i][0] - 1)) min_good = -1 if min_good != -1: answers.append((min_good, MAX_T)) #print(answers) for j in range(queries): min_t, max_t = map(int, input().split()) answer = 0 for i in answers: if i[0] > max_t: break if i[1] >= min_t: if i[0] <= min_t and i[1] >= max_t: answer = max_t - min_t + 1 break elif i[0] <= min_t and i[1] <= max_t: answer += i[1] - min_t + 1 elif i[0] >= min_t and i[1] <= max_t: answer += i[1] - i[0] + 1 elif i[0] >= min_t and i[1] >= max_t: answer += max_t - i[0] + 1 print(answer) ``` No
12,193
Provide tags and a correct Python 3 solution for this coding contest problem. Leha somehow found an array consisting of n integers. Looking at it, he came up with a task. Two players play the game on the array. Players move one by one. The first player can choose for his move a subsegment of non-zero length with an odd sum of numbers and remove it from the array, after that the remaining parts are glued together into one array and the game continues. The second player can choose a subsegment of non-zero length with an even sum and remove it. Loses the one who can not make a move. Who will win if both play optimally? Input First line of input data contains single integer n (1 ≀ n ≀ 106) β€” length of the array. Next line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 109). Output Output answer in single line. "First", if first player wins, and "Second" otherwise (without quotes). Examples Input 4 1 3 2 3 Output First Input 2 2 2 Output Second Note In first sample first player remove whole array in one move and win. In second sample first player can't make a move and lose. Tags: games, math Correct Solution: ``` def main(): import re input() for m in re.finditer(r'\d*(\d)',input()): if ord(m.group(1)) & 1: print("First") return print("Second") if __name__ == '__main__': main() ```
12,194
Provide tags and a correct Python 3 solution for this coding contest problem. Leha somehow found an array consisting of n integers. Looking at it, he came up with a task. Two players play the game on the array. Players move one by one. The first player can choose for his move a subsegment of non-zero length with an odd sum of numbers and remove it from the array, after that the remaining parts are glued together into one array and the game continues. The second player can choose a subsegment of non-zero length with an even sum and remove it. Loses the one who can not make a move. Who will win if both play optimally? Input First line of input data contains single integer n (1 ≀ n ≀ 106) β€” length of the array. Next line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 109). Output Output answer in single line. "First", if first player wins, and "Second" otherwise (without quotes). Examples Input 4 1 3 2 3 Output First Input 2 2 2 Output Second Note In first sample first player remove whole array in one move and win. In second sample first player can't make a move and lose. Tags: games, math Correct Solution: ``` n=int(input()) a=map(int,input().split()) odd_found=False for i in a: if i%2==1: odd_found=True if odd_found==False: #There was no odd means all were even print("Second") else: print("First") ```
12,195
Provide tags and a correct Python 3 solution for this coding contest problem. Leha somehow found an array consisting of n integers. Looking at it, he came up with a task. Two players play the game on the array. Players move one by one. The first player can choose for his move a subsegment of non-zero length with an odd sum of numbers and remove it from the array, after that the remaining parts are glued together into one array and the game continues. The second player can choose a subsegment of non-zero length with an even sum and remove it. Loses the one who can not make a move. Who will win if both play optimally? Input First line of input data contains single integer n (1 ≀ n ≀ 106) β€” length of the array. Next line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 109). Output Output answer in single line. "First", if first player wins, and "Second" otherwise (without quotes). Examples Input 4 1 3 2 3 Output First Input 2 2 2 Output Second Note In first sample first player remove whole array in one move and win. In second sample first player can't make a move and lose. Tags: games, math Correct Solution: ``` n=int(input()) a=list(map(int,input().split(' '))) flag=0 for i in range(0,n): if a[i]%2==1: flag=1 break if flag==1: print("First") else: print("Second") ```
12,196
Provide tags and a correct Python 3 solution for this coding contest problem. Leha somehow found an array consisting of n integers. Looking at it, he came up with a task. Two players play the game on the array. Players move one by one. The first player can choose for his move a subsegment of non-zero length with an odd sum of numbers and remove it from the array, after that the remaining parts are glued together into one array and the game continues. The second player can choose a subsegment of non-zero length with an even sum and remove it. Loses the one who can not make a move. Who will win if both play optimally? Input First line of input data contains single integer n (1 ≀ n ≀ 106) β€” length of the array. Next line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 109). Output Output answer in single line. "First", if first player wins, and "Second" otherwise (without quotes). Examples Input 4 1 3 2 3 Output First Input 2 2 2 Output Second Note In first sample first player remove whole array in one move and win. In second sample first player can't make a move and lose. Tags: games, math Correct Solution: ``` n = int(input()) a = list(map(int, input().split())) print('First' if any(map(lambda ai: ai % 2 == 1, a)) else 'Second') ```
12,197
Provide tags and a correct Python 3 solution for this coding contest problem. Leha somehow found an array consisting of n integers. Looking at it, he came up with a task. Two players play the game on the array. Players move one by one. The first player can choose for his move a subsegment of non-zero length with an odd sum of numbers and remove it from the array, after that the remaining parts are glued together into one array and the game continues. The second player can choose a subsegment of non-zero length with an even sum and remove it. Loses the one who can not make a move. Who will win if both play optimally? Input First line of input data contains single integer n (1 ≀ n ≀ 106) β€” length of the array. Next line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 109). Output Output answer in single line. "First", if first player wins, and "Second" otherwise (without quotes). Examples Input 4 1 3 2 3 Output First Input 2 2 2 Output Second Note In first sample first player remove whole array in one move and win. In second sample first player can't make a move and lose. Tags: games, math Correct Solution: ``` n = int(input()) a = map(int, input().split()) for x in a: if x % 2: print('First') exit(0) print('Second') ```
12,198
Provide tags and a correct Python 3 solution for this coding contest problem. Leha somehow found an array consisting of n integers. Looking at it, he came up with a task. Two players play the game on the array. Players move one by one. The first player can choose for his move a subsegment of non-zero length with an odd sum of numbers and remove it from the array, after that the remaining parts are glued together into one array and the game continues. The second player can choose a subsegment of non-zero length with an even sum and remove it. Loses the one who can not make a move. Who will win if both play optimally? Input First line of input data contains single integer n (1 ≀ n ≀ 106) β€” length of the array. Next line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 109). Output Output answer in single line. "First", if first player wins, and "Second" otherwise (without quotes). Examples Input 4 1 3 2 3 Output First Input 2 2 2 Output Second Note In first sample first player remove whole array in one move and win. In second sample first player can't make a move and lose. Tags: games, math Correct Solution: ``` n = int(input().strip()) a = [ int(i)%2 for i in input().strip().split(' ')] if sum(a)%2==1: print('First') else: if sum(a)==0: print('Second') else: print('First') ```
12,199