text stringlengths 198 433k | conversation_id int64 0 109k |
|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.
The park consists of n squares connected with (n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if a, b and c are distinct squares that a and b have a direct path between them, and b and c have a direct path between them, then balloon colors on these three squares are distinct.
Andryusha wants to use as little different colors as possible. Help him to choose the colors!
Input
The first line contains single integer n (3 β€ n β€ 2Β·105) β the number of squares in the park.
Each of the next (n - 1) lines contains two integers x and y (1 β€ x, y β€ n) β the indices of two squares directly connected by a path.
It is guaranteed that any square is reachable from any other using the paths.
Output
In the first line print single integer k β the minimum number of colors Andryusha has to use.
In the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k.
Examples
Input
3
2 3
1 3
Output
3
1 3 2
Input
5
2 3
5 3
4 3
1 3
Output
5
1 3 2 5 4
Input
5
2 1
3 2
4 3
5 4
Output
3
1 2 3 1 2
Note
In the first sample the park consists of three squares: 1 β 3 β 2. Thus, the balloon colors have to be distinct.
<image> Illustration for the first sample.
In the second example there are following triples of consequently connected squares:
* 1 β 3 β 2
* 1 β 3 β 4
* 1 β 3 β 5
* 2 β 3 β 4
* 2 β 3 β 5
* 4 β 3 β 5
We can see that each pair of squares is encountered in some triple, so all colors have to be distinct. <image> Illustration for the second sample.
In the third example there are following triples:
* 1 β 2 β 3
* 2 β 3 β 4
* 3 β 4 β 5
We can see that one or two colors is not enough, but there is an answer that uses three colors only. <image> Illustration for the third sample.
Tags: dfs and similar, graphs, greedy, trees
Correct Solution:
```
from collections import deque
n = int(input())
g = []
visited = []
color = []
max_child_color = []
parent = []
def initialize():
for i in range(n+10):
g.append([])
visited.append(False)
color.append(0)
max_child_color.append(0)
parent.append(0)
for i in range(n-1):
u, v = map(int, input().split())
g[u] += [v]
g[v] += [u]
# print(g)
def get_color(u):
for i in range(max_child_color[u]+1, n+1):
if i != color[parent[u]] and i != color[u]:
max_child_color[u] = i
# print(f'Setting max child color of node = {u} to color {i}')
return i
def bfs(start):
visited[start] = True
color[start] = 1
q = deque()
q.append(start)
while q:
u = q.popleft()
for v in g[u]:
parent[v] = u
if not visited[v]:
visited[v] = True
color[v] = get_color(u)
q.append(v)
if __name__ == '__main__':
initialize()
bfs(1)
print(max(color))
c_string = ""
for i in range(1, n+1):
c_string += str(color[i]) + " "
print(c_string)
```
| 13,800 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.
The park consists of n squares connected with (n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if a, b and c are distinct squares that a and b have a direct path between them, and b and c have a direct path between them, then balloon colors on these three squares are distinct.
Andryusha wants to use as little different colors as possible. Help him to choose the colors!
Input
The first line contains single integer n (3 β€ n β€ 2Β·105) β the number of squares in the park.
Each of the next (n - 1) lines contains two integers x and y (1 β€ x, y β€ n) β the indices of two squares directly connected by a path.
It is guaranteed that any square is reachable from any other using the paths.
Output
In the first line print single integer k β the minimum number of colors Andryusha has to use.
In the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k.
Examples
Input
3
2 3
1 3
Output
3
1 3 2
Input
5
2 3
5 3
4 3
1 3
Output
5
1 3 2 5 4
Input
5
2 1
3 2
4 3
5 4
Output
3
1 2 3 1 2
Note
In the first sample the park consists of three squares: 1 β 3 β 2. Thus, the balloon colors have to be distinct.
<image> Illustration for the first sample.
In the second example there are following triples of consequently connected squares:
* 1 β 3 β 2
* 1 β 3 β 4
* 1 β 3 β 5
* 2 β 3 β 4
* 2 β 3 β 5
* 4 β 3 β 5
We can see that each pair of squares is encountered in some triple, so all colors have to be distinct. <image> Illustration for the second sample.
In the third example there are following triples:
* 1 β 2 β 3
* 2 β 3 β 4
* 3 β 4 β 5
We can see that one or two colors is not enough, but there is an answer that uses three colors only. <image> Illustration for the third sample.
Tags: dfs and similar, graphs, greedy, trees
Correct Solution:
```
import sys
sys.setrecursionlimit(200000)
n = int(input())
arr = [[] for i in range(n)]
for i in range(n - 1):
a, b = map(int, input().split())
arr[a - 1].append(b - 1)
arr[b - 1].append(a - 1)
s = max([len(p) for p in arr]) + 1
print(s)
colored = [0] * n
def dfs(v, c, d):
colored[v] = p = c
for u in arr[v]:
if not colored[u]:
c = c + 1 if c < s else 1
if c == d:
c = c + 1 if c < s else 1
dfs(u, c, p)
if s > 3:
dfs(0, 1, 0)
else:
i = 0
c = 1
while len(arr[i]) != 1:
i += 1
for j in range(n):
colored[i] = c
c = c + 1 if c < s else 1
if j < n - 1:
i = arr[i][0] if not colored[arr[i][0]] else arr[i][1]
print(" ".join(map(str, colored)))
```
| 13,801 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.
The park consists of n squares connected with (n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if a, b and c are distinct squares that a and b have a direct path between them, and b and c have a direct path between them, then balloon colors on these three squares are distinct.
Andryusha wants to use as little different colors as possible. Help him to choose the colors!
Input
The first line contains single integer n (3 β€ n β€ 2Β·105) β the number of squares in the park.
Each of the next (n - 1) lines contains two integers x and y (1 β€ x, y β€ n) β the indices of two squares directly connected by a path.
It is guaranteed that any square is reachable from any other using the paths.
Output
In the first line print single integer k β the minimum number of colors Andryusha has to use.
In the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k.
Examples
Input
3
2 3
1 3
Output
3
1 3 2
Input
5
2 3
5 3
4 3
1 3
Output
5
1 3 2 5 4
Input
5
2 1
3 2
4 3
5 4
Output
3
1 2 3 1 2
Note
In the first sample the park consists of three squares: 1 β 3 β 2. Thus, the balloon colors have to be distinct.
<image> Illustration for the first sample.
In the second example there are following triples of consequently connected squares:
* 1 β 3 β 2
* 1 β 3 β 4
* 1 β 3 β 5
* 2 β 3 β 4
* 2 β 3 β 5
* 4 β 3 β 5
We can see that each pair of squares is encountered in some triple, so all colors have to be distinct. <image> Illustration for the second sample.
In the third example there are following triples:
* 1 β 2 β 3
* 2 β 3 β 4
* 3 β 4 β 5
We can see that one or two colors is not enough, but there is an answer that uses three colors only. <image> Illustration for the third sample.
Tags: dfs and similar, graphs, greedy, trees
Correct Solution:
```
import collections
def doit():
N = int(input())
graph = dict()
for n in range(N) :
graph[n] = list()
for n in range(N - 1):
a, b = map(int, input().split())
graph[a-1].append(b-1)
graph[b-1].append(a-1)
msize = 0
for k in graph:
msize = max(msize, len(graph[k]))
print(msize+1)
root = 0
colors = [0 for x in range(N)]
colors[root] = 1
parents = [0 for x in range(N)]
parents[root] = root
def colorgenerator(c1, c2):
colorindex = 0
while colorindex < msize + 1:
colorindex += 1
if colorindex == c1 or colorindex == c2:
continue
yield colorindex
queue = collections.deque([root])
while queue:
vertex = queue.popleft()
color = colorgenerator(colors[vertex], colors[parents[vertex]])
for neighbour in graph[vertex]:
if neighbour == parents[vertex]:
continue
parents[neighbour] = vertex
colors[neighbour] = next(color)
queue.append(neighbour)
print(" ".join(map(str, colors)))
doit()
```
| 13,802 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.
The park consists of n squares connected with (n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if a, b and c are distinct squares that a and b have a direct path between them, and b and c have a direct path between them, then balloon colors on these three squares are distinct.
Andryusha wants to use as little different colors as possible. Help him to choose the colors!
Input
The first line contains single integer n (3 β€ n β€ 2Β·105) β the number of squares in the park.
Each of the next (n - 1) lines contains two integers x and y (1 β€ x, y β€ n) β the indices of two squares directly connected by a path.
It is guaranteed that any square is reachable from any other using the paths.
Output
In the first line print single integer k β the minimum number of colors Andryusha has to use.
In the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k.
Examples
Input
3
2 3
1 3
Output
3
1 3 2
Input
5
2 3
5 3
4 3
1 3
Output
5
1 3 2 5 4
Input
5
2 1
3 2
4 3
5 4
Output
3
1 2 3 1 2
Note
In the first sample the park consists of three squares: 1 β 3 β 2. Thus, the balloon colors have to be distinct.
<image> Illustration for the first sample.
In the second example there are following triples of consequently connected squares:
* 1 β 3 β 2
* 1 β 3 β 4
* 1 β 3 β 5
* 2 β 3 β 4
* 2 β 3 β 5
* 4 β 3 β 5
We can see that each pair of squares is encountered in some triple, so all colors have to be distinct. <image> Illustration for the second sample.
In the third example there are following triples:
* 1 β 2 β 3
* 2 β 3 β 4
* 3 β 4 β 5
We can see that one or two colors is not enough, but there is an answer that uses three colors only. <image> Illustration for the third sample.
Tags: dfs and similar, graphs, greedy, trees
Correct Solution:
```
###### ### ####### ####### ## # ##### ### #####
# # # # # # # # # # # # # ###
# # # # # # # # # # # # # ###
###### ######### # # # # # # ######### #
###### ######### # # # # # # ######### #
# # # # # # # # # # #### # # #
# # # # # # # ## # # # # #
###### # # ####### ####### # # ##### # # # #
# mandatory imports
import os
import sys
from io import BytesIO, IOBase
from math import log2, ceil, sqrt, gcd, log
# optional imports
# from itertools import permutations
# from functools import cmp_to_key # for adding custom comparator
# from fractions import Fraction
# from collections import *
# from bisect import *
# from __future__ import print_function # for PyPy2
# from heapq import *
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
g = lambda : input().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
rr = lambda x : reversed(range(x))
mod = int(1e9)+7
inf = float("inf")
n, = gil()
adj = [[] for _ in range(n+1)]
for _ in range(n-1):
x, y = gil()
adj[x].append(y)
adj[y].append(x)
used = set()
clr = [0]*(n+1)
maxVal = 1
toClr = []
st = [1]
vis = [0]*(n+1)
while st:
i = st.pop()
if vis[i]:continue
vis[i] = 1
ptr = 1
used.add(clr[i])
if clr[i] == 0:
toClr.append(i)
for y in adj[i]:
if clr[y]:
used.add(clr[y])
else:
st.append(y)
toClr.append(y)
# print(toClr, used, end=' ')
while toClr:
while ptr in used:
ptr += 1
used.add(ptr)
clr[toClr.pop()] = ptr
maxVal = max(maxVal, ptr)
ptr += 1
# print(clr)
used.clear()
print(maxVal)
print(*clr[1:])
```
| 13,803 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.
The park consists of n squares connected with (n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if a, b and c are distinct squares that a and b have a direct path between them, and b and c have a direct path between them, then balloon colors on these three squares are distinct.
Andryusha wants to use as little different colors as possible. Help him to choose the colors!
Input
The first line contains single integer n (3 β€ n β€ 2Β·105) β the number of squares in the park.
Each of the next (n - 1) lines contains two integers x and y (1 β€ x, y β€ n) β the indices of two squares directly connected by a path.
It is guaranteed that any square is reachable from any other using the paths.
Output
In the first line print single integer k β the minimum number of colors Andryusha has to use.
In the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k.
Examples
Input
3
2 3
1 3
Output
3
1 3 2
Input
5
2 3
5 3
4 3
1 3
Output
5
1 3 2 5 4
Input
5
2 1
3 2
4 3
5 4
Output
3
1 2 3 1 2
Note
In the first sample the park consists of three squares: 1 β 3 β 2. Thus, the balloon colors have to be distinct.
<image> Illustration for the first sample.
In the second example there are following triples of consequently connected squares:
* 1 β 3 β 2
* 1 β 3 β 4
* 1 β 3 β 5
* 2 β 3 β 4
* 2 β 3 β 5
* 4 β 3 β 5
We can see that each pair of squares is encountered in some triple, so all colors have to be distinct. <image> Illustration for the second sample.
In the third example there are following triples:
* 1 β 2 β 3
* 2 β 3 β 4
* 3 β 4 β 5
We can see that one or two colors is not enough, but there is an answer that uses three colors only. <image> Illustration for the third sample.
Tags: dfs and similar, graphs, greedy, trees
Correct Solution:
```
from collections import deque
n = int(input())
dic = {}
for i in range(n):
dic[i]=[]
for i in range(n-1):
a,b = map(int,input().split())
dic[a-1].append(b-1)
dic[b-1].append(a-1)
color = {}
prevcolor = {}
color[0] = 1
prevcolor[0] = 1
lis = deque()
lis.append(0)
visited = {}
while(len(lis)!=0):
t = lis.popleft()
col = 0
visited[t] = 1
prev = prevcolor[t]
for i in dic[t]:
try:
z = visited[i]
except:
while(col==prev or col==color[t] or col==prevcolor[t]):
col+=1
color[i] = col
prevcolor[i] = color[t]
lis.append(i)
prev = col
print(max(color.values())+1)
for i in range(n):
print(color[i]+1,end=" ")
```
| 13,804 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.
The park consists of n squares connected with (n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if a, b and c are distinct squares that a and b have a direct path between them, and b and c have a direct path between them, then balloon colors on these three squares are distinct.
Andryusha wants to use as little different colors as possible. Help him to choose the colors!
Input
The first line contains single integer n (3 β€ n β€ 2Β·105) β the number of squares in the park.
Each of the next (n - 1) lines contains two integers x and y (1 β€ x, y β€ n) β the indices of two squares directly connected by a path.
It is guaranteed that any square is reachable from any other using the paths.
Output
In the first line print single integer k β the minimum number of colors Andryusha has to use.
In the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k.
Examples
Input
3
2 3
1 3
Output
3
1 3 2
Input
5
2 3
5 3
4 3
1 3
Output
5
1 3 2 5 4
Input
5
2 1
3 2
4 3
5 4
Output
3
1 2 3 1 2
Note
In the first sample the park consists of three squares: 1 β 3 β 2. Thus, the balloon colors have to be distinct.
<image> Illustration for the first sample.
In the second example there are following triples of consequently connected squares:
* 1 β 3 β 2
* 1 β 3 β 4
* 1 β 3 β 5
* 2 β 3 β 4
* 2 β 3 β 5
* 4 β 3 β 5
We can see that each pair of squares is encountered in some triple, so all colors have to be distinct. <image> Illustration for the second sample.
In the third example there are following triples:
* 1 β 2 β 3
* 2 β 3 β 4
* 3 β 4 β 5
We can see that one or two colors is not enough, but there is an answer that uses three colors only. <image> Illustration for the third sample.
Tags: dfs and similar, graphs, greedy, trees
Correct Solution:
```
from sys import stdin
from collections import deque
def main():
n = int(input()) + 1
g = [[] for _ in range(n)]
for s in stdin.read().splitlines():
u, v = map(int, s.split())
g[u].append(v)
g[v].append(u)
cc, palette, q = [0] * n, [True] * n, deque(((1, 0, 1),))
cc[1] = 1
while q:
u, a, b = q.popleft()
palette[a] = palette[b] = False
c = 1
for v in g[u]:
if not cc[v]:
while not palette[c]:
c += 1
cc[v] = c
q.append((v, b, c))
c += 1
palette[a] = palette[b] = True
print(max(cc))
print(' '.join(map(str, cc[1:])))
if __name__ == '__main__':
main()
```
| 13,805 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.
The park consists of n squares connected with (n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if a, b and c are distinct squares that a and b have a direct path between them, and b and c have a direct path between them, then balloon colors on these three squares are distinct.
Andryusha wants to use as little different colors as possible. Help him to choose the colors!
Input
The first line contains single integer n (3 β€ n β€ 2Β·105) β the number of squares in the park.
Each of the next (n - 1) lines contains two integers x and y (1 β€ x, y β€ n) β the indices of two squares directly connected by a path.
It is guaranteed that any square is reachable from any other using the paths.
Output
In the first line print single integer k β the minimum number of colors Andryusha has to use.
In the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k.
Examples
Input
3
2 3
1 3
Output
3
1 3 2
Input
5
2 3
5 3
4 3
1 3
Output
5
1 3 2 5 4
Input
5
2 1
3 2
4 3
5 4
Output
3
1 2 3 1 2
Note
In the first sample the park consists of three squares: 1 β 3 β 2. Thus, the balloon colors have to be distinct.
<image> Illustration for the first sample.
In the second example there are following triples of consequently connected squares:
* 1 β 3 β 2
* 1 β 3 β 4
* 1 β 3 β 5
* 2 β 3 β 4
* 2 β 3 β 5
* 4 β 3 β 5
We can see that each pair of squares is encountered in some triple, so all colors have to be distinct. <image> Illustration for the second sample.
In the third example there are following triples:
* 1 β 2 β 3
* 2 β 3 β 4
* 3 β 4 β 5
We can see that one or two colors is not enough, but there is an answer that uses three colors only. <image> Illustration for the third sample.
Tags: dfs and similar, graphs, greedy, trees
Correct Solution:
```
from collections import deque
n = int(input())
ans = [0]*n
g = [[]for _ in range(n)]
for _ in range(n - 1):
u, v = map(int, input().split())
u -= 1
v -= 1
g[u].append(v)
g[v].append(u)
col = 1
q = deque()
vis = [False]*n
q.append((0, 0))
vis[0] = True
ans[0] = 1
p_top = 0
while q:
top, p_top = q.popleft()
col = 1
for viz in g[top]:
if not vis[viz]:
vis[viz] = True
q.append((viz, top))
while ans[top] == col or ans[p_top] == col:
col += 1
ans[viz] = col
col += 1
print(max(ans))
print(" ".join(map(str, ans)))
```
| 13,806 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.
The park consists of n squares connected with (n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if a, b and c are distinct squares that a and b have a direct path between them, and b and c have a direct path between them, then balloon colors on these three squares are distinct.
Andryusha wants to use as little different colors as possible. Help him to choose the colors!
Input
The first line contains single integer n (3 β€ n β€ 2Β·105) β the number of squares in the park.
Each of the next (n - 1) lines contains two integers x and y (1 β€ x, y β€ n) β the indices of two squares directly connected by a path.
It is guaranteed that any square is reachable from any other using the paths.
Output
In the first line print single integer k β the minimum number of colors Andryusha has to use.
In the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k.
Examples
Input
3
2 3
1 3
Output
3
1 3 2
Input
5
2 3
5 3
4 3
1 3
Output
5
1 3 2 5 4
Input
5
2 1
3 2
4 3
5 4
Output
3
1 2 3 1 2
Note
In the first sample the park consists of three squares: 1 β 3 β 2. Thus, the balloon colors have to be distinct.
<image> Illustration for the first sample.
In the second example there are following triples of consequently connected squares:
* 1 β 3 β 2
* 1 β 3 β 4
* 1 β 3 β 5
* 2 β 3 β 4
* 2 β 3 β 5
* 4 β 3 β 5
We can see that each pair of squares is encountered in some triple, so all colors have to be distinct. <image> Illustration for the second sample.
In the third example there are following triples:
* 1 β 2 β 3
* 2 β 3 β 4
* 3 β 4 β 5
We can see that one or two colors is not enough, but there is an answer that uses three colors only. <image> Illustration for the third sample.
Tags: dfs and similar, graphs, greedy, trees
Correct Solution:
```
from collections import defaultdict,deque
class Graph:
def __init__(self,n):
self.graph = defaultdict(list)
self.parentColor = [-1] * (n+1)
self.color = [-1] * (n+1)
self.visited = [False] * (n+1)
self.n = n
def addEdge(self,fr,to):
self.graph[fr].append(to)
self.graph[to].append(fr)
def BFS(self,root):
queue = deque()
queue.append(root)
self.color[root] = 1
self.parentColor[root] = 0
while(queue):
s = queue.popleft()
Set = defaultdict(bool)
Set[self.color[s]] = True
Set[self.parentColor[s]] = True
culur = 1
for i in self.graph[s]:
if(self.visited[i] == False):
queue.append(i)
self.parentColor[i] = self.color[s]
while(1):
if(not Set[culur]):
self.color[i] = culur
culur+=1
break
culur+=1
self.visited[s] = True
def show(self):
print(max(self.color))
print(*self.color[1:])
n = int(input())
G = Graph(n)
for _ in range(n-1):
a,b = map(int,input().split())
G.addEdge(a,b)
G.BFS(1)
G.show()
```
| 13,807 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.
The park consists of n squares connected with (n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if a, b and c are distinct squares that a and b have a direct path between them, and b and c have a direct path between them, then balloon colors on these three squares are distinct.
Andryusha wants to use as little different colors as possible. Help him to choose the colors!
Input
The first line contains single integer n (3 β€ n β€ 2Β·105) β the number of squares in the park.
Each of the next (n - 1) lines contains two integers x and y (1 β€ x, y β€ n) β the indices of two squares directly connected by a path.
It is guaranteed that any square is reachable from any other using the paths.
Output
In the first line print single integer k β the minimum number of colors Andryusha has to use.
In the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k.
Examples
Input
3
2 3
1 3
Output
3
1 3 2
Input
5
2 3
5 3
4 3
1 3
Output
5
1 3 2 5 4
Input
5
2 1
3 2
4 3
5 4
Output
3
1 2 3 1 2
Note
In the first sample the park consists of three squares: 1 β 3 β 2. Thus, the balloon colors have to be distinct.
<image> Illustration for the first sample.
In the second example there are following triples of consequently connected squares:
* 1 β 3 β 2
* 1 β 3 β 4
* 1 β 3 β 5
* 2 β 3 β 4
* 2 β 3 β 5
* 4 β 3 β 5
We can see that each pair of squares is encountered in some triple, so all colors have to be distinct. <image> Illustration for the second sample.
In the third example there are following triples:
* 1 β 2 β 3
* 2 β 3 β 4
* 3 β 4 β 5
We can see that one or two colors is not enough, but there is an answer that uses three colors only. <image> Illustration for the third sample.
Submitted Solution:
```
from sys import stdin
input = stdin.readline
def put():
return map(int, input().split())
def dfs():
s = [(0,0)]
tree[0].append(1)
while s:
i,p = s.pop()
c = 1
for j in tree[i]:
if j!=p:
s.append((j,i))
while c in [color[i], color[p]]:
c+=1
color[j]=c
c+=1
n = int(input())
tree = [[] for i in range(n+1)]
color= [0]*(n+1)
for _ in range(n-1):
x,y = put()
tree[x].append(y)
tree[y].append(x)
ans = 0
for i in range(1,n+1):
ans = max(ans, len(tree[i])+1)
dfs()
print(ans)
print(*color[1:])
```
Yes
| 13,808 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.
The park consists of n squares connected with (n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if a, b and c are distinct squares that a and b have a direct path between them, and b and c have a direct path between them, then balloon colors on these three squares are distinct.
Andryusha wants to use as little different colors as possible. Help him to choose the colors!
Input
The first line contains single integer n (3 β€ n β€ 2Β·105) β the number of squares in the park.
Each of the next (n - 1) lines contains two integers x and y (1 β€ x, y β€ n) β the indices of two squares directly connected by a path.
It is guaranteed that any square is reachable from any other using the paths.
Output
In the first line print single integer k β the minimum number of colors Andryusha has to use.
In the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k.
Examples
Input
3
2 3
1 3
Output
3
1 3 2
Input
5
2 3
5 3
4 3
1 3
Output
5
1 3 2 5 4
Input
5
2 1
3 2
4 3
5 4
Output
3
1 2 3 1 2
Note
In the first sample the park consists of three squares: 1 β 3 β 2. Thus, the balloon colors have to be distinct.
<image> Illustration for the first sample.
In the second example there are following triples of consequently connected squares:
* 1 β 3 β 2
* 1 β 3 β 4
* 1 β 3 β 5
* 2 β 3 β 4
* 2 β 3 β 5
* 4 β 3 β 5
We can see that each pair of squares is encountered in some triple, so all colors have to be distinct. <image> Illustration for the second sample.
In the third example there are following triples:
* 1 β 2 β 3
* 2 β 3 β 4
* 3 β 4 β 5
We can see that one or two colors is not enough, but there is an answer that uses three colors only. <image> Illustration for the third sample.
Submitted Solution:
```
import sys, os, io
def rs(): return sys.stdin.readline().rstrip()
def ri(): return int(sys.stdin.readline())
def ria(): return list(map(int, sys.stdin.readline().split()))
def ws(s): sys.stdout.write(s + '\n')
def wi(n): sys.stdout.write(str(n) + '\n')
def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math,datetime,functools,itertools,operator,bisect,fractions,statistics
from collections import deque,defaultdict,OrderedDict,Counter
from fractions import Fraction
from decimal import Decimal
from sys import stdout
from heapq import heappush, heappop, heapify ,_heapify_max,_heappop_max,nsmallest,nlargest
# sys.setrecursionlimit(111111)
INF=999999999999999999999999
alphabets="abcdefghijklmnopqrstuvwxyz"
class SortedList:
def __init__(self, iterable=[], _load=200):
"""Initialize sorted list instance."""
values = sorted(iterable)
self._len = _len = len(values)
self._load = _load
self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
self._list_lens = [len(_list) for _list in _lists]
self._mins = [_list[0] for _list in _lists]
self._fen_tree = []
self._rebuild = True
def _fen_build(self):
"""Build a fenwick tree instance."""
self._fen_tree[:] = self._list_lens
_fen_tree = self._fen_tree
for i in range(len(_fen_tree)):
if i | i + 1 < len(_fen_tree):
_fen_tree[i | i + 1] += _fen_tree[i]
self._rebuild = False
def _fen_update(self, index, value):
"""Update `fen_tree[index] += value`."""
if not self._rebuild:
_fen_tree = self._fen_tree
while index < len(_fen_tree):
_fen_tree[index] += value
index |= index + 1
def _fen_query(self, end):
"""Return `sum(_fen_tree[:end])`."""
if self._rebuild:
self._fen_build()
_fen_tree = self._fen_tree
x = 0
while end:
x += _fen_tree[end - 1]
end &= end - 1
return x
def _fen_findkth(self, k):
"""Return a pair of (the largest `idx` such that `sum(_fen_tree[:idx]) <= k`, `k - sum(_fen_tree[:idx])`)."""
_list_lens = self._list_lens
if k < _list_lens[0]:
return 0, k
if k >= self._len - _list_lens[-1]:
return len(_list_lens) - 1, k + _list_lens[-1] - self._len
if self._rebuild:
self._fen_build()
_fen_tree = self._fen_tree
idx = -1
for d in reversed(range(len(_fen_tree).bit_length())):
right_idx = idx + (1 << d)
if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
idx = right_idx
k -= _fen_tree[idx]
return idx + 1, k
def _delete(self, pos, idx):
"""Delete value at the given `(pos, idx)`."""
_lists = self._lists
_mins = self._mins
_list_lens = self._list_lens
self._len -= 1
self._fen_update(pos, -1)
del _lists[pos][idx]
_list_lens[pos] -= 1
if _list_lens[pos]:
_mins[pos] = _lists[pos][0]
else:
del _lists[pos]
del _list_lens[pos]
del _mins[pos]
self._rebuild = True
def _loc_left(self, value):
"""Return an index pair that corresponds to the first position of `value` in the sorted list."""
if not self._len:
return 0, 0
_lists = self._lists
_mins = self._mins
lo, pos = -1, len(_lists) - 1
while lo + 1 < pos:
mi = (lo + pos) >> 1
if value <= _mins[mi]:
pos = mi
else:
lo = mi
if pos and value <= _lists[pos - 1][-1]:
pos -= 1
_list = _lists[pos]
lo, idx = -1, len(_list)
while lo + 1 < idx:
mi = (lo + idx) >> 1
if value <= _list[mi]:
idx = mi
else:
lo = mi
return pos, idx
def _loc_right(self, value):
"""Return an index pair that corresponds to the last position of `value` in the sorted list."""
if not self._len:
return 0, 0
_lists = self._lists
_mins = self._mins
pos, hi = 0, len(_lists)
while pos + 1 < hi:
mi = (pos + hi) >> 1
if value < _mins[mi]:
hi = mi
else:
pos = mi
_list = _lists[pos]
lo, idx = -1, len(_list)
while lo + 1 < idx:
mi = (lo + idx) >> 1
if value < _list[mi]:
idx = mi
else:
lo = mi
return pos, idx
def add(self, value):
"""Add `value` to sorted list."""
_load = self._load
_lists = self._lists
_mins = self._mins
_list_lens = self._list_lens
self._len += 1
if _lists:
pos, idx = self._loc_right(value)
self._fen_update(pos, 1)
_list = _lists[pos]
_list.insert(idx, value)
_list_lens[pos] += 1
_mins[pos] = _list[0]
if _load + _load < len(_list):
_lists.insert(pos + 1, _list[_load:])
_list_lens.insert(pos + 1, len(_list) - _load)
_mins.insert(pos + 1, _list[_load])
_list_lens[pos] = _load
del _list[_load:]
self._rebuild = True
else:
_lists.append([value])
_mins.append(value)
_list_lens.append(1)
self._rebuild = True
def discard(self, value):
"""Remove `value` from sorted list if it is a member."""
_lists = self._lists
if _lists:
pos, idx = self._loc_right(value)
if idx and _lists[pos][idx - 1] == value:
self._delete(pos, idx - 1)
def remove(self, value):
"""Remove `value` from sorted list; `value` must be a member."""
_len = self._len
self.discard(value)
if _len == self._len:
raise ValueError('{0!r} not in list'.format(value))
def pop(self, index=-1):
"""Remove and return value at `index` in sorted list."""
pos, idx = self._fen_findkth(self._len + index if index < 0 else index)
value = self._lists[pos][idx]
self._delete(pos, idx)
return value
def bisect_left(self, value):
"""Return the first index to insert `value` in the sorted list."""
pos, idx = self._loc_left(value)
return self._fen_query(pos) + idx
def bisect_right(self, value):
"""Return the last index to insert `value` in the sorted list."""
pos, idx = self._loc_right(value)
return self._fen_query(pos) + idx
def count(self, value):
"""Return number of occurrences of `value` in the sorted list."""
return self.bisect_right(value) - self.bisect_left(value)
def __len__(self):
"""Return the size of the sorted list."""
return self._len
def __getitem__(self, index):
"""Lookup value at `index` in sorted list."""
pos, idx = self._fen_findkth(self._len + index if index < 0 else index)
return self._lists[pos][idx]
def __delitem__(self, index):
"""Remove value at `index` from sorted list."""
pos, idx = self._fen_findkth(self._len + index if index < 0 else index)
self._delete(pos, idx)
def __contains__(self, value):
"""Return true if `value` is an element of the sorted list."""
_lists = self._lists
if _lists:
pos, idx = self._loc_left(value)
return idx < len(_lists[pos]) and _lists[pos][idx] == value
return False
def __iter__(self):
"""Return an iterator over the sorted list."""
return (value for _list in self._lists for value in _list)
def __reversed__(self):
"""Return a reverse iterator over the sorted list."""
return (value for _list in reversed(self._lists) for value in reversed(_list))
def __repr__(self):
"""Return string representation of sorted list."""
return 'SortedList({0})'.format(list(self))
class SegTree:
def __init__(self, n):
self.N = 1 << n.bit_length()
self.tree = [0] * (self.N<<1)
def update(self, i, j, v):
i += self.N
j += self.N
while i <= j:
if i%2==1: self.tree[i] += v
if j%2==0: self.tree[j] += v
i, j = (i+1) >> 1, (j-1) >> 1
def query(self, i):
v = 0
i += self.N
while i > 0:
v += self.tree[i]
i >>= 1
return v
def SieveOfEratosthenes(limit):
"""Returns all primes not greater than limit."""
isPrime = [True]*(limit+1)
isPrime[0] = isPrime[1] = False
primes = []
for i in range(2, limit+1):
if not isPrime[i]:continue
primes += [i]
for j in range(i*i, limit+1, i):
isPrime[j] = False
return primes
def main():
mod=1000000007
# InverseofNumber(mod)
# InverseofFactorial(mod)
# factorial(mod)
starttime=datetime.datetime.now()
if(os.path.exists('input.txt')):
sys.stdin = open("input.txt","r")
sys.stdout = open("output.txt","w")
###CODE
tc = 1
for _ in range(tc):
n=ri()
col=[0]*(n+1)
col[0]=INF
col[1]=1
par=[0]*(n+1)
graph=[[] for i in range(n+1)]
for i in range(n-1):
x,y=ria()
graph[x].append(y)
graph[y].append(x)
def dfs():
mxc=1
visited= [False] * (n+1)
start=1
stack = [start]
while stack:
start = stack[-1]
# push unvisited children into stack
if not visited[start]:
visited[start] = True
a,b=col[par[start]],col[start]
ind=1
for child in graph[start]:
if not visited[child]:
par[child]=start
while ind!=len(graph[start])+2:
if ind!=a and ind!=b:
col[child]=ind
mxc=max(mxc,ind)
ind+=1
break
ind+=1
stack.append(child)
else:
stack.pop()
return mxc
wi(dfs())
wia(col[1:])
#<--Solving Area Ends
endtime=datetime.datetime.now()
time=(endtime-starttime).total_seconds()*1000
if(os.path.exists('input.txt')):
print("Time:",time,"ms")
class FastReader(io.IOBase):
newlines = 0
def __init__(self, fd, chunk_size=1024 * 8):
self._fd = fd
self._chunk_size = chunk_size
self.buffer = io.BytesIO()
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
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, size=-1):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
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()
class FastWriter(io.IOBase):
def __init__(self, fd):
self._fd = fd
self.buffer = io.BytesIO()
self.write = self.buffer.write
def flush(self):
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class FastStdin(io.IOBase):
def __init__(self, fd=0):
self.buffer = FastReader(fd)
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
class FastStdout(io.IOBase):
def __init__(self, fd=1):
self.buffer = FastWriter(fd)
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.flush = self.buffer.flush
if __name__ == '__main__':
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()
```
Yes
| 13,809 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.
The park consists of n squares connected with (n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if a, b and c are distinct squares that a and b have a direct path between them, and b and c have a direct path between them, then balloon colors on these three squares are distinct.
Andryusha wants to use as little different colors as possible. Help him to choose the colors!
Input
The first line contains single integer n (3 β€ n β€ 2Β·105) β the number of squares in the park.
Each of the next (n - 1) lines contains two integers x and y (1 β€ x, y β€ n) β the indices of two squares directly connected by a path.
It is guaranteed that any square is reachable from any other using the paths.
Output
In the first line print single integer k β the minimum number of colors Andryusha has to use.
In the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k.
Examples
Input
3
2 3
1 3
Output
3
1 3 2
Input
5
2 3
5 3
4 3
1 3
Output
5
1 3 2 5 4
Input
5
2 1
3 2
4 3
5 4
Output
3
1 2 3 1 2
Note
In the first sample the park consists of three squares: 1 β 3 β 2. Thus, the balloon colors have to be distinct.
<image> Illustration for the first sample.
In the second example there are following triples of consequently connected squares:
* 1 β 3 β 2
* 1 β 3 β 4
* 1 β 3 β 5
* 2 β 3 β 4
* 2 β 3 β 5
* 4 β 3 β 5
We can see that each pair of squares is encountered in some triple, so all colors have to be distinct. <image> Illustration for the second sample.
In the third example there are following triples:
* 1 β 2 β 3
* 2 β 3 β 4
* 3 β 4 β 5
We can see that one or two colors is not enough, but there is an answer that uses three colors only. <image> Illustration for the third sample.
Submitted Solution:
```
import sys
sys.setrecursionlimit(200000)
n=int(input())
g=[[] for i in range(n)]
for i in range(n-1):
a,b=map(int, input().split())
g[a-1].append(b-1)
g[b-1].append(a-1)
s = max([len(p) for p in g]) + 1
print(s)
r=[0]*n
def dfs(v,c,d):
r[v]=p=c
for u in g[v]:
if not r[u]:
c=c+1 if c<s else 1
if c==d:
c=c+1 if c<s else 1
dfs(u,c,p)
if s>3:
dfs(0, 1, 0)
else:
i=0
c=1
while len(g[i])!=1:
i+=1
for j in range(n):
r[i]=c
c=c+1 if c<s else 1
if j<n-1:
i=g[i][0] if not r[g[i][0]] else g[i][1]
print(" ".join(map(str, r)))
```
Yes
| 13,810 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.
The park consists of n squares connected with (n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if a, b and c are distinct squares that a and b have a direct path between them, and b and c have a direct path between them, then balloon colors on these three squares are distinct.
Andryusha wants to use as little different colors as possible. Help him to choose the colors!
Input
The first line contains single integer n (3 β€ n β€ 2Β·105) β the number of squares in the park.
Each of the next (n - 1) lines contains two integers x and y (1 β€ x, y β€ n) β the indices of two squares directly connected by a path.
It is guaranteed that any square is reachable from any other using the paths.
Output
In the first line print single integer k β the minimum number of colors Andryusha has to use.
In the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k.
Examples
Input
3
2 3
1 3
Output
3
1 3 2
Input
5
2 3
5 3
4 3
1 3
Output
5
1 3 2 5 4
Input
5
2 1
3 2
4 3
5 4
Output
3
1 2 3 1 2
Note
In the first sample the park consists of three squares: 1 β 3 β 2. Thus, the balloon colors have to be distinct.
<image> Illustration for the first sample.
In the second example there are following triples of consequently connected squares:
* 1 β 3 β 2
* 1 β 3 β 4
* 1 β 3 β 5
* 2 β 3 β 4
* 2 β 3 β 5
* 4 β 3 β 5
We can see that each pair of squares is encountered in some triple, so all colors have to be distinct. <image> Illustration for the second sample.
In the third example there are following triples:
* 1 β 2 β 3
* 2 β 3 β 4
* 3 β 4 β 5
We can see that one or two colors is not enough, but there is an answer that uses three colors only. <image> Illustration for the third sample.
Submitted Solution:
```
from sys import stdin
input=lambda : stdin.readline().strip()
from math import ceil,sqrt,factorial,gcd
from collections import deque
n=int(input())
graph={i:set() for i in range(n)}
for i in range(n-1):
a,b=map(int,input().split())
graph[a-1].add(b-1)
graph[b-1].add(a-1)
ma=0
for i in graph:
if len(graph[i])+1>ma:
ma=len(graph[i])+1
x=i
print(ma)
ans=[0 for i in range(n)]
stack=[x]
papa=[0 for i in range(n)]
while stack:
x=stack.pop()
# z=set(s)
a=1
if ans[x]==0:
ans[x]=1
z=[1]
else:
z=[]
z.append(ans[x])
z.append(ans[papa[x]])
for j in graph[x]:
while 1:
if a in z:
a+=1
else:
ans[j]=a
a+=1
break
stack.append(j)
graph[j].remove(x)
papa[j]=x
print(*ans)
```
Yes
| 13,811 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.
The park consists of n squares connected with (n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if a, b and c are distinct squares that a and b have a direct path between them, and b and c have a direct path between them, then balloon colors on these three squares are distinct.
Andryusha wants to use as little different colors as possible. Help him to choose the colors!
Input
The first line contains single integer n (3 β€ n β€ 2Β·105) β the number of squares in the park.
Each of the next (n - 1) lines contains two integers x and y (1 β€ x, y β€ n) β the indices of two squares directly connected by a path.
It is guaranteed that any square is reachable from any other using the paths.
Output
In the first line print single integer k β the minimum number of colors Andryusha has to use.
In the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k.
Examples
Input
3
2 3
1 3
Output
3
1 3 2
Input
5
2 3
5 3
4 3
1 3
Output
5
1 3 2 5 4
Input
5
2 1
3 2
4 3
5 4
Output
3
1 2 3 1 2
Note
In the first sample the park consists of three squares: 1 β 3 β 2. Thus, the balloon colors have to be distinct.
<image> Illustration for the first sample.
In the second example there are following triples of consequently connected squares:
* 1 β 3 β 2
* 1 β 3 β 4
* 1 β 3 β 5
* 2 β 3 β 4
* 2 β 3 β 5
* 4 β 3 β 5
We can see that each pair of squares is encountered in some triple, so all colors have to be distinct. <image> Illustration for the second sample.
In the third example there are following triples:
* 1 β 2 β 3
* 2 β 3 β 4
* 3 β 4 β 5
We can see that one or two colors is not enough, but there is an answer that uses three colors only. <image> Illustration for the third sample.
Submitted Solution:
```
import collections
n = int(input())
g = collections.defaultdict(set)
for i in range(n-1):
u,v = map(int,input().split())
g[u].add(v)
g[v].add(u)
ans = [0,0]
for i in g:
ans = max(ans,[len(g[i])+1,len(g[i])+1,i])
maxC = ans[0]
visited = [0] * (n+1)
queue = collections.deque()
queue.append(ans)
colors = [1 for i in range(n+1)]
while queue:
color,carry,cur = queue.popleft()
colors[cur] = color
visited[cur] = 1
rem = set(list(range(1,maxC+1)))
# print(color,carry,cur)
try:
rem.remove(color)
rem.remove(carry)
except:
pass
# print(g[cur],rem)
for i,c in zip(list(g[cur]),list(rem)):
if not visited[i]:
queue.append([c,color,i])
print(ans[0])
print(" ".join([str(i) for i in colors[1:]]))
```
No
| 13,812 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.
The park consists of n squares connected with (n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if a, b and c are distinct squares that a and b have a direct path between them, and b and c have a direct path between them, then balloon colors on these three squares are distinct.
Andryusha wants to use as little different colors as possible. Help him to choose the colors!
Input
The first line contains single integer n (3 β€ n β€ 2Β·105) β the number of squares in the park.
Each of the next (n - 1) lines contains two integers x and y (1 β€ x, y β€ n) β the indices of two squares directly connected by a path.
It is guaranteed that any square is reachable from any other using the paths.
Output
In the first line print single integer k β the minimum number of colors Andryusha has to use.
In the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k.
Examples
Input
3
2 3
1 3
Output
3
1 3 2
Input
5
2 3
5 3
4 3
1 3
Output
5
1 3 2 5 4
Input
5
2 1
3 2
4 3
5 4
Output
3
1 2 3 1 2
Note
In the first sample the park consists of three squares: 1 β 3 β 2. Thus, the balloon colors have to be distinct.
<image> Illustration for the first sample.
In the second example there are following triples of consequently connected squares:
* 1 β 3 β 2
* 1 β 3 β 4
* 1 β 3 β 5
* 2 β 3 β 4
* 2 β 3 β 5
* 4 β 3 β 5
We can see that each pair of squares is encountered in some triple, so all colors have to be distinct. <image> Illustration for the second sample.
In the third example there are following triples:
* 1 β 2 β 3
* 2 β 3 β 4
* 3 β 4 β 5
We can see that one or two colors is not enough, but there is an answer that uses three colors only. <image> Illustration for the third sample.
Submitted Solution:
```
n=int(input())
L=[[] for i in range(n)]
out=[0 for i in range(n)]
oy=[[] for i in range(n)]
for i in range(n-1) :
a,b=map(int,input().split())
L[a-1].append(b-1)
L[b-1].append(a-1)
visited=[False for i in range(n)]
kil=len(max(L,key=lambda x :len(x)))+1
q=[0]
out[0]=1
visited[0]=True
while (len(q)>0) :
b=q[0]
k=0
oy[b].append(out[b]-1)
for x in L[b] :
if visited[x]==False :
while( k in oy[b]) :
k+=1
oy[x].append(b)
visited[x]=True
out[x]=k+1
q.append(x)
k+=1
del(q[0])
print(kil)
print(' '.join(map(str,out)))
```
No
| 13,813 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.
The park consists of n squares connected with (n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if a, b and c are distinct squares that a and b have a direct path between them, and b and c have a direct path between them, then balloon colors on these three squares are distinct.
Andryusha wants to use as little different colors as possible. Help him to choose the colors!
Input
The first line contains single integer n (3 β€ n β€ 2Β·105) β the number of squares in the park.
Each of the next (n - 1) lines contains two integers x and y (1 β€ x, y β€ n) β the indices of two squares directly connected by a path.
It is guaranteed that any square is reachable from any other using the paths.
Output
In the first line print single integer k β the minimum number of colors Andryusha has to use.
In the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k.
Examples
Input
3
2 3
1 3
Output
3
1 3 2
Input
5
2 3
5 3
4 3
1 3
Output
5
1 3 2 5 4
Input
5
2 1
3 2
4 3
5 4
Output
3
1 2 3 1 2
Note
In the first sample the park consists of three squares: 1 β 3 β 2. Thus, the balloon colors have to be distinct.
<image> Illustration for the first sample.
In the second example there are following triples of consequently connected squares:
* 1 β 3 β 2
* 1 β 3 β 4
* 1 β 3 β 5
* 2 β 3 β 4
* 2 β 3 β 5
* 4 β 3 β 5
We can see that each pair of squares is encountered in some triple, so all colors have to be distinct. <image> Illustration for the second sample.
In the third example there are following triples:
* 1 β 2 β 3
* 2 β 3 β 4
* 3 β 4 β 5
We can see that one or two colors is not enough, but there is an answer that uses three colors only. <image> Illustration for the third sample.
Submitted Solution:
```
n=int(input())
L=[[] for i in range(n)]
out=[0 for i in range(n)]
oy=[[] for i in range(n)]
for i in range(n-1) :
a,b=map(int,input().split())
L[a-1].append(b-1)
L[b-1].append(a-1)
visited=[False for i in range(n)]
kil=len(max(L,key=lambda x :len(x)))+1
q=[0]
out[0]=1
oy[0].append(0)
visited[0]=True
while (len(q)>0) :
b=q[0]
k=0
oy[b].append(out[b]-1)
for x in L[b] :
if visited[x]==False :
while( k in oy[b]) :
k+=1
oy[x].append(b)
visited[x]=True
out[x]=k+1
q.append(x)
k+=1
else :
k+=1
del(q[0])
print(kil)
print(' '.join(map(str,out)))
```
No
| 13,814 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.
The park consists of n squares connected with (n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if a, b and c are distinct squares that a and b have a direct path between them, and b and c have a direct path between them, then balloon colors on these three squares are distinct.
Andryusha wants to use as little different colors as possible. Help him to choose the colors!
Input
The first line contains single integer n (3 β€ n β€ 2Β·105) β the number of squares in the park.
Each of the next (n - 1) lines contains two integers x and y (1 β€ x, y β€ n) β the indices of two squares directly connected by a path.
It is guaranteed that any square is reachable from any other using the paths.
Output
In the first line print single integer k β the minimum number of colors Andryusha has to use.
In the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k.
Examples
Input
3
2 3
1 3
Output
3
1 3 2
Input
5
2 3
5 3
4 3
1 3
Output
5
1 3 2 5 4
Input
5
2 1
3 2
4 3
5 4
Output
3
1 2 3 1 2
Note
In the first sample the park consists of three squares: 1 β 3 β 2. Thus, the balloon colors have to be distinct.
<image> Illustration for the first sample.
In the second example there are following triples of consequently connected squares:
* 1 β 3 β 2
* 1 β 3 β 4
* 1 β 3 β 5
* 2 β 3 β 4
* 2 β 3 β 5
* 4 β 3 β 5
We can see that each pair of squares is encountered in some triple, so all colors have to be distinct. <image> Illustration for the second sample.
In the third example there are following triples:
* 1 β 2 β 3
* 2 β 3 β 4
* 3 β 4 β 5
We can see that one or two colors is not enough, but there is an answer that uses three colors only. <image> Illustration for the third sample.
Submitted Solution:
```
from sys import stdin
input=stdin.readline
n=int(input())
adjList=[[] for i in range(n+1)]
for i in range(n-1):
a,b=list(map(int, input().split(" ")))
adjList[a].append(b)
adjList[b].append(a)
color=[0 for i in range(n+1)]
def dfs(start, adjList, color):
stack=[start]
color[start]=1
while stack!=[]:
v=stack[-1]
advancing=False
# print("from: ", v, color)
if len(stack)>1:
for ind, neig in enumerate(adjList[v]):
if neig==stack[-2]:
del adjList[v][ind]
break
for ind, neig in enumerate(adjList[v]):
# print(ind, neig)
if not color[neig]:
stack.append(neig)
advancing=True
for col in range(1, n+1):
if len(stack)>2 and color[stack[-2]]!=col+ind and color[stack[-3]]!=col+ind:
color[neig]=col+ind
break
if len(stack)==2 and color[stack[-2]]!=col+ind:
color[neig]=col+ind
break
# print("change: ", color[neig])
# print(color[stack[-2]], color[neig])
# print("x", stack)
break
if not advancing:
stack.pop()
root=None
for v in range(1, n+1):
if len(adjList[v])==1:
root=v
print(root)
dfs(v, adjList, color)
print(max(color))
print(" ".join(map(str, color[1:])))
```
No
| 13,815 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of positive integers and q queries to this array. There are two types of queries:
* 1 l r x β for each index i such that l β€ i β€ r set ai = x.
* 2 l r β find the minimum among such ai that l β€ i β€ r.
We decided that this problem is too easy. So the array a is given in a compressed form: there is an array b consisting of n elements and a number k in the input, and before all queries a is equal to the concatenation of k arrays b (so the size of a is nΒ·k).
Input
The first line contains two integers n and k (1 β€ n β€ 105, 1 β€ k β€ 104).
The second line contains n integers β elements of the array b (1 β€ bi β€ 109).
The third line contains one integer q (1 β€ q β€ 105).
Then q lines follow, each representing a query. Each query is given either as 1 l r x β set all elements in the segment from l till r (including borders) to x (1 β€ l β€ r β€ nΒ·k, 1 β€ x β€ 109) or as 2 l r β find the minimum among all elements in the segment from l till r (1 β€ l β€ r β€ nΒ·k).
Output
For each query of type 2 print the answer to this query β the minimum on the corresponding segment.
Examples
Input
3 1
1 2 3
3
2 1 3
1 1 2 4
2 1 3
Output
1
3
Input
3 2
1 2 3
5
2 4 4
1 4 4 5
2 4 4
1 1 6 1
2 6 6
Output
1
5
1
Submitted Solution:
```
from sys import stdin, stdout
n, k = map(int, stdin.readline().split())
values = [0] + list(map(int, stdin.readline().split()))
m = int(stdin.readline())
questions = []
ar = set()
for i in range(m):
questions.append(tuple(map(int, stdin.readline().strip().split())))
ar.add(int(questions[-1][1]))
ar.add(int(questions[-1][2]))
compress = [0]
ar = sorted(list(ar))
counting = {}
d = {}
for i in range(len(ar)):
compress.append(ar[i])
d[ar[i]] = len(compress) - 1
if i != len(ar) - 1 and ar[i + 1] - ar[i] > 1:
compress.append(ar[i] + 1)
counting[len(compress) - 1] = float('inf')
d[ar[i] + 1] = len(compress) - 1
mn = min(values)
for ind in counting:
if (compress[ind + 1] - compress[ind - 1] - 1 >= n):
counting[ind] = mn
else:
for j in range(compress[ind - 1] + 1, compress[ind + 1]):
counting[ind] = min(counting[ind], values[j % n + n * (not(j % n))])
def update(l, r, ind, lb, rb, value):
if updating[ind] and ind * 2 < len(tree):
if (not updating[ind * 2] or (updating[ind * 2][1] < updating[ind][1])):
tree[ind * 2] = updating[ind][0]
updating[ind * 2] = updating[ind][::]
if (not updating[ind * 2 + 1] or (updating[ind * 2 + 1][1] < updating[ind][1])):
tree[ind * 2 + 1] = updating[ind][0]
updating[ind * 2 + 1] = updating[ind][::]
updating[ind] = 0
if (l == lb and r == rb):
tree[ind] = value
if (ind * 2 < len(tree)):
tree[ind * 2], tree[ind * 2 + 1] = value, value
updating[ind * 2], updating[ind * 2 + 1] = (value, time), (value, time)
while ind != 1:
ind //= 2
tree[ind] = min(tree[ind * 2], tree[ind * 2 + 1])
else:
m = (lb + rb) // 2
if (l <= m):
update(l, min(m, r), ind * 2, lb, m, value)
if (r > m):
update(max(l, m + 1), r, ind * 2 + 1, m + 1, rb, value)
def get(l, r, ind, lb, rb):
if updating[ind] and ind * 2 < len(tree):
if (not updating[ind * 2] or (updating[ind * 2][1] < updating[ind][1])):
tree[ind * 2] = updating[ind][0]
updating[ind * 2] = updating[ind][::]
if (not updating[ind * 2 + 1] or (updating[ind * 2 + 1][1] < updating[ind][1])):
tree[ind * 2 + 1] = updating[ind][0]
updating[ind * 2 + 1] = updating[ind][::]
updating[ind] = 0
if (l == lb and r == rb):
return tree[ind]
else:
m = (lb + rb) // 2
ans = float('inf')
if (l <= m):
ans = get(l, min(m, r), ind * 2, lb, m)
if (r > m):
ans = min(ans, get(max(l, m + 1), r, ind * 2 + 1, m + 1, rb))
return ans
def getvalue(ind):
if (ind < len(compress)):
return (values[compress[ind] % n + n * (not(compress[ind] % n))])
else:
return 0
size = 1
while (size < len(ar) * 2):
size *= 2
tree = [float('inf') for i in range(2 * size)]
updating = [0 for i in range (2 * size)]
for i in range(size, 2 * size):
if i - size + 1 in counting:
tree[i] = counting[i - size + 1]
else:
tree[i] = getvalue(i - size + 1)
for i in range(size - 1, 0, -1):
tree[i] = min(tree[i * 2], tree[i * 2 + 1])
for time in range(m):
if questions[time][0] == 1:
update(d[questions[time][1]], d[questions[time][2]], 1, 1, size, questions[time][-1])
else:
stdout.write(str(get(d[questions[time][1]], d[questions[time][2]], 1, 1, size)) + '\n')
'''
10 10
4 2 3 8 1 2 1 7 5 4
1
2 13 16
'''
```
No
| 13,816 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of positive integers and q queries to this array. There are two types of queries:
* 1 l r x β for each index i such that l β€ i β€ r set ai = x.
* 2 l r β find the minimum among such ai that l β€ i β€ r.
We decided that this problem is too easy. So the array a is given in a compressed form: there is an array b consisting of n elements and a number k in the input, and before all queries a is equal to the concatenation of k arrays b (so the size of a is nΒ·k).
Input
The first line contains two integers n and k (1 β€ n β€ 105, 1 β€ k β€ 104).
The second line contains n integers β elements of the array b (1 β€ bi β€ 109).
The third line contains one integer q (1 β€ q β€ 105).
Then q lines follow, each representing a query. Each query is given either as 1 l r x β set all elements in the segment from l till r (including borders) to x (1 β€ l β€ r β€ nΒ·k, 1 β€ x β€ 109) or as 2 l r β find the minimum among all elements in the segment from l till r (1 β€ l β€ r β€ nΒ·k).
Output
For each query of type 2 print the answer to this query β the minimum on the corresponding segment.
Examples
Input
3 1
1 2 3
3
2 1 3
1 1 2 4
2 1 3
Output
1
3
Input
3 2
1 2 3
5
2 4 4
1 4 4 5
2 4 4
1 1 6 1
2 6 6
Output
1
5
1
Submitted Solution:
```
from sys import stdin, stdout
n, k = map(int, stdin.readline().split())
values = [0] + list(map(int, stdin.readline().split()))
m = int(stdin.readline())
questions = []
ar = set()
for i in range(m):
questions.append(tuple(map(int, stdin.readline().strip().split())))
ar.add(int(questions[-1][1]))
ar.add(int(questions[-1][2]))
compress = [0]
ar = sorted(list(ar))
pw = 1
while (2 ** pw < n):
pw += 1
sparse = [[float('inf') for j in range(len(values))] for i in range(pw)]
sparse[0] = values
for i in range(1, pw):
for j in range(1, n - 2 ** i + 2):
sparse[i][j] = min(sparse[i - 1][j], sparse[i - 1][j + 2 ** (i - 1)])
mn = min(values[1:])
def getmin(first, second):
if second - first + 1 >= n:
return mn
else:
second = second % n + n * (not(second % n))
first = first % n + n * (not(first % n))
if second < first:
return min(getmin(1, second), getmin(first, n))
else:
length = second - first + 1
pw = 0
while 2 ** pw < length:
pw += 1
return min(sparse[pw][first], sparse[pw][second - 2 ** pw + 1])
counting = {}
d = {}
for i in range(len(ar)):
compress.append(ar[i])
d[ar[i]] = len(compress) - 1
if i != len(ar) - 1 and ar[i + 1] - ar[i] > 1:
compress.append(ar[i] + 1)
counting[len(compress) - 1] = float('inf')
d[ar[i] + 1] = len(compress) - 1
for ind in counting:
counting[ind] = getmin(compress[ind - 1] + 1, compress[ind + 1] + 1)
def update(l, r, ind, lb, rb, value):
if updating[ind] and ind * 2 < len(tree):
if (not updating[ind * 2] or (updating[ind * 2][1] < updating[ind][1])):
tree[ind * 2] = updating[ind][0]
updating[ind * 2] = updating[ind][::]
if (not updating[ind * 2 + 1] or (updating[ind * 2 + 1][1] < updating[ind][1])):
tree[ind * 2 + 1] = updating[ind][0]
updating[ind * 2 + 1] = updating[ind][::]
updating[ind] = 0
if (l == lb and r == rb):
tree[ind] = value
if (ind * 2 < len(tree)):
tree[ind * 2], tree[ind * 2 + 1] = value, value
updating[ind * 2], updating[ind * 2 + 1] = (value, time), (value, time)
while ind != 1:
ind //= 2
tree[ind] = min(tree[ind * 2], tree[ind * 2 + 1])
else:
m = (lb + rb) // 2
if (l <= m):
update(l, min(m, r), ind * 2, lb, m, value)
if (r > m):
update(max(l, m + 1), r, ind * 2 + 1, m + 1, rb, value)
def get(l, r, ind, lb, rb):
if updating[ind] and ind * 2 < len(tree):
if (not updating[ind * 2] or (updating[ind * 2][1] < updating[ind][1])):
tree[ind * 2] = updating[ind][0]
updating[ind * 2] = updating[ind][::]
if (not updating[ind * 2 + 1] or (updating[ind * 2 + 1][1] < updating[ind][1])):
tree[ind * 2 + 1] = updating[ind][0]
updating[ind * 2 + 1] = updating[ind][::]
updating[ind] = 0
if (l == lb and r == rb):
return tree[ind]
else:
m = (lb + rb) // 2
ans = float('inf')
if (l <= m):
ans = get(l, min(m, r), ind * 2, lb, m)
if (r > m):
ans = min(ans, get(max(l, m + 1), r, ind * 2 + 1, m + 1, rb))
return ans
def getvalue(ind):
if (ind < len(compress)):
return (values[compress[ind] % n + n * (not(compress[ind] % n))])
else:
return 0
size = 1
while (size < len(ar) * 2):
size *= 2
tree = [float('inf') for i in range(2 * size)]
updating = [0 for i in range (2 * size)]
for i in range(size, 2 * size):
if i - size + 1 in counting:
tree[i] = counting[i - size + 1]
else:
tree[i] = getvalue(i - size + 1)
for i in range(size - 1, 0, -1):
tree[i] = min(tree[i * 2], tree[i * 2 + 1])
for time in range(m):
if questions[time][0] == 1:
update(d[questions[time][1]], d[questions[time][2]], 1, 1, size, questions[time][-1])
else:
stdout.write(str(get(d[questions[time][1]], d[questions[time][2]], 1, 1, size)) + '\n')
'''
10 10
4 2 3 8 1 2 1 7 5 4
10
2 63 87
2 2 48
2 5 62
2 33 85
2 30 100
2 38 94
2 7 81
2 13 16
2 26 36
2 64 96
'''
```
No
| 13,817 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of positive integers and q queries to this array. There are two types of queries:
* 1 l r x β for each index i such that l β€ i β€ r set ai = x.
* 2 l r β find the minimum among such ai that l β€ i β€ r.
We decided that this problem is too easy. So the array a is given in a compressed form: there is an array b consisting of n elements and a number k in the input, and before all queries a is equal to the concatenation of k arrays b (so the size of a is nΒ·k).
Input
The first line contains two integers n and k (1 β€ n β€ 105, 1 β€ k β€ 104).
The second line contains n integers β elements of the array b (1 β€ bi β€ 109).
The third line contains one integer q (1 β€ q β€ 105).
Then q lines follow, each representing a query. Each query is given either as 1 l r x β set all elements in the segment from l till r (including borders) to x (1 β€ l β€ r β€ nΒ·k, 1 β€ x β€ 109) or as 2 l r β find the minimum among all elements in the segment from l till r (1 β€ l β€ r β€ nΒ·k).
Output
For each query of type 2 print the answer to this query β the minimum on the corresponding segment.
Examples
Input
3 1
1 2 3
3
2 1 3
1 1 2 4
2 1 3
Output
1
3
Input
3 2
1 2 3
5
2 4 4
1 4 4 5
2 4 4
1 1 6 1
2 6 6
Output
1
5
1
Submitted Solution:
```
from sys import stdin, stdout
n, k = map(int, stdin.readline().split())
values = [0] + list(map(int, stdin.readline().split()))
m = int(stdin.readline())
questions = []
ar = set()
for i in range(m):
questions.append(tuple(map(int, stdin.readline().strip().split())))
ar.add(int(questions[-1][1]))
ar.add(int(questions[-1][2]))
compress = [0]
ar = sorted(list(ar))
pw = 1
while (2 ** pw < n):
pw += 1
sparse = [[float('inf') for j in range(len(values))] for i in range(pw)]
sparse[0] = values[1:]
for i in range(1, pw):
for j in range(n - 2 ** i + 1):
sparse[i][j] = min(sparse[i - 1][j], sparse[i - 1][j + 2 ** (i - 1)])
mn = min(values[1:])
def getmin(first, second):
if second - first + 1 >= n:
return mn
else:
second = second % n + n * (not(second % n))
first = first % n + n * (not(first % n))
second -= 1
first -= 1
if second < first:
return min(getmin(1, second + 1), getmin(first + 1, n))
else:
length = second - first + 1
pw = 0
while 2 ** pw < length:
pw += 1
return min(sparse[pw][first], sparse[pw][second - 2 ** pw + 1])
counting = {}
d = {}
for i in range(len(ar)):
compress.append(ar[i])
d[ar[i]] = len(compress) - 1
if i != len(ar) - 1 and ar[i + 1] - ar[i] > 1:
compress.append(ar[i] + 1)
counting[len(compress) - 1] = float('inf')
d[ar[i] + 1] = len(compress) - 1
for ind in counting:
counting[ind] = getmin(compress[ind - 1] + 1, compress[ind + 1] + 1)
def update(l, r, ind, lb, rb, value):
if updating[ind] and ind * 2 < len(tree):
if (not updating[ind * 2] or (updating[ind * 2][1] < updating[ind][1])):
tree[ind * 2] = updating[ind][0]
updating[ind * 2] = updating[ind][::]
if (not updating[ind * 2 + 1] or (updating[ind * 2 + 1][1] < updating[ind][1])):
tree[ind * 2 + 1] = updating[ind][0]
updating[ind * 2 + 1] = updating[ind][::]
updating[ind] = 0
if (l == lb and r == rb):
tree[ind] = value
if (ind * 2 < len(tree)):
tree[ind * 2], tree[ind * 2 + 1] = value, value
updating[ind * 2], updating[ind * 2 + 1] = (value, time), (value, time)
while ind != 1:
ind //= 2
tree[ind] = min(tree[ind * 2], tree[ind * 2 + 1])
else:
m = (lb + rb) // 2
if (l <= m):
update(l, min(m, r), ind * 2, lb, m, value)
if (r > m):
update(max(l, m + 1), r, ind * 2 + 1, m + 1, rb, value)
def get(l, r, ind, lb, rb):
if updating[ind] and ind * 2 < len(tree):
if (not updating[ind * 2] or (updating[ind * 2][1] < updating[ind][1])):
tree[ind * 2] = updating[ind][0]
updating[ind * 2] = updating[ind][::]
if (not updating[ind * 2 + 1] or (updating[ind * 2 + 1][1] < updating[ind][1])):
tree[ind * 2 + 1] = updating[ind][0]
updating[ind * 2 + 1] = updating[ind][::]
updating[ind] = 0
if (l == lb and r == rb):
return tree[ind]
else:
m = (lb + rb) // 2
ans = float('inf')
if (l <= m):
ans = get(l, min(m, r), ind * 2, lb, m)
if (r > m):
ans = min(ans, get(max(l, m + 1), r, ind * 2 + 1, m + 1, rb))
return ans
def getvalue(ind):
if (ind < len(compress)):
return (values[compress[ind] % n + n * (not(compress[ind] % n))])
else:
return 0
size = 1
while (size < len(ar) * 2):
size *= 2
tree = [float('inf') for i in range(2 * size)]
updating = [0 for i in range (2 * size)]
for i in range(size, 2 * size):
if i - size + 1 in counting:
tree[i] = counting[i - size + 1]
else:
tree[i] = getvalue(i - size + 1)
for i in range(size - 1, 0, -1):
tree[i] = min(tree[i * 2], tree[i * 2 + 1])
for time in range(m):
if questions[time][0] == 1:
update(d[questions[time][1]], d[questions[time][2]], 1, 1, size, questions[time][-1])
else:
stdout.write(str(get(d[questions[time][1]], d[questions[time][2]], 1, 1, size)) + '\n')
'''
10 10
4 2 3 8 1 2 1 7 5 4
10
2 63 87
2 2 48
2 5 62
2 33 85
2 30 100
2 38 94
2 7 81
2 13 16
2 26 36
2 64 96
'''
```
No
| 13,818 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of positive integers and q queries to this array. There are two types of queries:
* 1 l r x β for each index i such that l β€ i β€ r set ai = x.
* 2 l r β find the minimum among such ai that l β€ i β€ r.
We decided that this problem is too easy. So the array a is given in a compressed form: there is an array b consisting of n elements and a number k in the input, and before all queries a is equal to the concatenation of k arrays b (so the size of a is nΒ·k).
Input
The first line contains two integers n and k (1 β€ n β€ 105, 1 β€ k β€ 104).
The second line contains n integers β elements of the array b (1 β€ bi β€ 109).
The third line contains one integer q (1 β€ q β€ 105).
Then q lines follow, each representing a query. Each query is given either as 1 l r x β set all elements in the segment from l till r (including borders) to x (1 β€ l β€ r β€ nΒ·k, 1 β€ x β€ 109) or as 2 l r β find the minimum among all elements in the segment from l till r (1 β€ l β€ r β€ nΒ·k).
Output
For each query of type 2 print the answer to this query β the minimum on the corresponding segment.
Examples
Input
3 1
1 2 3
3
2 1 3
1 1 2 4
2 1 3
Output
1
3
Input
3 2
1 2 3
5
2 4 4
1 4 4 5
2 4 4
1 1 6 1
2 6 6
Output
1
5
1
Submitted Solution:
```
from sys import stdin, stdout
n, k = map(int, stdin.readline().split())
values = list(map(int, stdin.readline().split())) * k
m = int(stdin.readline())
n *= k
size = 1
while (size < n):
size *= 2
tree = [float('inf') for i in range(2 * size)]
updating = [0 for i in range (2 * size)]
for i in range(n):
tree[size + i] = values[i]
for i in range(size - 1, 0, -1):
tree[i] = min(tree[i * 2], tree[i * 2 + 1])
def update(l, r, ind, lb, rb, value):
if updating[ind] and ind * 2 < len(tree):
if (not updating[ind * 2] or (updating[ind * 2][1] < updating[ind][1])):
tree[ind * 2] = updating[ind][0]
updating[ind * 2] = updating[ind][::]
if (not updating[ind * 2 + 1] or (updating[ind * 2 + 1][1] < updating[ind][1])):
tree[ind * 2 + 1] = updating[ind][0]
updating[ind * 2 + 1] = updating[ind][::]
updating[ind] = 0
if (l == lb and r == rb):
tree[ind] = value
if (ind * 2 < len(tree)):
tree[ind * 2], tree[ind * 2 + 1] = value, value
updating[ind * 2], updating[ind * 2 + 1] = (value, time), (value, time)
while ind != 1:
ind //= 2
tree[ind] = min(tree[ind * 2], tree[ind * 2 + 1])
if min(tree[ind * 2], tree[ind * 2 + 1]) != value:
break
else:
m = (lb + rb) // 2
if (l <= m):
update(l, min(m, r), ind * 2, lb, m, value)
if (r > m):
update(max(l, m + 1), r, ind * 2 + 1, m + 1, rb, value)
def get(l, r, ind, lb, rb):
if updating[ind] and ind * 2 < len(tree):
if (not updating[ind * 2] or (updating[ind * 2][1] < updating[ind][1])):
tree[ind * 2] = updating[ind][0]
updating[ind * 2] = updating[ind][::]
if (not updating[ind * 2 + 1] or (updating[ind * 2 + 1][1] < updating[ind][1])):
tree[ind * 2 + 1] = updating[ind][0]
updating[ind * 2 + 1] = updating[ind][::]
updating[ind] = 0
if (l == lb and r == rb):
return tree[ind]
else:
m = (lb + rb) // 2
ans = float('inf')
if (l <= m):
ans = get(l, min(m, r), ind * 2, lb, m)
if (r > m):
ans = min(ans, get(max(l, m + 1), r, ind * 2 + 1, m + 1, rb))
return ans
for time in range(1, m + 1):
questions = tuple(map(int, stdin.readline().split()))
if questions[0] == 1:
update(questions[1], questions[2], 1, 1, size, questions[-1])
else:
stdout.write('\n' + str(get(questions[1], questions[2], 1, 1, size)))
```
No
| 13,819 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 β€ n β€ 103) β the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| β€ 103) β the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k β the number of good points.
Then, print k integers, each on their own line β the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good.
Tags: brute force, geometry, math
Correct Solution:
```
n = int(input())
points = []
for p in range(n):
points.append(list(map(int, input().split())))
def dot(x, y):
res = 0
for a, b in zip(x, y):
res += a*b
return res
def minus(x, y):
res = []
for a, b in zip(x, y):
res.append(a-b)
return res
indices = set(range(n))
if n <= 50:
for x in range(n):
for y in range(n):
if x != y:
for z in range(n):
if z != y and z != x:
if dot(minus(points[y], points[x]), minus(points[z], points[x])) > 0:
indices.discard(x)
# indices.discard(z)
print(len(indices))
for i in sorted(indices):
print(i+1)
else:
print(0)
```
| 13,820 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 β€ n β€ 103) β the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| β€ 103) β the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k β the number of good points.
Then, print k integers, each on their own line β the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good.
Tags: brute force, geometry, math
Correct Solution:
```
d = lambda i, j, k: sum((a - c) * (b - c) for a, b, c in zip(p[i], p[j], p[k])) * (i != j)
n = int(input())
r = range(n)
p = [list(map(int, input().split())) for i in r]
t = [k + 1 for k in r if all(d(i, j, k) <= 0 for i in r for j in r)] if n < 12 else []
for q in [len(t)] + t: print(q)
```
| 13,821 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 β€ n β€ 103) β the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| β€ 103) β the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k β the number of good points.
Then, print k integers, each on their own line β the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good.
Tags: brute force, geometry, math
Correct Solution:
```
def check(coor1, coor2, coor3):
v1 = [coor2[i] - coor1[i] for i in range(5)]
v2 = [coor3[i] - coor1[i] for i in range(5)]
# print(v1, v2)
return scalar_product(v1, v2)
def scalar_product(coor1, coor2):
a1, a2, a3, a4, a5 = coor1
b1, b2, b3, b4, b5 = coor2
return (a1 * b1 + a2 * b2 + a3 * b3 + a4 * b4 + a5 * b5)
n = int(input())
idx___coor = []
for idx in range(n):
coor = [int(x) for x in input().split()]
idx___coor.append(coor)
if n > 129:
print(0)
else:
good_idxes = []
for idx1, coor1 in enumerate(idx___coor):
is_ok_flag = True
for idx2, coor2 in enumerate(idx___coor):
for idx3, coor3 in enumerate(idx___coor):
# print(pairs_checked)
if idx2 == idx3 or idx1 == idx3 or idx1 == idx2 or not is_ok_flag:
continue
if check(coor1, coor2, coor3) > 0:
is_ok_flag = False
# print(pairs_checked, is_ok_flag, idx1, idx2, idx3)
if is_ok_flag:
good_idxes.append(idx1)
good_idxes = sorted(good_idxes)
good_idxes = [(x + 1) for x in good_idxes]
print(len(good_idxes))
print(*good_idxes)
```
| 13,822 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 β€ n β€ 103) β the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| β€ 103) β the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k β the number of good points.
Then, print k integers, each on their own line β the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good.
Tags: brute force, geometry, math
Correct Solution:
```
import sys
import math
n = int(input())
if(n > 20):
print(0)
sys.exit()
points = []
for i in range(n):
p = list(map(int, input().split()))
points.append(p)
good = []
class Vector:
def __init__(self, first, last):
self.elems = [last[i] - first[i] for i in range(5)]
def __iter__(self):
return self.elems.__iter__()
def l(self):
return (sum(item**2 for item in self))**0.5
def ac(self, other):
ch = sum(self.elems[i] * other.elems[i] for i in range(5))
z = self.l() * other.l()
return ch / z
def check(index):
for i in range(n):
if i == index:
continue
for j in range(i + 1, n):
if j == index:
continue
f = Vector(points[index], points[i])
s = Vector(points[index], points[j])
tmp = f.ac(s)
if math.acos(tmp) < math.pi / 2:
return False
return True
for i in range(n):
if(check(i)):
good.append(str(i + 1))
print(len(good))
if(len(good)):
print(' '.join(good))
```
| 13,823 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 β€ n β€ 103) β the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| β€ 103) β the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k β the number of good points.
Then, print k integers, each on their own line β the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good.
Tags: brute force, geometry, math
Correct Solution:
```
n = int(input());
a = [];
used = [0 for i in range(n)];
def judge(s1, s2, s3):
sum = 0;
for i in range(5):
sum += (a[s1][i] - a[s3][i]) * (a[s2][i] - a[s3][i]);
if sum <= 0:
return 1;
else:
return 0;
for i in range(n):
b = [int (x) for x in input().split(' ')];
a.append(b);
if n <= 100:
num = 0;
flag = 0;
for i in range(n):
flag = 0;
for j in range(n):
if i == j:
continue;
for k in range(j+1, n):
if judge(k, j, i) == 0:
flag = 1;
if flag == 0:
used[i] = 1;
num += 1;
print(num, '\n');
for i in range(n):
if (used[i] == 1):
print(i+1, ' ');
else:
print("0");
```
| 13,824 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 β€ n β€ 103) β the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| β€ 103) β the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k β the number of good points.
Then, print k integers, each on their own line β the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good.
Tags: brute force, geometry, math
Correct Solution:
```
#Code by Sounak, IIESTS
#------------------------------warmup----------------------------
import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
import threading
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#-------------------game starts now-----------------------------------------------------
class Factorial:
def __init__(self, MOD):
self.MOD = MOD
self.factorials = [1, 1]
self.invModulos = [0, 1]
self.invFactorial_ = [1, 1]
def calc(self, n):
if n <= -1:
print("Invalid argument to calculate n!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.factorials):
return self.factorials[n]
nextArr = [0] * (n + 1 - len(self.factorials))
initialI = len(self.factorials)
prev = self.factorials[-1]
m = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = prev * i % m
self.factorials += nextArr
return self.factorials[n]
def inv(self, n):
if n <= -1:
print("Invalid argument to calculate n^(-1)")
print("n must be non-negative value. But the argument was " + str(n))
exit()
p = self.MOD
pi = n % p
if pi < len(self.invModulos):
return self.invModulos[pi]
nextArr = [0] * (n + 1 - len(self.invModulos))
initialI = len(self.invModulos)
for i in range(initialI, min(p, n + 1)):
next = -self.invModulos[p % i] * (p // i) % p
self.invModulos.append(next)
return self.invModulos[pi]
def invFactorial(self, n):
if n <= -1:
print("Invalid argument to calculate (n^(-1))!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.invFactorial_):
return self.invFactorial_[n]
self.inv(n) # To make sure already calculated n^-1
nextArr = [0] * (n + 1 - len(self.invFactorial_))
initialI = len(self.invFactorial_)
prev = self.invFactorial_[-1]
p = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p
self.invFactorial_ += nextArr
return self.invFactorial_[n]
class Combination:
def __init__(self, MOD):
self.MOD = MOD
self.factorial = Factorial(MOD)
def ncr(self, n, k):
if k < 0 or n < k:
return 0
k = min(k, n - k)
f = self.factorial
return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
#-------------------------------------------------------------------------
n=int(input())
l=[]
for i in range(n):
l.append(list(map(int,input().split())))
if n>=50:
print(0)
sys.exit(0)
if n<=2:
print(n)
for i in range(n):
print(i+1)
sys.exit(0)
er=set()
for i in range(n):
f=0
for j in range(n):
for k in range(n):
if i==j or i==k or j==k:
continue
w=0
for d in range(5):
w+=(l[i][d]-l[j][d])*(l[i][d]-l[k][d])
if w>0:
f=1
break
if f==1:
break
if f==0:
er.add(i+1)
print(len(er))
print(*er,sep='\n')
```
| 13,825 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 β€ n β€ 103) β the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| β€ 103) β the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k β the number of good points.
Then, print k integers, each on their own line β the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good.
Tags: brute force, geometry, math
Correct Solution:
```
# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------- fast io --------------------
from math import gcd, ceil
def prod(a, mod=10**9+7):
ans = 1
for each in a:
ans = (ans * each) % mod
return ans
def lcm(a, b): return a * b // gcd(a, b)
def binary(x, length=16):
y = bin(x)[2:]
return y if len(y) >= length else "0" * (length - len(y)) + y
for _ in range(int(input()) if not True else 1):
n = int(input())
#a, b = map(int, input().split())
#c, d = map(int, input().split())
#a = list(map(int, input().split()))
#b = list(map(int, input().split()))
#s = input()
from math import acos,pi,sqrt
points = []
for i in range(n):
points += [list(map(int, input().split()))]
if n <= 69:
ans = []
for i in range(n):
pos=True
for j in range(n):
for k in range(n):
if j==i or k==i or j==k:continue
ab = [points[i][x] - points[j][x] for x in range(5)]
ac = [points[i][x] - points[k][x] for x in range(5)]
xy = sum([ab[x]*ac[x] for x in range(5)])
m = sqrt(sum(x**2 for x in ab)) * sqrt(sum(x**2 for x in ac))
angle = acos(xy/m)*180/pi
if angle < 90:
pos=False
break
if not pos:break
if pos:ans+=[i+1]
print(len(ans))
print(*ans)
else:
print(0)
```
| 13,826 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 β€ n β€ 103) β the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| β€ 103) β the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k β the number of good points.
Then, print k integers, each on their own line β the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good.
Tags: brute force, geometry, math
Correct Solution:
```
n = int(input())
points = []
for i in range(n):
pt = list(map(int, input().split()))
points.append(pt)
flag = [1] * n
for i in range(n):
a = points[i]
ok = False
for j in range(n):
if ok:
break
if j == i:
continue
b = points[j]
for k in range(n):
if k == i or k == j:
continue
c = points[k]
summ = 0
for z in range(5):
u = b[z] - a[z]
v = c[z] - a[z]
summ += u*v
if summ > 0:
flag[i] = 0
ok = True
break
res = []
for i in range(n):
if flag[i]:
res.append(i + 1)
print(len(res))
for i in range(len(res)):
print(res[i])
```
| 13,827 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 β€ n β€ 103) β the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| β€ 103) β the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k β the number of good points.
Then, print k integers, each on their own line β the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good.
Submitted Solution:
```
n = int(input())
p = [tuple(map(int, input().split())) for i in range(n)]
def d(a, b):
return (a[0]-b[0], a[1]-b[1], a[2]-b[2], a[3]-b[3], a[4]-b[4])
def m(a, b):
t = 0
for i in range(5):
t += a[i] * b[i]
return t
good_points = []
for i in range(n):
good = True
for j in range(n):
if j == i:
continue
ab = d(p[j], p[i])
for k in range(j + 1, n):
if k == i:
continue
ac = d(p[k], p[i])
if m(ab, ac) > 0:
good = False
break
if not good:
break
if good:
good_points.append(i)
print(len(good_points))
for i in good_points:
print(i + 1)
```
Yes
| 13,828 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 β€ n β€ 103) β the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| β€ 103) β the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k β the number of good points.
Then, print k integers, each on their own line β the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good.
Submitted Solution:
```
import sys
from math import acos, sqrt, pi
n = int(input())
p = []
def get_angle(a, b, c):
v = [(b[i]-a[i], c[i]-a[i]) for i in range(5)]
sp = sum([v[i][0]*v[i][1] for i in range(5)])
sab = sqrt(sum([v[i][0]*v[i][0] for i in range(5)]))
sac = sqrt(sum([v[i][1]*v[i][1] for i in range(5)]))
if 2*acos(sp/(sab*sac))< pi:
return True
else:
return False
for i in range(n):
p.append(list(map(int, input().split())))
if n>38:
print('0')
sys.exit()
s = set()
t = [False]*n
for k in range(n):
if not t[k]:
for i in range(n):
if k != i:
for j in range(n):
if i != j and k != j:
if get_angle(p[k],p[i],p[j]):
s.add(k)
t[k] = True
if get_angle(p[i],p[k],p[j]):
s.add(i)
t[i] = True
if get_angle(p[j],p[k],p[i]):
s.add(j)
t[j] = True
if t[k]:
break
if t[k]:
break
t[k] = True
s = sorted(list(set([i for i in range(n)]) - s))
print(len(s))
[print(i+1) for i in s]
```
Yes
| 13,829 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 β€ n β€ 103) β the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| β€ 103) β the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k β the number of good points.
Then, print k integers, each on their own line β the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good.
Submitted Solution:
```
n = int(input())
points = [[-1, -1, -1, -1, -1]]
for _ in range(n):
pts = list(map(int, input().split()))
points.append(pts)
def check(i, j, k):
res = 0
for t in range(5):
a = points[j][t] - points[i][t]
b = points[k][t] - points[i][t]
res += a * b
return res > 0
res = []
for i in range(1, n+1):
valid = True
for j in range(1, n+1):
if j != i and valid:
for k in range(1, n+1):
if j != k:
if check(i, j, k):
valid = False
break
if not valid:
break
if valid:
res.append(i)
print(len(res))
print(*res)
```
Yes
| 13,830 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 β€ n β€ 103) β the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| β€ 103) β the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k β the number of good points.
Then, print k integers, each on their own line β the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good.
Submitted Solution:
```
n = int(input())
dots = []
for i in range(n):
dots.append(list(map(int, input().split())))
good_dots = []
for a_index in range(n):
a_dot = dots[a_index]
a_is_good = True
for b_index in range(n):
for c_index in range(n):
if a_index != b_index and a_index != c_index and b_index < c_index:
b_dot = dots[b_index]
c_dot = dots[c_index]
ab_vec = [b_coord - a_coord for a_coord, b_coord in zip(a_dot, b_dot)]
ac_vec = [c_coord - a_coord for a_coord, c_coord in zip(a_dot, c_dot)]
# print(ab_vec)
# print(ac_vec)
prod = sum([a * b for a, b in zip(ab_vec, ac_vec)])
# print(prod)
if prod > 0:
a_is_good = False
break
if not a_is_good:
break
if a_is_good:
good_dots.append(a_index + 1)
print(len(good_dots))
for dot in good_dots:
print(dot)
```
Yes
| 13,831 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 β€ n β€ 103) β the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| β€ 103) β the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k β the number of good points.
Then, print k integers, each on their own line β the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good.
Submitted Solution:
```
n= int(input())
m=[]
mm=n
ans=[]
def sc(i,j,k):
xx=0
for t in range(5):
xx+=(m[i][t]-m[j][t])*(m[i][t]-m[k][t])
return xx
for i in range(n):
ans.append(1)
a,b,c,d,e=map(int,input().split())
m.append([a,b,c,d,e])
for i in range(n):
for j in range(n):
if (i != j):
for k in range(n):
if (i != k) and ( j != k):
if sc(i,j,k) <=0:
ans[i]=-1
for i in range(n):
if ans[i]==1:
mm+=-1
print(mm)
for i in range(n):
if ans[i]==-1:
print(i+1)
```
No
| 13,832 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 β€ n β€ 103) β the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| β€ 103) β the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k β the number of good points.
Then, print k integers, each on their own line β the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good.
Submitted Solution:
```
from math import acos, sqrt, pi
n = int(input())
p = []
def get_angle(a, b, c):
v = [(b[i]-a[i], c[i]-a[i]) for i in range(5)]
sp = sum([v[i][0]*v[i][1] for i in range(5)])
sab = sqrt(sum([v[i][0]*v[i][0] for i in range(5)]))
sac = sqrt(sum([v[i][1]*v[i][1] for i in range(5)]))
if 2*acos(sp/(sab*sac))< pi:
return True
else:
return False
for i in range(n):
p.append(list(map(int, input().split())))
s = set()
t = [False]*n
for k in range(n):
if not t[k]:
for i in range(n):
if not t[i] and k != i:
for j in range(n):
if i != j and k != j and not t[j]:
if get_angle(p[k],p[i],p[j]):
s.add(k)
t[k] = True
if get_angle(p[i],p[k],p[j]):
s.add(i)
t[i] = True
if get_angle(p[j],p[k],p[i]):
s.add(j)
t[j] = True
if t[k] or t[i]:
break
if t[k]:
break
t[k] = True
s = sorted(list(set([i for i in range(n)]) - s))
print(len(s))
[print(i+1) for i in s]
```
No
| 13,833 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 β€ n β€ 103) β the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| β€ 103) β the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k β the number of good points.
Then, print k integers, each on their own line β the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good.
Submitted Solution:
```
from math import pi
n = int(input())
tocke = [list(map(int, input().split())) for i in range(n)]
def vektor(t1, t2):
vector = []
for i in range(5):
vector.append(t2[i] - t1[i])
return vector
def skalarni_produkt(v1, v2):
vsota = 0
for i in range(5):
vsota += v1[i] * v2[i]
return vsota
def dolzina(v1):
return (skalarni_produkt(v1, v1)) ** (1 / 2)
konec = []
for i in range(n):
for j in range(n):
v1 = vektor(tocke[i], tocke[j])
for k in range(n):
v2 = vektor(tocke[i], tocke[k])
if not dolzina(v1) == 0 and not dolzina(v2) == 0:
if skalarni_produkt(v1, v2) <= 0 and not i in konec:
konec.append(i)
print(len(konec))
if not len(konec) == 0:
for i in konec:
print(i + 1)
```
No
| 13,834 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 β€ n β€ 103) β the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| β€ 103) β the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k β the number of good points.
Then, print k integers, each on their own line β the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good.
Submitted Solution:
```
from collections import defaultdict
from math import acos, sqrt
EPS = 10 ** -6
def less_than(a, b):
return b - a > EPS
def dot(pa, pb):
return sum(pa[i] * pb[i] for i in range(5))
def angle(x, y):
try:
return acos(dot(x, y) / sqrt(dot(x, x) ) / sqrt(dot(y, y)))
except:
return 90.0
def main():
n = int(input())
acute = {i: set() for i in range(n)}
points = [[int(x) for x in input().split()] for _ in range(n)]
for i in range(n):
for j in range(i+1, n):
if less_than(angle(points[i], points[j]), 90.0):
acute[i].add(j)
acute[j].add(i)
output = []
for i, acute_set in acute.items():
if len(acute_set) < 2:
output.append(i)
print(len(output))
for i in sorted(output):
print(i + 1)
if __name__ == "__main__":
main()
```
No
| 13,835 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
1. Initially Ivan's score is 0;
2. In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped;
3. Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
Input
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
Output
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
Examples
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
Note
In the first example Ivan will replace the element a1, 2.
Tags: greedy, two pointers
Correct Solution:
```
n,m,k = map(int,input().split())
x,y = 0,0
a = []
for i in range(n):
a.append(list(map(int, input().split())))
for i in zip(*a):
u,v = 0,0
for j in range(n-k+1):
p,q = sum(i[j:j+k]), sum(i[:j])
if p > u:
u = p
v = q
x += u
y += v
print(x,y)
```
| 13,836 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
1. Initially Ivan's score is 0;
2. In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped;
3. Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
Input
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
Output
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
Examples
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
Note
In the first example Ivan will replace the element a1, 2.
Tags: greedy, two pointers
Correct Solution:
```
n,m, k = map(int, input().split())
a = []
for i in range(n):
a.append(list(map(int, input().split())))
ans = 0
ans1 = 0
for j in range(m):
mv = 0
td = 0
cc = 0
no = 0
for i in range(n):
if a[i][j]:
v = 0
for z in range(i, min(i+k, n)):
v += a[z][j]
if v > mv:
mv = v
td = no
no += 1
ans += mv
ans1 += td
print(ans, ans1)
```
| 13,837 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
1. Initially Ivan's score is 0;
2. In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped;
3. Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
Input
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
Output
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
Examples
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
Note
In the first example Ivan will replace the element a1, 2.
Tags: greedy, two pointers
Correct Solution:
```
row,colum,k = map(int, input().split())
colums = [[] for i in range(colum)]
for i in range(row):
temp = list(map(int, input().split()))
for i in range(colum):
colums[i].append(temp[i])
c = 0
ans = 0
for i in colums:
count = 0
for j in range(row-k+1):
count = max(count,i[j:j+k].count(1))
if count==k:
break
for j in range(row-k+1):
if sum(i[j:j+k]) == count:
c+=sum(i[:j])
break
ans+=count
print(ans,c)
```
| 13,838 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
1. Initially Ivan's score is 0;
2. In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped;
3. Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
Input
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
Output
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
Examples
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
Note
In the first example Ivan will replace the element a1, 2.
Tags: greedy, two pointers
Correct Solution:
```
def cal(col,index,k):
one = 0
for i in range(index,len(col)):
if col[i] == 1:
for j in range(i,min(len(col),i+k)):
if col[j] == 1:
one += 1
break
return one
def solve(col,k):
change = 0
score = 0
n = len(col)
for i in range(n):
if col[i] == 1:
one = 0
for j in range(i,min(n,i+k)):
if col[j] == 1:
one += 1
score = one
break
remove = 0
for i in range(n):
if col[i] == 1:
change += 1
one = cal(col,i+1,k)
if one > score:
remove = change
score = one
return score,remove
def main():
n,m,k = map(int,input().split())
matrix = []
for i in range(n):
matrix.append(list(map(int,input().split())))
score = 0
change = 0
for i in range(m):
col = []
for j in range(n):
col.append(matrix[j][i])
curr_score,curr_rem = solve(col,k)
score += curr_score
change += curr_rem
print(score,change)
main()
```
| 13,839 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
1. Initially Ivan's score is 0;
2. In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped;
3. Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
Input
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
Output
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
Examples
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
Note
In the first example Ivan will replace the element a1, 2.
Tags: greedy, two pointers
Correct Solution:
```
import math
#n,m=map(int,input().split())
from collections import Counter
#for i in range(n):
import math
#for _ in range(int(input())):
#n = int(input())
#for _ in range(int(input())):
#n = int(input())
import bisect
'''for _ in range(int(input())):
n=int(input())
n,k=map(int, input().split())
arr = list(map(int, input().split()))'''
n,m,k=map(int,input().split())
R=[list(map(int,input().split()))for i in range(n)]
s=0
t=0
for i in zip(*R):
var=0
tt=0
flag=0
for j in range(n):
if i[j]==1:
f=1
for r in range(j+1,min(j+k,n)):
f+=i[r]
if f>var:
var=f
tt=flag
flag+=1
s+=var
t+=tt
print(s, t)
```
| 13,840 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
1. Initially Ivan's score is 0;
2. In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped;
3. Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
Input
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
Output
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
Examples
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
Note
In the first example Ivan will replace the element a1, 2.
Tags: greedy, two pointers
Correct Solution:
```
f = lambda: map(int, input().split())
n, m, k = f()
s = d = 0
for t in zip(*[f() for i in range(n)]):
p, q = x, y = sum(t[:k]), 0
for j in range(n - k):
p += t[j + k] - t[j]
q += t[j]
if p > x: x, y = p, q
s += x
d += y
print(s, d)
```
| 13,841 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
1. Initially Ivan's score is 0;
2. In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped;
3. Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
Input
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
Output
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
Examples
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
Note
In the first example Ivan will replace the element a1, 2.
Tags: greedy, two pointers
Correct Solution:
```
n, m, k = map(int, input().split()); a = []; b = []; score = []; ct = 0
for i in range(n):
a.append([int(x) for x in input().split()])
for i in range(m): b.append([])
for i in range(m):
for j in range(n):
b[i].append(a[j][i])
for i in range(m):
maxsums = []
for j in range(n):
if b[i][j] == 1:
if j + k < n: maxsums.append(sum(b[i][j:j + k]))
else: maxsums.append(sum(b[i][j:]))
try:
score.append(max(maxsums))
ct += maxsums.index(max(maxsums))
except: pass
print(sum(score), ct)
```
| 13,842 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
1. Initially Ivan's score is 0;
2. In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped;
3. Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
Input
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
Output
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
Examples
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
Note
In the first example Ivan will replace the element a1, 2.
Tags: greedy, two pointers
Correct Solution:
```
# -*- coding: utf-8 -*-
import math
import collections
import bisect
import heapq
import time
import random
"""
created by shhuan at 2017/10/12 23:03
"""
n, m, k = map(int, input().split())
a = []
for i in range(n):
a.append([int(x) for x in input().split()])
removed = 0
score = 0
for c in range(m):
count, sr = 0, 0
for r in range(n):
tc = sum([a[x][c] for x in range(r, min(r+k, n))])
if tc > count:
count = tc
sr = r
score += count
removed += sum([a[r][c] for r in range(sr)])
print(score, removed)
```
| 13,843 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
1. Initially Ivan's score is 0;
2. In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped;
3. Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
Input
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
Output
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
Examples
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
Note
In the first example Ivan will replace the element a1, 2.
Submitted Solution:
```
def main():
n, m, k = [int(x) for x in input().split()]
matr = []
for i in range(n):
matr.append(input().split())
matr = list(zip(*matr))
score = 0
repl = 0
for i in range(m):
window = count1(matr[i][:k])
row_scores = []
for j in range(k, n+k):
if matr[i][j-k] == '1':
row_scores.append(window)
window -= 1
else:
row_scores.append(0)
if j < n and matr[i][j] == '1':
window += 1
row_best = max(row_scores)
ind_best = row_scores.index(row_best)
repl += count1(matr[i][:ind_best])
score += row_best
print(score, repl)
def count1(lst):
return len(list(filter(lambda x: x == '1', lst)))
if __name__ == "__main__":
main()
```
Yes
| 13,844 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
1. Initially Ivan's score is 0;
2. In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped;
3. Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
Input
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
Output
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
Examples
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
Note
In the first example Ivan will replace the element a1, 2.
Submitted Solution:
```
h, w, k = map(int, input().split())
d = [[int(x) for x in input().split()] for _ in range(h)]
d = [[x for x in line] for line in zip(*d)]
totalScore = 0
totalCost = 0
for line in d:
max = 0
cost = 0
cur = 0
curCost = 0
for c1, c2 in zip(line, [0] * k + line):
cur += c1
cur -= c2
curCost += c2
if cur > max:
cost = curCost
max = cur
totalScore += max
totalCost += cost
print(totalScore, totalCost)
```
Yes
| 13,845 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
1. Initially Ivan's score is 0;
2. In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped;
3. Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
Input
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
Output
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
Examples
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
Note
In the first example Ivan will replace the element a1, 2.
Submitted Solution:
```
n, m, k = map(int, input().split())
ar = []
for i in range(n):
ar.append(list(map(int, input().split())))
score = 0
min_moves = 0
for j in range(m):
cr = [ar[i][j] for i in range(n)]
c = 0
maxi = 0
r_s = 0
r_m = 0
for i in range(len(cr)):
if cr[i] == 1:
maxi = sum(cr[i:i+k])
if maxi > r_s:
r_s = maxi
r_m = c
c += 1
score += r_s
min_moves += r_m
print(score, min_moves)
```
Yes
| 13,846 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
1. Initially Ivan's score is 0;
2. In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped;
3. Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
Input
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
Output
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
Examples
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
Note
In the first example Ivan will replace the element a1, 2.
Submitted Solution:
```
nmk = list(map(int, input().split()))
n = nmk[0]
m = nmk[1]
k = nmk[2]
a = []
for i in range(n):
a.append(list(map(int, input().split())))
sum = 0
z = 0
for i in range(m):
sums = [a[0][i]]
for j in range(1, n):
sums.append(sums[-1] + a[j][i])
max_sum = 0
max_ones = 0
cur_ones = 0
for j in range(n):
if a[j][i] == 1:
cur_ones += 1
if j + k - 1 < n:
if j > 0:
s = sums[j + k - 1] - sums[j - 1]
else:
s = sums[j + k - 1]
else:
if j > 0:
s = sums[n - 1] - sums[j - 1]
else:
s = sums[n - 1]
if s > max_sum:
max_sum = s
max_ones = cur_ones - 1
sum += max_sum
z += max_ones
print(sum, z)
```
Yes
| 13,847 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
1. Initially Ivan's score is 0;
2. In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped;
3. Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
Input
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
Output
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
Examples
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
Note
In the first example Ivan will replace the element a1, 2.
Submitted Solution:
```
def find(li,k):
li=li
mm=len(li)
x,y,c,an=0,0,0,0
# print(li)
while x<mm and y<mm:
while y<mm and li[y]-li[x]<k:
y+=1
if y-x>an:
an=y-x
c=x
x+=1
# print(an)
return [an,c]
n,m,k = map(int,input().split())
lis=[]
fin=ans=0
for i in range(n):
a=list(map(int,input().split()))
lis.append(a)
for j in range(m):
tmp=[]
for i in range(n):
if lis[i][j]==1:
tmp.append(i)
a,b=find(tmp,k)
ans+=a
# print(a,ans,j,end=' ')
if b>1:
fin+=(b-1)
print(ans,fin)
```
No
| 13,848 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
1. Initially Ivan's score is 0;
2. In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped;
3. Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
Input
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
Output
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
Examples
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
Note
In the first example Ivan will replace the element a1, 2.
Submitted Solution:
```
import sys
input=sys.stdin.readline
n,m,k=map(int,input().split())
a=[list(map(int,input().split())) for i in range(n)]
s=0
cnt=0
for j in range(m):
score1=0
for i in range(n):
if a[i][j]==1:
for ii in range(i,min(n,i+k)):
if a[ii][j]==1:
score1+=1
a[i][j]=0
break
score2=0
for i in range(n):
if a[i][j]==1:
for ii in range(i,min(n,i+k)):
if a[ii][j]==1:
score2+=1
break
if score2>score1:
cnt+=1
s+=score2
else:
s+=score1
print(s,cnt)
```
No
| 13,849 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
1. Initially Ivan's score is 0;
2. In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped;
3. Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
Input
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
Output
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
Examples
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
Note
In the first example Ivan will replace the element a1, 2.
Submitted Solution:
```
n,m,k = map(int,input().split())
arr = []
for _ in range(n):
arr.append(list(map(int,input().split())))
delete = ans = 0
for i in range(m):
mx = 0
ind = -1
j = 0
while j < n:
if arr[j][i] == 1:
cur = 0
while j < n and arr[j][i] == 1:
cur += 1
j += 1
if cur > mx:
mx = cur
ind = j - cur
j += 1
if mx > 1:
q = 0
while q < ind:
delete += arr[q][i]
q += 1
ans += mx
print(ans,delete)
```
No
| 13,850 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
1. Initially Ivan's score is 0;
2. In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped;
3. Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
Input
The first line contains three integer numbers n, m and k (1 β€ k β€ n β€ 100, 1 β€ m β€ 100).
Then n lines follow, i-th of them contains m integer numbers β the elements of i-th row of matrix a. Each number is either 0 or 1.
Output
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
Examples
Input
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Output
4 1
Input
3 2 1
1 0
0 1
0 0
Output
2 0
Note
In the first example Ivan will replace the element a1, 2.
Submitted Solution:
```
y, x, k = map(int, input().split())
h = [[int(i) for i in input().split()] for j in range(y)]
q, w = 0, 0
sh = -1
for i in range(x):
a = 0
s = 0
for j in range(y):
if h[j][i]:
g = sum([h[lp][i] for lp in range(j, min(j + k, y))])
if g > a:
if sh == i:
s += 1
sh = i
a = g
q += a
w += s
print(q, w)
```
No
| 13,851 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Everybody in Russia uses Gregorian calendar. In this calendar there are 31 days in January, 28 or 29 days in February (depending on whether the year is leap or not), 31 days in March, 30 days in April, 31 days in May, 30 in June, 31 in July, 31 in August, 30 in September, 31 in October, 30 in November, 31 in December.
A year is leap in one of two cases: either its number is divisible by 4, but not divisible by 100, or is divisible by 400. For example, the following years are leap: 2000, 2004, but years 1900 and 2018 are not leap.
In this problem you are given n (1 β€ n β€ 24) integers a1, a2, ..., an, and you have to check if these integers could be durations in days of n consecutive months, according to Gregorian calendar. Note that these months could belong to several consecutive years. In other words, check if there is a month in some year, such that its duration is a1 days, duration of the next month is a2 days, and so on.
Input
The first line contains single integer n (1 β€ n β€ 24) β the number of integers.
The second line contains n integers a1, a2, ..., an (28 β€ ai β€ 31) β the numbers you are to check.
Output
If there are several consecutive months that fit the sequence, print "YES" (without quotes). Otherwise, print "NO" (without quotes).
You can print each letter in arbitrary case (small or large).
Examples
Input
4
31 31 30 31
Output
Yes
Input
2
30 30
Output
No
Input
5
29 31 30 31 30
Output
Yes
Input
3
31 28 30
Output
No
Input
3
31 31 28
Output
Yes
Note
In the first example the integers can denote months July, August, September and October.
In the second example the answer is no, because there are no two consecutive months each having 30 days.
In the third example the months are: February (leap year) β March β April β May β June.
In the fourth example the number of days in the second month is 28, so this is February. March follows February and has 31 days, but not 30, so the answer is NO.
In the fifth example the months are: December β January β February (non-leap year).
Tags: implementation
Correct Solution:
```
def inl(a, b):
"""a in b"""
for i in range(len(b) - len(a)):
if a == b[i:i + len(a)]:
return True
return False
n = int(input())
A = list(map(int, input().split()))
d = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
u = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
ddd = d + d + d
ddu = d + d + u
dud = d + u + d
udd = u + d + d
if inl(A, ddd) or inl(A, ddu) or inl(A, dud) or inl(A, udd):
print("Yes")
else:
print("No")
```
| 13,852 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Everybody in Russia uses Gregorian calendar. In this calendar there are 31 days in January, 28 or 29 days in February (depending on whether the year is leap or not), 31 days in March, 30 days in April, 31 days in May, 30 in June, 31 in July, 31 in August, 30 in September, 31 in October, 30 in November, 31 in December.
A year is leap in one of two cases: either its number is divisible by 4, but not divisible by 100, or is divisible by 400. For example, the following years are leap: 2000, 2004, but years 1900 and 2018 are not leap.
In this problem you are given n (1 β€ n β€ 24) integers a1, a2, ..., an, and you have to check if these integers could be durations in days of n consecutive months, according to Gregorian calendar. Note that these months could belong to several consecutive years. In other words, check if there is a month in some year, such that its duration is a1 days, duration of the next month is a2 days, and so on.
Input
The first line contains single integer n (1 β€ n β€ 24) β the number of integers.
The second line contains n integers a1, a2, ..., an (28 β€ ai β€ 31) β the numbers you are to check.
Output
If there are several consecutive months that fit the sequence, print "YES" (without quotes). Otherwise, print "NO" (without quotes).
You can print each letter in arbitrary case (small or large).
Examples
Input
4
31 31 30 31
Output
Yes
Input
2
30 30
Output
No
Input
5
29 31 30 31 30
Output
Yes
Input
3
31 28 30
Output
No
Input
3
31 31 28
Output
Yes
Note
In the first example the integers can denote months July, August, September and October.
In the second example the answer is no, because there are no two consecutive months each having 30 days.
In the third example the months are: February (leap year) β March β April β May β June.
In the fourth example the number of days in the second month is 28, so this is February. March follows February and has 31 days, but not 30, so the answer is NO.
In the fifth example the months are: December β January β February (non-leap year).
Tags: implementation
Correct Solution:
```
month1 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
month2 = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
month11 = month1 + month1
month12 = month1 + month2
month21 = month2 + month1
month11 += month11
month12 += month12
month21 += month21
n = int(input())
a = list(map(int, input().split()))
flag = False
for i in range(49 - n):
if a == month11[i:i + n]:
flag = True
elif a == month12[i: i + n]:
flag = True
elif a == month21[i:i + n]:
flag = True
if flag:
print("Yes")
else:
print("No")
```
| 13,853 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Everybody in Russia uses Gregorian calendar. In this calendar there are 31 days in January, 28 or 29 days in February (depending on whether the year is leap or not), 31 days in March, 30 days in April, 31 days in May, 30 in June, 31 in July, 31 in August, 30 in September, 31 in October, 30 in November, 31 in December.
A year is leap in one of two cases: either its number is divisible by 4, but not divisible by 100, or is divisible by 400. For example, the following years are leap: 2000, 2004, but years 1900 and 2018 are not leap.
In this problem you are given n (1 β€ n β€ 24) integers a1, a2, ..., an, and you have to check if these integers could be durations in days of n consecutive months, according to Gregorian calendar. Note that these months could belong to several consecutive years. In other words, check if there is a month in some year, such that its duration is a1 days, duration of the next month is a2 days, and so on.
Input
The first line contains single integer n (1 β€ n β€ 24) β the number of integers.
The second line contains n integers a1, a2, ..., an (28 β€ ai β€ 31) β the numbers you are to check.
Output
If there are several consecutive months that fit the sequence, print "YES" (without quotes). Otherwise, print "NO" (without quotes).
You can print each letter in arbitrary case (small or large).
Examples
Input
4
31 31 30 31
Output
Yes
Input
2
30 30
Output
No
Input
5
29 31 30 31 30
Output
Yes
Input
3
31 28 30
Output
No
Input
3
31 31 28
Output
Yes
Note
In the first example the integers can denote months July, August, September and October.
In the second example the answer is no, because there are no two consecutive months each having 30 days.
In the third example the months are: February (leap year) β March β April β May β June.
In the fourth example the number of days in the second month is 28, so this is February. March follows February and has 31 days, but not 30, so the answer is NO.
In the fifth example the months are: December β January β February (non-leap year).
Tags: implementation
Correct Solution:
```
n=int(input())
lst = list(map(int, input().strip().split(' ')))
l1=[31,28,31,30,31,30,31,31,30,31,30,31]*12
l1[13]=29
l1[61]=29
l1[97]=29
l1[133]=29
f=0
for i in range(144-n+1):
if l1[i:i+n]==lst:
f=1
print('yes')
break
if f==0:
print('no')
```
| 13,854 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Everybody in Russia uses Gregorian calendar. In this calendar there are 31 days in January, 28 or 29 days in February (depending on whether the year is leap or not), 31 days in March, 30 days in April, 31 days in May, 30 in June, 31 in July, 31 in August, 30 in September, 31 in October, 30 in November, 31 in December.
A year is leap in one of two cases: either its number is divisible by 4, but not divisible by 100, or is divisible by 400. For example, the following years are leap: 2000, 2004, but years 1900 and 2018 are not leap.
In this problem you are given n (1 β€ n β€ 24) integers a1, a2, ..., an, and you have to check if these integers could be durations in days of n consecutive months, according to Gregorian calendar. Note that these months could belong to several consecutive years. In other words, check if there is a month in some year, such that its duration is a1 days, duration of the next month is a2 days, and so on.
Input
The first line contains single integer n (1 β€ n β€ 24) β the number of integers.
The second line contains n integers a1, a2, ..., an (28 β€ ai β€ 31) β the numbers you are to check.
Output
If there are several consecutive months that fit the sequence, print "YES" (without quotes). Otherwise, print "NO" (without quotes).
You can print each letter in arbitrary case (small or large).
Examples
Input
4
31 31 30 31
Output
Yes
Input
2
30 30
Output
No
Input
5
29 31 30 31 30
Output
Yes
Input
3
31 28 30
Output
No
Input
3
31 31 28
Output
Yes
Note
In the first example the integers can denote months July, August, September and October.
In the second example the answer is no, because there are no two consecutive months each having 30 days.
In the third example the months are: February (leap year) β March β April β May β June.
In the fourth example the number of days in the second month is 28, so this is February. March follows February and has 31 days, but not 30, so the answer is NO.
In the fifth example the months are: December β January β February (non-leap year).
Tags: implementation
Correct Solution:
```
def main():
N = int(input())
A = tuple(map(int, input().split()))
d = {28: {2}, 29: {2}, 30: {4, 6, 9, 11}, 31: {1, 3, 5, 7, 8, 10, 12}}
for m in d[A[0]]:
leap = 1 if A[0] == 29 else 0
for i in range(1, N):
if A[i] == 29:
leap += 1
if m == 12:
m = 1
else:
m += 1
if not m in d[A[i]] or leap > 1:
break
else:
print('YES')
return
print('NO')
main()
```
| 13,855 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Everybody in Russia uses Gregorian calendar. In this calendar there are 31 days in January, 28 or 29 days in February (depending on whether the year is leap or not), 31 days in March, 30 days in April, 31 days in May, 30 in June, 31 in July, 31 in August, 30 in September, 31 in October, 30 in November, 31 in December.
A year is leap in one of two cases: either its number is divisible by 4, but not divisible by 100, or is divisible by 400. For example, the following years are leap: 2000, 2004, but years 1900 and 2018 are not leap.
In this problem you are given n (1 β€ n β€ 24) integers a1, a2, ..., an, and you have to check if these integers could be durations in days of n consecutive months, according to Gregorian calendar. Note that these months could belong to several consecutive years. In other words, check if there is a month in some year, such that its duration is a1 days, duration of the next month is a2 days, and so on.
Input
The first line contains single integer n (1 β€ n β€ 24) β the number of integers.
The second line contains n integers a1, a2, ..., an (28 β€ ai β€ 31) β the numbers you are to check.
Output
If there are several consecutive months that fit the sequence, print "YES" (without quotes). Otherwise, print "NO" (without quotes).
You can print each letter in arbitrary case (small or large).
Examples
Input
4
31 31 30 31
Output
Yes
Input
2
30 30
Output
No
Input
5
29 31 30 31 30
Output
Yes
Input
3
31 28 30
Output
No
Input
3
31 31 28
Output
Yes
Note
In the first example the integers can denote months July, August, September and October.
In the second example the answer is no, because there are no two consecutive months each having 30 days.
In the third example the months are: February (leap year) β March β April β May β June.
In the fourth example the number of days in the second month is 28, so this is February. March follows February and has 31 days, but not 30, so the answer is NO.
In the fifth example the months are: December β January β February (non-leap year).
Tags: implementation
Correct Solution:
```
l1=[31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31]
l2=[31,29,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31]
l1=''.join(map(str,l1))
l2=''.join(map(str,l2))
l3=l1+l2+l1
n=int(input())
l=list(map(str,input().split()))
l=''.join(l)
if l in l3 and l.count('29')<=1:
print ("YES")
else:
print ("NO")
```
| 13,856 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Everybody in Russia uses Gregorian calendar. In this calendar there are 31 days in January, 28 or 29 days in February (depending on whether the year is leap or not), 31 days in March, 30 days in April, 31 days in May, 30 in June, 31 in July, 31 in August, 30 in September, 31 in October, 30 in November, 31 in December.
A year is leap in one of two cases: either its number is divisible by 4, but not divisible by 100, or is divisible by 400. For example, the following years are leap: 2000, 2004, but years 1900 and 2018 are not leap.
In this problem you are given n (1 β€ n β€ 24) integers a1, a2, ..., an, and you have to check if these integers could be durations in days of n consecutive months, according to Gregorian calendar. Note that these months could belong to several consecutive years. In other words, check if there is a month in some year, such that its duration is a1 days, duration of the next month is a2 days, and so on.
Input
The first line contains single integer n (1 β€ n β€ 24) β the number of integers.
The second line contains n integers a1, a2, ..., an (28 β€ ai β€ 31) β the numbers you are to check.
Output
If there are several consecutive months that fit the sequence, print "YES" (without quotes). Otherwise, print "NO" (without quotes).
You can print each letter in arbitrary case (small or large).
Examples
Input
4
31 31 30 31
Output
Yes
Input
2
30 30
Output
No
Input
5
29 31 30 31 30
Output
Yes
Input
3
31 28 30
Output
No
Input
3
31 31 28
Output
Yes
Note
In the first example the integers can denote months July, August, September and October.
In the second example the answer is no, because there are no two consecutive months each having 30 days.
In the third example the months are: February (leap year) β March β April β May β June.
In the fourth example the number of days in the second month is 28, so this is February. March follows February and has 31 days, but not 30, so the answer is NO.
In the fifth example the months are: December β January β February (non-leap year).
Tags: implementation
Correct Solution:
```
arr = "31 28 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31 31 29 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31"
n = int(input())
s = input()
if(s in arr):
print("YES",end = "")
else:
print("NO",end = "")
```
| 13,857 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Everybody in Russia uses Gregorian calendar. In this calendar there are 31 days in January, 28 or 29 days in February (depending on whether the year is leap or not), 31 days in March, 30 days in April, 31 days in May, 30 in June, 31 in July, 31 in August, 30 in September, 31 in October, 30 in November, 31 in December.
A year is leap in one of two cases: either its number is divisible by 4, but not divisible by 100, or is divisible by 400. For example, the following years are leap: 2000, 2004, but years 1900 and 2018 are not leap.
In this problem you are given n (1 β€ n β€ 24) integers a1, a2, ..., an, and you have to check if these integers could be durations in days of n consecutive months, according to Gregorian calendar. Note that these months could belong to several consecutive years. In other words, check if there is a month in some year, such that its duration is a1 days, duration of the next month is a2 days, and so on.
Input
The first line contains single integer n (1 β€ n β€ 24) β the number of integers.
The second line contains n integers a1, a2, ..., an (28 β€ ai β€ 31) β the numbers you are to check.
Output
If there are several consecutive months that fit the sequence, print "YES" (without quotes). Otherwise, print "NO" (without quotes).
You can print each letter in arbitrary case (small or large).
Examples
Input
4
31 31 30 31
Output
Yes
Input
2
30 30
Output
No
Input
5
29 31 30 31 30
Output
Yes
Input
3
31 28 30
Output
No
Input
3
31 31 28
Output
Yes
Note
In the first example the integers can denote months July, August, September and October.
In the second example the answer is no, because there are no two consecutive months each having 30 days.
In the third example the months are: February (leap year) β March β April β May β June.
In the fourth example the number of days in the second month is 28, so this is February. March follows February and has 31 days, but not 30, so the answer is NO.
In the fifth example the months are: December β January β February (non-leap year).
Tags: implementation
Correct Solution:
```
year = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
leap = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
year = list(map(str, year))
leap = list(map(str, leap))
year1 = " ".join(year+year+year+year)
year2 = " ".join(leap+year+year+year)
year3 = " ".join(year+leap+year+year)
year4 = " ".join(year+year+leap+year)
year5 = " ".join(year+year+year+leap)
n = int(input())
str = input()
if str in year1 or str in year2 or str in year3 or str in year4 or str in year5:
print('Yes')
else:
print('No')
```
| 13,858 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Everybody in Russia uses Gregorian calendar. In this calendar there are 31 days in January, 28 or 29 days in February (depending on whether the year is leap or not), 31 days in March, 30 days in April, 31 days in May, 30 in June, 31 in July, 31 in August, 30 in September, 31 in October, 30 in November, 31 in December.
A year is leap in one of two cases: either its number is divisible by 4, but not divisible by 100, or is divisible by 400. For example, the following years are leap: 2000, 2004, but years 1900 and 2018 are not leap.
In this problem you are given n (1 β€ n β€ 24) integers a1, a2, ..., an, and you have to check if these integers could be durations in days of n consecutive months, according to Gregorian calendar. Note that these months could belong to several consecutive years. In other words, check if there is a month in some year, such that its duration is a1 days, duration of the next month is a2 days, and so on.
Input
The first line contains single integer n (1 β€ n β€ 24) β the number of integers.
The second line contains n integers a1, a2, ..., an (28 β€ ai β€ 31) β the numbers you are to check.
Output
If there are several consecutive months that fit the sequence, print "YES" (without quotes). Otherwise, print "NO" (without quotes).
You can print each letter in arbitrary case (small or large).
Examples
Input
4
31 31 30 31
Output
Yes
Input
2
30 30
Output
No
Input
5
29 31 30 31 30
Output
Yes
Input
3
31 28 30
Output
No
Input
3
31 31 28
Output
Yes
Note
In the first example the integers can denote months July, August, September and October.
In the second example the answer is no, because there are no two consecutive months each having 30 days.
In the third example the months are: February (leap year) β March β April β May β June.
In the fourth example the number of days in the second month is 28, so this is February. March follows February and has 31 days, but not 30, so the answer is NO.
In the fifth example the months are: December β January β February (non-leap year).
Tags: implementation
Correct Solution:
```
s="312831303130313130313031"
t="312931303130313130313031";
n=int(input())
s1=s+s+s;
s2=t+s+s;
s3=s+t+s;
s4=s+s+t;
q=""
f=0
p=list(map(str,input().split()))
for i in range(n):
q=q+p[i]
if q in s1:
f=f+1
if q in s2:
f=f+1
if q in s3:
f=f+1
if q in s4:
f=f+1
if(f==0):
print("No")
else:
print("Yes")
```
| 13,859 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Everybody in Russia uses Gregorian calendar. In this calendar there are 31 days in January, 28 or 29 days in February (depending on whether the year is leap or not), 31 days in March, 30 days in April, 31 days in May, 30 in June, 31 in July, 31 in August, 30 in September, 31 in October, 30 in November, 31 in December.
A year is leap in one of two cases: either its number is divisible by 4, but not divisible by 100, or is divisible by 400. For example, the following years are leap: 2000, 2004, but years 1900 and 2018 are not leap.
In this problem you are given n (1 β€ n β€ 24) integers a1, a2, ..., an, and you have to check if these integers could be durations in days of n consecutive months, according to Gregorian calendar. Note that these months could belong to several consecutive years. In other words, check if there is a month in some year, such that its duration is a1 days, duration of the next month is a2 days, and so on.
Input
The first line contains single integer n (1 β€ n β€ 24) β the number of integers.
The second line contains n integers a1, a2, ..., an (28 β€ ai β€ 31) β the numbers you are to check.
Output
If there are several consecutive months that fit the sequence, print "YES" (without quotes). Otherwise, print "NO" (without quotes).
You can print each letter in arbitrary case (small or large).
Examples
Input
4
31 31 30 31
Output
Yes
Input
2
30 30
Output
No
Input
5
29 31 30 31 30
Output
Yes
Input
3
31 28 30
Output
No
Input
3
31 31 28
Output
Yes
Note
In the first example the integers can denote months July, August, September and October.
In the second example the answer is no, because there are no two consecutive months each having 30 days.
In the third example the months are: February (leap year) β March β April β May β June.
In the fourth example the number of days in the second month is 28, so this is February. March follows February and has 31 days, but not 30, so the answer is NO.
In the fifth example the months are: December β January β February (non-leap year).
Submitted Solution:
```
n = int(input())
b = list(map(int, input().split()))
a = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
i = -1
j = -1
u = - 2
if b.count(29) > 1:
print('No')
else:
for l in range(12):
f = True
u += 1
i = u
j = -1
while f and j + 1 < n:
i = (i + 1) % 12
j += 1
if a[i] == b[j] or (a[i] == 28 and (b[j] == 28 or b[j] == 29)):
pass
else:
f = False
if f:
print('Yes')
break
if f == False:
print('No')
```
Yes
| 13,860 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Everybody in Russia uses Gregorian calendar. In this calendar there are 31 days in January, 28 or 29 days in February (depending on whether the year is leap or not), 31 days in March, 30 days in April, 31 days in May, 30 in June, 31 in July, 31 in August, 30 in September, 31 in October, 30 in November, 31 in December.
A year is leap in one of two cases: either its number is divisible by 4, but not divisible by 100, or is divisible by 400. For example, the following years are leap: 2000, 2004, but years 1900 and 2018 are not leap.
In this problem you are given n (1 β€ n β€ 24) integers a1, a2, ..., an, and you have to check if these integers could be durations in days of n consecutive months, according to Gregorian calendar. Note that these months could belong to several consecutive years. In other words, check if there is a month in some year, such that its duration is a1 days, duration of the next month is a2 days, and so on.
Input
The first line contains single integer n (1 β€ n β€ 24) β the number of integers.
The second line contains n integers a1, a2, ..., an (28 β€ ai β€ 31) β the numbers you are to check.
Output
If there are several consecutive months that fit the sequence, print "YES" (without quotes). Otherwise, print "NO" (without quotes).
You can print each letter in arbitrary case (small or large).
Examples
Input
4
31 31 30 31
Output
Yes
Input
2
30 30
Output
No
Input
5
29 31 30 31 30
Output
Yes
Input
3
31 28 30
Output
No
Input
3
31 31 28
Output
Yes
Note
In the first example the integers can denote months July, August, September and October.
In the second example the answer is no, because there are no two consecutive months each having 30 days.
In the third example the months are: February (leap year) β March β April β May β June.
In the fourth example the number of days in the second month is 28, so this is February. March follows February and has 31 days, but not 30, so the answer is NO.
In the fifth example the months are: December β January β February (non-leap year).
Submitted Solution:
```
n = int(input())
l = input()
ynormal = " 31 28 31 30 31 30 31 31 30 31 30 31"
yleap = " 31 29 31 30 31 30 31 31 30 31 30 31"
year = yleap + ynormal*3 + yleap + ynormal*3 + yleap
if l in year:
print('Yes')
else:
print('No')
```
Yes
| 13,861 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Everybody in Russia uses Gregorian calendar. In this calendar there are 31 days in January, 28 or 29 days in February (depending on whether the year is leap or not), 31 days in March, 30 days in April, 31 days in May, 30 in June, 31 in July, 31 in August, 30 in September, 31 in October, 30 in November, 31 in December.
A year is leap in one of two cases: either its number is divisible by 4, but not divisible by 100, or is divisible by 400. For example, the following years are leap: 2000, 2004, but years 1900 and 2018 are not leap.
In this problem you are given n (1 β€ n β€ 24) integers a1, a2, ..., an, and you have to check if these integers could be durations in days of n consecutive months, according to Gregorian calendar. Note that these months could belong to several consecutive years. In other words, check if there is a month in some year, such that its duration is a1 days, duration of the next month is a2 days, and so on.
Input
The first line contains single integer n (1 β€ n β€ 24) β the number of integers.
The second line contains n integers a1, a2, ..., an (28 β€ ai β€ 31) β the numbers you are to check.
Output
If there are several consecutive months that fit the sequence, print "YES" (without quotes). Otherwise, print "NO" (without quotes).
You can print each letter in arbitrary case (small or large).
Examples
Input
4
31 31 30 31
Output
Yes
Input
2
30 30
Output
No
Input
5
29 31 30 31 30
Output
Yes
Input
3
31 28 30
Output
No
Input
3
31 31 28
Output
Yes
Note
In the first example the integers can denote months July, August, September and October.
In the second example the answer is no, because there are no two consecutive months each having 30 days.
In the third example the months are: February (leap year) β March β April β May β June.
In the fourth example the number of days in the second month is 28, so this is February. March follows February and has 31 days, but not 30, so the answer is NO.
In the fifth example the months are: December β January β February (non-leap year).
Submitted Solution:
```
n = int(input())
months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
a = list(map(int, input().split()))
done = False
for i in range(len(months)):
mnt = i
cnt = 0
leaped = False
for j in range(len(a)):
if months[mnt%12] == a[j] or (not leaped and mnt%12 == 1 and months[mnt%12]+1 == a[j]):
if (mnt%12 == 1 and months[mnt%12]+1 == a[j]):
leaped = True
mnt += 1
cnt += 1
else:
break
if cnt == len(a):
done = True
break
print ("Yes" if done else "No")
```
Yes
| 13,862 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Everybody in Russia uses Gregorian calendar. In this calendar there are 31 days in January, 28 or 29 days in February (depending on whether the year is leap or not), 31 days in March, 30 days in April, 31 days in May, 30 in June, 31 in July, 31 in August, 30 in September, 31 in October, 30 in November, 31 in December.
A year is leap in one of two cases: either its number is divisible by 4, but not divisible by 100, or is divisible by 400. For example, the following years are leap: 2000, 2004, but years 1900 and 2018 are not leap.
In this problem you are given n (1 β€ n β€ 24) integers a1, a2, ..., an, and you have to check if these integers could be durations in days of n consecutive months, according to Gregorian calendar. Note that these months could belong to several consecutive years. In other words, check if there is a month in some year, such that its duration is a1 days, duration of the next month is a2 days, and so on.
Input
The first line contains single integer n (1 β€ n β€ 24) β the number of integers.
The second line contains n integers a1, a2, ..., an (28 β€ ai β€ 31) β the numbers you are to check.
Output
If there are several consecutive months that fit the sequence, print "YES" (without quotes). Otherwise, print "NO" (without quotes).
You can print each letter in arbitrary case (small or large).
Examples
Input
4
31 31 30 31
Output
Yes
Input
2
30 30
Output
No
Input
5
29 31 30 31 30
Output
Yes
Input
3
31 28 30
Output
No
Input
3
31 31 28
Output
Yes
Note
In the first example the integers can denote months July, August, September and October.
In the second example the answer is no, because there are no two consecutive months each having 30 days.
In the third example the months are: February (leap year) β March β April β May β June.
In the fourth example the number of days in the second month is 28, so this is February. March follows February and has 31 days, but not 30, so the answer is NO.
In the fifth example the months are: December β January β February (non-leap year).
Submitted Solution:
```
k = "31 28 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31 31 29 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31"
t = int(input())
s = input()
p = k.find(s)
if p == -1:
print("NO")
else:
print("YES")
```
Yes
| 13,863 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Everybody in Russia uses Gregorian calendar. In this calendar there are 31 days in January, 28 or 29 days in February (depending on whether the year is leap or not), 31 days in March, 30 days in April, 31 days in May, 30 in June, 31 in July, 31 in August, 30 in September, 31 in October, 30 in November, 31 in December.
A year is leap in one of two cases: either its number is divisible by 4, but not divisible by 100, or is divisible by 400. For example, the following years are leap: 2000, 2004, but years 1900 and 2018 are not leap.
In this problem you are given n (1 β€ n β€ 24) integers a1, a2, ..., an, and you have to check if these integers could be durations in days of n consecutive months, according to Gregorian calendar. Note that these months could belong to several consecutive years. In other words, check if there is a month in some year, such that its duration is a1 days, duration of the next month is a2 days, and so on.
Input
The first line contains single integer n (1 β€ n β€ 24) β the number of integers.
The second line contains n integers a1, a2, ..., an (28 β€ ai β€ 31) β the numbers you are to check.
Output
If there are several consecutive months that fit the sequence, print "YES" (without quotes). Otherwise, print "NO" (without quotes).
You can print each letter in arbitrary case (small or large).
Examples
Input
4
31 31 30 31
Output
Yes
Input
2
30 30
Output
No
Input
5
29 31 30 31 30
Output
Yes
Input
3
31 28 30
Output
No
Input
3
31 31 28
Output
Yes
Note
In the first example the integers can denote months July, August, September and October.
In the second example the answer is no, because there are no two consecutive months each having 30 days.
In the third example the months are: February (leap year) β March β April β May β June.
In the fourth example the number of days in the second month is 28, so this is February. March follows February and has 31 days, but not 30, so the answer is NO.
In the fifth example the months are: December β January β February (non-leap year).
Submitted Solution:
```
n = int(input())
months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
a = list(map(int, input().split()))
done = False
for i in range(len(months)):
mnt = i
cnt = 0
leaped = False
for j in range(len(a)):
if months[mnt%12] == a[j] or (not leaped and mnt == 1 and months[mnt%12]+1 == a[j]):
if (mnt == 1 and months[mnt%12]+1 == a[j]):
leaped = True
mnt += 1
cnt += 1
else:
break
if cnt == len(a):
done = True
break
print ("Yes" if done else "No")
```
No
| 13,864 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Everybody in Russia uses Gregorian calendar. In this calendar there are 31 days in January, 28 or 29 days in February (depending on whether the year is leap or not), 31 days in March, 30 days in April, 31 days in May, 30 in June, 31 in July, 31 in August, 30 in September, 31 in October, 30 in November, 31 in December.
A year is leap in one of two cases: either its number is divisible by 4, but not divisible by 100, or is divisible by 400. For example, the following years are leap: 2000, 2004, but years 1900 and 2018 are not leap.
In this problem you are given n (1 β€ n β€ 24) integers a1, a2, ..., an, and you have to check if these integers could be durations in days of n consecutive months, according to Gregorian calendar. Note that these months could belong to several consecutive years. In other words, check if there is a month in some year, such that its duration is a1 days, duration of the next month is a2 days, and so on.
Input
The first line contains single integer n (1 β€ n β€ 24) β the number of integers.
The second line contains n integers a1, a2, ..., an (28 β€ ai β€ 31) β the numbers you are to check.
Output
If there are several consecutive months that fit the sequence, print "YES" (without quotes). Otherwise, print "NO" (without quotes).
You can print each letter in arbitrary case (small or large).
Examples
Input
4
31 31 30 31
Output
Yes
Input
2
30 30
Output
No
Input
5
29 31 30 31 30
Output
Yes
Input
3
31 28 30
Output
No
Input
3
31 31 28
Output
Yes
Note
In the first example the integers can denote months July, August, September and October.
In the second example the answer is no, because there are no two consecutive months each having 30 days.
In the third example the months are: February (leap year) β March β April β May β June.
In the fourth example the number of days in the second month is 28, so this is February. March follows February and has 31 days, but not 30, so the answer is NO.
In the fifth example the months are: December β January β February (non-leap year).
Submitted Solution:
```
import sys
k=[31,29,31,30,31,30,31,31,30,31,30,31]
j=[31,28,31,30,31,30,31,31,30,31,30,31]
n=int(input())
a=list(map(int,input().split()))
if n==24:
if a==k+k or a==k+j or a==j+j or a==j+k:
print('Yes')
else:
print('No')
sys.exit()
h=k+k
for i in range(len(h)-n):
if h[i:i+n]==a:
print('Yes')
sys.exit()
h=k+j
for i in range(len(h)-n):
if h[i:i+n]==a:
print('Yes')
sys.exit()
h=j+j
for i in range(len(h)-n):
if h[i:i+n]==a:
print('Yes')
sys.exit()
h=j+k
for i in range(len(h)-n):
if h[i:i+n]==a:
print('Yes')
sys.exit()
print('No')
```
No
| 13,865 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Everybody in Russia uses Gregorian calendar. In this calendar there are 31 days in January, 28 or 29 days in February (depending on whether the year is leap or not), 31 days in March, 30 days in April, 31 days in May, 30 in June, 31 in July, 31 in August, 30 in September, 31 in October, 30 in November, 31 in December.
A year is leap in one of two cases: either its number is divisible by 4, but not divisible by 100, or is divisible by 400. For example, the following years are leap: 2000, 2004, but years 1900 and 2018 are not leap.
In this problem you are given n (1 β€ n β€ 24) integers a1, a2, ..., an, and you have to check if these integers could be durations in days of n consecutive months, according to Gregorian calendar. Note that these months could belong to several consecutive years. In other words, check if there is a month in some year, such that its duration is a1 days, duration of the next month is a2 days, and so on.
Input
The first line contains single integer n (1 β€ n β€ 24) β the number of integers.
The second line contains n integers a1, a2, ..., an (28 β€ ai β€ 31) β the numbers you are to check.
Output
If there are several consecutive months that fit the sequence, print "YES" (without quotes). Otherwise, print "NO" (without quotes).
You can print each letter in arbitrary case (small or large).
Examples
Input
4
31 31 30 31
Output
Yes
Input
2
30 30
Output
No
Input
5
29 31 30 31 30
Output
Yes
Input
3
31 28 30
Output
No
Input
3
31 31 28
Output
Yes
Note
In the first example the integers can denote months July, August, September and October.
In the second example the answer is no, because there are no two consecutive months each having 30 days.
In the third example the months are: February (leap year) β March β April β May β June.
In the fourth example the number of days in the second month is 28, so this is February. March follows February and has 31 days, but not 30, so the answer is NO.
In the fifth example the months are: December β January β February (non-leap year).
Submitted Solution:
```
cal = [31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31,
31, 29, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31,
31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31,
31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31,]
n = int(input())
d = list(map(int, input().split()))
for i in range(len(cal) - n):
if(cal[i:i+n] == d):
print("YES")
exit()
print("NO")
```
No
| 13,866 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Everybody in Russia uses Gregorian calendar. In this calendar there are 31 days in January, 28 or 29 days in February (depending on whether the year is leap or not), 31 days in March, 30 days in April, 31 days in May, 30 in June, 31 in July, 31 in August, 30 in September, 31 in October, 30 in November, 31 in December.
A year is leap in one of two cases: either its number is divisible by 4, but not divisible by 100, or is divisible by 400. For example, the following years are leap: 2000, 2004, but years 1900 and 2018 are not leap.
In this problem you are given n (1 β€ n β€ 24) integers a1, a2, ..., an, and you have to check if these integers could be durations in days of n consecutive months, according to Gregorian calendar. Note that these months could belong to several consecutive years. In other words, check if there is a month in some year, such that its duration is a1 days, duration of the next month is a2 days, and so on.
Input
The first line contains single integer n (1 β€ n β€ 24) β the number of integers.
The second line contains n integers a1, a2, ..., an (28 β€ ai β€ 31) β the numbers you are to check.
Output
If there are several consecutive months that fit the sequence, print "YES" (without quotes). Otherwise, print "NO" (without quotes).
You can print each letter in arbitrary case (small or large).
Examples
Input
4
31 31 30 31
Output
Yes
Input
2
30 30
Output
No
Input
5
29 31 30 31 30
Output
Yes
Input
3
31 28 30
Output
No
Input
3
31 31 28
Output
Yes
Note
In the first example the integers can denote months July, August, September and October.
In the second example the answer is no, because there are no two consecutive months each having 30 days.
In the third example the months are: February (leap year) β March β April β May β June.
In the fourth example the number of days in the second month is 28, so this is February. March follows February and has 31 days, but not 30, so the answer is NO.
In the fifth example the months are: December β January β February (non-leap year).
Submitted Solution:
```
n = int(input())
l = list(map(int, input().split(" ")))
pre = l[0]
lcount = 0 if pre!= 29 else 1
flag = 1
for i in range(1, len(l)):
if pre == 31:
if l[i] != 30 and l[i] != 29 and l[i] != 28 and l[i]!=31:
flag = 0
break
if l[i] == 29:
if lcount:
flag = 0
break
else:
if l[i] != 31:
flag=0
break
pre = l[i]
if flag==1:
print("YES")
else:
print("NO")
```
No
| 13,867 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A ski base is planned to be built in Walrusland. Recently, however, the project is still in the constructing phase. A large land lot was chosen for the construction. It contains n ski junctions, numbered from 1 to n. Initially the junctions aren't connected in any way.
In the constructing process m bidirectional ski roads will be built. The roads are built one after another: first the road number 1 will be built, then the road number 2, and so on. The i-th road connects the junctions with numbers ai and bi.
Track is the route with the following properties:
* The route is closed, that is, it begins and ends in one and the same junction.
* The route contains at least one road.
* The route doesn't go on one road more than once, however it can visit any junction any number of times.
Let's consider the ski base as a non-empty set of roads that can be divided into one or more tracks so that exactly one track went along each road of the chosen set. Besides, each track can consist only of roads from the chosen set. Ski base doesn't have to be connected.
Two ski bases are considered different if they consist of different road sets.
After building each new road the Walrusland government wants to know the number of variants of choosing a ski base based on some subset of the already built roads. The government asks you to help them solve the given problem.
Input
The first line contains two integers n and m (2 β€ n β€ 105, 1 β€ m β€ 105). They represent the number of junctions and the number of roads correspondingly. Then on m lines follows the description of the roads in the order in which they were built. Each road is described by a pair of integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of the connected junctions. There could be more than one road between a pair of junctions.
Output
Print m lines: the i-th line should represent the number of ways to build a ski base after the end of construction of the road number i. The numbers should be printed modulo 1000000009 (109 + 9).
Examples
Input
3 4
1 3
2 3
1 2
1 2
Output
0
0
1
3
Note
Let us have 3 junctions and 4 roads between the junctions have already been built (as after building all the roads in the sample): 1 and 3, 2 and 3, 2 roads between junctions 1 and 2. The land lot for the construction will look like this:
<image>
The land lot for the construction will look in the following way:
<image>
We can choose a subset of roads in three ways:
<image>
In the first and the second ways you can choose one path, for example, 1 - 2 - 3 - 1. In the first case you can choose one path 1 - 2 - 1.
Tags: combinatorics, dsu, graphs
Correct Solution:
```
import sys
from array import array # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
class UnionFind(object):
__slots__ = ['nodes']
def __init__(self, n: int):
self.nodes = [-1] * n
def find(self, x: int) -> int:
if self.nodes[x] < 0:
return x
else:
self.nodes[x] = self.find(self.nodes[x])
return self.nodes[x]
def unite(self, x: int, y: int) -> bool:
root_x, root_y, nodes = self.find(x), self.find(y), self.nodes
if root_x != root_y:
if nodes[root_x] > nodes[root_y]:
root_x, root_y = root_y, root_x
nodes[root_x] += nodes[root_y]
nodes[root_y] = root_x
return root_x != root_y
if __name__ == '__main__':
n, m = map(int, input().split())
uf = UnionFind(n + 10)
ans = [0] * m
x = 1
mod = 10**9 + 9
for i, (u, v) in enumerate(map(int, input().split()) for _ in range(m)):
if not uf.unite(u, v):
x = x * 2 % mod
ans[i] = (x - 1) % mod
sys.stdout.buffer.write('\n'.join(map(str, ans)).encode('utf-8'))
```
| 13,868 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A ski base is planned to be built in Walrusland. Recently, however, the project is still in the constructing phase. A large land lot was chosen for the construction. It contains n ski junctions, numbered from 1 to n. Initially the junctions aren't connected in any way.
In the constructing process m bidirectional ski roads will be built. The roads are built one after another: first the road number 1 will be built, then the road number 2, and so on. The i-th road connects the junctions with numbers ai and bi.
Track is the route with the following properties:
* The route is closed, that is, it begins and ends in one and the same junction.
* The route contains at least one road.
* The route doesn't go on one road more than once, however it can visit any junction any number of times.
Let's consider the ski base as a non-empty set of roads that can be divided into one or more tracks so that exactly one track went along each road of the chosen set. Besides, each track can consist only of roads from the chosen set. Ski base doesn't have to be connected.
Two ski bases are considered different if they consist of different road sets.
After building each new road the Walrusland government wants to know the number of variants of choosing a ski base based on some subset of the already built roads. The government asks you to help them solve the given problem.
Input
The first line contains two integers n and m (2 β€ n β€ 105, 1 β€ m β€ 105). They represent the number of junctions and the number of roads correspondingly. Then on m lines follows the description of the roads in the order in which they were built. Each road is described by a pair of integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of the connected junctions. There could be more than one road between a pair of junctions.
Output
Print m lines: the i-th line should represent the number of ways to build a ski base after the end of construction of the road number i. The numbers should be printed modulo 1000000009 (109 + 9).
Examples
Input
3 4
1 3
2 3
1 2
1 2
Output
0
0
1
3
Note
Let us have 3 junctions and 4 roads between the junctions have already been built (as after building all the roads in the sample): 1 and 3, 2 and 3, 2 roads between junctions 1 and 2. The land lot for the construction will look like this:
<image>
The land lot for the construction will look in the following way:
<image>
We can choose a subset of roads in three ways:
<image>
In the first and the second ways you can choose one path, for example, 1 - 2 - 3 - 1. In the first case you can choose one path 1 - 2 - 1.
Tags: combinatorics, dsu, graphs
Correct Solution:
```
n, m = map(int, input().split())
p, r = list(range(n + 1)), [0] * (n + 1)
def find(x):
if p[x] != x:
p[x] = find(p[x])
return p[x]
def union(x, y):
x, y = find(x), find(y)
if x == y: return 0
if r[x] < r[y]: x, y = y, x
p[y] = x
r[x] += r[x] == r[y]
return 1
MOD = 10 ** 9 + 9
ans = 1
out = []
for _ in range(m):
u, v = map(int, input().split())
if not union(u, v):
ans = (ans << 1) % MOD
out.append((ans + MOD - 1) % MOD)
print(*out, sep='\n')
```
| 13,869 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A ski base is planned to be built in Walrusland. Recently, however, the project is still in the constructing phase. A large land lot was chosen for the construction. It contains n ski junctions, numbered from 1 to n. Initially the junctions aren't connected in any way.
In the constructing process m bidirectional ski roads will be built. The roads are built one after another: first the road number 1 will be built, then the road number 2, and so on. The i-th road connects the junctions with numbers ai and bi.
Track is the route with the following properties:
* The route is closed, that is, it begins and ends in one and the same junction.
* The route contains at least one road.
* The route doesn't go on one road more than once, however it can visit any junction any number of times.
Let's consider the ski base as a non-empty set of roads that can be divided into one or more tracks so that exactly one track went along each road of the chosen set. Besides, each track can consist only of roads from the chosen set. Ski base doesn't have to be connected.
Two ski bases are considered different if they consist of different road sets.
After building each new road the Walrusland government wants to know the number of variants of choosing a ski base based on some subset of the already built roads. The government asks you to help them solve the given problem.
Input
The first line contains two integers n and m (2 β€ n β€ 105, 1 β€ m β€ 105). They represent the number of junctions and the number of roads correspondingly. Then on m lines follows the description of the roads in the order in which they were built. Each road is described by a pair of integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of the connected junctions. There could be more than one road between a pair of junctions.
Output
Print m lines: the i-th line should represent the number of ways to build a ski base after the end of construction of the road number i. The numbers should be printed modulo 1000000009 (109 + 9).
Examples
Input
3 4
1 3
2 3
1 2
1 2
Output
0
0
1
3
Note
Let us have 3 junctions and 4 roads between the junctions have already been built (as after building all the roads in the sample): 1 and 3, 2 and 3, 2 roads between junctions 1 and 2. The land lot for the construction will look like this:
<image>
The land lot for the construction will look in the following way:
<image>
We can choose a subset of roads in three ways:
<image>
In the first and the second ways you can choose one path, for example, 1 - 2 - 3 - 1. In the first case you can choose one path 1 - 2 - 1.
Submitted Solution:
```
import sys
from array import array # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
class UnionFind(object):
__slots__ = ['nodes']
def __init__(self, n: int):
self.nodes = [-1] * n
def find(self, x: int) -> int:
if self.nodes[x] < 0:
return x
else:
self.nodes[x] = self.find(self.nodes[x])
return self.nodes[x]
def unite(self, x: int, y: int) -> bool:
root_x, root_y, nodes = self.find(x), self.find(y), self.nodes
if root_x != root_y:
if nodes[root_x] > nodes[root_y]:
root_x, root_y = root_y, root_x
nodes[root_x] += nodes[root_y]
nodes[root_y] = root_x
return root_x != root_y
if __name__ == '__main__':
n, m = map(int, input().split())
uf = UnionFind(n + 10)
ans = [0] * m
x = 1
for i, (u, v) in enumerate(map(int, input().split()) for _ in range(m)):
if not uf.unite(u, v):
x *= 2
ans[i] = x - 1
sys.stdout.buffer.write('\n'.join(map(str, ans)).encode('utf-8'))
```
No
| 13,870 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A ski base is planned to be built in Walrusland. Recently, however, the project is still in the constructing phase. A large land lot was chosen for the construction. It contains n ski junctions, numbered from 1 to n. Initially the junctions aren't connected in any way.
In the constructing process m bidirectional ski roads will be built. The roads are built one after another: first the road number 1 will be built, then the road number 2, and so on. The i-th road connects the junctions with numbers ai and bi.
Track is the route with the following properties:
* The route is closed, that is, it begins and ends in one and the same junction.
* The route contains at least one road.
* The route doesn't go on one road more than once, however it can visit any junction any number of times.
Let's consider the ski base as a non-empty set of roads that can be divided into one or more tracks so that exactly one track went along each road of the chosen set. Besides, each track can consist only of roads from the chosen set. Ski base doesn't have to be connected.
Two ski bases are considered different if they consist of different road sets.
After building each new road the Walrusland government wants to know the number of variants of choosing a ski base based on some subset of the already built roads. The government asks you to help them solve the given problem.
Input
The first line contains two integers n and m (2 β€ n β€ 105, 1 β€ m β€ 105). They represent the number of junctions and the number of roads correspondingly. Then on m lines follows the description of the roads in the order in which they were built. Each road is described by a pair of integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of the connected junctions. There could be more than one road between a pair of junctions.
Output
Print m lines: the i-th line should represent the number of ways to build a ski base after the end of construction of the road number i. The numbers should be printed modulo 1000000009 (109 + 9).
Examples
Input
3 4
1 3
2 3
1 2
1 2
Output
0
0
1
3
Note
Let us have 3 junctions and 4 roads between the junctions have already been built (as after building all the roads in the sample): 1 and 3, 2 and 3, 2 roads between junctions 1 and 2. The land lot for the construction will look like this:
<image>
The land lot for the construction will look in the following way:
<image>
We can choose a subset of roads in three ways:
<image>
In the first and the second ways you can choose one path, for example, 1 - 2 - 3 - 1. In the first case you can choose one path 1 - 2 - 1.
Submitted Solution:
```
n, m = map(int, input().split())
p, r = list(range(n + 1)), [0] * (n + 1)
def find(x):
if p[x] != x:
p[x] = find(p[x])
return p[x]
def union(x, y):
x, y = find(x), find(y)
if x == y: return 0
if r[x] < r[y]: x, y = y, x
p[y] = x
r[x] += r[x] == r[y]
return 1
MOD = 10 ** 9 + 7
ans = 1
out = []
for _ in range(m):
u, v = map(int, input().split())
if not union(u, v):
ans = (ans << 1) % MOD
out.append((ans + MOD - 1) % MOD)
print(*out, sep='\n')
```
No
| 13,871 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to wolf attacks. He thus decided to place some shepherd dogs in such a way that all his sheep are protected.
The pasture is a rectangle consisting of R Γ C cells. Each cell is either empty, contains a sheep, a wolf or a dog. Sheep and dogs always stay in place, but wolves can roam freely around the pasture, by repeatedly moving to the left, right, up or down to a neighboring cell. When a wolf enters a cell with a sheep, it consumes it. However, no wolf can enter a cell with a dog.
Initially there are no dogs. Place dogs onto the pasture in such a way that no wolf can reach any sheep, or determine that it is impossible. Note that since you have many dogs, you do not need to minimize their number.
Input
First line contains two integers R (1 β€ R β€ 500) and C (1 β€ C β€ 500), denoting the number of rows and the numbers of columns respectively.
Each of the following R lines is a string consisting of exactly C characters, representing one row of the pasture. Here, 'S' means a sheep, 'W' a wolf and '.' an empty cell.
Output
If it is impossible to protect all sheep, output a single line with the word "No".
Otherwise, output a line with the word "Yes". Then print R lines, representing the pasture after placing dogs. Again, 'S' means a sheep, 'W' a wolf, 'D' is a dog and '.' an empty space. You are not allowed to move, remove or add a sheep or a wolf.
If there are multiple solutions, you may print any of them. You don't have to minimize the number of dogs.
Examples
Input
6 6
..S...
..S.W.
.S....
..W...
...W..
......
Output
Yes
..SD..
..SDW.
.SD...
.DW...
DD.W..
......
Input
1 2
SW
Output
No
Input
5 5
.S...
...S.
S....
...S.
.S...
Output
Yes
.S...
...S.
S.D..
...S.
.S...
Note
In the first example, we can split the pasture into two halves, one containing wolves and one containing sheep. Note that the sheep at (2,1) is safe, as wolves cannot move diagonally.
In the second example, there are no empty spots to put dogs that would guard the lone sheep.
In the third example, there are no wolves, so the task is very easy. We put a dog in the center to observe the peacefulness of the meadow, but the solution would be correct even without him.
Tags: brute force, dfs and similar, graphs, implementation
Correct Solution:
```
n, m = [int(x) for x in input().split()]
a = [[x for x in input()] for _ in range(n)]
def set(i, j):
if a[i][j] == "S":
return False
elif a[i][j] == ".":
a[i][j] = "D"
return True
flag = True
for i in range(n):
for j in range(m):
if a[i][j] != 'W': continue
if i>0 and not set(i-1, j):
flag = False
if j>0 and not set(i, j-1):
flag = False
if i<n-1 and not set(i+1, j):
flag = False
if j<m-1 and not set(i, j+1):
flag = False
if flag == False:
break
if flag:
print("Yes")
for s in a:
print("".join(s))
else:
print("No")
```
| 13,872 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to wolf attacks. He thus decided to place some shepherd dogs in such a way that all his sheep are protected.
The pasture is a rectangle consisting of R Γ C cells. Each cell is either empty, contains a sheep, a wolf or a dog. Sheep and dogs always stay in place, but wolves can roam freely around the pasture, by repeatedly moving to the left, right, up or down to a neighboring cell. When a wolf enters a cell with a sheep, it consumes it. However, no wolf can enter a cell with a dog.
Initially there are no dogs. Place dogs onto the pasture in such a way that no wolf can reach any sheep, or determine that it is impossible. Note that since you have many dogs, you do not need to minimize their number.
Input
First line contains two integers R (1 β€ R β€ 500) and C (1 β€ C β€ 500), denoting the number of rows and the numbers of columns respectively.
Each of the following R lines is a string consisting of exactly C characters, representing one row of the pasture. Here, 'S' means a sheep, 'W' a wolf and '.' an empty cell.
Output
If it is impossible to protect all sheep, output a single line with the word "No".
Otherwise, output a line with the word "Yes". Then print R lines, representing the pasture after placing dogs. Again, 'S' means a sheep, 'W' a wolf, 'D' is a dog and '.' an empty space. You are not allowed to move, remove or add a sheep or a wolf.
If there are multiple solutions, you may print any of them. You don't have to minimize the number of dogs.
Examples
Input
6 6
..S...
..S.W.
.S....
..W...
...W..
......
Output
Yes
..SD..
..SDW.
.SD...
.DW...
DD.W..
......
Input
1 2
SW
Output
No
Input
5 5
.S...
...S.
S....
...S.
.S...
Output
Yes
.S...
...S.
S.D..
...S.
.S...
Note
In the first example, we can split the pasture into two halves, one containing wolves and one containing sheep. Note that the sheep at (2,1) is safe, as wolves cannot move diagonally.
In the second example, there are no empty spots to put dogs that would guard the lone sheep.
In the third example, there are no wolves, so the task is very easy. We put a dog in the center to observe the peacefulness of the meadow, but the solution would be correct even without him.
Tags: brute force, dfs and similar, graphs, implementation
Correct Solution:
```
def main():
r,c = list(map(int,input().split()))
field = [[x for x in input()] for _ in range(r)]
good = True
for i in range(r):
for j in range(c):
if field[i][j] == 'S':
if i>0 and field[i-1][j] == 'W':
good = False
break
if i<r-1 and field[i+1][j] == 'W':
good = False
break
if j>0 and field[i][j-1] == 'W':
good = False
if j<c-1 and field[i][j+1] == 'W':
good = False
break
if not good:
print('NO')
return
for i in range(r):
for j in range(c):
if field[i][j] == '.':
field[i][j] = 'D'
print('YES')
for i in range(r):
print(''.join(field[i]))
if __name__ == '__main__':
main()
```
| 13,873 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to wolf attacks. He thus decided to place some shepherd dogs in such a way that all his sheep are protected.
The pasture is a rectangle consisting of R Γ C cells. Each cell is either empty, contains a sheep, a wolf or a dog. Sheep and dogs always stay in place, but wolves can roam freely around the pasture, by repeatedly moving to the left, right, up or down to a neighboring cell. When a wolf enters a cell with a sheep, it consumes it. However, no wolf can enter a cell with a dog.
Initially there are no dogs. Place dogs onto the pasture in such a way that no wolf can reach any sheep, or determine that it is impossible. Note that since you have many dogs, you do not need to minimize their number.
Input
First line contains two integers R (1 β€ R β€ 500) and C (1 β€ C β€ 500), denoting the number of rows and the numbers of columns respectively.
Each of the following R lines is a string consisting of exactly C characters, representing one row of the pasture. Here, 'S' means a sheep, 'W' a wolf and '.' an empty cell.
Output
If it is impossible to protect all sheep, output a single line with the word "No".
Otherwise, output a line with the word "Yes". Then print R lines, representing the pasture after placing dogs. Again, 'S' means a sheep, 'W' a wolf, 'D' is a dog and '.' an empty space. You are not allowed to move, remove or add a sheep or a wolf.
If there are multiple solutions, you may print any of them. You don't have to minimize the number of dogs.
Examples
Input
6 6
..S...
..S.W.
.S....
..W...
...W..
......
Output
Yes
..SD..
..SDW.
.SD...
.DW...
DD.W..
......
Input
1 2
SW
Output
No
Input
5 5
.S...
...S.
S....
...S.
.S...
Output
Yes
.S...
...S.
S.D..
...S.
.S...
Note
In the first example, we can split the pasture into two halves, one containing wolves and one containing sheep. Note that the sheep at (2,1) is safe, as wolves cannot move diagonally.
In the second example, there are no empty spots to put dogs that would guard the lone sheep.
In the third example, there are no wolves, so the task is very easy. We put a dog in the center to observe the peacefulness of the meadow, but the solution would be correct even without him.
Tags: brute force, dfs and similar, graphs, implementation
Correct Solution:
```
def Fill(A,x,y,n,m):
Bool = False
if x>0:
if A[x-1][y] == '.':
A[x-1][y]= 'D'
if A[x-1][y] == 'W':
Bool=True
if y>0:
if A[x][y-1] == '.':
A[x][y-1]= 'D'
if A[x][y-1] == 'W':
Bool=True
if y<m-1:
if A[x][y+1] == '.':
A[x][y+1]= 'D'
if A[x][y+1] == 'W':
Bool=True
if x<n-1:
if A[x+1][y] == '.':
A[x+1][y]= 'D'
if A[x+1][y] == 'W':
Bool=True
return Bool
n,m=list(map(int,input().split()))
A=[]
for _ in range(n):
S=list(input())
A.append(S)
Ans=False
for n1 in range(n):
for n2 in range(m):
if A[n1][n2] == 'S':
Temp = Fill(A,n1,n2,n,m)
if Temp == True:
Ans = True
break
if Ans == True:
break
if Ans == False:
print("Yes")
for val in A:
print(''.join(val))
else:
print("No")
```
| 13,874 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to wolf attacks. He thus decided to place some shepherd dogs in such a way that all his sheep are protected.
The pasture is a rectangle consisting of R Γ C cells. Each cell is either empty, contains a sheep, a wolf or a dog. Sheep and dogs always stay in place, but wolves can roam freely around the pasture, by repeatedly moving to the left, right, up or down to a neighboring cell. When a wolf enters a cell with a sheep, it consumes it. However, no wolf can enter a cell with a dog.
Initially there are no dogs. Place dogs onto the pasture in such a way that no wolf can reach any sheep, or determine that it is impossible. Note that since you have many dogs, you do not need to minimize their number.
Input
First line contains two integers R (1 β€ R β€ 500) and C (1 β€ C β€ 500), denoting the number of rows and the numbers of columns respectively.
Each of the following R lines is a string consisting of exactly C characters, representing one row of the pasture. Here, 'S' means a sheep, 'W' a wolf and '.' an empty cell.
Output
If it is impossible to protect all sheep, output a single line with the word "No".
Otherwise, output a line with the word "Yes". Then print R lines, representing the pasture after placing dogs. Again, 'S' means a sheep, 'W' a wolf, 'D' is a dog and '.' an empty space. You are not allowed to move, remove or add a sheep or a wolf.
If there are multiple solutions, you may print any of them. You don't have to minimize the number of dogs.
Examples
Input
6 6
..S...
..S.W.
.S....
..W...
...W..
......
Output
Yes
..SD..
..SDW.
.SD...
.DW...
DD.W..
......
Input
1 2
SW
Output
No
Input
5 5
.S...
...S.
S....
...S.
.S...
Output
Yes
.S...
...S.
S.D..
...S.
.S...
Note
In the first example, we can split the pasture into two halves, one containing wolves and one containing sheep. Note that the sheep at (2,1) is safe, as wolves cannot move diagonally.
In the second example, there are no empty spots to put dogs that would guard the lone sheep.
In the third example, there are no wolves, so the task is very easy. We put a dog in the center to observe the peacefulness of the meadow, but the solution would be correct even without him.
Tags: brute force, dfs and similar, graphs, implementation
Correct Solution:
```
x,y=map(int,input().split())
lol=[]
f=0
for _ in range(x):
a=input()
a=a.replace(".","D")
#print(a)
for i in range(1,y):
if (a[i]=="S" and a[i-1]=="W") or (a[i-1]=="S" and a[i]=="W"):
f=1
break
if f==1:
break
lol.append(a)
if not f and len(lol)>1:
for i in range(x):
for j in range(y):
if i==0:
yo=[lol[i][j],lol[i+1][j]]
elif i==x-1:
yo=[lol[i][j],lol[i-1][j]]
else:
yo=[lol[i][j],lol[i-1][j],lol[i+1][j]]
if "W" in yo and "S" in yo and lol[i][j]!="D":
f=1
#print(yo,i,j,lol[i],lol[i-1],lol[i+1])
break
if f:
break
if f:
print("NO")
else:
print("YES")
for i in lol:
print(i)
```
| 13,875 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to wolf attacks. He thus decided to place some shepherd dogs in such a way that all his sheep are protected.
The pasture is a rectangle consisting of R Γ C cells. Each cell is either empty, contains a sheep, a wolf or a dog. Sheep and dogs always stay in place, but wolves can roam freely around the pasture, by repeatedly moving to the left, right, up or down to a neighboring cell. When a wolf enters a cell with a sheep, it consumes it. However, no wolf can enter a cell with a dog.
Initially there are no dogs. Place dogs onto the pasture in such a way that no wolf can reach any sheep, or determine that it is impossible. Note that since you have many dogs, you do not need to minimize their number.
Input
First line contains two integers R (1 β€ R β€ 500) and C (1 β€ C β€ 500), denoting the number of rows and the numbers of columns respectively.
Each of the following R lines is a string consisting of exactly C characters, representing one row of the pasture. Here, 'S' means a sheep, 'W' a wolf and '.' an empty cell.
Output
If it is impossible to protect all sheep, output a single line with the word "No".
Otherwise, output a line with the word "Yes". Then print R lines, representing the pasture after placing dogs. Again, 'S' means a sheep, 'W' a wolf, 'D' is a dog and '.' an empty space. You are not allowed to move, remove or add a sheep or a wolf.
If there are multiple solutions, you may print any of them. You don't have to minimize the number of dogs.
Examples
Input
6 6
..S...
..S.W.
.S....
..W...
...W..
......
Output
Yes
..SD..
..SDW.
.SD...
.DW...
DD.W..
......
Input
1 2
SW
Output
No
Input
5 5
.S...
...S.
S....
...S.
.S...
Output
Yes
.S...
...S.
S.D..
...S.
.S...
Note
In the first example, we can split the pasture into two halves, one containing wolves and one containing sheep. Note that the sheep at (2,1) is safe, as wolves cannot move diagonally.
In the second example, there are no empty spots to put dogs that would guard the lone sheep.
In the third example, there are no wolves, so the task is very easy. We put a dog in the center to observe the peacefulness of the meadow, but the solution would be correct even without him.
Tags: brute force, dfs and similar, graphs, implementation
Correct Solution:
```
R,C=map(int, input().split())
m=[[str(j) for j in input()] for i in range(R)]
row=0
for i in range(R):
if row == 1:
break
for j in range(C):
if m[i][j] == ".":
m[i][j] = "D"
elif m[i][j] == "S":
if j > 0 and m[i][j-1] == "W":
row=1
break
elif j < C-1 and m[i][j+1] == "W":
row=1
break
elif i < R-1 and m[i+1][j] == "W":
row=1
break
elif i > 0 and m[i-1][j] == "W":
row=1
break
if row == 1:
print("NO")
else:
print("YES")
for i in m:
print("".join(i))
```
| 13,876 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to wolf attacks. He thus decided to place some shepherd dogs in such a way that all his sheep are protected.
The pasture is a rectangle consisting of R Γ C cells. Each cell is either empty, contains a sheep, a wolf or a dog. Sheep and dogs always stay in place, but wolves can roam freely around the pasture, by repeatedly moving to the left, right, up or down to a neighboring cell. When a wolf enters a cell with a sheep, it consumes it. However, no wolf can enter a cell with a dog.
Initially there are no dogs. Place dogs onto the pasture in such a way that no wolf can reach any sheep, or determine that it is impossible. Note that since you have many dogs, you do not need to minimize their number.
Input
First line contains two integers R (1 β€ R β€ 500) and C (1 β€ C β€ 500), denoting the number of rows and the numbers of columns respectively.
Each of the following R lines is a string consisting of exactly C characters, representing one row of the pasture. Here, 'S' means a sheep, 'W' a wolf and '.' an empty cell.
Output
If it is impossible to protect all sheep, output a single line with the word "No".
Otherwise, output a line with the word "Yes". Then print R lines, representing the pasture after placing dogs. Again, 'S' means a sheep, 'W' a wolf, 'D' is a dog and '.' an empty space. You are not allowed to move, remove or add a sheep or a wolf.
If there are multiple solutions, you may print any of them. You don't have to minimize the number of dogs.
Examples
Input
6 6
..S...
..S.W.
.S....
..W...
...W..
......
Output
Yes
..SD..
..SDW.
.SD...
.DW...
DD.W..
......
Input
1 2
SW
Output
No
Input
5 5
.S...
...S.
S....
...S.
.S...
Output
Yes
.S...
...S.
S.D..
...S.
.S...
Note
In the first example, we can split the pasture into two halves, one containing wolves and one containing sheep. Note that the sheep at (2,1) is safe, as wolves cannot move diagonally.
In the second example, there are no empty spots to put dogs that would guard the lone sheep.
In the third example, there are no wolves, so the task is very easy. We put a dog in the center to observe the peacefulness of the meadow, but the solution would be correct even without him.
Tags: brute force, dfs and similar, graphs, implementation
Correct Solution:
```
import sys
import math
import bisect
import itertools
import random
import re
def solve(A):
n = len(A)
m = len(A[0])
for i in range(n):
for j in range(m):
if A[i][j] == 'S':
if i - 1 >= 0:
if A[i-1][j] == 'W':
return False
if i + 1 < n:
if A[i+1][j] == 'W':
return False
if j - 1 >= 0:
if A[i][j-1] == 'W':
return False
if j + 1 < m:
if A[i][j+1] == 'W':
return False
return True
def main():
n, m = map(int, input().split())
A = []
for i in range(n):
A.append(input())
if solve(A) == False:
print('No')
else:
print('Yes')
for i in range(n):
A[i] = re.sub(r'[.]', 'D', A[i])
print(''.join(A[i]))
if __name__ == "__main__":
main()
```
| 13,877 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to wolf attacks. He thus decided to place some shepherd dogs in such a way that all his sheep are protected.
The pasture is a rectangle consisting of R Γ C cells. Each cell is either empty, contains a sheep, a wolf or a dog. Sheep and dogs always stay in place, but wolves can roam freely around the pasture, by repeatedly moving to the left, right, up or down to a neighboring cell. When a wolf enters a cell with a sheep, it consumes it. However, no wolf can enter a cell with a dog.
Initially there are no dogs. Place dogs onto the pasture in such a way that no wolf can reach any sheep, or determine that it is impossible. Note that since you have many dogs, you do not need to minimize their number.
Input
First line contains two integers R (1 β€ R β€ 500) and C (1 β€ C β€ 500), denoting the number of rows and the numbers of columns respectively.
Each of the following R lines is a string consisting of exactly C characters, representing one row of the pasture. Here, 'S' means a sheep, 'W' a wolf and '.' an empty cell.
Output
If it is impossible to protect all sheep, output a single line with the word "No".
Otherwise, output a line with the word "Yes". Then print R lines, representing the pasture after placing dogs. Again, 'S' means a sheep, 'W' a wolf, 'D' is a dog and '.' an empty space. You are not allowed to move, remove or add a sheep or a wolf.
If there are multiple solutions, you may print any of them. You don't have to minimize the number of dogs.
Examples
Input
6 6
..S...
..S.W.
.S....
..W...
...W..
......
Output
Yes
..SD..
..SDW.
.SD...
.DW...
DD.W..
......
Input
1 2
SW
Output
No
Input
5 5
.S...
...S.
S....
...S.
.S...
Output
Yes
.S...
...S.
S.D..
...S.
.S...
Note
In the first example, we can split the pasture into two halves, one containing wolves and one containing sheep. Note that the sheep at (2,1) is safe, as wolves cannot move diagonally.
In the second example, there are no empty spots to put dogs that would guard the lone sheep.
In the third example, there are no wolves, so the task is very easy. We put a dog in the center to observe the peacefulness of the meadow, but the solution would be correct even without him.
Tags: brute force, dfs and similar, graphs, implementation
Correct Solution:
```
def okay(sl,r,c):
rv = [sl[i][j] for i in range(r) for j in range(c)]
cv = [sl[i][j] for j in range(c) for i in range(r)]
for i in range(1,r*c):
if i%c>0 and ((rv[i-1]=='S' and rv[i]=='W') or (rv[i-1]=='W' and rv[i]=='S')):
return False
if i%r>0 and ((cv[i-1]=='S' and cv[i]=='W') or (cv[i-1]=='W' and cv[i]=='S')):
return False
return True
r,c = list(map(int,input().split())) #500,500
sl = [input() for i in range(r)]
if okay(sl,r,c):
print('YES')
for s in sl:
print(s.replace('.','D'))
else:
print('NO')
```
| 13,878 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to wolf attacks. He thus decided to place some shepherd dogs in such a way that all his sheep are protected.
The pasture is a rectangle consisting of R Γ C cells. Each cell is either empty, contains a sheep, a wolf or a dog. Sheep and dogs always stay in place, but wolves can roam freely around the pasture, by repeatedly moving to the left, right, up or down to a neighboring cell. When a wolf enters a cell with a sheep, it consumes it. However, no wolf can enter a cell with a dog.
Initially there are no dogs. Place dogs onto the pasture in such a way that no wolf can reach any sheep, or determine that it is impossible. Note that since you have many dogs, you do not need to minimize their number.
Input
First line contains two integers R (1 β€ R β€ 500) and C (1 β€ C β€ 500), denoting the number of rows and the numbers of columns respectively.
Each of the following R lines is a string consisting of exactly C characters, representing one row of the pasture. Here, 'S' means a sheep, 'W' a wolf and '.' an empty cell.
Output
If it is impossible to protect all sheep, output a single line with the word "No".
Otherwise, output a line with the word "Yes". Then print R lines, representing the pasture after placing dogs. Again, 'S' means a sheep, 'W' a wolf, 'D' is a dog and '.' an empty space. You are not allowed to move, remove or add a sheep or a wolf.
If there are multiple solutions, you may print any of them. You don't have to minimize the number of dogs.
Examples
Input
6 6
..S...
..S.W.
.S....
..W...
...W..
......
Output
Yes
..SD..
..SDW.
.SD...
.DW...
DD.W..
......
Input
1 2
SW
Output
No
Input
5 5
.S...
...S.
S....
...S.
.S...
Output
Yes
.S...
...S.
S.D..
...S.
.S...
Note
In the first example, we can split the pasture into two halves, one containing wolves and one containing sheep. Note that the sheep at (2,1) is safe, as wolves cannot move diagonally.
In the second example, there are no empty spots to put dogs that would guard the lone sheep.
In the third example, there are no wolves, so the task is very easy. We put a dog in the center to observe the peacefulness of the meadow, but the solution would be correct even without him.
Tags: brute force, dfs and similar, graphs, implementation
Correct Solution:
```
# cf 948 A 1000
r, c = map(int, input().split())
ans = True
M = []
for _ in range(r):
s = input()
if 'SW' in s or 'WS' in s:
ans = False
if len(M) > 0:
for i in range(len(s)):
if s[i] == 'W' and M[-1][i] == 'S':
ans = False
if s[i] == 'S' and M[-1][i] == 'W':
ans = False
M.append(s.replace('.', 'D'))
print("Yes" if ans else "No")
if ans:
for m in M:
print(m)
```
| 13,879 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to wolf attacks. He thus decided to place some shepherd dogs in such a way that all his sheep are protected.
The pasture is a rectangle consisting of R Γ C cells. Each cell is either empty, contains a sheep, a wolf or a dog. Sheep and dogs always stay in place, but wolves can roam freely around the pasture, by repeatedly moving to the left, right, up or down to a neighboring cell. When a wolf enters a cell with a sheep, it consumes it. However, no wolf can enter a cell with a dog.
Initially there are no dogs. Place dogs onto the pasture in such a way that no wolf can reach any sheep, or determine that it is impossible. Note that since you have many dogs, you do not need to minimize their number.
Input
First line contains two integers R (1 β€ R β€ 500) and C (1 β€ C β€ 500), denoting the number of rows and the numbers of columns respectively.
Each of the following R lines is a string consisting of exactly C characters, representing one row of the pasture. Here, 'S' means a sheep, 'W' a wolf and '.' an empty cell.
Output
If it is impossible to protect all sheep, output a single line with the word "No".
Otherwise, output a line with the word "Yes". Then print R lines, representing the pasture after placing dogs. Again, 'S' means a sheep, 'W' a wolf, 'D' is a dog and '.' an empty space. You are not allowed to move, remove or add a sheep or a wolf.
If there are multiple solutions, you may print any of them. You don't have to minimize the number of dogs.
Examples
Input
6 6
..S...
..S.W.
.S....
..W...
...W..
......
Output
Yes
..SD..
..SDW.
.SD...
.DW...
DD.W..
......
Input
1 2
SW
Output
No
Input
5 5
.S...
...S.
S....
...S.
.S...
Output
Yes
.S...
...S.
S.D..
...S.
.S...
Note
In the first example, we can split the pasture into two halves, one containing wolves and one containing sheep. Note that the sheep at (2,1) is safe, as wolves cannot move diagonally.
In the second example, there are no empty spots to put dogs that would guard the lone sheep.
In the third example, there are no wolves, so the task is very easy. We put a dog in the center to observe the peacefulness of the meadow, but the solution would be correct even without him.
Submitted Solution:
```
r,c = list(map(int,input().split()))
grid = []
for i in range(r):
grid.append([x for x in input()])
for i in range(r):
for j in range(c):
if grid[i][j] == "." : grid[i][j] = "D"
ans = "Yes"
def valid(i,j):
return i >=0 and j >=0 and i < r and j < c
masks = [(1,0),(-1,0),(0,1),(0,-1)]
for i in range(r):
for j in range(c):
if grid[i][j] != "W" : continue
for a,b in masks:
if valid(i+a,j+b) and grid[i+a][j+b]=="S": ans = "No"
print(ans)
if ans == "Yes":
for x in grid:
print("".join(x))
```
Yes
| 13,880 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to wolf attacks. He thus decided to place some shepherd dogs in such a way that all his sheep are protected.
The pasture is a rectangle consisting of R Γ C cells. Each cell is either empty, contains a sheep, a wolf or a dog. Sheep and dogs always stay in place, but wolves can roam freely around the pasture, by repeatedly moving to the left, right, up or down to a neighboring cell. When a wolf enters a cell with a sheep, it consumes it. However, no wolf can enter a cell with a dog.
Initially there are no dogs. Place dogs onto the pasture in such a way that no wolf can reach any sheep, or determine that it is impossible. Note that since you have many dogs, you do not need to minimize their number.
Input
First line contains two integers R (1 β€ R β€ 500) and C (1 β€ C β€ 500), denoting the number of rows and the numbers of columns respectively.
Each of the following R lines is a string consisting of exactly C characters, representing one row of the pasture. Here, 'S' means a sheep, 'W' a wolf and '.' an empty cell.
Output
If it is impossible to protect all sheep, output a single line with the word "No".
Otherwise, output a line with the word "Yes". Then print R lines, representing the pasture after placing dogs. Again, 'S' means a sheep, 'W' a wolf, 'D' is a dog and '.' an empty space. You are not allowed to move, remove or add a sheep or a wolf.
If there are multiple solutions, you may print any of them. You don't have to minimize the number of dogs.
Examples
Input
6 6
..S...
..S.W.
.S....
..W...
...W..
......
Output
Yes
..SD..
..SDW.
.SD...
.DW...
DD.W..
......
Input
1 2
SW
Output
No
Input
5 5
.S...
...S.
S....
...S.
.S...
Output
Yes
.S...
...S.
S.D..
...S.
.S...
Note
In the first example, we can split the pasture into two halves, one containing wolves and one containing sheep. Note that the sheep at (2,1) is safe, as wolves cannot move diagonally.
In the second example, there are no empty spots to put dogs that would guard the lone sheep.
In the third example, there are no wolves, so the task is very easy. We put a dog in the center to observe the peacefulness of the meadow, but the solution would be correct even without him.
Submitted Solution:
```
n,m = [int(x) for x in input().split()]
mat = []
for i in range(n):
mat.append(input())
ok = True
dx = [0,0,1,-1]
dy = [1,-1,0,0]
f = lambda x,y,c,d : x >= 0 and y >= 0 and x < c and y < d
for i in range(n):
for j in range(m):
if mat[i][j] == 'S':
for k in range(4):
tx = i + dx[k]
ty = j + dy[k]
if f(tx,ty,n,m) == False: continue
if mat[tx][ty] == 'W': ok = False
if ok:
print("Yes")
for i in range(n):
mat[i] = mat[i].replace('.','D')
for x in mat: print(x)
else:
print("No")
```
Yes
| 13,881 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to wolf attacks. He thus decided to place some shepherd dogs in such a way that all his sheep are protected.
The pasture is a rectangle consisting of R Γ C cells. Each cell is either empty, contains a sheep, a wolf or a dog. Sheep and dogs always stay in place, but wolves can roam freely around the pasture, by repeatedly moving to the left, right, up or down to a neighboring cell. When a wolf enters a cell with a sheep, it consumes it. However, no wolf can enter a cell with a dog.
Initially there are no dogs. Place dogs onto the pasture in such a way that no wolf can reach any sheep, or determine that it is impossible. Note that since you have many dogs, you do not need to minimize their number.
Input
First line contains two integers R (1 β€ R β€ 500) and C (1 β€ C β€ 500), denoting the number of rows and the numbers of columns respectively.
Each of the following R lines is a string consisting of exactly C characters, representing one row of the pasture. Here, 'S' means a sheep, 'W' a wolf and '.' an empty cell.
Output
If it is impossible to protect all sheep, output a single line with the word "No".
Otherwise, output a line with the word "Yes". Then print R lines, representing the pasture after placing dogs. Again, 'S' means a sheep, 'W' a wolf, 'D' is a dog and '.' an empty space. You are not allowed to move, remove or add a sheep or a wolf.
If there are multiple solutions, you may print any of them. You don't have to minimize the number of dogs.
Examples
Input
6 6
..S...
..S.W.
.S....
..W...
...W..
......
Output
Yes
..SD..
..SDW.
.SD...
.DW...
DD.W..
......
Input
1 2
SW
Output
No
Input
5 5
.S...
...S.
S....
...S.
.S...
Output
Yes
.S...
...S.
S.D..
...S.
.S...
Note
In the first example, we can split the pasture into two halves, one containing wolves and one containing sheep. Note that the sheep at (2,1) is safe, as wolves cannot move diagonally.
In the second example, there are no empty spots to put dogs that would guard the lone sheep.
In the third example, there are no wolves, so the task is very easy. We put a dog in the center to observe the peacefulness of the meadow, but the solution would be correct even without him.
Submitted Solution:
```
import math as mt
import sys,string
input=sys.stdin.readline
L=lambda : list(map(int,input().split()))
Ls=lambda : list(input().split())
M=lambda : map(int,input().split())
I=lambda :int(input())
from collections import defaultdict
def dfs(n,dx):
vis[n]=1
dis[n]=dx
for i in adj[n]:
if(vis[i]==0):
dfs(i,dx+1)
r,c=M()
l=[]
x=[]
for i in range(r):
s=input().strip()
s=list(s)
for j in range(c):
if(s[j]=='.'):
s[j]='D'
if(s[j]=='W'):
x.append((i,j))
l.append(s)
for i in range(len(x)):
if(x[i][0]-1>=0):
if(l[x[i][0]-1][x[i][1]]=='S'):
print("No")
exit()
if(x[i][0]+1<r):
if(l[x[i][0]+1][x[i][1]]=='S'):
print("No")
exit()
if(x[i][1]-1>=0):
if(l[x[i][0]][x[i][1]-1]=='S'):
print("No")
exit()
if(x[i][1]+1<c):
if(l[x[i][0]][x[i][1]+1]=='S'):
print("No")
exit()
print("Yes")
for i in range(len(l)):
for j in l[i]:
print(j,end="")
print()
```
Yes
| 13,882 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to wolf attacks. He thus decided to place some shepherd dogs in such a way that all his sheep are protected.
The pasture is a rectangle consisting of R Γ C cells. Each cell is either empty, contains a sheep, a wolf or a dog. Sheep and dogs always stay in place, but wolves can roam freely around the pasture, by repeatedly moving to the left, right, up or down to a neighboring cell. When a wolf enters a cell with a sheep, it consumes it. However, no wolf can enter a cell with a dog.
Initially there are no dogs. Place dogs onto the pasture in such a way that no wolf can reach any sheep, or determine that it is impossible. Note that since you have many dogs, you do not need to minimize their number.
Input
First line contains two integers R (1 β€ R β€ 500) and C (1 β€ C β€ 500), denoting the number of rows and the numbers of columns respectively.
Each of the following R lines is a string consisting of exactly C characters, representing one row of the pasture. Here, 'S' means a sheep, 'W' a wolf and '.' an empty cell.
Output
If it is impossible to protect all sheep, output a single line with the word "No".
Otherwise, output a line with the word "Yes". Then print R lines, representing the pasture after placing dogs. Again, 'S' means a sheep, 'W' a wolf, 'D' is a dog and '.' an empty space. You are not allowed to move, remove or add a sheep or a wolf.
If there are multiple solutions, you may print any of them. You don't have to minimize the number of dogs.
Examples
Input
6 6
..S...
..S.W.
.S....
..W...
...W..
......
Output
Yes
..SD..
..SDW.
.SD...
.DW...
DD.W..
......
Input
1 2
SW
Output
No
Input
5 5
.S...
...S.
S....
...S.
.S...
Output
Yes
.S...
...S.
S.D..
...S.
.S...
Note
In the first example, we can split the pasture into two halves, one containing wolves and one containing sheep. Note that the sheep at (2,1) is safe, as wolves cannot move diagonally.
In the second example, there are no empty spots to put dogs that would guard the lone sheep.
In the third example, there are no wolves, so the task is very easy. We put a dog in the center to observe the peacefulness of the meadow, but the solution would be correct even without him.
Submitted Solution:
```
import re
r,c=input().split()
s='\n'.join(input().replace('.','D')for _ in[0]*int(r))
print('No'if re.search('(?s)(S(.{'+c+'})?W)|(W(.{'+c+'})?S)',s)else'Yes\n'+s)
```
Yes
| 13,883 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to wolf attacks. He thus decided to place some shepherd dogs in such a way that all his sheep are protected.
The pasture is a rectangle consisting of R Γ C cells. Each cell is either empty, contains a sheep, a wolf or a dog. Sheep and dogs always stay in place, but wolves can roam freely around the pasture, by repeatedly moving to the left, right, up or down to a neighboring cell. When a wolf enters a cell with a sheep, it consumes it. However, no wolf can enter a cell with a dog.
Initially there are no dogs. Place dogs onto the pasture in such a way that no wolf can reach any sheep, or determine that it is impossible. Note that since you have many dogs, you do not need to minimize their number.
Input
First line contains two integers R (1 β€ R β€ 500) and C (1 β€ C β€ 500), denoting the number of rows and the numbers of columns respectively.
Each of the following R lines is a string consisting of exactly C characters, representing one row of the pasture. Here, 'S' means a sheep, 'W' a wolf and '.' an empty cell.
Output
If it is impossible to protect all sheep, output a single line with the word "No".
Otherwise, output a line with the word "Yes". Then print R lines, representing the pasture after placing dogs. Again, 'S' means a sheep, 'W' a wolf, 'D' is a dog and '.' an empty space. You are not allowed to move, remove or add a sheep or a wolf.
If there are multiple solutions, you may print any of them. You don't have to minimize the number of dogs.
Examples
Input
6 6
..S...
..S.W.
.S....
..W...
...W..
......
Output
Yes
..SD..
..SDW.
.SD...
.DW...
DD.W..
......
Input
1 2
SW
Output
No
Input
5 5
.S...
...S.
S....
...S.
.S...
Output
Yes
.S...
...S.
S.D..
...S.
.S...
Note
In the first example, we can split the pasture into two halves, one containing wolves and one containing sheep. Note that the sheep at (2,1) is safe, as wolves cannot move diagonally.
In the second example, there are no empty spots to put dogs that would guard the lone sheep.
In the third example, there are no wolves, so the task is very easy. We put a dog in the center to observe the peacefulness of the meadow, but the solution would be correct even without him.
Submitted Solution:
```
import sys, math, os.path
FILE_INPUT = "A.in"
DEBUG = os.path.isfile(FILE_INPUT)
if DEBUG:
sys.stdin = open(FILE_INPUT)
def ni():
return map(int, input().split(" "))
def nia():
return list(map(int,input().split()))
def log(x):
if (DEBUG):
print(x)
r, c = ni()
a = []
print(r, c)
def check():
for i in range(r):
for j in range(c):
if a[i][j] == 'W':
# print(i,j)
if i > 0 and a[i-1][j]=='S':
return False
if (i < r-1) and a[i+1][j]=='S':
return False
if j > 0 and a[i][j-1]=='S':
return False
if (j < c-1) and a[i][j+1]=='S':
return False
return True
for i in range(r):
s = input()
a.append(s)
if check():
print("Yes")
for i in range(r):
print(''.join('D' if x == '.' else x for x in a[i]))
else:
print("No")
```
No
| 13,884 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to wolf attacks. He thus decided to place some shepherd dogs in such a way that all his sheep are protected.
The pasture is a rectangle consisting of R Γ C cells. Each cell is either empty, contains a sheep, a wolf or a dog. Sheep and dogs always stay in place, but wolves can roam freely around the pasture, by repeatedly moving to the left, right, up or down to a neighboring cell. When a wolf enters a cell with a sheep, it consumes it. However, no wolf can enter a cell with a dog.
Initially there are no dogs. Place dogs onto the pasture in such a way that no wolf can reach any sheep, or determine that it is impossible. Note that since you have many dogs, you do not need to minimize their number.
Input
First line contains two integers R (1 β€ R β€ 500) and C (1 β€ C β€ 500), denoting the number of rows and the numbers of columns respectively.
Each of the following R lines is a string consisting of exactly C characters, representing one row of the pasture. Here, 'S' means a sheep, 'W' a wolf and '.' an empty cell.
Output
If it is impossible to protect all sheep, output a single line with the word "No".
Otherwise, output a line with the word "Yes". Then print R lines, representing the pasture after placing dogs. Again, 'S' means a sheep, 'W' a wolf, 'D' is a dog and '.' an empty space. You are not allowed to move, remove or add a sheep or a wolf.
If there are multiple solutions, you may print any of them. You don't have to minimize the number of dogs.
Examples
Input
6 6
..S...
..S.W.
.S....
..W...
...W..
......
Output
Yes
..SD..
..SDW.
.SD...
.DW...
DD.W..
......
Input
1 2
SW
Output
No
Input
5 5
.S...
...S.
S....
...S.
.S...
Output
Yes
.S...
...S.
S.D..
...S.
.S...
Note
In the first example, we can split the pasture into two halves, one containing wolves and one containing sheep. Note that the sheep at (2,1) is safe, as wolves cannot move diagonally.
In the second example, there are no empty spots to put dogs that would guard the lone sheep.
In the third example, there are no wolves, so the task is very easy. We put a dog in the center to observe the peacefulness of the meadow, but the solution would be correct even without him.
Submitted Solution:
```
row, strings = map(int, input().split())
# ..S...
# ..S.W.
# .S....
# ..W...
# ...W..
# ......
all_past = []
for i in range(row):
all_past.append([])
array = input()
all_past[i].append(array)
# if row==1:
# if ('SW' or 'WS') in str(all_past[0]):
# print('NO')
flag = False
for i in range(0, row):
first = str(all_past[i])
if i != row-1:
second = str(all_past[i+1])
if ('SW' or 'WS') in (first or second):
print('No')
flag = True
break
for i in range(strings):
if (first[i] == 'S' and second[i] == 'W') or (first[i] == 'W' and second[i] == 'S'):
print('No')
flag = True
break
if not flag:
print('Yes')
for i in all_past:
new_mini = ''
for j in str(i):
if j == '.':
new_mini += 'D'
else:
new_mini += j
print(new_mini[2:-2])
```
No
| 13,885 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to wolf attacks. He thus decided to place some shepherd dogs in such a way that all his sheep are protected.
The pasture is a rectangle consisting of R Γ C cells. Each cell is either empty, contains a sheep, a wolf or a dog. Sheep and dogs always stay in place, but wolves can roam freely around the pasture, by repeatedly moving to the left, right, up or down to a neighboring cell. When a wolf enters a cell with a sheep, it consumes it. However, no wolf can enter a cell with a dog.
Initially there are no dogs. Place dogs onto the pasture in such a way that no wolf can reach any sheep, or determine that it is impossible. Note that since you have many dogs, you do not need to minimize their number.
Input
First line contains two integers R (1 β€ R β€ 500) and C (1 β€ C β€ 500), denoting the number of rows and the numbers of columns respectively.
Each of the following R lines is a string consisting of exactly C characters, representing one row of the pasture. Here, 'S' means a sheep, 'W' a wolf and '.' an empty cell.
Output
If it is impossible to protect all sheep, output a single line with the word "No".
Otherwise, output a line with the word "Yes". Then print R lines, representing the pasture after placing dogs. Again, 'S' means a sheep, 'W' a wolf, 'D' is a dog and '.' an empty space. You are not allowed to move, remove or add a sheep or a wolf.
If there are multiple solutions, you may print any of them. You don't have to minimize the number of dogs.
Examples
Input
6 6
..S...
..S.W.
.S....
..W...
...W..
......
Output
Yes
..SD..
..SDW.
.SD...
.DW...
DD.W..
......
Input
1 2
SW
Output
No
Input
5 5
.S...
...S.
S....
...S.
.S...
Output
Yes
.S...
...S.
S.D..
...S.
.S...
Note
In the first example, we can split the pasture into two halves, one containing wolves and one containing sheep. Note that the sheep at (2,1) is safe, as wolves cannot move diagonally.
In the second example, there are no empty spots to put dogs that would guard the lone sheep.
In the third example, there are no wolves, so the task is very easy. We put a dog in the center to observe the peacefulness of the meadow, but the solution would be correct even without him.
Submitted Solution:
```
def test_2():
# B - Protect Sheep
import math
r, c = list(map(int, input().split(" ")))
aa = []
aa.append(list('.' * (c + 2)))
for i in range(r):
c_1 = list('.'+input()+'.')
# print(c_1)
aa.append(c_1)
aa.append(list('.' * (c + 2)))
# print(aa)
for i in range(1, r+1):
for j in range(1, c+1):
# print(aa[i][j])
if aa[i][j]=='S':
bb = [aa[i][j+1], aa[i][j-1], aa[i+1][j], aa[i-1][j]]
# print('b:', bb)
if 'W' in bb:
print('No')
return 0
else:
cc = ''.join([aa[i][j+1], aa[i][j-1], aa[i+1][j], aa[i-1][j]])
# print(cc)
cc = cc.replace('.', 'D')
cc = list(cc)
# print('cc:', cc)
aa[i][j + 1], aa[i][j - 1], aa[i + 1][j], aa[i - 1][j] = cc
# print(aa)
print('Yes')
cc = aa[1:-1]
dd = []
for c in cc:
c = c[1: -1]
dd.append(c)
# print(len(cc[1]))
print(dd)
test_2()
```
No
| 13,886 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to wolf attacks. He thus decided to place some shepherd dogs in such a way that all his sheep are protected.
The pasture is a rectangle consisting of R Γ C cells. Each cell is either empty, contains a sheep, a wolf or a dog. Sheep and dogs always stay in place, but wolves can roam freely around the pasture, by repeatedly moving to the left, right, up or down to a neighboring cell. When a wolf enters a cell with a sheep, it consumes it. However, no wolf can enter a cell with a dog.
Initially there are no dogs. Place dogs onto the pasture in such a way that no wolf can reach any sheep, or determine that it is impossible. Note that since you have many dogs, you do not need to minimize their number.
Input
First line contains two integers R (1 β€ R β€ 500) and C (1 β€ C β€ 500), denoting the number of rows and the numbers of columns respectively.
Each of the following R lines is a string consisting of exactly C characters, representing one row of the pasture. Here, 'S' means a sheep, 'W' a wolf and '.' an empty cell.
Output
If it is impossible to protect all sheep, output a single line with the word "No".
Otherwise, output a line with the word "Yes". Then print R lines, representing the pasture after placing dogs. Again, 'S' means a sheep, 'W' a wolf, 'D' is a dog and '.' an empty space. You are not allowed to move, remove or add a sheep or a wolf.
If there are multiple solutions, you may print any of them. You don't have to minimize the number of dogs.
Examples
Input
6 6
..S...
..S.W.
.S....
..W...
...W..
......
Output
Yes
..SD..
..SDW.
.SD...
.DW...
DD.W..
......
Input
1 2
SW
Output
No
Input
5 5
.S...
...S.
S....
...S.
.S...
Output
Yes
.S...
...S.
S.D..
...S.
.S...
Note
In the first example, we can split the pasture into two halves, one containing wolves and one containing sheep. Note that the sheep at (2,1) is safe, as wolves cannot move diagonally.
In the second example, there are no empty spots to put dogs that would guard the lone sheep.
In the third example, there are no wolves, so the task is very easy. We put a dog in the center to observe the peacefulness of the meadow, but the solution would be correct even without him.
Submitted Solution:
```
r,c = map(int, input().split())
arr = []
for i in range(r):
arr.append(list(input()))
for i in range(r):
for j in range(c):
if j<c-1:
if arr[i][j] == 'S' and arr[i][j+1] == 'W' or arr[i][j] == 'W' and arr[i][j+1] == 'S':
print('No')
exit()
if arr[i][j] =='.':
arr[i][j] = 'D'
print('Yes ')
for i in range(c):
print(''.join(arr[i]))
```
No
| 13,887 |
Provide tags and a correct Python 3 solution for this coding contest problem.
String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001".
You are given a correct string s.
You can perform two different operations on this string:
1. swap any pair of adjacent characters (for example, "101" <image> "110");
2. replace "11" with "1" (for example, "110" <image> "10").
Let val(s) be such a number that s is its binary representation.
Correct string a is less than some other correct string b iff val(a) < val(b).
Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all).
Input
The first line contains integer number n (1 β€ n β€ 100) β the length of string s.
The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct.
Output
Print one string β the minimum correct string that you can obtain from the given one.
Examples
Input
4
1001
Output
100
Input
1
1
Output
1
Note
In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100".
In the second example you can't obtain smaller answer no matter what operations you use.
Tags: implementation
Correct Solution:
```
n = input()
s = input()
n = int(n)
print('1'*min(s.count('1'), 1)+'0'*s.count('0'))
```
| 13,888 |
Provide tags and a correct Python 3 solution for this coding contest problem.
String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001".
You are given a correct string s.
You can perform two different operations on this string:
1. swap any pair of adjacent characters (for example, "101" <image> "110");
2. replace "11" with "1" (for example, "110" <image> "10").
Let val(s) be such a number that s is its binary representation.
Correct string a is less than some other correct string b iff val(a) < val(b).
Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all).
Input
The first line contains integer number n (1 β€ n β€ 100) β the length of string s.
The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct.
Output
Print one string β the minimum correct string that you can obtain from the given one.
Examples
Input
4
1001
Output
100
Input
1
1
Output
1
Note
In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100".
In the second example you can't obtain smaller answer no matter what operations you use.
Tags: implementation
Correct Solution:
```
n = int(input())
s = input()
flag=0
ans=""
for ele in s:
if ele=='1' and flag==0:
ans+=ele
flag=1
if ele=='0':
ans+=ele
print(ans)
```
| 13,889 |
Provide tags and a correct Python 3 solution for this coding contest problem.
String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001".
You are given a correct string s.
You can perform two different operations on this string:
1. swap any pair of adjacent characters (for example, "101" <image> "110");
2. replace "11" with "1" (for example, "110" <image> "10").
Let val(s) be such a number that s is its binary representation.
Correct string a is less than some other correct string b iff val(a) < val(b).
Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all).
Input
The first line contains integer number n (1 β€ n β€ 100) β the length of string s.
The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct.
Output
Print one string β the minimum correct string that you can obtain from the given one.
Examples
Input
4
1001
Output
100
Input
1
1
Output
1
Note
In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100".
In the second example you can't obtain smaller answer no matter what operations you use.
Tags: implementation
Correct Solution:
```
n=int(input())
s=input()
m=''
counter=0
for i in range(n):
if s[i]== '1':
counter=counter+1
for j in range(n-counter):
m=m+'0'
if s=='0':
print(0)
else:
print('1'+m)
```
| 13,890 |
Provide tags and a correct Python 3 solution for this coding contest problem.
String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001".
You are given a correct string s.
You can perform two different operations on this string:
1. swap any pair of adjacent characters (for example, "101" <image> "110");
2. replace "11" with "1" (for example, "110" <image> "10").
Let val(s) be such a number that s is its binary representation.
Correct string a is less than some other correct string b iff val(a) < val(b).
Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all).
Input
The first line contains integer number n (1 β€ n β€ 100) β the length of string s.
The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct.
Output
Print one string β the minimum correct string that you can obtain from the given one.
Examples
Input
4
1001
Output
100
Input
1
1
Output
1
Note
In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100".
In the second example you can't obtain smaller answer no matter what operations you use.
Tags: implementation
Correct Solution:
```
n=int(input())
s=input()
while('01' in s or '11' in s):
if('01' in s):
s=s.replace('01','10')
elif('11' in s):
s=s.replace('11','1')
print(s)
```
| 13,891 |
Provide tags and a correct Python 3 solution for this coding contest problem.
String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001".
You are given a correct string s.
You can perform two different operations on this string:
1. swap any pair of adjacent characters (for example, "101" <image> "110");
2. replace "11" with "1" (for example, "110" <image> "10").
Let val(s) be such a number that s is its binary representation.
Correct string a is less than some other correct string b iff val(a) < val(b).
Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all).
Input
The first line contains integer number n (1 β€ n β€ 100) β the length of string s.
The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct.
Output
Print one string β the minimum correct string that you can obtain from the given one.
Examples
Input
4
1001
Output
100
Input
1
1
Output
1
Note
In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100".
In the second example you can't obtain smaller answer no matter what operations you use.
Tags: implementation
Correct Solution:
```
num = int(input())
s = input()
if "1" not in s:
print(s)
else:
ans = "1" + s.count("0")*"0"
print(ans)
```
| 13,892 |
Provide tags and a correct Python 3 solution for this coding contest problem.
String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001".
You are given a correct string s.
You can perform two different operations on this string:
1. swap any pair of adjacent characters (for example, "101" <image> "110");
2. replace "11" with "1" (for example, "110" <image> "10").
Let val(s) be such a number that s is its binary representation.
Correct string a is less than some other correct string b iff val(a) < val(b).
Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all).
Input
The first line contains integer number n (1 β€ n β€ 100) β the length of string s.
The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct.
Output
Print one string β the minimum correct string that you can obtain from the given one.
Examples
Input
4
1001
Output
100
Input
1
1
Output
1
Note
In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100".
In the second example you can't obtain smaller answer no matter what operations you use.
Tags: implementation
Correct Solution:
```
n=int(input())
s=input()
if n==1:
print(s)
exit()
s=list(s)
zero=s.count('0')
print('1'+'0'*zero)
```
| 13,893 |
Provide tags and a correct Python 3 solution for this coding contest problem.
String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001".
You are given a correct string s.
You can perform two different operations on this string:
1. swap any pair of adjacent characters (for example, "101" <image> "110");
2. replace "11" with "1" (for example, "110" <image> "10").
Let val(s) be such a number that s is its binary representation.
Correct string a is less than some other correct string b iff val(a) < val(b).
Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all).
Input
The first line contains integer number n (1 β€ n β€ 100) β the length of string s.
The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct.
Output
Print one string β the minimum correct string that you can obtain from the given one.
Examples
Input
4
1001
Output
100
Input
1
1
Output
1
Note
In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100".
In the second example you can't obtain smaller answer no matter what operations you use.
Tags: implementation
Correct Solution:
```
# int(input())
# [int(s) for s in input().split()]
# input()
def solve():
n = int(input())
s = input()
if s.count("1") == 0:
print(s)
else:
print("1"+"0"*(s.count("0")))
if __name__ == "__main__":
solve()
```
| 13,894 |
Provide tags and a correct Python 3 solution for this coding contest problem.
String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001".
You are given a correct string s.
You can perform two different operations on this string:
1. swap any pair of adjacent characters (for example, "101" <image> "110");
2. replace "11" with "1" (for example, "110" <image> "10").
Let val(s) be such a number that s is its binary representation.
Correct string a is less than some other correct string b iff val(a) < val(b).
Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all).
Input
The first line contains integer number n (1 β€ n β€ 100) β the length of string s.
The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct.
Output
Print one string β the minimum correct string that you can obtain from the given one.
Examples
Input
4
1001
Output
100
Input
1
1
Output
1
Note
In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100".
In the second example you can't obtain smaller answer no matter what operations you use.
Tags: implementation
Correct Solution:
```
n=int(input())
s=input()
z=0
o=0
for c in s:
if c=="1":
o+=1
else:
z+=1
t=""
for i in range(z):
t+="0"
if o==0:
print(t)
else:
t="1"+t
print(t)
```
| 13,895 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001".
You are given a correct string s.
You can perform two different operations on this string:
1. swap any pair of adjacent characters (for example, "101" <image> "110");
2. replace "11" with "1" (for example, "110" <image> "10").
Let val(s) be such a number that s is its binary representation.
Correct string a is less than some other correct string b iff val(a) < val(b).
Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all).
Input
The first line contains integer number n (1 β€ n β€ 100) β the length of string s.
The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct.
Output
Print one string β the minimum correct string that you can obtain from the given one.
Examples
Input
4
1001
Output
100
Input
1
1
Output
1
Note
In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100".
In the second example you can't obtain smaller answer no matter what operations you use.
Submitted Solution:
```
n, s = input(), input()
if s == '0':
print(s)
else:
print('1','0'*s.count('0'),sep='')
```
Yes
| 13,896 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001".
You are given a correct string s.
You can perform two different operations on this string:
1. swap any pair of adjacent characters (for example, "101" <image> "110");
2. replace "11" with "1" (for example, "110" <image> "10").
Let val(s) be such a number that s is its binary representation.
Correct string a is less than some other correct string b iff val(a) < val(b).
Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all).
Input
The first line contains integer number n (1 β€ n β€ 100) β the length of string s.
The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct.
Output
Print one string β the minimum correct string that you can obtain from the given one.
Examples
Input
4
1001
Output
100
Input
1
1
Output
1
Note
In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100".
In the second example you can't obtain smaller answer no matter what operations you use.
Submitted Solution:
```
n=int(input())
s=input()
print(s[0]+'0'*s[1:].count('0'))
```
Yes
| 13,897 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001".
You are given a correct string s.
You can perform two different operations on this string:
1. swap any pair of adjacent characters (for example, "101" <image> "110");
2. replace "11" with "1" (for example, "110" <image> "10").
Let val(s) be such a number that s is its binary representation.
Correct string a is less than some other correct string b iff val(a) < val(b).
Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all).
Input
The first line contains integer number n (1 β€ n β€ 100) β the length of string s.
The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct.
Output
Print one string β the minimum correct string that you can obtain from the given one.
Examples
Input
4
1001
Output
100
Input
1
1
Output
1
Note
In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100".
In the second example you can't obtain smaller answer no matter what operations you use.
Submitted Solution:
```
n = int(input())
s1 = input()
if s1.count('1') == 0:
print(0)
else:
print('1' + '0' * s1.count('0'))
```
Yes
| 13,898 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001".
You are given a correct string s.
You can perform two different operations on this string:
1. swap any pair of adjacent characters (for example, "101" <image> "110");
2. replace "11" with "1" (for example, "110" <image> "10").
Let val(s) be such a number that s is its binary representation.
Correct string a is less than some other correct string b iff val(a) < val(b).
Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all).
Input
The first line contains integer number n (1 β€ n β€ 100) β the length of string s.
The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct.
Output
Print one string β the minimum correct string that you can obtain from the given one.
Examples
Input
4
1001
Output
100
Input
1
1
Output
1
Note
In the first example you can obtain the answer by the following sequence of operations: "1001" <image> "1010" <image> "1100" <image> "100".
In the second example you can't obtain smaller answer no matter what operations you use.
Submitted Solution:
```
n = int(input())
a = input()
while '01' in a or '11' in a:
a = a.replace('01', '10')
a = a.replace('11', '1')
print(a)
```
Yes
| 13,899 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.