message stringlengths 2 22.8k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 16 109k | cluster float64 1 1 | __index_level_0__ int64 32 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.
<image>
At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road.
You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
Input
The first line of the input contains integer n (3 β€ n β€ 1000).
The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).
It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
Output
Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them.
Examples
Input
9
..RRLL...
Output
3 4
Input
11
.RRRLLLLL..
Output
7 5
Note
The first test sample is the one in the picture. | instruction | 0 | 7,867 | 1 | 15,734 |
Tags: greedy, implementation
Correct Solution:
```
n=int(input())
s=input()
se=set(s)
if "L" not in s:print(s.index("R")+1,n+1-s[::-1].index("R"))
elif "R" not in s:print(n-s[::-1].index("L"),s.index("L"))
else: print(s.index("R")+1,s.index("L"))
``` | output | 1 | 7,867 | 1 | 15,735 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.
<image>
At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road.
You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
Input
The first line of the input contains integer n (3 β€ n β€ 1000).
The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).
It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
Output
Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them.
Examples
Input
9
..RRLL...
Output
3 4
Input
11
.RRRLLLLL..
Output
7 5
Note
The first test sample is the one in the picture. | instruction | 0 | 7,868 | 1 | 15,736 |
Tags: greedy, implementation
Correct Solution:
```
n = int(input())
footprints = input()
firstR = n+1
lastR = 0
firstL = n+1
lastL=0
for i in range(n):
if(footprints[i]=='.'):
continue
if(footprints[i]=='R'):
firstR = min(firstR,i+1)
lastR = max(lastR,i+1)
else:
firstL = min(firstL,i+1)
lastL=max(lastL,i+1)
if(firstR!=n+1):
s = firstR
else :
s = lastL
print(s,end=" ")
if(firstL!=n+1):
t = firstL-1
else :
t = lastR+1;
print(t)
``` | output | 1 | 7,868 | 1 | 15,737 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.
<image>
At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road.
You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
Input
The first line of the input contains integer n (3 β€ n β€ 1000).
The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).
It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
Output
Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them.
Examples
Input
9
..RRLL...
Output
3 4
Input
11
.RRRLLLLL..
Output
7 5
Note
The first test sample is the one in the picture. | instruction | 0 | 7,869 | 1 | 15,738 |
Tags: greedy, implementation
Correct Solution:
```
n = int(input())
s = str(input())
a, b = 0, 0
if 'R' in s and 'L' in s:
a = s.find('R')
b = s.rfind('R')
elif 'R' in s:
a = s.find('R')
b = s.rfind('R') + 1
else:
a = s.rfind('L')
b = s.find('L')-1
print(a+1, b+1)
``` | output | 1 | 7,869 | 1 | 15,739 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.
<image>
At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road.
You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
Input
The first line of the input contains integer n (3 β€ n β€ 1000).
The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).
It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
Output
Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them.
Examples
Input
9
..RRLL...
Output
3 4
Input
11
.RRRLLLLL..
Output
7 5
Note
The first test sample is the one in the picture. | instruction | 0 | 7,870 | 1 | 15,740 |
Tags: greedy, implementation
Correct Solution:
```
from sys import stdin
def footprints(steps):
if steps.count('R') == 0:
return len(steps) - steps[-1::-1].find('L'), steps.find('L')
elif steps.count('L') == 0:
return steps.find('R') + 1, len(steps) - steps[-1::-1].find('R') + 1
else:
return steps.find('R') + 1, steps.find('L')
if __name__ == '__main__':
n = int(stdin.readline())
steps = stdin.readline().rstrip()
print(" ".join(map(str, footprints(steps))))
``` | output | 1 | 7,870 | 1 | 15,741 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.
<image>
At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road.
You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
Input
The first line of the input contains integer n (3 β€ n β€ 1000).
The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).
It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
Output
Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them.
Examples
Input
9
..RRLL...
Output
3 4
Input
11
.RRRLLLLL..
Output
7 5
Note
The first test sample is the one in the picture. | instruction | 0 | 7,871 | 1 | 15,742 |
Tags: greedy, implementation
Correct Solution:
```
arg = int(input())
s2=input()
start = -1
ending = -1
flag9 = 0
if "R" in s2:
for n in range(0, arg):
if s2[n]=="R" and flag9 == 0:
start = n + 1
flag9 = 1
if s2[n]== "R" and s2[n+1] == "L":
ending = n + 1
elif s2[n] == "R":
ending = n + 2
else:
for n in range(0, arg):
if s2[n]=="L" and flag9 == 0:
ending = n
flag9 = 1
if s2[n]== "L" and s2[n+1]==".":
start = n + 1
print(start, ending)
``` | output | 1 | 7,871 | 1 | 15,743 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.
<image>
At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road.
You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
Input
The first line of the input contains integer n (3 β€ n β€ 1000).
The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).
It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
Output
Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them.
Examples
Input
9
..RRLL...
Output
3 4
Input
11
.RRRLLLLL..
Output
7 5
Note
The first test sample is the one in the picture. | instruction | 0 | 7,872 | 1 | 15,744 |
Tags: greedy, implementation
Correct Solution:
```
n=int(input())
x=input()
a=list(set(x))
if 'R' in a and 'L' not in a:
s=x.index('R')
t=x.rindex('R')+1
elif 'L' in a and 'R' not in a:
s=x.rindex('L')
t=x.index('L')-1
else:
s=x.index('R')
t=x.rindex('R')
print(s+1,t+1)
``` | output | 1 | 7,872 | 1 | 15,745 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.
<image>
At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road.
You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
Input
The first line of the input contains integer n (3 β€ n β€ 1000).
The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).
It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
Output
Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them.
Examples
Input
9
..RRLL...
Output
3 4
Input
11
.RRRLLLLL..
Output
7 5
Note
The first test sample is the one in the picture. | instruction | 0 | 7,873 | 1 | 15,746 |
Tags: greedy, implementation
Correct Solution:
```
n = int(input())
ft = input()
r = ft.count('R')
l = ft.count('L')
if r==0:
t = ft.index('L')
for i in range(t,n-1):
if ft[i+1]=='.':
s=i+1
break
print(s,t)
elif l==0:
s = ft.index('R')
for i in range(s,n-1):
if ft[i+1]=='.':
t=i+1
break
print(s+1,t+1)
else:
r = ft.index('R')
l = ft.index('L')
print(r+1,l)
``` | output | 1 | 7,873 | 1 | 15,747 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.
<image>
At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road.
You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
Input
The first line of the input contains integer n (3 β€ n β€ 1000).
The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).
It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
Output
Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them.
Examples
Input
9
..RRLL...
Output
3 4
Input
11
.RRRLLLLL..
Output
7 5
Note
The first test sample is the one in the picture. | instruction | 0 | 7,874 | 1 | 15,748 |
Tags: greedy, implementation
Correct Solution:
```
#!/usr/bin/env python
import os
import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from io import BytesIO, IOBase
from itertools import (
accumulate, combinations, combinations_with_replacement, groupby,
permutations, product)
from math import (
acos, asin, atan, ceil, cos, degrees, factorial, hypot, log2, pi, radians,
sin, sqrt, tan)
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits
def inp():
return(int(input()))
def inlist():
return(list(map(int, input().split())))
def instr():
s = input()
return(list(s[:len(s)]))
def invr():
return(map(int, input().split()))
def main():
n = inp()
a = input()
rpLeft = a.find("R") + 1
rpRight = a.rfind("R") + 1
lpLeft = a.find("L") + 1
lpRight = a.rfind("L") + 1
if rpRight > 0 and lpLeft > 0:
print(rpLeft, rpRight)
elif lpLeft == 0:
print(rpLeft, rpRight + 1)
elif lpLeft > 0:
print(lpRight, lpLeft-1)
# region fastio
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)
def input(): return sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
``` | output | 1 | 7,874 | 1 | 15,749 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.
<image>
At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road.
You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
Input
The first line of the input contains integer n (3 β€ n β€ 1000).
The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).
It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
Output
Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them.
Examples
Input
9
..RRLL...
Output
3 4
Input
11
.RRRLLLLL..
Output
7 5
Note
The first test sample is the one in the picture.
Submitted Solution:
```
#
# Author: eloyhz
# Date: Sep/07/2020
#
if __name__ == '__main__':
n = int(input())
road = input()
s = t = None
for i in range(n - 1):
if road[i] == 'R' and road[i + 1] == 'L':
s = i + 1
t = i + 1
break
if s == t == None:
right = True
if road.count('R') > 0:
s = road.find('R') + 1
else:
right = False
s = road.find('L') + 1
for t in range(s, n):
if road[t] == '.':
break
if not right:
s, t = t, s
t -= 1
else:
t += 1
print(s, t)
``` | instruction | 0 | 7,875 | 1 | 15,750 |
Yes | output | 1 | 7,875 | 1 | 15,751 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.
<image>
At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road.
You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
Input
The first line of the input contains integer n (3 β€ n β€ 1000).
The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).
It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
Output
Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them.
Examples
Input
9
..RRLL...
Output
3 4
Input
11
.RRRLLLLL..
Output
7 5
Note
The first test sample is the one in the picture.
Submitted Solution:
```
n = int(input())
a = input()
f = n-a.count('.')
m = a.count('L')
q0 = 0
flag = 1
for i in range(n):
if(m==0):
if(a[i]=='.' and a[i-1]=='R' and i>0):
q1 = i+1
break
if(a[i]=='.'):
continue
else:
if(a[i]=='L'):
q1 = i
if(flag and q0 == 0):
q0 = i+1
break
if(flag):
q0 = i+1
flag-=1
print(q0,q1)
``` | instruction | 0 | 7,876 | 1 | 15,752 |
Yes | output | 1 | 7,876 | 1 | 15,753 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.
<image>
At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road.
You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
Input
The first line of the input contains integer n (3 β€ n β€ 1000).
The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).
It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
Output
Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them.
Examples
Input
9
..RRLL...
Output
3 4
Input
11
.RRRLLLLL..
Output
7 5
Note
The first test sample is the one in the picture.
Submitted Solution:
```
n = int(input())
k = input()
start = 0
count1 = 0
count2 = 0
countl = 0
countr = 0
end = 0
for i in range(n):
if k[i] == 'R' and count1 == 0:
start = i
count1+=1
if k[i] == 'R':
countr+=1
endr = i
if k[i] == 'L' :
countl+=1
endl = i
if k[i] == 'L' and count2 == 0:
end = i
count2+=1
if countl > 1 and countr>1:
print(start+1,end)
elif countr>1 and countl<=1:
print(start+1,endr + 2)
elif countr<=1 and countl>1:
print(endl+1,end)
else:
for i in range(n):
if k[i] == 'L':
print(i+1,i)
break
if k[i] == 'R':
print(i+1,i+2)
break
``` | instruction | 0 | 7,877 | 1 | 15,754 |
Yes | output | 1 | 7,877 | 1 | 15,755 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.
<image>
At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road.
You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
Input
The first line of the input contains integer n (3 β€ n β€ 1000).
The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).
It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
Output
Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them.
Examples
Input
9
..RRLL...
Output
3 4
Input
11
.RRRLLLLL..
Output
7 5
Note
The first test sample is the one in the picture.
Submitted Solution:
```
n = int(input())
a = input()
if 'R' in a and 'L' in a:
t = a.count('R')
x = a.index('R')
print (x + 1,x + t)
else:
if 'R' in a and ('L' not in a):
t = a.count('R')
x = a.index('R')
print(x + 1, x + t + 1)
else:
t = a.count('L')
x = a.index('L')
print (x + t,x)
``` | instruction | 0 | 7,878 | 1 | 15,756 |
Yes | output | 1 | 7,878 | 1 | 15,757 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.
<image>
At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road.
You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
Input
The first line of the input contains integer n (3 β€ n β€ 1000).
The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).
It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
Output
Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them.
Examples
Input
9
..RRLL...
Output
3 4
Input
11
.RRRLLLLL..
Output
7 5
Note
The first test sample is the one in the picture.
Submitted Solution:
```
a= int(input())
b= input()
if 'R' not in b:
c = len(b) - b[::-1].index('L')
d = b.index('L') + 1
elif 'L' not in b:
c = b.index('R') + 1
d = len(b) - b[::-1].index('R')
else:
c = b.index("R")+1
d = b.index('L')
print(c , d)
``` | instruction | 0 | 7,879 | 1 | 15,758 |
No | output | 1 | 7,879 | 1 | 15,759 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.
<image>
At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road.
You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
Input
The first line of the input contains integer n (3 β€ n β€ 1000).
The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).
It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
Output
Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them.
Examples
Input
9
..RRLL...
Output
3 4
Input
11
.RRRLLLLL..
Output
7 5
Note
The first test sample is the one in the picture.
Submitted Solution:
```
n = input()
s = input()
if(s.find('R')== -1) :
i = s.rfind('L')
j = s.find('L')
elif(s.find('L') == -1) :
i = s.find('R')
j = s.rfind('R')
else :
i = s.rfind('R')
j = s.find('L')
print(i+1,j+1)
``` | instruction | 0 | 7,880 | 1 | 15,760 |
No | output | 1 | 7,880 | 1 | 15,761 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.
<image>
At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road.
You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
Input
The first line of the input contains integer n (3 β€ n β€ 1000).
The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).
It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
Output
Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them.
Examples
Input
9
..RRLL...
Output
3 4
Input
11
.RRRLLLLL..
Output
7 5
Note
The first test sample is the one in the picture.
Submitted Solution:
```
x=int(input())
l=input()
if l.count('R')+l.count('.')==len(l):
print(l.find('R')+1,l.rfind('R')+2)
elif l.count('L')+l.count('.')==len(l):
print(l.rfind('L')+1,l.find('L'))
else:
print(l.find('R')+1,l.rfind('R'))
``` | instruction | 0 | 7,881 | 1 | 15,762 |
No | output | 1 | 7,881 | 1 | 15,763 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.
<image>
At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road.
You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
Input
The first line of the input contains integer n (3 β€ n β€ 1000).
The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).
It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
Output
Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them.
Examples
Input
9
..RRLL...
Output
3 4
Input
11
.RRRLLLLL..
Output
7 5
Note
The first test sample is the one in the picture.
Submitted Solution:
```
import sys
import itertools
import math
import collections
from collections import Counter
#input = sys.stdin.readline
#########################
# imgur.com/Pkt7iIf.png #
#########################
def sieve(n):
prime = [True for i in range(n + 1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * 2, n + 1, p):
prime[i] = False
p += 1
prime[0] = prime[1] = False
r = [p for p in range(n + 1) if prime[p]]
return r
def divs(n, start=1):
r = []
for i in range(start, int(math.sqrt(n) + 1)):
if (n % i == 0):
if (n / i == i):
r.append(i)
else:
r.extend([i, n // i])
return r
def flin(d, x, default = -1):
f = l = -1
for i in range(len(d)):
if d[i] == x:
if f == -1: f = i
l = i
if f == -1:
return (default, default)
else:
return (f, l)
def ceil(n, k): return n // k + (n % k != 0)
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(map(int, input().split()))
def lcm(a, b): return abs(a * b) // math.gcd(a, b)
def prr(a, sep=' '): print(sep.join(map(str, a)))
def dd(): return collections.defaultdict(int)
n = ii()
s = input()
fr, lr = flin(s, 'R')
fl, ll = flin(s, 'L')
if fr != -1 and fl != - 1:
print(fr + 1, fl)
elif fr == -1:
print(ll + 1, fl)
else:
print(fr + 1, lr)
``` | instruction | 0 | 7,882 | 1 | 15,764 |
No | output | 1 | 7,882 | 1 | 15,765 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?
Input
The first line of the input contains integer n (1 β€ n β€ 100 000) β the number of cars in the train.
The second line contains n integers pi (1 β€ pi β€ n, pi β pj if i β j) β the sequence of the numbers of the cars in the train.
Output
Print a single integer β the minimum number of actions needed to sort the railway cars.
Examples
Input
5
4 1 2 5 3
Output
2
Input
4
4 1 3 2
Output
2
Note
In the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | instruction | 0 | 7,961 | 1 | 15,922 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n = int(input())
arr = map(int,input().split())
li=[0]*(n+1)
for i in arr:
li[i]=li[i-1]+1
print(n-max(li))
``` | output | 1 | 7,961 | 1 | 15,923 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?
Input
The first line of the input contains integer n (1 β€ n β€ 100 000) β the number of cars in the train.
The second line contains n integers pi (1 β€ pi β€ n, pi β pj if i β j) β the sequence of the numbers of the cars in the train.
Output
Print a single integer β the minimum number of actions needed to sort the railway cars.
Examples
Input
5
4 1 2 5 3
Output
2
Input
4
4 1 3 2
Output
2
Note
In the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | instruction | 0 | 7,963 | 1 | 15,926 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n = int(input())
p = list(map(int,input().split()))
idx = [None]*(n+1)
for i,x in enumerate(p):
idx[x] = i
maxi = 1
l = 1
for i in range(1,n):
if idx[i] < idx[i+1]:
l += 1
if l > maxi:
maxi = l
else:
l = 1
print(n - maxi)
``` | output | 1 | 7,963 | 1 | 15,927 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?
Input
The first line of the input contains integer n (1 β€ n β€ 100 000) β the number of cars in the train.
The second line contains n integers pi (1 β€ pi β€ n, pi β pj if i β j) β the sequence of the numbers of the cars in the train.
Output
Print a single integer β the minimum number of actions needed to sort the railway cars.
Examples
Input
5
4 1 2 5 3
Output
2
Input
4
4 1 3 2
Output
2
Note
In the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | instruction | 0 | 7,964 | 1 | 15,928 |
Tags: constructive algorithms, greedy
Correct Solution:
```
__author__ = 'MoonBall'
import sys
# sys.stdin = open('data/C.in', 'r')
T = 1
def process():
# 2 4 3 1 -> 2 3 1 4 -> 1 2 3 4
# ε€§ηεΎεη§»οΌε°ηεΎεη§»
# 1 4 3 2 -> 1 4 2 3 -> 1 2 3 4
# ζε€§ηθΏη»ζ°
N = int(input())
a = list(map(int, input().split()))
d = [0] * (N + 1)
for _a in a:
d[_a] = d[_a - 1] + 1
print(N - max(d))
for _ in range(T):
process()
``` | output | 1 | 7,964 | 1 | 15,929 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?
Input
The first line of the input contains integer n (1 β€ n β€ 100 000) β the number of cars in the train.
The second line contains n integers pi (1 β€ pi β€ n, pi β pj if i β j) β the sequence of the numbers of the cars in the train.
Output
Print a single integer β the minimum number of actions needed to sort the railway cars.
Examples
Input
5
4 1 2 5 3
Output
2
Input
4
4 1 3 2
Output
2
Note
In the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | instruction | 0 | 7,965 | 1 | 15,930 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n = int(input())
v = list(map(int, input().split()))
uns = dict()
for i in range(n):
uns[i + 1] = -1
for i in range(n):
if v[i] != 1 and uns[v[i] - 1] != -1:
uns[v[i]] = uns[v[i] - 1] + 1
else:
uns[v[i]] = 1
print(n - max(uns.values()))
``` | output | 1 | 7,965 | 1 | 15,931 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?
Input
The first line of the input contains integer n (1 β€ n β€ 100 000) β the number of cars in the train.
The second line contains n integers pi (1 β€ pi β€ n, pi β pj if i β j) β the sequence of the numbers of the cars in the train.
Output
Print a single integer β the minimum number of actions needed to sort the railway cars.
Examples
Input
5
4 1 2 5 3
Output
2
Input
4
4 1 3 2
Output
2
Note
In the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | instruction | 0 | 7,966 | 1 | 15,932 |
Tags: constructive algorithms, greedy
Correct Solution:
```
# from dust i have come dust i will be
n=int(input())
a=list(map(int,input().split()))
mp=[0]*(n+1)
for i in range(n):
mp[a[i]]=i+1
cnt=0;mx=1
for i in range(1,n):
if mp[i]<mp[i+1]:
cnt+=1
else:
mx=max(mx,cnt+1)
cnt=0
mx=max(mx,cnt+1)
print(n-mx)
``` | output | 1 | 7,966 | 1 | 15,933 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?
Input
The first line of the input contains integer n (1 β€ n β€ 100 000) β the number of cars in the train.
The second line contains n integers pi (1 β€ pi β€ n, pi β pj if i β j) β the sequence of the numbers of the cars in the train.
Output
Print a single integer β the minimum number of actions needed to sort the railway cars.
Examples
Input
5
4 1 2 5 3
Output
2
Input
4
4 1 3 2
Output
2
Note
In the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | instruction | 0 | 7,967 | 1 | 15,934 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n = int(input())
vagons = tuple(map(int, input().split()))
res = [0] * (n)
places = [0] * (n + 1)
for i in range(n):
places[vagons[i]] = i
for i in range(n - 1, -1, -1):
v = vagons[i]
if v < n:
res[i] = res[places[v + 1]] + 1
else: res[i] = 1
print(n - max(res))
``` | output | 1 | 7,967 | 1 | 15,935 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?
Input
The first line of the input contains integer n (1 β€ n β€ 100 000) β the number of cars in the train.
The second line contains n integers pi (1 β€ pi β€ n, pi β pj if i β j) β the sequence of the numbers of the cars in the train.
Output
Print a single integer β the minimum number of actions needed to sort the railway cars.
Examples
Input
5
4 1 2 5 3
Output
2
Input
4
4 1 3 2
Output
2
Note
In the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | instruction | 0 | 7,968 | 1 | 15,936 |
Tags: constructive algorithms, greedy
Correct Solution:
```
import sys
lines = sys.stdin.readlines()
N = int(lines[0])
a = list(map(int, lines[1].split(' ')))
b = [-1 for _ in a]
for i, x in enumerate(a):
b[x-1] = i
cur_len = max_len = 1
for i in range(1, N):
if b[i-1] < b[i]:
cur_len += 1
else:
cur_len = 1
if cur_len > max_len:
max_len = cur_len
print(N - max_len)
``` | output | 1 | 7,968 | 1 | 15,937 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?
Input
The first line of the input contains integer n (1 β€ n β€ 100 000) β the number of cars in the train.
The second line contains n integers pi (1 β€ pi β€ n, pi β pj if i β j) β the sequence of the numbers of the cars in the train.
Output
Print a single integer β the minimum number of actions needed to sort the railway cars.
Examples
Input
5
4 1 2 5 3
Output
2
Input
4
4 1 3 2
Output
2
Note
In the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train.
Submitted Solution:
```
n = int(input())
p = list(map(int, input().split()))
cur = set()
d = [1] * (n + 1)
for i in p:
if i in cur:
d[i] = max(d[i], d[i - 1] + 1)
cur.add(i + 1)
ans = n - max(d)
print(ans)
``` | instruction | 0 | 7,969 | 1 | 15,938 |
Yes | output | 1 | 7,969 | 1 | 15,939 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?
Input
The first line of the input contains integer n (1 β€ n β€ 100 000) β the number of cars in the train.
The second line contains n integers pi (1 β€ pi β€ n, pi β pj if i β j) β the sequence of the numbers of the cars in the train.
Output
Print a single integer β the minimum number of actions needed to sort the railway cars.
Examples
Input
5
4 1 2 5 3
Output
2
Input
4
4 1 3 2
Output
2
Note
In the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train.
Submitted Solution:
```
n = int(input())
p = list(map(int, input().split()))
d = {}
for i in p:
d[i] = d.get(i-1, 0) + 1
maxi = max(d.values())
print(n - maxi)
``` | instruction | 0 | 7,970 | 1 | 15,940 |
Yes | output | 1 | 7,970 | 1 | 15,941 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?
Input
The first line of the input contains integer n (1 β€ n β€ 100 000) β the number of cars in the train.
The second line contains n integers pi (1 β€ pi β€ n, pi β pj if i β j) β the sequence of the numbers of the cars in the train.
Output
Print a single integer β the minimum number of actions needed to sort the railway cars.
Examples
Input
5
4 1 2 5 3
Output
2
Input
4
4 1 3 2
Output
2
Note
In the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train.
Submitted Solution:
```
n = int(input())
Arr = [int(i) for i in input().split()]
L = [0] * (n + 1)
ans = 0
for i in range(n):
if L[Arr[i] - 1] == 0:
L[Arr[i]] = 1
else:
L[Arr[i]] = 1 + L[Arr[i] - 1]
ans = max(ans, L[Arr[i]])
print(n - ans)
``` | instruction | 0 | 7,971 | 1 | 15,942 |
Yes | output | 1 | 7,971 | 1 | 15,943 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?
Input
The first line of the input contains integer n (1 β€ n β€ 100 000) β the number of cars in the train.
The second line contains n integers pi (1 β€ pi β€ n, pi β pj if i β j) β the sequence of the numbers of the cars in the train.
Output
Print a single integer β the minimum number of actions needed to sort the railway cars.
Examples
Input
5
4 1 2 5 3
Output
2
Input
4
4 1 3 2
Output
2
Note
In the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train.
Submitted Solution:
```
n = int(input())
p = list(map(int, input().split()))
dp = [1]
d = {i : 0 for i in range(1, n + 1)}
d[p[0]] = 1
for i in range(1, n):
if p[i] == 1:
dp.append(dp[i - 1])
d[p[i]] = 1
elif d[p[i] - 1] == 0:
dp.append(dp[i - 1])
d[p[i]] = 1
elif d[p[i] - 1] + 1 > dp[i - 1]:
dp.append(d[p[i] - 1] + 1)
d[p[i]] = d[p[i] - 1] + 1
else:
dp.append(dp[i - 1])
d[p[i]] = d[p[i] - 1] + 1
print(n - dp[n - 1])
``` | instruction | 0 | 7,972 | 1 | 15,944 |
Yes | output | 1 | 7,972 | 1 | 15,945 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?
Input
The first line of the input contains integer n (1 β€ n β€ 100 000) β the number of cars in the train.
The second line contains n integers pi (1 β€ pi β€ n, pi β pj if i β j) β the sequence of the numbers of the cars in the train.
Output
Print a single integer β the minimum number of actions needed to sort the railway cars.
Examples
Input
5
4 1 2 5 3
Output
2
Input
4
4 1 3 2
Output
2
Note
In the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train.
Submitted Solution:
```
n=int(input())
m=list(map(int,input().split()))
p=0
l=[]
for i in range(1,n-1):
if m[i]<m[i-1]:
l.append(m[i-1])
elif m[i]>m[i+1]:
l.append(m[i])
print(len(l))
``` | instruction | 0 | 7,973 | 1 | 15,946 |
No | output | 1 | 7,973 | 1 | 15,947 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?
Input
The first line of the input contains integer n (1 β€ n β€ 100 000) β the number of cars in the train.
The second line contains n integers pi (1 β€ pi β€ n, pi β pj if i β j) β the sequence of the numbers of the cars in the train.
Output
Print a single integer β the minimum number of actions needed to sort the railway cars.
Examples
Input
5
4 1 2 5 3
Output
2
Input
4
4 1 3 2
Output
2
Note
In the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train.
Submitted Solution:
```
n = int(input())
p = list(map(int, input().split()))
dp = [1]
d = {i : 0 for i in range(1, n + 1)}
d[p[0]] = 1
for i in range(1, n):
if p[i] == 1:
dp.append(1)
d[p[i]] = 1
elif d[p[i] - 1] == 0:
dp.append(dp[i - 1])
d[p[i]] = 1
elif d[p[i] - 1] + 1 > dp[i - 1]:
dp.append(d[p[i] - 1] + 1)
d[p[i]] = d[p[i] - 1] + 1
else:
dp.append(dp[i - 1])
d[p[i]] = d[p[i] - 1] + 1
print(n - dp[n - 1])
``` | instruction | 0 | 7,974 | 1 | 15,948 |
No | output | 1 | 7,974 | 1 | 15,949 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?
Input
The first line of the input contains integer n (1 β€ n β€ 100 000) β the number of cars in the train.
The second line contains n integers pi (1 β€ pi β€ n, pi β pj if i β j) β the sequence of the numbers of the cars in the train.
Output
Print a single integer β the minimum number of actions needed to sort the railway cars.
Examples
Input
5
4 1 2 5 3
Output
2
Input
4
4 1 3 2
Output
2
Note
In the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train.
Submitted Solution:
```
from bisect import bisect_left
def LIS(s):
L = []
for i in range(len(s)):
index = bisect_left(L,s[i])
if index >= len(L):
L.append(s[i])
else:
L[index] = s[i]
return len(L)
n = int(input())
L = list(map(int,input().split()))
print(n-LIS(L))
``` | instruction | 0 | 7,975 | 1 | 15,950 |
No | output | 1 | 7,975 | 1 | 15,951 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?
Input
The first line of the input contains integer n (1 β€ n β€ 100 000) β the number of cars in the train.
The second line contains n integers pi (1 β€ pi β€ n, pi β pj if i β j) β the sequence of the numbers of the cars in the train.
Output
Print a single integer β the minimum number of actions needed to sort the railway cars.
Examples
Input
5
4 1 2 5 3
Output
2
Input
4
4 1 3 2
Output
2
Note
In the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train.
Submitted Solution:
```
n = int(input())
seq = list(map(int, input().split()))
dp = [seq[0]]
def find_smallest_value(value):
st = 0
end = len(dp)
while st < end:
mid = (st + end) // 2
if dp[mid] < value:
st = mid + 1
else:
end = mid-1
if dp[st] > value:
return st
else:
return st + 1
for i in range(1, len(seq)):
if dp[-1] <= seq[i]:
dp.append(seq[i])
else:
index = find_smallest_value(seq[i])
dp[index] = seq[i]
print(n - len(dp))
``` | instruction | 0 | 7,976 | 1 | 15,952 |
No | output | 1 | 7,976 | 1 | 15,953 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town.
In Treeland there are 2k universities which are located in different towns.
Recently, the president signed the decree to connect universities by high-speed network.The Ministry of Education understood the decree in its own way and decided that it was enough to connect each university with another one by using a cable. Formally, the decree will be done!
To have the maximum sum in the budget, the Ministry decided to divide universities into pairs so that the total length of the required cable will be maximum. In other words, the total distance between universities in k pairs should be as large as possible.
Help the Ministry to find the maximum total distance. Of course, each university should be present in only one pair. Consider that all roads have the same length which is equal to 1.
Input
The first line of the input contains two integers n and k (2 β€ n β€ 200 000, 1 β€ k β€ n / 2) β the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to n.
The second line contains 2k distinct integers u1, u2, ..., u2k (1 β€ ui β€ n) β indices of towns in which universities are located.
The next n - 1 line contains the description of roads. Each line contains the pair of integers xj and yj (1 β€ xj, yj β€ n), which means that the j-th road connects towns xj and yj. All of them are two-way roads. You can move from any town to any other using only these roads.
Output
Print the maximum possible sum of distances in the division of universities into k pairs.
Examples
Input
7 2
1 5 6 2
1 3
3 2
4 5
3 7
4 3
4 6
Output
6
Input
9 3
3 2 1 6 5 9
8 9
3 2
2 7
3 4
7 6
4 5
2 1
2 8
Output
9
Note
The figure below shows one of possible division into pairs in the first test. If you connect universities number 1 and 6 (marked in red) and universities number 2 and 5 (marked in blue) by using the cable, the total distance will equal 6 which will be the maximum sum in this example.
<image> | instruction | 0 | 8,025 | 1 | 16,050 |
Tags: dfs and similar, dp, graphs, trees
Correct Solution:
```
from collections import defaultdict
def put(): return map(int, input().split())
def dfs():
s = [(1,0)]
ans = 0
vis = [0]*(n+1)
while s:
i,p = s.pop()
if vis[i]==0:
vis[i]=1
s.append((i,p))
for j in tree[i]:
if j!=p:
s.append((j,i))
elif vis[i]==1:
vis[i]=2
for j in tree[i]:
if j != p:
mark[i]+= mark[j]
ans += min(mark[i], 2*k - mark[i])
print(ans)
n,k = put()
l = list(put())
edge = defaultdict()
tree = [[] for i in range(n+1)]
mark = [0]*(n+1)
for i in l:
mark[i]=1
for _ in range(n-1):
x,y = put()
tree[x].append(y)
tree[y].append(x)
dfs()
``` | output | 1 | 8,025 | 1 | 16,051 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town.
In Treeland there are 2k universities which are located in different towns.
Recently, the president signed the decree to connect universities by high-speed network.The Ministry of Education understood the decree in its own way and decided that it was enough to connect each university with another one by using a cable. Formally, the decree will be done!
To have the maximum sum in the budget, the Ministry decided to divide universities into pairs so that the total length of the required cable will be maximum. In other words, the total distance between universities in k pairs should be as large as possible.
Help the Ministry to find the maximum total distance. Of course, each university should be present in only one pair. Consider that all roads have the same length which is equal to 1.
Input
The first line of the input contains two integers n and k (2 β€ n β€ 200 000, 1 β€ k β€ n / 2) β the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to n.
The second line contains 2k distinct integers u1, u2, ..., u2k (1 β€ ui β€ n) β indices of towns in which universities are located.
The next n - 1 line contains the description of roads. Each line contains the pair of integers xj and yj (1 β€ xj, yj β€ n), which means that the j-th road connects towns xj and yj. All of them are two-way roads. You can move from any town to any other using only these roads.
Output
Print the maximum possible sum of distances in the division of universities into k pairs.
Examples
Input
7 2
1 5 6 2
1 3
3 2
4 5
3 7
4 3
4 6
Output
6
Input
9 3
3 2 1 6 5 9
8 9
3 2
2 7
3 4
7 6
4 5
2 1
2 8
Output
9
Note
The figure below shows one of possible division into pairs in the first test. If you connect universities number 1 and 6 (marked in red) and universities number 2 and 5 (marked in blue) by using the cable, the total distance will equal 6 which will be the maximum sum in this example.
<image> | instruction | 0 | 8,026 | 1 | 16,052 |
Tags: dfs and similar, dp, graphs, trees
Correct Solution:
```
def bfs(source):
q = [0] * (n + 1);
fa = [-1] * n
l, r = [1] * 2
fa[source] = source
q[1] = source
while l <= r:
x = q[l]
l += 1
for y in e[x]:
if fa[y] == -1:
fa[y] = x
r += 1
q[r] = y
i = r;
while i >= 1:
x = q[i]
for y in e[x]:
if fa[y] == x:
sum[x] += sum[y]
dp[x] += dp[y] + min(sum[y], m - sum[y])
i -= 1
n, m =[int(x) for x in input().split()]
m <<= 1
t = [int(x) for x in input().split()]
e = [list() for i in range(n)]
sum = [0] * n
dp = [0] * n
#print(len(e), e)
for i in range(n - 1):
x, y = [int(a) for a in input().split()]
e[x - 1].append(y - 1)
e[y - 1].append(x - 1)
for x in t:
sum[x - 1] = 1
bfs(0)
print(dp[0])
``` | output | 1 | 8,026 | 1 | 16,053 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town.
In Treeland there are 2k universities which are located in different towns.
Recently, the president signed the decree to connect universities by high-speed network.The Ministry of Education understood the decree in its own way and decided that it was enough to connect each university with another one by using a cable. Formally, the decree will be done!
To have the maximum sum in the budget, the Ministry decided to divide universities into pairs so that the total length of the required cable will be maximum. In other words, the total distance between universities in k pairs should be as large as possible.
Help the Ministry to find the maximum total distance. Of course, each university should be present in only one pair. Consider that all roads have the same length which is equal to 1.
Input
The first line of the input contains two integers n and k (2 β€ n β€ 200 000, 1 β€ k β€ n / 2) β the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to n.
The second line contains 2k distinct integers u1, u2, ..., u2k (1 β€ ui β€ n) β indices of towns in which universities are located.
The next n - 1 line contains the description of roads. Each line contains the pair of integers xj and yj (1 β€ xj, yj β€ n), which means that the j-th road connects towns xj and yj. All of them are two-way roads. You can move from any town to any other using only these roads.
Output
Print the maximum possible sum of distances in the division of universities into k pairs.
Examples
Input
7 2
1 5 6 2
1 3
3 2
4 5
3 7
4 3
4 6
Output
6
Input
9 3
3 2 1 6 5 9
8 9
3 2
2 7
3 4
7 6
4 5
2 1
2 8
Output
9
Note
The figure below shows one of possible division into pairs in the first test. If you connect universities number 1 and 6 (marked in red) and universities number 2 and 5 (marked in blue) by using the cable, the total distance will equal 6 which will be the maximum sum in this example.
<image> | instruction | 0 | 8,027 | 1 | 16,054 |
Tags: dfs and similar, dp, graphs, trees
Correct Solution:
```
n, k = map(int, input().split())
s = [0] * n
for i in map(int, input().split()):
s[i - 1] = 1
e = [[] for _ in range(n)]
for _ in range(n - 1):
x, y = (int(s) - 1 for s in input().split())
e[x].append(y)
e[y].append(x)
q, fa = [0], [-1] * n
fa[0] = 0
for i in range(n):
x = q[i]
for y in e[x]:
if fa[y] == -1:
fa[y] = x
q.append(y)
dp, k2 = [0] * n, k * 2
for x in reversed(q):
for y in e[x]:
if fa[y] == x:
i = s[y]
s[x] += i
dp[x] += dp[y] + (k2 - i if i > k else i)
print(dp[0])
``` | output | 1 | 8,027 | 1 | 16,055 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town.
In Treeland there are 2k universities which are located in different towns.
Recently, the president signed the decree to connect universities by high-speed network.The Ministry of Education understood the decree in its own way and decided that it was enough to connect each university with another one by using a cable. Formally, the decree will be done!
To have the maximum sum in the budget, the Ministry decided to divide universities into pairs so that the total length of the required cable will be maximum. In other words, the total distance between universities in k pairs should be as large as possible.
Help the Ministry to find the maximum total distance. Of course, each university should be present in only one pair. Consider that all roads have the same length which is equal to 1.
Input
The first line of the input contains two integers n and k (2 β€ n β€ 200 000, 1 β€ k β€ n / 2) β the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to n.
The second line contains 2k distinct integers u1, u2, ..., u2k (1 β€ ui β€ n) β indices of towns in which universities are located.
The next n - 1 line contains the description of roads. Each line contains the pair of integers xj and yj (1 β€ xj, yj β€ n), which means that the j-th road connects towns xj and yj. All of them are two-way roads. You can move from any town to any other using only these roads.
Output
Print the maximum possible sum of distances in the division of universities into k pairs.
Examples
Input
7 2
1 5 6 2
1 3
3 2
4 5
3 7
4 3
4 6
Output
6
Input
9 3
3 2 1 6 5 9
8 9
3 2
2 7
3 4
7 6
4 5
2 1
2 8
Output
9
Note
The figure below shows one of possible division into pairs in the first test. If you connect universities number 1 and 6 (marked in red) and universities number 2 and 5 (marked in blue) by using the cable, the total distance will equal 6 which will be the maximum sum in this example.
<image> | instruction | 0 | 8,028 | 1 | 16,056 |
Tags: dfs and similar, dp, graphs, trees
Correct Solution:
```
WHITE = 0
GRAY = 1
BLACK = 2
def dfs_iter(G,u=0):
stack = []
stack.append({"u":u,"v":0,"started": False})
while len(stack) != 0 :
current = stack[len(stack)-1]
u = current["u"]
v_index = current["v"]
started = current["started"]
if started:
##despues de que se llama recursivo:
s_v[u]+=s_v[G[u][v_index]] #osea a s_v[u] le suma el valor de su vertice adyacente que se calculo despues de la recursividad#
##
current["v"] += 1
current["started"] = False
continue
if v_index == len(G[u]):
stack.pop()
color[u] = BLACK
continue
color[u] =GRAY
v=G[u][v_index]
## to do:
if color[v] == WHITE:
s = {"u":v, "v":0, "started":False}
current["started"] = True
stack.append(s)
continue
###
current["v"]+=1
def dfs_calc_s(G,u = 0):
color[u] = GRAY
s_v[u] = 1 if uni[u] else 0
for v in G[u]:
if color[v] == WHITE:
dfs_calc_s(G,v)
s_v[u] += s_v[v]
def solution(G, k):
sol = 0
for v in range(len(G)):
sol += min(s_v[v], 2*k - s_v[v])
return sol
def main():
s = input().split(" ")
n = int(s[0])
k = int(s[1])
global uni, color, s_v
uni = [False for i in range(n)]
color = [0] * n
s_v = [0] * n
s = input().split(" ")
for v in s:
a = int(v)-1
uni[a] = True
s_v[a] = 1
G = [[] for _ in range(n)]
for _ in range(n-1):
s = input().split(" ")
a = int(s[0])-1
b = int(s[1])-1
G[a].append(b)
G[b].append(a)
dfs_iter(G)
print(solution(G,k))
try:
main()
except Exception as e:
print(str(e).replace(" ","_"))
``` | output | 1 | 8,028 | 1 | 16,057 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town.
In Treeland there are 2k universities which are located in different towns.
Recently, the president signed the decree to connect universities by high-speed network.The Ministry of Education understood the decree in its own way and decided that it was enough to connect each university with another one by using a cable. Formally, the decree will be done!
To have the maximum sum in the budget, the Ministry decided to divide universities into pairs so that the total length of the required cable will be maximum. In other words, the total distance between universities in k pairs should be as large as possible.
Help the Ministry to find the maximum total distance. Of course, each university should be present in only one pair. Consider that all roads have the same length which is equal to 1.
Input
The first line of the input contains two integers n and k (2 β€ n β€ 200 000, 1 β€ k β€ n / 2) β the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to n.
The second line contains 2k distinct integers u1, u2, ..., u2k (1 β€ ui β€ n) β indices of towns in which universities are located.
The next n - 1 line contains the description of roads. Each line contains the pair of integers xj and yj (1 β€ xj, yj β€ n), which means that the j-th road connects towns xj and yj. All of them are two-way roads. You can move from any town to any other using only these roads.
Output
Print the maximum possible sum of distances in the division of universities into k pairs.
Examples
Input
7 2
1 5 6 2
1 3
3 2
4 5
3 7
4 3
4 6
Output
6
Input
9 3
3 2 1 6 5 9
8 9
3 2
2 7
3 4
7 6
4 5
2 1
2 8
Output
9
Note
The figure below shows one of possible division into pairs in the first test. If you connect universities number 1 and 6 (marked in red) and universities number 2 and 5 (marked in blue) by using the cable, the total distance will equal 6 which will be the maximum sum in this example.
<image> | instruction | 0 | 8,029 | 1 | 16,058 |
Tags: dfs and similar, dp, graphs, trees
Correct Solution:
```
def parser():
return [int(x) for x in input().split(" ")]
def DFS():
visited[0]=True
stack=[]
intruduction_order=[]
stack.append(0)
while len(stack)>0:
v=stack.pop()
for u in adjacents_list[v]:
if not visited[u]:
pi[u]=v
visited[u]=True
if university_in_city[u]:
count_universities_subtree[u]+=1
stack.append(u)
intruduction_order.append(u)
for v in intruduction_order[::-1]:
count_universities_subtree[pi[v]]+=count_universities_subtree[v]
n,k=parser()
visited=[False for x in range(n)]
pi=[0 for x in range(n)]
count_universities_subtree=[0 for x in range(n)]
university_in_city=[False for x in range(n)]
cities_universities=parser()
for i in range(2*k):
university_in_city[cities_universities[i]-1]=True
adjacents_list=[[] for x in range(n)]
for i in range(n-1):
edge=parser()
adjacents_list[edge[0]-1].append(edge[1]-1)
adjacents_list[edge[1]-1].append(edge[0]-1)
DFS()
total=0
for i in range(1,n):
total+=min(count_universities_subtree[i],2*k-count_universities_subtree[i])
print(total)
``` | output | 1 | 8,029 | 1 | 16,059 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town.
In Treeland there are 2k universities which are located in different towns.
Recently, the president signed the decree to connect universities by high-speed network.The Ministry of Education understood the decree in its own way and decided that it was enough to connect each university with another one by using a cable. Formally, the decree will be done!
To have the maximum sum in the budget, the Ministry decided to divide universities into pairs so that the total length of the required cable will be maximum. In other words, the total distance between universities in k pairs should be as large as possible.
Help the Ministry to find the maximum total distance. Of course, each university should be present in only one pair. Consider that all roads have the same length which is equal to 1.
Input
The first line of the input contains two integers n and k (2 β€ n β€ 200 000, 1 β€ k β€ n / 2) β the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to n.
The second line contains 2k distinct integers u1, u2, ..., u2k (1 β€ ui β€ n) β indices of towns in which universities are located.
The next n - 1 line contains the description of roads. Each line contains the pair of integers xj and yj (1 β€ xj, yj β€ n), which means that the j-th road connects towns xj and yj. All of them are two-way roads. You can move from any town to any other using only these roads.
Output
Print the maximum possible sum of distances in the division of universities into k pairs.
Examples
Input
7 2
1 5 6 2
1 3
3 2
4 5
3 7
4 3
4 6
Output
6
Input
9 3
3 2 1 6 5 9
8 9
3 2
2 7
3 4
7 6
4 5
2 1
2 8
Output
9
Note
The figure below shows one of possible division into pairs in the first test. If you connect universities number 1 and 6 (marked in red) and universities number 2 and 5 (marked in blue) by using the cable, the total distance will equal 6 which will be the maximum sum in this example.
<image> | instruction | 0 | 8,030 | 1 | 16,060 |
Tags: dfs and similar, dp, graphs, trees
Correct Solution:
```
#parsea una lΓnea
def parser():
return [int(x) for x in input().split()]
#MΓ©todo usado para contar las universidades en cada subΓ‘rbol
def DFS():
visited[0]=True
stack=[]
introduction_order=[]
stack.append(0)
while len(stack)>0:
v=stack.pop()
for u in adjacents_list[v]:
if not visited[u]:
pi[u]=v
visited[u]=True
if university_in_city[u]:
count_universities_subtree[u]+=1
stack.append(u)
introduction_order.append(u)
#Recorriendo para saber la cantidad de universidades que hay en el subarbol de cada vertice
for v in introduction_order[::-1]:
count_universities_subtree[pi[v]]+=count_universities_subtree[v]
#Recibiendo los valores de n y k
n,k=parser()
#visitados
visited=[False for x in range(n)]
#padres
pi=[0 for x in range(n)]
#universidades en el subarbol
count_universities_subtree=[0 for x in range(n)]
#universidad en la ciudad
university_in_city=[False for x in range(n)]
#Recibiendo las ciudades que tienen universidades
cities_universities=parser()
for i in cities_universities:
university_in_city[i-1]=True
#Armando el Γ‘rbol que representa a Treeland
adjacents_list=[[] for x in range(n)]
for i in range(n-1):
#Leyendo una arista
edge=parser()
adjacents_list[edge[0]-1].append(edge[1]-1)
adjacents_list[edge[1]-1].append(edge[0]-1)
DFS()
#Calculando el total
total=0
for i in range(1,n):
total+=min(count_universities_subtree[i],2*k-count_universities_subtree[i])
#Imprimiendo el resultado
print(total)
``` | output | 1 | 8,030 | 1 | 16,061 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town.
In Treeland there are 2k universities which are located in different towns.
Recently, the president signed the decree to connect universities by high-speed network.The Ministry of Education understood the decree in its own way and decided that it was enough to connect each university with another one by using a cable. Formally, the decree will be done!
To have the maximum sum in the budget, the Ministry decided to divide universities into pairs so that the total length of the required cable will be maximum. In other words, the total distance between universities in k pairs should be as large as possible.
Help the Ministry to find the maximum total distance. Of course, each university should be present in only one pair. Consider that all roads have the same length which is equal to 1.
Input
The first line of the input contains two integers n and k (2 β€ n β€ 200 000, 1 β€ k β€ n / 2) β the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to n.
The second line contains 2k distinct integers u1, u2, ..., u2k (1 β€ ui β€ n) β indices of towns in which universities are located.
The next n - 1 line contains the description of roads. Each line contains the pair of integers xj and yj (1 β€ xj, yj β€ n), which means that the j-th road connects towns xj and yj. All of them are two-way roads. You can move from any town to any other using only these roads.
Output
Print the maximum possible sum of distances in the division of universities into k pairs.
Examples
Input
7 2
1 5 6 2
1 3
3 2
4 5
3 7
4 3
4 6
Output
6
Input
9 3
3 2 1 6 5 9
8 9
3 2
2 7
3 4
7 6
4 5
2 1
2 8
Output
9
Note
The figure below shows one of possible division into pairs in the first test. If you connect universities number 1 and 6 (marked in red) and universities number 2 and 5 (marked in blue) by using the cable, the total distance will equal 6 which will be the maximum sum in this example.
<image> | instruction | 0 | 8,031 | 1 | 16,062 |
Tags: dfs and similar, dp, graphs, trees
Correct Solution:
```
def main():
n, k = map(int, input().split())
s = [0] * n
for i in map(int, input().split()):
s[i - 1] = 1
e = [[] for _ in range(n)]
for _ in range(n - 1):
x, y = (int(s) - 1 for s in input().split())
e[x].append(y)
e[y].append(x)
q, fa = [0], [-1] * n
fa[0] = 0
for i in range(n):
x = q[i]
for y in e[x]:
if fa[y] == -1:
fa[y] = x
q.append(y)
dp, k2 = [0] * n, k * 2
for x in reversed(q):
for y in e[x]:
if fa[y] == x:
i = s[y]
s[x] += i
dp[x] += dp[y] + (k2 - i if i > k else i)
print(dp[0])
if __name__ == "__main__":
main()
``` | output | 1 | 8,031 | 1 | 16,063 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town.
In Treeland there are 2k universities which are located in different towns.
Recently, the president signed the decree to connect universities by high-speed network.The Ministry of Education understood the decree in its own way and decided that it was enough to connect each university with another one by using a cable. Formally, the decree will be done!
To have the maximum sum in the budget, the Ministry decided to divide universities into pairs so that the total length of the required cable will be maximum. In other words, the total distance between universities in k pairs should be as large as possible.
Help the Ministry to find the maximum total distance. Of course, each university should be present in only one pair. Consider that all roads have the same length which is equal to 1.
Input
The first line of the input contains two integers n and k (2 β€ n β€ 200 000, 1 β€ k β€ n / 2) β the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to n.
The second line contains 2k distinct integers u1, u2, ..., u2k (1 β€ ui β€ n) β indices of towns in which universities are located.
The next n - 1 line contains the description of roads. Each line contains the pair of integers xj and yj (1 β€ xj, yj β€ n), which means that the j-th road connects towns xj and yj. All of them are two-way roads. You can move from any town to any other using only these roads.
Output
Print the maximum possible sum of distances in the division of universities into k pairs.
Examples
Input
7 2
1 5 6 2
1 3
3 2
4 5
3 7
4 3
4 6
Output
6
Input
9 3
3 2 1 6 5 9
8 9
3 2
2 7
3 4
7 6
4 5
2 1
2 8
Output
9
Note
The figure below shows one of possible division into pairs in the first test. If you connect universities number 1 and 6 (marked in red) and universities number 2 and 5 (marked in blue) by using the cable, the total distance will equal 6 which will be the maximum sum in this example.
<image> | instruction | 0 | 8,032 | 1 | 16,064 |
Tags: dfs and similar, dp, graphs, trees
Correct Solution:
```
from collections import defaultdict
from sys import stdin
def put(): return map(int, stdin.readline().split())
def dfs():
s = [(1,0)]
ans = 0
vis = [0]*(n+1)
while s:
i,p = s.pop()
if vis[i]==0:
vis[i]=1
s.append((i,p))
for j in tree[i]:
if j!=p:
s.append((j,i))
elif vis[i]==1:
vis[i]=2
for j in tree[i]:
if j != p:
mark[i]+= mark[j]
ans += min(mark[i], 2*k - mark[i])
print(ans)
n,k = put()
l = list(put())
edge = defaultdict()
tree = [[] for i in range(n+1)]
mark = [0]*(n+1)
for i in l:
mark[i]=1
for _ in range(n-1):
x,y = put()
tree[x].append(y)
tree[y].append(x)
dfs()
``` | output | 1 | 8,032 | 1 | 16,065 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town.
In Treeland there are 2k universities which are located in different towns.
Recently, the president signed the decree to connect universities by high-speed network.The Ministry of Education understood the decree in its own way and decided that it was enough to connect each university with another one by using a cable. Formally, the decree will be done!
To have the maximum sum in the budget, the Ministry decided to divide universities into pairs so that the total length of the required cable will be maximum. In other words, the total distance between universities in k pairs should be as large as possible.
Help the Ministry to find the maximum total distance. Of course, each university should be present in only one pair. Consider that all roads have the same length which is equal to 1.
Input
The first line of the input contains two integers n and k (2 β€ n β€ 200 000, 1 β€ k β€ n / 2) β the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to n.
The second line contains 2k distinct integers u1, u2, ..., u2k (1 β€ ui β€ n) β indices of towns in which universities are located.
The next n - 1 line contains the description of roads. Each line contains the pair of integers xj and yj (1 β€ xj, yj β€ n), which means that the j-th road connects towns xj and yj. All of them are two-way roads. You can move from any town to any other using only these roads.
Output
Print the maximum possible sum of distances in the division of universities into k pairs.
Examples
Input
7 2
1 5 6 2
1 3
3 2
4 5
3 7
4 3
4 6
Output
6
Input
9 3
3 2 1 6 5 9
8 9
3 2
2 7
3 4
7 6
4 5
2 1
2 8
Output
9
Note
The figure below shows one of possible division into pairs in the first test. If you connect universities number 1 and 6 (marked in red) and universities number 2 and 5 (marked in blue) by using the cable, the total distance will equal 6 which will be the maximum sum in this example.
<image>
Submitted Solution:
```
# [https://codeforces.com/contest/700/submission/19421380]
(n, k) = map(int, input().split())
s = [0] * n
for i in map(int, input().split()):
s[i - 1] = 1
e = [[] for _ in range(n)]
for _ in range(n - 1):
(x, y) = (int(s) - 1 for s in input().split())
e[x].append(y)
e[y].append(x)
q = [0]
fa = [-1] * n
fa[0] = 0
for i in range(n):
x = q[i]
for y in e[x]:
if fa[y] == -1:
fa[y] = x
q.append(y)
dp = [0] * n
k2 = k * 2
for x in reversed(q):
for y in e[x]:
if fa[y] == x:
i = s[y]
s[x] += i
dp[x] += dp[y] + (k2 - i if i > k else i)
print(dp[0])
``` | instruction | 0 | 8,033 | 1 | 16,066 |
Yes | output | 1 | 8,033 | 1 | 16,067 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town.
In Treeland there are 2k universities which are located in different towns.
Recently, the president signed the decree to connect universities by high-speed network.The Ministry of Education understood the decree in its own way and decided that it was enough to connect each university with another one by using a cable. Formally, the decree will be done!
To have the maximum sum in the budget, the Ministry decided to divide universities into pairs so that the total length of the required cable will be maximum. In other words, the total distance between universities in k pairs should be as large as possible.
Help the Ministry to find the maximum total distance. Of course, each university should be present in only one pair. Consider that all roads have the same length which is equal to 1.
Input
The first line of the input contains two integers n and k (2 β€ n β€ 200 000, 1 β€ k β€ n / 2) β the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to n.
The second line contains 2k distinct integers u1, u2, ..., u2k (1 β€ ui β€ n) β indices of towns in which universities are located.
The next n - 1 line contains the description of roads. Each line contains the pair of integers xj and yj (1 β€ xj, yj β€ n), which means that the j-th road connects towns xj and yj. All of them are two-way roads. You can move from any town to any other using only these roads.
Output
Print the maximum possible sum of distances in the division of universities into k pairs.
Examples
Input
7 2
1 5 6 2
1 3
3 2
4 5
3 7
4 3
4 6
Output
6
Input
9 3
3 2 1 6 5 9
8 9
3 2
2 7
3 4
7 6
4 5
2 1
2 8
Output
9
Note
The figure below shows one of possible division into pairs in the first test. If you connect universities number 1 and 6 (marked in red) and universities number 2 and 5 (marked in blue) by using the cable, the total distance will equal 6 which will be the maximum sum in this example.
<image>
Submitted Solution:
```
def bfs(source):
q = [0] * (n + 1);
fa = [-1] * n
l, r = [1] * 2
fa[source] = source
q[1] = source
while l <= r:
x = q[l]
l += 1
for y in e[x]:
if fa[y] == -1:
fa[y] = x
r += 1
q[r] = y
i = r;
while i >= 1:
x = q[i]
for y in e[x]:
if fa[y] == x:
sum[x] += sum[y]
dp[x] += dp[y] + min(sum[y], m - sum[y])
i -= 1
n, m =[int(x) for x in input().split()]
m <<= 1
t = [int(x) for x in input().split()]
e = [list() for i in range(n)]
sum = [0] * n
dp = [0] * n
#print(len(e), e)
for i in range(n - 1):
x, y = [int(a) for a in input().split()]
e[x - 1].append(y - 1)
e[y - 1].append(x - 1)
for x in t:
sum[x - 1] = 1
bfs(0)
print(dp[0])
# Made By Mostafa_Khaled
``` | instruction | 0 | 8,034 | 1 | 16,068 |
Yes | output | 1 | 8,034 | 1 | 16,069 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town.
In Treeland there are 2k universities which are located in different towns.
Recently, the president signed the decree to connect universities by high-speed network.The Ministry of Education understood the decree in its own way and decided that it was enough to connect each university with another one by using a cable. Formally, the decree will be done!
To have the maximum sum in the budget, the Ministry decided to divide universities into pairs so that the total length of the required cable will be maximum. In other words, the total distance between universities in k pairs should be as large as possible.
Help the Ministry to find the maximum total distance. Of course, each university should be present in only one pair. Consider that all roads have the same length which is equal to 1.
Input
The first line of the input contains two integers n and k (2 β€ n β€ 200 000, 1 β€ k β€ n / 2) β the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to n.
The second line contains 2k distinct integers u1, u2, ..., u2k (1 β€ ui β€ n) β indices of towns in which universities are located.
The next n - 1 line contains the description of roads. Each line contains the pair of integers xj and yj (1 β€ xj, yj β€ n), which means that the j-th road connects towns xj and yj. All of them are two-way roads. You can move from any town to any other using only these roads.
Output
Print the maximum possible sum of distances in the division of universities into k pairs.
Examples
Input
7 2
1 5 6 2
1 3
3 2
4 5
3 7
4 3
4 6
Output
6
Input
9 3
3 2 1 6 5 9
8 9
3 2
2 7
3 4
7 6
4 5
2 1
2 8
Output
9
Note
The figure below shows one of possible division into pairs in the first test. If you connect universities number 1 and 6 (marked in red) and universities number 2 and 5 (marked in blue) by using the cable, the total distance will equal 6 which will be the maximum sum in this example.
<image>
Submitted Solution:
```
a = input()
if a[0] == '7':
print(6)
else:
print(9)
``` | instruction | 0 | 8,035 | 1 | 16,070 |
No | output | 1 | 8,035 | 1 | 16,071 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town.
In Treeland there are 2k universities which are located in different towns.
Recently, the president signed the decree to connect universities by high-speed network.The Ministry of Education understood the decree in its own way and decided that it was enough to connect each university with another one by using a cable. Formally, the decree will be done!
To have the maximum sum in the budget, the Ministry decided to divide universities into pairs so that the total length of the required cable will be maximum. In other words, the total distance between universities in k pairs should be as large as possible.
Help the Ministry to find the maximum total distance. Of course, each university should be present in only one pair. Consider that all roads have the same length which is equal to 1.
Input
The first line of the input contains two integers n and k (2 β€ n β€ 200 000, 1 β€ k β€ n / 2) β the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to n.
The second line contains 2k distinct integers u1, u2, ..., u2k (1 β€ ui β€ n) β indices of towns in which universities are located.
The next n - 1 line contains the description of roads. Each line contains the pair of integers xj and yj (1 β€ xj, yj β€ n), which means that the j-th road connects towns xj and yj. All of them are two-way roads. You can move from any town to any other using only these roads.
Output
Print the maximum possible sum of distances in the division of universities into k pairs.
Examples
Input
7 2
1 5 6 2
1 3
3 2
4 5
3 7
4 3
4 6
Output
6
Input
9 3
3 2 1 6 5 9
8 9
3 2
2 7
3 4
7 6
4 5
2 1
2 8
Output
9
Note
The figure below shows one of possible division into pairs in the first test. If you connect universities number 1 and 6 (marked in red) and universities number 2 and 5 (marked in blue) by using the cable, the total distance will equal 6 which will be the maximum sum in this example.
<image>
Submitted Solution:
```
a = input()
if a[0] == 7:
print(6)
else:
print(9)
``` | instruction | 0 | 8,036 | 1 | 16,072 |
No | output | 1 | 8,036 | 1 | 16,073 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town.
In Treeland there are 2k universities which are located in different towns.
Recently, the president signed the decree to connect universities by high-speed network.The Ministry of Education understood the decree in its own way and decided that it was enough to connect each university with another one by using a cable. Formally, the decree will be done!
To have the maximum sum in the budget, the Ministry decided to divide universities into pairs so that the total length of the required cable will be maximum. In other words, the total distance between universities in k pairs should be as large as possible.
Help the Ministry to find the maximum total distance. Of course, each university should be present in only one pair. Consider that all roads have the same length which is equal to 1.
Input
The first line of the input contains two integers n and k (2 β€ n β€ 200 000, 1 β€ k β€ n / 2) β the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to n.
The second line contains 2k distinct integers u1, u2, ..., u2k (1 β€ ui β€ n) β indices of towns in which universities are located.
The next n - 1 line contains the description of roads. Each line contains the pair of integers xj and yj (1 β€ xj, yj β€ n), which means that the j-th road connects towns xj and yj. All of them are two-way roads. You can move from any town to any other using only these roads.
Output
Print the maximum possible sum of distances in the division of universities into k pairs.
Examples
Input
7 2
1 5 6 2
1 3
3 2
4 5
3 7
4 3
4 6
Output
6
Input
9 3
3 2 1 6 5 9
8 9
3 2
2 7
3 4
7 6
4 5
2 1
2 8
Output
9
Note
The figure below shows one of possible division into pairs in the first test. If you connect universities number 1 and 6 (marked in red) and universities number 2 and 5 (marked in blue) by using the cable, the total distance will equal 6 which will be the maximum sum in this example.
<image>
Submitted Solution:
```
n, k = [int(x) for x in input().split(' ')]
arr = list(map(int, input().split(' ')))
pairs = []
for i in range(k):
pairs.append([arr[i], arr[i+k]])
linked_list = {}
for i in range(n-1):
a, b = [int(x) for x in input().split(' ')]
if a not in linked_list:
linked_list[a] = [b]
else:
linked_list[a].append(b)
if b not in linked_list:
linked_list[b] = [a]
else:
linked_list[b].append(a)
visited = set()
def dfs(i, j):
global visited
if i == j:
return 0
if i in visited:
return -1
visited.add(i)
for k in linked_list[i]:
ans = dfs(k, j)
if ans != -1:
return ans+1
return -1
tot = 0
for i in pairs:
tot += dfs(*i)
visited = set()
print(tot)
``` | instruction | 0 | 8,037 | 1 | 16,074 |
No | output | 1 | 8,037 | 1 | 16,075 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town.
In Treeland there are 2k universities which are located in different towns.
Recently, the president signed the decree to connect universities by high-speed network.The Ministry of Education understood the decree in its own way and decided that it was enough to connect each university with another one by using a cable. Formally, the decree will be done!
To have the maximum sum in the budget, the Ministry decided to divide universities into pairs so that the total length of the required cable will be maximum. In other words, the total distance between universities in k pairs should be as large as possible.
Help the Ministry to find the maximum total distance. Of course, each university should be present in only one pair. Consider that all roads have the same length which is equal to 1.
Input
The first line of the input contains two integers n and k (2 β€ n β€ 200 000, 1 β€ k β€ n / 2) β the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to n.
The second line contains 2k distinct integers u1, u2, ..., u2k (1 β€ ui β€ n) β indices of towns in which universities are located.
The next n - 1 line contains the description of roads. Each line contains the pair of integers xj and yj (1 β€ xj, yj β€ n), which means that the j-th road connects towns xj and yj. All of them are two-way roads. You can move from any town to any other using only these roads.
Output
Print the maximum possible sum of distances in the division of universities into k pairs.
Examples
Input
7 2
1 5 6 2
1 3
3 2
4 5
3 7
4 3
4 6
Output
6
Input
9 3
3 2 1 6 5 9
8 9
3 2
2 7
3 4
7 6
4 5
2 1
2 8
Output
9
Note
The figure below shows one of possible division into pairs in the first test. If you connect universities number 1 and 6 (marked in red) and universities number 2 and 5 (marked in blue) by using the cable, the total distance will equal 6 which will be the maximum sum in this example.
<image>
Submitted Solution:
```
import sys
sys.setrecursionlimit(10**9)
epicdebug = 0
tree = []
tin = []
tout = []
p = []
h = []
used = []
timer = 1
def dfs(v, par):
global timer
used[v] = True
tin[v] = timer;
h[v] = h[par] + 1
timer += 1
p[v][0] = par
for i in range(1, 18):
p[v][i] = p[p[v][i - 1]][i - 1]
for i in range(len(tree[v])):
to = tree[v][i];
if not used[to]:
dfs(to, v)
tout[v] = timer;
timer += 1
def isAncestor(u, v):
if tin[u] <= tin[v] and tout[u] >= tout[v]: return True
return False
def getLCA(u, v):
if isAncestor(u, v): return u
if isAncestor(v, u): return v
for i in range(17, -1, -1):
if not isAncestor(p[u][i], v):
u = p[u][i]
return p[u][0]
def pathLen(u, v):
return h[u] + h[v] - 2 * h[getLCA(u, v)]
try:
n, k = map(int, input().split())
if n == 200000 and k == 100000:
epicdebug = 1
tree = [[] for i in range(n)]
tin = [0] * n
tout = [0] * n
h = [0] * n
used = [False] * n
p = [[0] * 18 for i in range(n)]
k *= 2
good = list(map(int, input().split()))
for i in range(len(good)):
good[i] -= 1
for i in range(n - 1):
u, v = map(int, input().split())
u -= 1
v -= 1
tree[u].append(v)
tree[v].append(u)
if epicdebug:
exit(0)
dfs(0, 0)
if epicdebug:
exit(0)
for i in range(len(good)):
good[i] = (tin[good[i]], good[i])
good.sort()
res = 0
j = len(good) // 2
for i in range(len(good) // 2):
res += pathLen(good[i][1], good[j][1])
j += 1
print(res)
except Exception as e:
print(str(e))
``` | instruction | 0 | 8,038 | 1 | 16,076 |
No | output | 1 | 8,038 | 1 | 16,077 |
Provide a correct Python 3 solution for this coding contest problem.
There are N towns in Takahashi Kingdom. They are conveniently numbered 1 through N.
Takahashi the king is planning to go on a tour of inspection for M days. He will determine a sequence of towns c, and visit town c_i on the i-th day. That is, on the i-th day, he will travel from his current location to town c_i. If he is already at town c_i, he will stay at that town. His location just before the beginning of the tour is town 1, the capital. The tour ends at town c_M, without getting back to the capital.
The problem is that there is no paved road in this kingdom. He decided to resolve this issue by paving the road himself while traveling. When he travels from town a to town b, there will be a newly paved one-way road from town a to town b.
Since he cares for his people, he wants the following condition to be satisfied after his tour is over: "it is possible to travel from any town to any other town by traversing roads paved by him". How many sequences of towns c satisfy this condition?
Constraints
* 2β¦Nβ¦300
* 1β¦Mβ¦300
Input
The input is given from Standard Input in the following format:
N M
Output
Print the number of sequences of towns satisfying the condition, modulo 1000000007 (=10^9+7).
Examples
Input
3 3
Output
2
Input
150 300
Output
734286322
Input
300 150
Output
0 | instruction | 0 | 8,947 | 1 | 17,894 |
"Correct Solution:
```
mod=10**9+7
N,M=map(int,input().split())
dp=[[0 for i in range(N+1)] for j in range(N+1)]
dp[N][N]=1
for i in range(M-1,-1,-1):
for k in range(1,N+1):
for j in range(k,N+1):
if j==k:
if j<N:
dp[j][k]=(j*dp[j][k]+(N-j)*dp[j+1][k])%mod
else:
dp[j][k]=(N*dp[j][k])%mod
else:
if j<N:
dp[j][k]=(k*dp[j][j]+(N-j)*dp[j+1][k]+(j-k)*dp[j][k])%mod
else:
dp[j][k]=(k*dp[j][j]+(N-k)*dp[j][k])%mod
print(dp[1][1])
``` | output | 1 | 8,947 | 1 | 17,895 |
Provide a correct Python 3 solution for this coding contest problem.
There are N towns in Takahashi Kingdom. They are conveniently numbered 1 through N.
Takahashi the king is planning to go on a tour of inspection for M days. He will determine a sequence of towns c, and visit town c_i on the i-th day. That is, on the i-th day, he will travel from his current location to town c_i. If he is already at town c_i, he will stay at that town. His location just before the beginning of the tour is town 1, the capital. The tour ends at town c_M, without getting back to the capital.
The problem is that there is no paved road in this kingdom. He decided to resolve this issue by paving the road himself while traveling. When he travels from town a to town b, there will be a newly paved one-way road from town a to town b.
Since he cares for his people, he wants the following condition to be satisfied after his tour is over: "it is possible to travel from any town to any other town by traversing roads paved by him". How many sequences of towns c satisfy this condition?
Constraints
* 2β¦Nβ¦300
* 1β¦Mβ¦300
Input
The input is given from Standard Input in the following format:
N M
Output
Print the number of sequences of towns satisfying the condition, modulo 1000000007 (=10^9+7).
Examples
Input
3 3
Output
2
Input
150 300
Output
734286322
Input
300 150
Output
0 | instruction | 0 | 8,948 | 1 | 17,896 |
"Correct Solution:
```
mod = 1000000007
eps = 10**-9
def main():
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
dp = [[0] * N for _ in range(N+1)]
dp[1][0] = 1
for i in range(M):
dp_new = [[0] * N for _ in range(N+1)]
for j in range(N+1):
for k in range(N):
dp_new[j][k] = (dp_new[j][k] + dp[j][k] * k)%mod
if k+1 < N:
dp_new[j][k+1] = (dp_new[j][k+1] + dp[j][k] * (N - j - k))%mod
if j+k <= N:
dp_new[j+k][0] = (dp_new[j+k][0] + dp[j][k] * j)%mod
dp = dp_new
print(dp[N][0])
if __name__ == '__main__':
main()
``` | output | 1 | 8,948 | 1 | 17,897 |
Provide a correct Python 3 solution for this coding contest problem.
There are N towns in Takahashi Kingdom. They are conveniently numbered 1 through N.
Takahashi the king is planning to go on a tour of inspection for M days. He will determine a sequence of towns c, and visit town c_i on the i-th day. That is, on the i-th day, he will travel from his current location to town c_i. If he is already at town c_i, he will stay at that town. His location just before the beginning of the tour is town 1, the capital. The tour ends at town c_M, without getting back to the capital.
The problem is that there is no paved road in this kingdom. He decided to resolve this issue by paving the road himself while traveling. When he travels from town a to town b, there will be a newly paved one-way road from town a to town b.
Since he cares for his people, he wants the following condition to be satisfied after his tour is over: "it is possible to travel from any town to any other town by traversing roads paved by him". How many sequences of towns c satisfy this condition?
Constraints
* 2β¦Nβ¦300
* 1β¦Mβ¦300
Input
The input is given from Standard Input in the following format:
N M
Output
Print the number of sequences of towns satisfying the condition, modulo 1000000007 (=10^9+7).
Examples
Input
3 3
Output
2
Input
150 300
Output
734286322
Input
300 150
Output
0 | instruction | 0 | 8,949 | 1 | 17,898 |
"Correct Solution:
```
n,m = map(int,input().split())
mod = 10**9+7
dp = [[[0 for i in range(j+1)] for j in range(n+1)] for k in range(m+1)]
dp[0][1][1] = 1
for i in range(m):
for j in range(n+1):
for k in range(j+1):
x = dp[i][j][k]
if j < n:
dp[i+1][j+1][k] += x*(n-j)
dp[i+1][j+1][k] %= mod
dp[i+1][j][j] = (dp[i+1][j][j]+x*k)%mod
dp[i+1][j][k] = (dp[i+1][j][k]+x*(j-k))%mod
print(dp[m][n][n])
``` | output | 1 | 8,949 | 1 | 17,899 |
Provide a correct Python 3 solution for this coding contest problem.
There are N towns in Takahashi Kingdom. They are conveniently numbered 1 through N.
Takahashi the king is planning to go on a tour of inspection for M days. He will determine a sequence of towns c, and visit town c_i on the i-th day. That is, on the i-th day, he will travel from his current location to town c_i. If he is already at town c_i, he will stay at that town. His location just before the beginning of the tour is town 1, the capital. The tour ends at town c_M, without getting back to the capital.
The problem is that there is no paved road in this kingdom. He decided to resolve this issue by paving the road himself while traveling. When he travels from town a to town b, there will be a newly paved one-way road from town a to town b.
Since he cares for his people, he wants the following condition to be satisfied after his tour is over: "it is possible to travel from any town to any other town by traversing roads paved by him". How many sequences of towns c satisfy this condition?
Constraints
* 2β¦Nβ¦300
* 1β¦Mβ¦300
Input
The input is given from Standard Input in the following format:
N M
Output
Print the number of sequences of towns satisfying the condition, modulo 1000000007 (=10^9+7).
Examples
Input
3 3
Output
2
Input
150 300
Output
734286322
Input
300 150
Output
0 | instruction | 0 | 8,950 | 1 | 17,900 |
"Correct Solution:
```
import sys
readline = sys.stdin.readline
N, M = map(int, readline().split())
MOD = 10**9+7
dpscc = [[0]*(N+1) for _ in range(N+1)]
dpus = [[0]*(N+1) for _ in range(N+1)]
dpscc[1][0] = 1
for m in range(M):
dpscc2 = [[0]*(N+1) for _ in range(N+1)]
dpus2 = [[0]*(N+1) for _ in range(N+1)]
for i in range(1, N+1):
for j in range(N+1-i):
kscc = dpscc[i][j]
kus = dpus[i][j]
dpscc2[i][j] = (dpscc2[i][j] + i*kscc) % MOD
dpus2[i][j] = (dpus2[i][j] + j*(kus+kscc)) % MOD
dpscc2[i+j][0] = (dpscc2[i+j][0] + i*kus) % MOD
if N-i-j:
dpus2[i][j+1] = (dpus2[i][j+1] + (N-i-j)*(kus+kscc)) % MOD
dpscc = [d[:] for d in dpscc2]
dpus = [d[:] for d in dpus2]
print(dpscc[N][0])
``` | output | 1 | 8,950 | 1 | 17,901 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N towns in Takahashi Kingdom. They are conveniently numbered 1 through N.
Takahashi the king is planning to go on a tour of inspection for M days. He will determine a sequence of towns c, and visit town c_i on the i-th day. That is, on the i-th day, he will travel from his current location to town c_i. If he is already at town c_i, he will stay at that town. His location just before the beginning of the tour is town 1, the capital. The tour ends at town c_M, without getting back to the capital.
The problem is that there is no paved road in this kingdom. He decided to resolve this issue by paving the road himself while traveling. When he travels from town a to town b, there will be a newly paved one-way road from town a to town b.
Since he cares for his people, he wants the following condition to be satisfied after his tour is over: "it is possible to travel from any town to any other town by traversing roads paved by him". How many sequences of towns c satisfy this condition?
Constraints
* 2β¦Nβ¦300
* 1β¦Mβ¦300
Input
The input is given from Standard Input in the following format:
N M
Output
Print the number of sequences of towns satisfying the condition, modulo 1000000007 (=10^9+7).
Examples
Input
3 3
Output
2
Input
150 300
Output
734286322
Input
300 150
Output
0
Submitted Solution:
```
mod=10**9+7
N,M=map(int,input().split())
dp=[[[0 for i in range(N+1)] for j in range(N+1)] for k in range(M+1)]
dp[M][N][N]=1
for i in range(M-1,-1,-1):
for k in range(1,N+1):
for j in range(k,N+1):
if j==k:
if j<N:
dp[i][j][k]=(j*dp[i+1][j][k]+(N-j)*dp[i+1][j+1][k])%mod
else:
dp[i][j][k]=(N*dp[i+1][j][k])%mod
else:
if j<N:
dp[i][j][k]=(k*dp[i+1][j][j]+(N-j)*dp[i+1][j+1][k]+(j-k)*dp[i+1][j][k])%mod
else:
dp[i][j][k]=(k*dp[i+1][j][j]+(N-k)*dp[i+1][j][k])%mod
print(dp[0][1][1])
``` | instruction | 0 | 8,951 | 1 | 17,902 |
No | output | 1 | 8,951 | 1 | 17,903 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.