message stringlengths 2 49.9k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 446 108k | cluster float64 13 13 | __index_level_0__ int64 892 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed from the root. Vertex u is called a child of vertex v and vertex v is called a parent of vertex u if there exists a directed edge from v to u. A vertex is called a leaf if it doesn't have children and has a parent.
Let's call a rooted tree a spruce if its every non-leaf vertex has at least 3 leaf children. You are given a rooted tree, check whether it's a spruce.
The definition of a rooted tree can be found [here](https://goo.gl/1dqvzz).
Input
The first line contains one integer n — the number of vertices in the tree (3 ≤ n ≤ 1 000). Each of the next n - 1 lines contains one integer pi (1 ≤ i ≤ n - 1) — the index of the parent of the i + 1-th vertex (1 ≤ pi ≤ i).
Vertex 1 is the root. It's guaranteed that the root has at least 2 children.
Output
Print "Yes" if the tree is a spruce and "No" otherwise.
Examples
Input
4
1
1
1
Output
Yes
Input
7
1
1
1
2
2
2
Output
No
Input
8
1
1
1
1
3
3
3
Output
Yes
Note
The first example:
<image>
The second example:
<image>
It is not a spruce, because the non-leaf vertex 1 has only 2 leaf children.
The third example:
<image> | instruction | 0 | 102,630 | 13 | 205,260 |
Tags: implementation, trees
Correct Solution:
```
n = int(input())
parent = [None] * n
children = [[] for i in range(n)]
for i in range(1, n):
p = int(input())
p = p - 1
parent[i] = p
children[p].append(i)
leaf = {}
for i in range(n):
if len(children[i]) == 0:
leaf[i] = True
else:
leaf[i] = False
for i in range(n):
if leaf[i]:
continue
c = 0
for j in children[i]:
if leaf[j]:
c = c + 1
if c < 3:
print("No")
quit()
print("Yes")
``` | output | 1 | 102,630 | 13 | 205,261 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed from the root. Vertex u is called a child of vertex v and vertex v is called a parent of vertex u if there exists a directed edge from v to u. A vertex is called a leaf if it doesn't have children and has a parent.
Let's call a rooted tree a spruce if its every non-leaf vertex has at least 3 leaf children. You are given a rooted tree, check whether it's a spruce.
The definition of a rooted tree can be found [here](https://goo.gl/1dqvzz).
Input
The first line contains one integer n — the number of vertices in the tree (3 ≤ n ≤ 1 000). Each of the next n - 1 lines contains one integer pi (1 ≤ i ≤ n - 1) — the index of the parent of the i + 1-th vertex (1 ≤ pi ≤ i).
Vertex 1 is the root. It's guaranteed that the root has at least 2 children.
Output
Print "Yes" if the tree is a spruce and "No" otherwise.
Examples
Input
4
1
1
1
Output
Yes
Input
7
1
1
1
2
2
2
Output
No
Input
8
1
1
1
1
3
3
3
Output
Yes
Note
The first example:
<image>
The second example:
<image>
It is not a spruce, because the non-leaf vertex 1 has only 2 leaf children.
The third example:
<image> | instruction | 0 | 102,631 | 13 | 205,262 |
Tags: implementation, trees
Correct Solution:
```
from collections import defaultdict
n = int(input())
vert = defaultdict(list)
parents = set()
for i in range(1, n):
item = int(input())
if item != 1:
parents.add(item)
vert[item].append(i + 1)
#print(vert)
result = True
for par, childs in vert.items():
#print(set(childs) ^ set(not_lists))
if len(set(childs) - parents) < 3:
result = False
break
if result:
print("Yes")
else:
print("No")
``` | output | 1 | 102,631 | 13 | 205,263 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed from the root. Vertex u is called a child of vertex v and vertex v is called a parent of vertex u if there exists a directed edge from v to u. A vertex is called a leaf if it doesn't have children and has a parent.
Let's call a rooted tree a spruce if its every non-leaf vertex has at least 3 leaf children. You are given a rooted tree, check whether it's a spruce.
The definition of a rooted tree can be found [here](https://goo.gl/1dqvzz).
Input
The first line contains one integer n — the number of vertices in the tree (3 ≤ n ≤ 1 000). Each of the next n - 1 lines contains one integer pi (1 ≤ i ≤ n - 1) — the index of the parent of the i + 1-th vertex (1 ≤ pi ≤ i).
Vertex 1 is the root. It's guaranteed that the root has at least 2 children.
Output
Print "Yes" if the tree is a spruce and "No" otherwise.
Examples
Input
4
1
1
1
Output
Yes
Input
7
1
1
1
2
2
2
Output
No
Input
8
1
1
1
1
3
3
3
Output
Yes
Note
The first example:
<image>
The second example:
<image>
It is not a spruce, because the non-leaf vertex 1 has only 2 leaf children.
The third example:
<image> | instruction | 0 | 102,632 | 13 | 205,264 |
Tags: implementation, trees
Correct Solution:
```
#http://codeforces.com/problemset/problem/913/B
#solved
n = int(input())
tree = [int(input()) for _ in range(n - 1)]
leafs = []
for i in range(1, n + 1):
if i not in tree:
leafs.append(i)
for b in set(tree) - set(leafs):
leaf = 0
for l in range(n - 1):
if tree[l] == b and l + 2 in leafs:
leaf += 1
if leaf < 3:
print("No")
exit()
print("Yes")
``` | output | 1 | 102,632 | 13 | 205,265 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed from the root. Vertex u is called a child of vertex v and vertex v is called a parent of vertex u if there exists a directed edge from v to u. A vertex is called a leaf if it doesn't have children and has a parent.
Let's call a rooted tree a spruce if its every non-leaf vertex has at least 3 leaf children. You are given a rooted tree, check whether it's a spruce.
The definition of a rooted tree can be found [here](https://goo.gl/1dqvzz).
Input
The first line contains one integer n — the number of vertices in the tree (3 ≤ n ≤ 1 000). Each of the next n - 1 lines contains one integer pi (1 ≤ i ≤ n - 1) — the index of the parent of the i + 1-th vertex (1 ≤ pi ≤ i).
Vertex 1 is the root. It's guaranteed that the root has at least 2 children.
Output
Print "Yes" if the tree is a spruce and "No" otherwise.
Examples
Input
4
1
1
1
Output
Yes
Input
7
1
1
1
2
2
2
Output
No
Input
8
1
1
1
1
3
3
3
Output
Yes
Note
The first example:
<image>
The second example:
<image>
It is not a spruce, because the non-leaf vertex 1 has only 2 leaf children.
The third example:
<image>
Submitted Solution:
```
import math
from collections import defaultdict as dt
from sys import stdin
inp = lambda : stdin.readline().strip()#input()
intinp = lambda : int(inp())#int(input())
mod = int(1e9)+7
inf = float("inf")
listinp= lambda : list(map(int,stdin.readline().split()))#list(input())
mapinp = lambda : map(int,stdin.readline().split())#map(input())
##### Code Goes here #####################
for _ in range(1):
n=intinp()
b=[[] for i in range(n+1)]
for i in range(2,n+1):
b[intinp()].append(i);
c=True
for i in range(n+1):
d=0
if(len(b[i])==0): continue
for j in b[i]:
if(len(b[j])==0): d+=1
if(d<3):
c=False
break;
if(c):print("YES")
else:print("NO")
``` | instruction | 0 | 102,633 | 13 | 205,266 |
Yes | output | 1 | 102,633 | 13 | 205,267 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed from the root. Vertex u is called a child of vertex v and vertex v is called a parent of vertex u if there exists a directed edge from v to u. A vertex is called a leaf if it doesn't have children and has a parent.
Let's call a rooted tree a spruce if its every non-leaf vertex has at least 3 leaf children. You are given a rooted tree, check whether it's a spruce.
The definition of a rooted tree can be found [here](https://goo.gl/1dqvzz).
Input
The first line contains one integer n — the number of vertices in the tree (3 ≤ n ≤ 1 000). Each of the next n - 1 lines contains one integer pi (1 ≤ i ≤ n - 1) — the index of the parent of the i + 1-th vertex (1 ≤ pi ≤ i).
Vertex 1 is the root. It's guaranteed that the root has at least 2 children.
Output
Print "Yes" if the tree is a spruce and "No" otherwise.
Examples
Input
4
1
1
1
Output
Yes
Input
7
1
1
1
2
2
2
Output
No
Input
8
1
1
1
1
3
3
3
Output
Yes
Note
The first example:
<image>
The second example:
<image>
It is not a spruce, because the non-leaf vertex 1 has only 2 leaf children.
The third example:
<image>
Submitted Solution:
```
n=int(input())
arr = [0]*(n+2)
arri = [False]*(n+2)
itrmap = dict()
mitrmap = dict()
# root = 1
# previnp = 0
for i in range(n-1):
inp = int(input())
# if previnp!=inp:
# itr = i+2
# previnp = inp
# arr[inp]+=1
# itrmap[inp]=[(itr,i+2)]
# maplist = list(itrmap.keys())
# #maplist.sort()
# if inp>=itrmap[root][0] and inp<=itrmap[root][1]:
# pass
# else:
# for j in maplist:
# if inp>=itrmap[j][0] and inp<=itrmap[j][1]:
# root = j
# break
# if inp>=itrmap[root][0] and inp<=itrmap[root][1] and arri[inp]==False:
# arr[root]-=1
# arri[inp]=True
# print(maplist,root)
# print(arr)
# print(itrmap)
# flag = 0
# for j in maplist:
# if arr[j]<3:
# flag = 1
# break
# if flag == 1:
# print("No")
# else:
# print("Yes")
arr[i+2]=inp
try:
itrmap[inp].append(i+2)
mitrmap[inp]+=1
except:
itrmap[inp] = [i+2]
mitrmap[inp] = 1
try:
# pass
if arri[inp]==False:
mitrmap[arr[inp]]-=1
arri[inp]=True
except:
pass
# print(arr)
# print(itrmap)
# print(mitrmap)
maplist = list(mitrmap.keys())
flag = 0
for j in maplist:
if (mitrmap[j])<3:
flag = 1
break
if flag == 1:
print("No")
else:
print("Yes")
``` | instruction | 0 | 102,634 | 13 | 205,268 |
Yes | output | 1 | 102,634 | 13 | 205,269 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed from the root. Vertex u is called a child of vertex v and vertex v is called a parent of vertex u if there exists a directed edge from v to u. A vertex is called a leaf if it doesn't have children and has a parent.
Let's call a rooted tree a spruce if its every non-leaf vertex has at least 3 leaf children. You are given a rooted tree, check whether it's a spruce.
The definition of a rooted tree can be found [here](https://goo.gl/1dqvzz).
Input
The first line contains one integer n — the number of vertices in the tree (3 ≤ n ≤ 1 000). Each of the next n - 1 lines contains one integer pi (1 ≤ i ≤ n - 1) — the index of the parent of the i + 1-th vertex (1 ≤ pi ≤ i).
Vertex 1 is the root. It's guaranteed that the root has at least 2 children.
Output
Print "Yes" if the tree is a spruce and "No" otherwise.
Examples
Input
4
1
1
1
Output
Yes
Input
7
1
1
1
2
2
2
Output
No
Input
8
1
1
1
1
3
3
3
Output
Yes
Note
The first example:
<image>
The second example:
<image>
It is not a spruce, because the non-leaf vertex 1 has only 2 leaf children.
The third example:
<image>
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 17 13:44:48 2020
@author: pctab
"""
import sys
sys.setrecursionlimit(99999999)
from collections import defaultdict
parent=defaultdict(int)
children=defaultdict(set)
n=int(input())
arr=[]
for i in range(n-1):
x=int(input())
children[x].add(i+2)
parent[i+2]=x
leaf=set()
for i in range(1,n+1):
if not children[i] and parent[i]!=0:
leaf.add(i)
f=True
for x in children:
if x in leaf:
continue
c=0
for y in children[x]:
if y in leaf:
c+=1
if c<3:
f=False
break
if f:
print('Yes')
else:
print('No')
``` | instruction | 0 | 102,635 | 13 | 205,270 |
Yes | output | 1 | 102,635 | 13 | 205,271 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed from the root. Vertex u is called a child of vertex v and vertex v is called a parent of vertex u if there exists a directed edge from v to u. A vertex is called a leaf if it doesn't have children and has a parent.
Let's call a rooted tree a spruce if its every non-leaf vertex has at least 3 leaf children. You are given a rooted tree, check whether it's a spruce.
The definition of a rooted tree can be found [here](https://goo.gl/1dqvzz).
Input
The first line contains one integer n — the number of vertices in the tree (3 ≤ n ≤ 1 000). Each of the next n - 1 lines contains one integer pi (1 ≤ i ≤ n - 1) — the index of the parent of the i + 1-th vertex (1 ≤ pi ≤ i).
Vertex 1 is the root. It's guaranteed that the root has at least 2 children.
Output
Print "Yes" if the tree is a spruce and "No" otherwise.
Examples
Input
4
1
1
1
Output
Yes
Input
7
1
1
1
2
2
2
Output
No
Input
8
1
1
1
1
3
3
3
Output
Yes
Note
The first example:
<image>
The second example:
<image>
It is not a spruce, because the non-leaf vertex 1 has only 2 leaf children.
The third example:
<image>
Submitted Solution:
```
n = int(input())
a = [0,0]+[int(input()) for _ in range(n-1)]
isleaf = [0,0]+[1 for _ in range(n)]
chleaf = [0,0]+[0 for _ in range(n)]
for i in range(2,n+1):
isleaf[a[i]] = 0
for i in range(2,n+1):
if isleaf[i]:
chleaf[a[i]] += 1
ans = "Yes"
for i in range(1, n+1):
if not isleaf[i] and chleaf[i] < 3:
ans = "No"
break
print(ans)
``` | instruction | 0 | 102,636 | 13 | 205,272 |
Yes | output | 1 | 102,636 | 13 | 205,273 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed from the root. Vertex u is called a child of vertex v and vertex v is called a parent of vertex u if there exists a directed edge from v to u. A vertex is called a leaf if it doesn't have children and has a parent.
Let's call a rooted tree a spruce if its every non-leaf vertex has at least 3 leaf children. You are given a rooted tree, check whether it's a spruce.
The definition of a rooted tree can be found [here](https://goo.gl/1dqvzz).
Input
The first line contains one integer n — the number of vertices in the tree (3 ≤ n ≤ 1 000). Each of the next n - 1 lines contains one integer pi (1 ≤ i ≤ n - 1) — the index of the parent of the i + 1-th vertex (1 ≤ pi ≤ i).
Vertex 1 is the root. It's guaranteed that the root has at least 2 children.
Output
Print "Yes" if the tree is a spruce and "No" otherwise.
Examples
Input
4
1
1
1
Output
Yes
Input
7
1
1
1
2
2
2
Output
No
Input
8
1
1
1
1
3
3
3
Output
Yes
Note
The first example:
<image>
The second example:
<image>
It is not a spruce, because the non-leaf vertex 1 has only 2 leaf children.
The third example:
<image>
Submitted Solution:
```
class Node:
def __init__(self,value):
self.data = value
self.children = []
def add_children(self,value):
self.children.append(Node(value))
class Tree:
def __init__(self):
self.root = Node(1)
def Search(self,value,node = None):
if node is None:
node = self.root
q = None
if node.data == value:
return node
else:
for i in range(len(node.children)):
q = self.Search(value,node.children[i])
if q is not None:
return q
return q
def Insert(self,value,parent):
node = self.Search(parent)
node.add_children(value)
def is_spruce(self,node = None):
if node == None:
node = self.root
spruce = True
for i in range(len(node.children)):
spruce = self.is_spruce(node.children[i])
if not spruce:
return spruce
if len(node.children) > 0:
num = 0
for i in range(len(node.children)):
child = node.children[i]
if len(child.children) == 0:
num = num + 1
if num != 3:
return False
return True
if __name__ == "__main__":
n = int(input())
tree = Tree()
for i in range(n-1):
parent = int(input())
value = i+2
tree.Insert(value,parent)
if tree.is_spruce():
print("Yes")
else:
print("No")
``` | instruction | 0 | 102,637 | 13 | 205,274 |
No | output | 1 | 102,637 | 13 | 205,275 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed from the root. Vertex u is called a child of vertex v and vertex v is called a parent of vertex u if there exists a directed edge from v to u. A vertex is called a leaf if it doesn't have children and has a parent.
Let's call a rooted tree a spruce if its every non-leaf vertex has at least 3 leaf children. You are given a rooted tree, check whether it's a spruce.
The definition of a rooted tree can be found [here](https://goo.gl/1dqvzz).
Input
The first line contains one integer n — the number of vertices in the tree (3 ≤ n ≤ 1 000). Each of the next n - 1 lines contains one integer pi (1 ≤ i ≤ n - 1) — the index of the parent of the i + 1-th vertex (1 ≤ pi ≤ i).
Vertex 1 is the root. It's guaranteed that the root has at least 2 children.
Output
Print "Yes" if the tree is a spruce and "No" otherwise.
Examples
Input
4
1
1
1
Output
Yes
Input
7
1
1
1
2
2
2
Output
No
Input
8
1
1
1
1
3
3
3
Output
Yes
Note
The first example:
<image>
The second example:
<image>
It is not a spruce, because the non-leaf vertex 1 has only 2 leaf children.
The third example:
<image>
Submitted Solution:
```
x = int(input())
m = [[] for i in range(x)]
for i in range(x - 1):
l = int(input())
m[l - 1] += [i + 2]
for i in range(x):
if len(m[i]) != 0:
m[i] += [len(m[i])]
else:
m[i] += [-10000]
k = 1
f = 0
def dfs(k,f):
if len(m[k - 1]) > 2:
for i in range(len(m[k - 1])):
dfs(m[k-1][i], (k - 1))
elif m[k - 1][-1] == 1 or m[k - 1][-1] == 2:
m[f][-1] -= 1
return 0
dfs(k,f)
t = 0
for i in range(x):
if m[i][-1] < 3 and m[i][-1] != -10000:
print('No')
t = 1
break
if t == 0:
print('Yes')
``` | instruction | 0 | 102,638 | 13 | 205,276 |
No | output | 1 | 102,638 | 13 | 205,277 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed from the root. Vertex u is called a child of vertex v and vertex v is called a parent of vertex u if there exists a directed edge from v to u. A vertex is called a leaf if it doesn't have children and has a parent.
Let's call a rooted tree a spruce if its every non-leaf vertex has at least 3 leaf children. You are given a rooted tree, check whether it's a spruce.
The definition of a rooted tree can be found [here](https://goo.gl/1dqvzz).
Input
The first line contains one integer n — the number of vertices in the tree (3 ≤ n ≤ 1 000). Each of the next n - 1 lines contains one integer pi (1 ≤ i ≤ n - 1) — the index of the parent of the i + 1-th vertex (1 ≤ pi ≤ i).
Vertex 1 is the root. It's guaranteed that the root has at least 2 children.
Output
Print "Yes" if the tree is a spruce and "No" otherwise.
Examples
Input
4
1
1
1
Output
Yes
Input
7
1
1
1
2
2
2
Output
No
Input
8
1
1
1
1
3
3
3
Output
Yes
Note
The first example:
<image>
The second example:
<image>
It is not a spruce, because the non-leaf vertex 1 has only 2 leaf children.
The third example:
<image>
Submitted Solution:
```
import sys;readline = sys.stdin.readline
def i1(): return int(readline())
def nl(): return [int(s) for s in readline().split()]
def nn(n): return [int(readline()) for i in range(n)]
def nnp(n,x): return [int(readline())+x for i in range(n)]
def nmp(n,x): return (int(readline())+x for i in range(n))
def nlp(x): return [int(s)+x for s in readline().split()]
def nll(n): return [[int(s) for s in readline().split()] for i in range(n)]
def mll(n): return ([int(s) for s in readline().split()] for i in range(n))
def s1(): return readline().rstrip()
def sl(): return [s for s in readline().split()]
def sn(n): return [readline().rstrip() for i in range(n)]
def sm(n): return (readline().rstrip() for i in range(n))
def redir(s): global readline;import os;fn=sys.argv[0] + f'/../in-{s}.txt';readline = open(fn).readline if os.path.exists(fn) else readline
redir('b')
n = i1()
# arr = nn(n-1)
# print(n, arr)
children = [[] for i in range(n+1)]
for i in range(2,n+1):
children[int(readline())].append(i)
for i in range(1,n+1):
# print(f"i={i} {children[i]}")
if children[i] and len([i for i in children[i] if i]):
print("NO")
break
else:
print("YES")
# print(children)
``` | instruction | 0 | 102,639 | 13 | 205,278 |
No | output | 1 | 102,639 | 13 | 205,279 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed from the root. Vertex u is called a child of vertex v and vertex v is called a parent of vertex u if there exists a directed edge from v to u. A vertex is called a leaf if it doesn't have children and has a parent.
Let's call a rooted tree a spruce if its every non-leaf vertex has at least 3 leaf children. You are given a rooted tree, check whether it's a spruce.
The definition of a rooted tree can be found [here](https://goo.gl/1dqvzz).
Input
The first line contains one integer n — the number of vertices in the tree (3 ≤ n ≤ 1 000). Each of the next n - 1 lines contains one integer pi (1 ≤ i ≤ n - 1) — the index of the parent of the i + 1-th vertex (1 ≤ pi ≤ i).
Vertex 1 is the root. It's guaranteed that the root has at least 2 children.
Output
Print "Yes" if the tree is a spruce and "No" otherwise.
Examples
Input
4
1
1
1
Output
Yes
Input
7
1
1
1
2
2
2
Output
No
Input
8
1
1
1
1
3
3
3
Output
Yes
Note
The first example:
<image>
The second example:
<image>
It is not a spruce, because the non-leaf vertex 1 has only 2 leaf children.
The third example:
<image>
Submitted Solution:
```
import sys;readline = sys.stdin.readline
def i1(): return int(readline())
def nl(): return [int(s) for s in readline().split()]
def nn(n): return [int(readline()) for i in range(n)]
def nnp(n,x): return [int(readline())+x for i in range(n)]
def nmp(n,x): return (int(readline())+x for i in range(n))
def nlp(x): return [int(s)+x for s in readline().split()]
def nll(n): return [[int(s) for s in readline().split()] for i in range(n)]
def mll(n): return ([int(s) for s in readline().split()] for i in range(n))
def s1(): return readline().rstrip()
def sl(): return [s for s in readline().split()]
def sn(n): return [readline().rstrip() for i in range(n)]
def sm(n): return (readline().rstrip() for i in range(n))
def redir(s): global readline;import os;fn=sys.argv[0] + f'/../in-{s}.txt';readline = open(fn).readline if os.path.exists(fn) else readline
redir('b')
from collections import defaultdict
n = i1()
arr = nn(n-1)
cnt = defaultdict(int)
for i in arr:
cnt[i] += 1
print('YES' if set(cnt.values()) == {3} else 'NO')
# for s in sn(n-1):
# cnt[int(s)] += 1
print(n, arr)
``` | instruction | 0 | 102,640 | 13 | 205,280 |
No | output | 1 | 102,640 | 13 | 205,281 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has a tree consisting of n vertices with root in vertex 1. At first all vertices has 0 written on it.
Let d(i, j) be the distance between vertices i and j, i.e. number of edges in the shortest path from i to j. Also, let's denote k-subtree of vertex x — set of vertices y such that next two conditions are met:
* x is the ancestor of y (each vertex is the ancestor of itself);
* d(x, y) ≤ k.
Vasya needs you to process m queries. The i-th query is a triple v_i, d_i and x_i. For each query Vasya adds value x_i to each vertex from d_i-subtree of v_i.
Report to Vasya all values, written on vertices of the tree after processing all queries.
Input
The first line contains single integer n (1 ≤ n ≤ 3 ⋅ 10^5) — number of vertices in the tree.
Each of next n - 1 lines contains two integers x and y (1 ≤ x, y ≤ n) — edge between vertices x and y. It is guarantied that given graph is a tree.
Next line contains single integer m (1 ≤ m ≤ 3 ⋅ 10^5) — number of queries.
Each of next m lines contains three integers v_i, d_i, x_i (1 ≤ v_i ≤ n, 0 ≤ d_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — description of the i-th query.
Output
Print n integers. The i-th integers is the value, written in the i-th vertex after processing all queries.
Examples
Input
5
1 2
1 3
2 4
2 5
3
1 1 1
2 0 10
4 10 100
Output
1 11 1 100 0
Input
5
2 3
2 1
5 4
3 4
5
2 0 4
3 10 1
1 2 3
2 3 10
1 1 7
Output
10 24 14 11 11
Note
In the first exapmle initial values in vertices are 0, 0, 0, 0, 0. After the first query values will be equal to 1, 1, 1, 0, 0. After the second query values will be equal to 1, 11, 1, 0, 0. After the third query values will be equal to 1, 11, 1, 100, 0. | instruction | 0 | 102,910 | 13 | 205,820 |
Tags: data structures, trees
Correct Solution:
```
def EulerTour(g, root):
n = len(g)
root = root
g = g
tank = [root]
eulerTour = []
left = [0]*n
right = [-1]*n
depth = [-1]*n
parent = [-1]*n
child = [[] for i in range(n)]
eulerNum = -1
de = -1
while tank:
#print(tank)
v = tank.pop()
if v >= 0:
eulerNum += 1
eulerTour.append(v)
left[v] = eulerNum
right[v] = eulerNum
tank.append(~v)
de += 1
depth[v] = de
for u in g[v]:
if parent[v] == u:
continue
tank.append(u)
parent[u] = v
child[v].append(u)
else:
de -= 1
if ~v != root:
eulerTour.append(parent[~v])
eulerNum += 1
right[parent[~v]] = eulerNum
return eulerTour, left, right, depth, parent
import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n = int(input())
g = [[] for i in range(n)]
for i in range(n-1):
x, y = map(int, input().split())
x, y = x-1, y-1
g[x].append(y)
g[y].append(x)
et, left, right, depth, parent = EulerTour(g, 0)
m = int(input())
Q = [[] for i in range(n)]
for i in range(m):
v, d, x = map(int, input().split())
v -= 1
Q[v].append((d, x))
#print(et)
#print(left)
#print(right)
#print(depth)
ans = [0]*n
imos = [0]*(n+2)
for i, v in enumerate(et):
# in
if i == left[v]:
for d, x in Q[v]:
imos[depth[v]] += x
imos[min(depth[v]+d+1, n+1)] -= x
if parent[v] != -1:
ans[v] = ans[parent[v]]+imos[depth[v]]
else:
ans[v] = imos[depth[v]]
# out
if i == right[v]:
for d, x in Q[v]:
imos[depth[v]] -= x
imos[min(depth[v]+d+1, n+1)] += x
print(*ans)
``` | output | 1 | 102,910 | 13 | 205,821 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has a tree consisting of n vertices with root in vertex 1. At first all vertices has 0 written on it.
Let d(i, j) be the distance between vertices i and j, i.e. number of edges in the shortest path from i to j. Also, let's denote k-subtree of vertex x — set of vertices y such that next two conditions are met:
* x is the ancestor of y (each vertex is the ancestor of itself);
* d(x, y) ≤ k.
Vasya needs you to process m queries. The i-th query is a triple v_i, d_i and x_i. For each query Vasya adds value x_i to each vertex from d_i-subtree of v_i.
Report to Vasya all values, written on vertices of the tree after processing all queries.
Input
The first line contains single integer n (1 ≤ n ≤ 3 ⋅ 10^5) — number of vertices in the tree.
Each of next n - 1 lines contains two integers x and y (1 ≤ x, y ≤ n) — edge between vertices x and y. It is guarantied that given graph is a tree.
Next line contains single integer m (1 ≤ m ≤ 3 ⋅ 10^5) — number of queries.
Each of next m lines contains three integers v_i, d_i, x_i (1 ≤ v_i ≤ n, 0 ≤ d_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — description of the i-th query.
Output
Print n integers. The i-th integers is the value, written in the i-th vertex after processing all queries.
Examples
Input
5
1 2
1 3
2 4
2 5
3
1 1 1
2 0 10
4 10 100
Output
1 11 1 100 0
Input
5
2 3
2 1
5 4
3 4
5
2 0 4
3 10 1
1 2 3
2 3 10
1 1 7
Output
10 24 14 11 11
Note
In the first exapmle initial values in vertices are 0, 0, 0, 0, 0. After the first query values will be equal to 1, 1, 1, 0, 0. After the second query values will be equal to 1, 11, 1, 0, 0. After the third query values will be equal to 1, 11, 1, 100, 0. | instruction | 0 | 102,911 | 13 | 205,822 |
Tags: data structures, trees
Correct Solution:
```
def EulerTour(g, root):
n = len(g)
root = root
g = g
tank = [root]
eulerTour = []
left = [0]*n
right = [-1]*n
depth = [-1]*n
parent = [-1]*n
child = [[] for i in range(n)]
eulerNum = -1
de = -1
while tank:
#print(tank)
v = tank.pop()
if v >= 0:
eulerNum += 1
eulerTour.append(v)
left[v] = eulerNum
right[v] = eulerNum
tank.append(~v)
de += 1
depth[v] = de
for u in g[v]:
if parent[v] == u:
continue
tank.append(u)
parent[u] = v
child[v].append(u)
else:
de -= 1
if ~v != root:
eulerTour.append(parent[~v])
eulerNum += 1
right[parent[~v]] = eulerNum
return eulerTour, left, right, depth
import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n = int(input())
g = [[] for i in range(n)]
for i in range(n-1):
x, y = map(int, input().split())
x, y = x-1, y-1
g[x].append(y)
g[y].append(x)
et, left, right, depth = EulerTour(g, 0)
m = int(input())
Q = [[] for i in range(n)]
for i in range(m):
v, d, x = map(int, input().split())
v -= 1
Q[v].append((d, x))
#print(et)
#print(left)
#print(right)
#print(depth)
ans = [0]*n
imos = [0]*(n+2)
for d, x in Q[0]:
imos[0] += x
imos[min(d+1, n+1)] -= x
ans[0] = imos[0]
#print(0, imos)
for i in range(1, len(et)):
cur_v = et[i]
pre_v = et[i-1]
if depth[cur_v] > depth[pre_v]:
for d, x in Q[cur_v]:
imos[depth[cur_v]] += x
imos[min(depth[cur_v]+d+1, n+1)] -= x
ans[cur_v] = ans[pre_v]+imos[depth[cur_v]]
if left[cur_v] == right[cur_v]:
for d, x in Q[cur_v]:
imos[depth[cur_v]] -= x
imos[min(depth[cur_v]+d+1, n+1)] += x
else:
if i == right[cur_v]:
for d, x in Q[cur_v]:
imos[depth[cur_v]] -= x
imos[min(depth[cur_v]+d+1, n+1)] += x
#print(cur_v, imos)
print(*ans)
``` | output | 1 | 102,911 | 13 | 205,823 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has a tree consisting of n vertices with root in vertex 1. At first all vertices has 0 written on it.
Let d(i, j) be the distance between vertices i and j, i.e. number of edges in the shortest path from i to j. Also, let's denote k-subtree of vertex x — set of vertices y such that next two conditions are met:
* x is the ancestor of y (each vertex is the ancestor of itself);
* d(x, y) ≤ k.
Vasya needs you to process m queries. The i-th query is a triple v_i, d_i and x_i. For each query Vasya adds value x_i to each vertex from d_i-subtree of v_i.
Report to Vasya all values, written on vertices of the tree after processing all queries.
Input
The first line contains single integer n (1 ≤ n ≤ 3 ⋅ 10^5) — number of vertices in the tree.
Each of next n - 1 lines contains two integers x and y (1 ≤ x, y ≤ n) — edge between vertices x and y. It is guarantied that given graph is a tree.
Next line contains single integer m (1 ≤ m ≤ 3 ⋅ 10^5) — number of queries.
Each of next m lines contains three integers v_i, d_i, x_i (1 ≤ v_i ≤ n, 0 ≤ d_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — description of the i-th query.
Output
Print n integers. The i-th integers is the value, written in the i-th vertex after processing all queries.
Examples
Input
5
1 2
1 3
2 4
2 5
3
1 1 1
2 0 10
4 10 100
Output
1 11 1 100 0
Input
5
2 3
2 1
5 4
3 4
5
2 0 4
3 10 1
1 2 3
2 3 10
1 1 7
Output
10 24 14 11 11
Note
In the first exapmle initial values in vertices are 0, 0, 0, 0, 0. After the first query values will be equal to 1, 1, 1, 0, 0. After the second query values will be equal to 1, 11, 1, 0, 0. After the third query values will be equal to 1, 11, 1, 100, 0. | instruction | 0 | 102,912 | 13 | 205,824 |
Tags: data structures, trees
Correct Solution:
```
import math
import os
import sys
from io import BytesIO, IOBase
from types import GeneratorType
from collections import defaultdict
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")
sys.setrecursionlimit(10**5)
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
@bootstrap
def dfs(u,i,p):
global d
global s
s+=-d[i-1]
for j in val[u]:
d[i+j[0]]+=j[1]
s+=j[1]
ans[u]=s
for j in adj[u]:
if j!=p:
yield dfs(j,i+1,u)
for j in val[u]:
d[i + j[0]] += -j[1]
s += -j[1]
s+=d[i-1]
yield
n=int(input())
adj=[[] for i in range(n+1)]
for j in range(n-1):
u,v=map(int,input().split())
adj[u].append(v)
adj[v].append(u)
val=[[] for i in range(n+1)]
m=int(input())
for j in range(m):
v,d,va=map(int,input().split())
val[v].append([d,va])
s=0
d=defaultdict(lambda:0)
ans=[0 for i in range(n+1)]
dfs(1,0,0)
print(*ans[1:])
``` | output | 1 | 102,912 | 13 | 205,825 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has a tree consisting of n vertices with root in vertex 1. At first all vertices has 0 written on it.
Let d(i, j) be the distance between vertices i and j, i.e. number of edges in the shortest path from i to j. Also, let's denote k-subtree of vertex x — set of vertices y such that next two conditions are met:
* x is the ancestor of y (each vertex is the ancestor of itself);
* d(x, y) ≤ k.
Vasya needs you to process m queries. The i-th query is a triple v_i, d_i and x_i. For each query Vasya adds value x_i to each vertex from d_i-subtree of v_i.
Report to Vasya all values, written on vertices of the tree after processing all queries.
Input
The first line contains single integer n (1 ≤ n ≤ 3 ⋅ 10^5) — number of vertices in the tree.
Each of next n - 1 lines contains two integers x and y (1 ≤ x, y ≤ n) — edge between vertices x and y. It is guarantied that given graph is a tree.
Next line contains single integer m (1 ≤ m ≤ 3 ⋅ 10^5) — number of queries.
Each of next m lines contains three integers v_i, d_i, x_i (1 ≤ v_i ≤ n, 0 ≤ d_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — description of the i-th query.
Output
Print n integers. The i-th integers is the value, written in the i-th vertex after processing all queries.
Examples
Input
5
1 2
1 3
2 4
2 5
3
1 1 1
2 0 10
4 10 100
Output
1 11 1 100 0
Input
5
2 3
2 1
5 4
3 4
5
2 0 4
3 10 1
1 2 3
2 3 10
1 1 7
Output
10 24 14 11 11
Note
In the first exapmle initial values in vertices are 0, 0, 0, 0, 0. After the first query values will be equal to 1, 1, 1, 0, 0. After the second query values will be equal to 1, 11, 1, 0, 0. After the third query values will be equal to 1, 11, 1, 100, 0. | instruction | 0 | 102,913 | 13 | 205,826 |
Tags: data structures, trees
Correct Solution:
```
def EulerTour(g, root):
n = len(g)
root = root
g = g
tank = [root]
eulerTour = []
left = [0]*n
right = [-1]*n
depth = [-1]*n
parent = [-1]*n
child = [[] for i in range(n)]
eulerNum = -1
de = -1
while tank:
#print(tank)
v = tank.pop()
if v >= 0:
eulerNum += 1
eulerTour.append(v)
left[v] = eulerNum
right[v] = eulerNum
tank.append(~v)
de += 1
depth[v] = de
for u in g[v]:
if parent[v] == u:
continue
tank.append(u)
parent[u] = v
child[v].append(u)
else:
de -= 1
if ~v != root:
eulerTour.append(parent[~v])
eulerNum += 1
right[parent[~v]] = eulerNum
return eulerTour, left, right, depth
import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
def main():
n = int(input())
g = [[] for i in range(n)]
for i in range(n-1):
x, y = map(int, input().split())
x, y = x-1, y-1
g[x].append(y)
g[y].append(x)
et, left, right, depth = EulerTour(g, 0)
m = int(input())
Q = [[] for i in range(n)]
for i in range(m):
v, d, x = map(int, input().split())
v -= 1
Q[v].append((d, x))
#print(et)
#print(left)
#print(right)
#print(depth)
ans = [0]*n
imos = [0]*(n+2)
for d, x in Q[0]:
imos[0] += x
imos[min(d+1, n+1)] -= x
ans[0] = imos[0]
#print(0, imos)
for i in range(1, len(et)):
cur_v = et[i]
pre_v = et[i-1]
if depth[cur_v] > depth[pre_v]:
for d, x in Q[cur_v]:
imos[depth[cur_v]] += x
imos[min(depth[cur_v]+d+1, n+1)] -= x
ans[cur_v] = ans[pre_v]+imos[depth[cur_v]]
if left[cur_v] == right[cur_v]:
for d, x in Q[cur_v]:
imos[depth[cur_v]] -= x
imos[min(depth[cur_v]+d+1, n+1)] += x
else:
if i == right[cur_v]:
for d, x in Q[cur_v]:
imos[depth[cur_v]] -= x
imos[min(depth[cur_v]+d+1, n+1)] += x
#print(cur_v, imos)
print(*ans)
if __name__ == '__main__':
main()
``` | output | 1 | 102,913 | 13 | 205,827 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has a tree consisting of n vertices with root in vertex 1. At first all vertices has 0 written on it.
Let d(i, j) be the distance between vertices i and j, i.e. number of edges in the shortest path from i to j. Also, let's denote k-subtree of vertex x — set of vertices y such that next two conditions are met:
* x is the ancestor of y (each vertex is the ancestor of itself);
* d(x, y) ≤ k.
Vasya needs you to process m queries. The i-th query is a triple v_i, d_i and x_i. For each query Vasya adds value x_i to each vertex from d_i-subtree of v_i.
Report to Vasya all values, written on vertices of the tree after processing all queries.
Input
The first line contains single integer n (1 ≤ n ≤ 3 ⋅ 10^5) — number of vertices in the tree.
Each of next n - 1 lines contains two integers x and y (1 ≤ x, y ≤ n) — edge between vertices x and y. It is guarantied that given graph is a tree.
Next line contains single integer m (1 ≤ m ≤ 3 ⋅ 10^5) — number of queries.
Each of next m lines contains three integers v_i, d_i, x_i (1 ≤ v_i ≤ n, 0 ≤ d_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — description of the i-th query.
Output
Print n integers. The i-th integers is the value, written in the i-th vertex after processing all queries.
Examples
Input
5
1 2
1 3
2 4
2 5
3
1 1 1
2 0 10
4 10 100
Output
1 11 1 100 0
Input
5
2 3
2 1
5 4
3 4
5
2 0 4
3 10 1
1 2 3
2 3 10
1 1 7
Output
10 24 14 11 11
Note
In the first exapmle initial values in vertices are 0, 0, 0, 0, 0. After the first query values will be equal to 1, 1, 1, 0, 0. After the second query values will be equal to 1, 11, 1, 0, 0. After the third query values will be equal to 1, 11, 1, 100, 0. | instruction | 0 | 102,914 | 13 | 205,828 |
Tags: data structures, trees
Correct Solution:
```
def EulerTour(g, root):
n = len(g)
root = root
g = g
tank = [root]
eulerTour = []
left = [0]*n
right = [-1]*n
depth = [-1]*n
parent = [-1]*n
child = [[] for i in range(n)]
eulerNum = -1
de = -1
while tank:
#print(tank)
v = tank.pop()
if v >= 0:
eulerNum += 1
eulerTour.append(v)
left[v] = eulerNum
right[v] = eulerNum
tank.append(~v)
de += 1
depth[v] = de
for u in g[v]:
if parent[v] == u:
continue
tank.append(u)
parent[u] = v
child[v].append(u)
else:
de -= 1
if ~v != root:
eulerTour.append(parent[~v])
eulerNum += 1
right[parent[~v]] = eulerNum
return eulerTour, left, right, depth, parent
import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n = int(input())
g = [[] for i in range(n)]
for i in range(n-1):
x, y = map(int, input().split())
x, y = x-1, y-1
g[x].append(y)
g[y].append(x)
et, left, right, depth, parent = EulerTour(g, 0)
m = int(input())
Q = [[] for i in range(n)]
for i in range(m):
v, d, x = map(int, input().split())
v -= 1
Q[v].append((d, x))
#print(et)
#print(left)
#print(right)
#print(depth)
ans = [0]*n
imos = [0]*(n+2)
for i, v in enumerate(et):
if i == left[v]:
for d, x in Q[v]:
imos[depth[v]] += x
imos[min(depth[v]+d+1, n+1)] -= x
if parent[v] != -1:
ans[v] = ans[parent[v]]+imos[depth[v]]
else:
ans[v] = imos[depth[v]]
if i == right[v]:
for d, x in Q[v]:
imos[depth[v]] -= x
imos[min(depth[v]+d+1, n+1)] += x
print(*ans)
``` | output | 1 | 102,914 | 13 | 205,829 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has a tree consisting of n vertices with root in vertex 1. At first all vertices has 0 written on it.
Let d(i, j) be the distance between vertices i and j, i.e. number of edges in the shortest path from i to j. Also, let's denote k-subtree of vertex x — set of vertices y such that next two conditions are met:
* x is the ancestor of y (each vertex is the ancestor of itself);
* d(x, y) ≤ k.
Vasya needs you to process m queries. The i-th query is a triple v_i, d_i and x_i. For each query Vasya adds value x_i to each vertex from d_i-subtree of v_i.
Report to Vasya all values, written on vertices of the tree after processing all queries.
Input
The first line contains single integer n (1 ≤ n ≤ 3 ⋅ 10^5) — number of vertices in the tree.
Each of next n - 1 lines contains two integers x and y (1 ≤ x, y ≤ n) — edge between vertices x and y. It is guarantied that given graph is a tree.
Next line contains single integer m (1 ≤ m ≤ 3 ⋅ 10^5) — number of queries.
Each of next m lines contains three integers v_i, d_i, x_i (1 ≤ v_i ≤ n, 0 ≤ d_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — description of the i-th query.
Output
Print n integers. The i-th integers is the value, written in the i-th vertex after processing all queries.
Examples
Input
5
1 2
1 3
2 4
2 5
3
1 1 1
2 0 10
4 10 100
Output
1 11 1 100 0
Input
5
2 3
2 1
5 4
3 4
5
2 0 4
3 10 1
1 2 3
2 3 10
1 1 7
Output
10 24 14 11 11
Note
In the first exapmle initial values in vertices are 0, 0, 0, 0, 0. After the first query values will be equal to 1, 1, 1, 0, 0. After the second query values will be equal to 1, 11, 1, 0, 0. After the third query values will be equal to 1, 11, 1, 100, 0. | instruction | 0 | 102,915 | 13 | 205,830 |
Tags: data structures, trees
Correct Solution:
```
import sys
from array import array # noqa: F401
import typing as Tp # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
def main():
n = int(input())
adj = [[] for _ in range(n)]
for u, v in (map(int, input().split()) for _ in range(n - 1)):
adj[u - 1].append(v - 1)
adj[v - 1].append(u - 1)
m = int(input())
query = [[] for _ in range(n)]
for _ in range(m):
v, d, x = map(int, input().split())
query[v - 1].append((d, float(x)))
ans = [''] * n
acc = [0.0] * (n + 100)
value, stack, depth = 0.0, [[0, 0, -1]], 0
d_inf, eps = n + 50, 1e-8
while stack:
v, ei, parent = stack[-1]
if ei == 0:
# arrive
for d, x in query[v]:
acc[depth] += x
acc[min(d_inf, depth + d + 1)] -= x
value += acc[depth]
ans[v] = str(int(value + eps))
for i in range(ei, len(adj[v])):
if adj[v][i] == parent:
continue
stack[-1][1] = i + 1
stack.append([adj[v][i], 0, v])
depth += 1
break
else:
# leave
value -= acc[depth]
for d, x in query[v]:
acc[depth] -= x
acc[min(d_inf, depth + d + 1)] += x
stack.pop()
depth -= 1
sys.stdout.buffer.write((' '.join(ans) + '\n').encode('utf-8'))
if __name__ == '__main__':
main()
``` | output | 1 | 102,915 | 13 | 205,831 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has a tree consisting of n vertices with root in vertex 1. At first all vertices has 0 written on it.
Let d(i, j) be the distance between vertices i and j, i.e. number of edges in the shortest path from i to j. Also, let's denote k-subtree of vertex x — set of vertices y such that next two conditions are met:
* x is the ancestor of y (each vertex is the ancestor of itself);
* d(x, y) ≤ k.
Vasya needs you to process m queries. The i-th query is a triple v_i, d_i and x_i. For each query Vasya adds value x_i to each vertex from d_i-subtree of v_i.
Report to Vasya all values, written on vertices of the tree after processing all queries.
Input
The first line contains single integer n (1 ≤ n ≤ 3 ⋅ 10^5) — number of vertices in the tree.
Each of next n - 1 lines contains two integers x and y (1 ≤ x, y ≤ n) — edge between vertices x and y. It is guarantied that given graph is a tree.
Next line contains single integer m (1 ≤ m ≤ 3 ⋅ 10^5) — number of queries.
Each of next m lines contains three integers v_i, d_i, x_i (1 ≤ v_i ≤ n, 0 ≤ d_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — description of the i-th query.
Output
Print n integers. The i-th integers is the value, written in the i-th vertex after processing all queries.
Examples
Input
5
1 2
1 3
2 4
2 5
3
1 1 1
2 0 10
4 10 100
Output
1 11 1 100 0
Input
5
2 3
2 1
5 4
3 4
5
2 0 4
3 10 1
1 2 3
2 3 10
1 1 7
Output
10 24 14 11 11
Note
In the first exapmle initial values in vertices are 0, 0, 0, 0, 0. After the first query values will be equal to 1, 1, 1, 0, 0. After the second query values will be equal to 1, 11, 1, 0, 0. After the third query values will be equal to 1, 11, 1, 100, 0.
Submitted Solution:
```
def EulerTour(g, root):
n = len(g)
root = root
g = g
tank = [root]
eulerTour = []
left = [0]*n
right = [-1]*n
depth = [-1]*n
parent = [-1]*n
child = [[] for i in range(n)]
eulerNum = -1
de = -1
while tank:
#print(tank)
v = tank.pop()
if v >= 0:
eulerNum += 1
eulerTour.append(v)
left[v] = eulerNum
right[v] = eulerNum
tank.append(~v)
de += 1
depth[v] = de
for u in g[v]:
if parent[v] == u:
continue
tank.append(u)
parent[u] = v
child[v].append(u)
else:
de -= 1
if ~v != root:
eulerTour.append(parent[~v])
eulerNum += 1
right[parent[~v]] = eulerNum
return eulerTour, left, right, depth
import sys
import io, os
input = sys.stdin.buffer.readline
#input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n = int(input())
g = [[] for i in range(n)]
for i in range(n-1):
x, y = map(int, input().split())
x, y = x-1, y-1
g[x].append(y)
g[y].append(x)
et, left, right, depth = EulerTour(g, 0)
m = int(input())
Q = [[] for i in range(n)]
for i in range(m):
v, d, x = map(int, input().split())
v -= 1
Q[v].append((d, x))
#print(et)
#print(left)
#print(right)
#print(depth)
ans = [0]*n
imos = [0]*(n+2)
for d, x in Q[0]:
imos[0] += x
imos[min(d+1, n+1)] -= x
ans[0] = imos[0]
#print(0, imos)
for i in range(1, len(et)):
cur_v = et[i]
pre_v = et[i-1]
if depth[cur_v] > depth[pre_v]:
for d, x in Q[cur_v]:
imos[depth[cur_v]] += x
imos[min(depth[cur_v]+d+1, n+1)] -= x
ans[cur_v] = ans[pre_v]+imos[depth[cur_v]]
else:
if i == right[cur_v]:
for d, x in Q[cur_v]:
imos[depth[cur_v]] -= x
imos[min(depth[cur_v]+d+1, n+1)] += x
#print(cur_v, imos)
print(*ans)
``` | instruction | 0 | 102,916 | 13 | 205,832 |
No | output | 1 | 102,916 | 13 | 205,833 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has a tree consisting of n vertices with root in vertex 1. At first all vertices has 0 written on it.
Let d(i, j) be the distance between vertices i and j, i.e. number of edges in the shortest path from i to j. Also, let's denote k-subtree of vertex x — set of vertices y such that next two conditions are met:
* x is the ancestor of y (each vertex is the ancestor of itself);
* d(x, y) ≤ k.
Vasya needs you to process m queries. The i-th query is a triple v_i, d_i and x_i. For each query Vasya adds value x_i to each vertex from d_i-subtree of v_i.
Report to Vasya all values, written on vertices of the tree after processing all queries.
Input
The first line contains single integer n (1 ≤ n ≤ 3 ⋅ 10^5) — number of vertices in the tree.
Each of next n - 1 lines contains two integers x and y (1 ≤ x, y ≤ n) — edge between vertices x and y. It is guarantied that given graph is a tree.
Next line contains single integer m (1 ≤ m ≤ 3 ⋅ 10^5) — number of queries.
Each of next m lines contains three integers v_i, d_i, x_i (1 ≤ v_i ≤ n, 0 ≤ d_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — description of the i-th query.
Output
Print n integers. The i-th integers is the value, written in the i-th vertex after processing all queries.
Examples
Input
5
1 2
1 3
2 4
2 5
3
1 1 1
2 0 10
4 10 100
Output
1 11 1 100 0
Input
5
2 3
2 1
5 4
3 4
5
2 0 4
3 10 1
1 2 3
2 3 10
1 1 7
Output
10 24 14 11 11
Note
In the first exapmle initial values in vertices are 0, 0, 0, 0, 0. After the first query values will be equal to 1, 1, 1, 0, 0. After the second query values will be equal to 1, 11, 1, 0, 0. After the third query values will be equal to 1, 11, 1, 100, 0.
Submitted Solution:
```
n = int(input())
g = {}
par = {}
q = {}
for _ in range(n-1):
u, v = map(int, input().split())
if u not in g:
g[u] = []
g[u].append(v)
if v not in g:
g[v] = []
g[v].append(u)
m = int(input())
for _ in range(m):
# add v to d-th subtree of u
u, d, v = map(int, input().split())
if u not in q:
q[u] = []
q[u].append([d, v])
# node depth
s = [[1, 0]]
suft = []
used = [False] * (n+1)
ans = [0] * (n+1)
add = [0] * (n+1)
par[1] = 0
while len(s) > 0:
cur, d = s[-1]
if used[cur] == True:
suft.append(s.pop())
elif used[cur] == False:
used[cur] = True
suft.append([cur, d])
if len(g[cur]) == 1 and cur != 1:
s.pop()
else:
for x in g[cur]:
if used[x] == False:
par[x] = cur
s.append([x, d+1])
ans[0] = 0
used = [False] * (n+1)
for u, d in suft:
if used[u] == False:
used[u] = True
sum = ans[par[u]]
if u in q:
for d_, v in q[u]:
add[d] += v
if d + d_ + 1 < n:
add[d+d_+1] -= v
ans[u] = sum + add[d]
else:
if u in q:
for d_, v in q[u]:
add[d] -= v
if d + d_ + 1 < n:
add[d+d_+1] += v
print(' '.join([str(x) for x in ans[1:]]))
#5
#2 3
#2 1
#5 4
#3 4
#5
#2 0 4
#3 10 1
#1 2 3
#2 3 10
#1 1 7
#--> [[1, 0], [2, 1], [3, 2], [4, 3], [5, 4], [4, 3], [3, 2], [2, 1], [1, 0]]
``` | instruction | 0 | 102,917 | 13 | 205,834 |
No | output | 1 | 102,917 | 13 | 205,835 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has a tree consisting of n vertices with root in vertex 1. At first all vertices has 0 written on it.
Let d(i, j) be the distance between vertices i and j, i.e. number of edges in the shortest path from i to j. Also, let's denote k-subtree of vertex x — set of vertices y such that next two conditions are met:
* x is the ancestor of y (each vertex is the ancestor of itself);
* d(x, y) ≤ k.
Vasya needs you to process m queries. The i-th query is a triple v_i, d_i and x_i. For each query Vasya adds value x_i to each vertex from d_i-subtree of v_i.
Report to Vasya all values, written on vertices of the tree after processing all queries.
Input
The first line contains single integer n (1 ≤ n ≤ 3 ⋅ 10^5) — number of vertices in the tree.
Each of next n - 1 lines contains two integers x and y (1 ≤ x, y ≤ n) — edge between vertices x and y. It is guarantied that given graph is a tree.
Next line contains single integer m (1 ≤ m ≤ 3 ⋅ 10^5) — number of queries.
Each of next m lines contains three integers v_i, d_i, x_i (1 ≤ v_i ≤ n, 0 ≤ d_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — description of the i-th query.
Output
Print n integers. The i-th integers is the value, written in the i-th vertex after processing all queries.
Examples
Input
5
1 2
1 3
2 4
2 5
3
1 1 1
2 0 10
4 10 100
Output
1 11 1 100 0
Input
5
2 3
2 1
5 4
3 4
5
2 0 4
3 10 1
1 2 3
2 3 10
1 1 7
Output
10 24 14 11 11
Note
In the first exapmle initial values in vertices are 0, 0, 0, 0, 0. After the first query values will be equal to 1, 1, 1, 0, 0. After the second query values will be equal to 1, 11, 1, 0, 0. After the third query values will be equal to 1, 11, 1, 100, 0.
Submitted Solution:
```
import math
n = int(input())
T = []
for _ in range(n-1):
T.append(list(map(int, input().split())))
Q = []
m = int(input())
for _ in range(m):
Q.append(list(map(int, input().split())))
V_ = [[] for _ in range(n+1)]
for v in T:
V_[v[0]].append(v[1])
V_[v[1]].append(v[0])
V = [[] for _ in range(n+1)]
se = [0 for i in range(n+1)]
se[1] = 1
def Tree(v,V_):
print(V)
print(se)
for i in V_[v]:
if se[i] == 0:
V[v].append(i)
se[i] = 1
Tree(i,V_)
Tree(1,V_)
p = [0 for _ in range(n+1)]
for v,d,x in Q:
ini = [v]
nex = [v]
for i in range(d):
nex_ = []
for j in nex:
nex_ += V[j]
ini += nex_
nex = nex_
print(ini)
for i in ini:
p[i] += x
print(" ".join(map(str,p[1:])))
``` | instruction | 0 | 102,918 | 13 | 205,836 |
No | output | 1 | 102,918 | 13 | 205,837 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has a tree consisting of n vertices with root in vertex 1. At first all vertices has 0 written on it.
Let d(i, j) be the distance between vertices i and j, i.e. number of edges in the shortest path from i to j. Also, let's denote k-subtree of vertex x — set of vertices y such that next two conditions are met:
* x is the ancestor of y (each vertex is the ancestor of itself);
* d(x, y) ≤ k.
Vasya needs you to process m queries. The i-th query is a triple v_i, d_i and x_i. For each query Vasya adds value x_i to each vertex from d_i-subtree of v_i.
Report to Vasya all values, written on vertices of the tree after processing all queries.
Input
The first line contains single integer n (1 ≤ n ≤ 3 ⋅ 10^5) — number of vertices in the tree.
Each of next n - 1 lines contains two integers x and y (1 ≤ x, y ≤ n) — edge between vertices x and y. It is guarantied that given graph is a tree.
Next line contains single integer m (1 ≤ m ≤ 3 ⋅ 10^5) — number of queries.
Each of next m lines contains three integers v_i, d_i, x_i (1 ≤ v_i ≤ n, 0 ≤ d_i ≤ 10^9, 1 ≤ x_i ≤ 10^9) — description of the i-th query.
Output
Print n integers. The i-th integers is the value, written in the i-th vertex after processing all queries.
Examples
Input
5
1 2
1 3
2 4
2 5
3
1 1 1
2 0 10
4 10 100
Output
1 11 1 100 0
Input
5
2 3
2 1
5 4
3 4
5
2 0 4
3 10 1
1 2 3
2 3 10
1 1 7
Output
10 24 14 11 11
Note
In the first exapmle initial values in vertices are 0, 0, 0, 0, 0. After the first query values will be equal to 1, 1, 1, 0, 0. After the second query values will be equal to 1, 11, 1, 0, 0. After the third query values will be equal to 1, 11, 1, 100, 0.
Submitted Solution:
```
class HLD:
def __init__(self, g):
self.g = g
self.n = len(g)
self.parent = [-1]*self.n
self.size = [1]*self.n
self.head = [0]*self.n
self.preorder = [0]*self.n
self.k = 0
self.depth = [0]*self.n
for v in range(self.n):
if self.parent[v] == -1:
self.dfs_pre(v)
self.dfs_hld(v)
def dfs_pre(self, v):
g = self.g
stack = [v]
order = [v]
while stack:
v = stack.pop()
for u in g[v]:
if self.parent[v] == u:
continue
self.parent[u] = v
self.depth[u] = self.depth[v]+1
stack.append(u)
order.append(u)
# 隣接リストの左端: heavyな頂点への辺
# 隣接リストの右端: 親への辺
while order:
v = order.pop()
child_v = g[v]
if len(child_v) and child_v[0] == self.parent[v]:
child_v[0], child_v[-1] = child_v[-1], child_v[0]
for i, u in enumerate(child_v):
if u == self.parent[v]:
continue
self.size[v] += self.size[u]
if self.size[u] > self.size[child_v[0]]:
child_v[i], child_v[0] = child_v[0], child_v[i]
def dfs_hld(self, v):
stack = [v]
while stack:
v = stack.pop()
self.preorder[v] = self.k
self.k += 1
top = self.g[v][0]
# 隣接リストを逆順に見ていく(親 > lightな頂点への辺 > heavyな頂点 (top))
# 連結成分が連続するようにならべる
for u in reversed(self.g[v]):
if u == self.parent[v]:
continue
if u == top:
self.head[u] = self.head[v]
else:
self.head[u] = u
stack.append(u)
def for_each(self, u, v):
# [u, v]上の頂点集合の区間を列挙
while True:
if self.preorder[u] > self.preorder[v]:
u, v = v, u
l = max(self.preorder[self.head[v]], self.preorder[u])
r = self.preorder[v]
yield l, r # [l, r]
if self.head[u] != self.head[v]:
v = self.parent[self.head[v]]
else:
return
def for_each_edge(self, u, v):
# [u, v]上の辺集合の区間列挙
# 辺の情報は子の頂点に
while True:
if self.preorder[u] > self.preorder[v]:
u, v = v, u
if self.head[u] != self.head[v]:
yield self.preorder[self.head[v]], self.preorder[v]
v = self.parent[self.head[v]]
else:
if u != v:
yield self.preorder[u]+1, self.preorder[v]
break
def subtree(self, v):
# 頂点vの部分木の頂点集合の区間 [l, r)
l = self.preorder[v]
r = self.preorder[v]+self.size[v]
return l, r
def lca(self, u, v):
# 頂点u, vのLCA
while True:
if self.preorder[u] > self.preorder[v]:
u, v = v, u
if self.head[u] == self.head[v]:
return u
v = self.parent[self.head[v]]
class BIT:
def __init__(self, n):
self.n = n
self.bit = [0]*(self.n+1) # 1-indexed
def init(self, init_val):
for i, v in enumerate(init_val):
self.add(i, v)
def add(self, i, x):
# i: 0-indexed
i += 1 # to 1-indexed
while i <= self.n:
self.bit[i] += x
i += (i & -i)
def sum(self, i, j):
# return sum of [i, j)
# i, j: 0-indexed
return self._sum(j) - self._sum(i)
def _sum(self, i):
# return sum of [0, i)
# i: 0-indexed
res = 0
while i > 0:
res += self.bit[i]
i -= i & (-i)
return res
class RangeAddBIT:
def __init__(self, n):
self.n = n
self.bit1 = BIT(n)
self.bit2 = BIT(n)
def init(self, init_val):
self.bit2.init(init_val)
def add(self, l, r, x):
# add x to [l, r)
# l, r: 0-indexed
self.bit1.add(l, x)
self.bit1.add(r, -x)
self.bit2.add(l, -x*l)
self.bit2.add(r, x*r)
def sum(self, l, r):
# return sum of [l, r)
# l, r: 0-indexed
return self._sum(r) - self._sum(l)
def _sum(self, i):
# return sum of [0, i)
# i: 0-indexed
return self.bit1._sum(i)*i + self.bit2._sum(i)
def __str__(self): # for debug
arr = [self.sum(i,i+1) for i in range(self.n)]
return str(arr)
import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
import math
def main():
n = int(input())
g = [[] for i in range(n)]
for i in range(n-1):
x, y = map(int, input().split())
x, y = x-1, y-1
g[x].append(y)
g[y].append(x)
hld = HLD(g)
bit = RangeAddBIT(n+1)
s = []
s.append(0)
parent = [-1]*n
order = []
dist = [0]*n
while s:
v = s.pop()
order.append(v)
for u in g[v]:
if u != parent[v]:
s.append(u)
parent[u] = v
dist[u] = dist[v]+1
order.reverse()
C = [1]*n
D = [0 for i in range(n)]
for v in order:
D[v] = max(dist[v], D[v])
if parent[v] != -1:
C[parent[v]] += C[v]
D[parent[v]] = max(D[parent[v]], D[v])
for v in range(n):
D[v] -= dist[v]
#print(D)
from collections import defaultdict, deque
X = [defaultdict(lambda: 0) for i in range(n)]
m = int(input())
ans = [0]*n
for i in range(m):
v, d, x = map(int, input().split())
v -= 1
if d >= D[v]:
l, r = hld.subtree(v)
bit.add(l, r, x)
else:
X[v][d] += x
#print(bit)
#print(ans)
#print(X)
order.reverse()
for v in order:
for d, x in X[v].items():
ans[v] += x
if d != 0:
for u in g[v]:
if u != parent[v]:
X[u][d-1] += x
for i in range(n):
ans[i] += bit.sum(i, i+1)
print(*ans)
if __name__ == '__main__':
main()
``` | instruction | 0 | 102,919 | 13 | 205,838 |
No | output | 1 | 102,919 | 13 | 205,839 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As you must know, the maximum clique problem in an arbitrary graph is NP-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively.
Just in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are connected by an edge. In particular, an empty set of vertexes and a set consisting of a single vertex, are cliques.
Let's define a divisibility graph for a set of positive integers A = {a1, a2, ..., an} as follows. The vertices of the given graph are numbers from set A, and two numbers ai and aj (i ≠ j) are connected by an edge if and only if either ai is divisible by aj, or aj is divisible by ai.
You are given a set of non-negative integers A. Determine the size of a maximum clique in a divisibility graph for set A.
Input
The first line contains integer n (1 ≤ n ≤ 106), that sets the size of set A.
The second line contains n distinct positive integers a1, a2, ..., an (1 ≤ ai ≤ 106) — elements of subset A. The numbers in the line follow in the ascending order.
Output
Print a single number — the maximum size of a clique in a divisibility graph for set A.
Examples
Input
8
3 4 6 8 10 18 21 24
Output
3
Note
In the first sample test a clique of size 3 is, for example, a subset of vertexes {3, 6, 18}. A clique of a larger size doesn't exist in this graph. | instruction | 0 | 103,279 | 13 | 206,558 |
Tags: dp, math, number theory
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
MAX = 10**6+1
L = [0]*MAX
for x in a:
L[x] = 1
for i in range(n):
if L[a[i]]:
for x in range(a[i]*2, MAX, a[i]):
if L[x]:
L[x] = max(L[x], L[a[i]]+1)
print(max(L))
``` | output | 1 | 103,279 | 13 | 206,559 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As you must know, the maximum clique problem in an arbitrary graph is NP-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively.
Just in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are connected by an edge. In particular, an empty set of vertexes and a set consisting of a single vertex, are cliques.
Let's define a divisibility graph for a set of positive integers A = {a1, a2, ..., an} as follows. The vertices of the given graph are numbers from set A, and two numbers ai and aj (i ≠ j) are connected by an edge if and only if either ai is divisible by aj, or aj is divisible by ai.
You are given a set of non-negative integers A. Determine the size of a maximum clique in a divisibility graph for set A.
Input
The first line contains integer n (1 ≤ n ≤ 106), that sets the size of set A.
The second line contains n distinct positive integers a1, a2, ..., an (1 ≤ ai ≤ 106) — elements of subset A. The numbers in the line follow in the ascending order.
Output
Print a single number — the maximum size of a clique in a divisibility graph for set A.
Examples
Input
8
3 4 6 8 10 18 21 24
Output
3
Note
In the first sample test a clique of size 3 is, for example, a subset of vertexes {3, 6, 18}. A clique of a larger size doesn't exist in this graph. | instruction | 0 | 103,280 | 13 | 206,560 |
Tags: dp, math, number theory
Correct Solution:
```
R = lambda: map(int, input().split())
n = int(input())
dp = [0] * (10**6 + 1)
for x in R():
dp[x] = 1
for i in range(10**6, -1, -1):
if dp[i]:
for x in range(i + i, 10**6 + 1, i):
if dp[x]:
dp[i] = max(dp[i], dp[x] + 1)
print(max(dp))
``` | output | 1 | 103,280 | 13 | 206,561 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As you must know, the maximum clique problem in an arbitrary graph is NP-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively.
Just in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are connected by an edge. In particular, an empty set of vertexes and a set consisting of a single vertex, are cliques.
Let's define a divisibility graph for a set of positive integers A = {a1, a2, ..., an} as follows. The vertices of the given graph are numbers from set A, and two numbers ai and aj (i ≠ j) are connected by an edge if and only if either ai is divisible by aj, or aj is divisible by ai.
You are given a set of non-negative integers A. Determine the size of a maximum clique in a divisibility graph for set A.
Input
The first line contains integer n (1 ≤ n ≤ 106), that sets the size of set A.
The second line contains n distinct positive integers a1, a2, ..., an (1 ≤ ai ≤ 106) — elements of subset A. The numbers in the line follow in the ascending order.
Output
Print a single number — the maximum size of a clique in a divisibility graph for set A.
Examples
Input
8
3 4 6 8 10 18 21 24
Output
3
Note
In the first sample test a clique of size 3 is, for example, a subset of vertexes {3, 6, 18}. A clique of a larger size doesn't exist in this graph. | instruction | 0 | 103,281 | 13 | 206,562 |
Tags: dp, math, number theory
Correct Solution:
```
n = int(input())
base = list(map(int, input().split()))
stock = [0 for k in range(int(1e6+1))]
for zbi in base :
stock[zbi] = 1
t = base[-1]
for k in range(2,n+1) :
num = base[n-k]
for i in range(2, (t//num)+1) :
if stock[i*num] >= 1 :
stock[num] = max(stock[num], 1 + stock[i*num] )
print(max(stock))
``` | output | 1 | 103,281 | 13 | 206,563 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As you must know, the maximum clique problem in an arbitrary graph is NP-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively.
Just in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are connected by an edge. In particular, an empty set of vertexes and a set consisting of a single vertex, are cliques.
Let's define a divisibility graph for a set of positive integers A = {a1, a2, ..., an} as follows. The vertices of the given graph are numbers from set A, and two numbers ai and aj (i ≠ j) are connected by an edge if and only if either ai is divisible by aj, or aj is divisible by ai.
You are given a set of non-negative integers A. Determine the size of a maximum clique in a divisibility graph for set A.
Input
The first line contains integer n (1 ≤ n ≤ 106), that sets the size of set A.
The second line contains n distinct positive integers a1, a2, ..., an (1 ≤ ai ≤ 106) — elements of subset A. The numbers in the line follow in the ascending order.
Output
Print a single number — the maximum size of a clique in a divisibility graph for set A.
Examples
Input
8
3 4 6 8 10 18 21 24
Output
3
Note
In the first sample test a clique of size 3 is, for example, a subset of vertexes {3, 6, 18}. A clique of a larger size doesn't exist in this graph. | instruction | 0 | 103,282 | 13 | 206,564 |
Tags: dp, math, number theory
Correct Solution:
```
import sys
input=sys.stdin.readline
n=int(input())
a=list(map(int,input().split()))
a.sort()
dp=[0]*(10**6+1)
for i in range(n):
dp[a[i]]=1
for i in range(n):
if dp[a[i]]>0:
for j in range(a[i]*2,10**6+1,a[i]):
if dp[j]>0:
dp[j]=max(dp[j],dp[a[i]]+1)
print(max(dp))
``` | output | 1 | 103,282 | 13 | 206,565 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As you must know, the maximum clique problem in an arbitrary graph is NP-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively.
Just in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are connected by an edge. In particular, an empty set of vertexes and a set consisting of a single vertex, are cliques.
Let's define a divisibility graph for a set of positive integers A = {a1, a2, ..., an} as follows. The vertices of the given graph are numbers from set A, and two numbers ai and aj (i ≠ j) are connected by an edge if and only if either ai is divisible by aj, or aj is divisible by ai.
You are given a set of non-negative integers A. Determine the size of a maximum clique in a divisibility graph for set A.
Input
The first line contains integer n (1 ≤ n ≤ 106), that sets the size of set A.
The second line contains n distinct positive integers a1, a2, ..., an (1 ≤ ai ≤ 106) — elements of subset A. The numbers in the line follow in the ascending order.
Output
Print a single number — the maximum size of a clique in a divisibility graph for set A.
Examples
Input
8
3 4 6 8 10 18 21 24
Output
3
Note
In the first sample test a clique of size 3 is, for example, a subset of vertexes {3, 6, 18}. A clique of a larger size doesn't exist in this graph. | instruction | 0 | 103,283 | 13 | 206,566 |
Tags: dp, math, number theory
Correct Solution:
```
#!/usr/bin/env python3
import os
import sys
from io import BytesIO, IOBase
class FastO:
def __init__(self, fd=1):
stream = BytesIO()
self.flush = lambda: os.write(fd, stream.getvalue()) and not stream.truncate(0) and stream.seek(0)
self.write = lambda b: stream.write(b.encode())
class ostream:
def __lshift__(self, a):
sys.stdout.write(str(a))
return self
sys.stdout, cout = FastO(), ostream()
numbers, num, sign = [], 0, True
for char in os.read(0, os.fstat(0).st_size):
if char >= 48:
num = num * 10 + char - 48
elif char == 45:
sign = False
elif char != 13:
numbers.append(num if sign else -num)
num, sign = 0, True
if char >= 48:
numbers.append(num if sign else -num)
getnum = iter(numbers).__next__
n = getnum()
dp = [0] * (10**6 + 1)
for _ in range(n):
dp[getnum()] = 1
for i in reversed(range(10**6 + 1)):
dp[i] = max((dp[x] + 1 for x in range(2 * i, 10**6 + 1, i) if dp[x]), default=1) if dp[i] else 0
cout << max(dp)
``` | output | 1 | 103,283 | 13 | 206,567 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As you must know, the maximum clique problem in an arbitrary graph is NP-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively.
Just in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are connected by an edge. In particular, an empty set of vertexes and a set consisting of a single vertex, are cliques.
Let's define a divisibility graph for a set of positive integers A = {a1, a2, ..., an} as follows. The vertices of the given graph are numbers from set A, and two numbers ai and aj (i ≠ j) are connected by an edge if and only if either ai is divisible by aj, or aj is divisible by ai.
You are given a set of non-negative integers A. Determine the size of a maximum clique in a divisibility graph for set A.
Input
The first line contains integer n (1 ≤ n ≤ 106), that sets the size of set A.
The second line contains n distinct positive integers a1, a2, ..., an (1 ≤ ai ≤ 106) — elements of subset A. The numbers in the line follow in the ascending order.
Output
Print a single number — the maximum size of a clique in a divisibility graph for set A.
Examples
Input
8
3 4 6 8 10 18 21 24
Output
3
Note
In the first sample test a clique of size 3 is, for example, a subset of vertexes {3, 6, 18}. A clique of a larger size doesn't exist in this graph. | instruction | 0 | 103,284 | 13 | 206,568 |
Tags: dp, math, number theory
Correct Solution:
```
def abc(n, a):
MAX = 10 ** 6 + 1
L = [0] * MAX
for v in a:
L[v] = 1
for i in range(n):
if L[a[i]]:
for x in range(a[i] * 2, MAX, a[i]):
if L[x]:
L[x] = max(L[x], L[a[i]] + 1)
return max(L)
if __name__ == "__main__":
n = int(input())
a = list(map(int, input().split()))
result = abc(n, a)
print(result)
``` | output | 1 | 103,284 | 13 | 206,569 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As you must know, the maximum clique problem in an arbitrary graph is NP-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively.
Just in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are connected by an edge. In particular, an empty set of vertexes and a set consisting of a single vertex, are cliques.
Let's define a divisibility graph for a set of positive integers A = {a1, a2, ..., an} as follows. The vertices of the given graph are numbers from set A, and two numbers ai and aj (i ≠ j) are connected by an edge if and only if either ai is divisible by aj, or aj is divisible by ai.
You are given a set of non-negative integers A. Determine the size of a maximum clique in a divisibility graph for set A.
Input
The first line contains integer n (1 ≤ n ≤ 106), that sets the size of set A.
The second line contains n distinct positive integers a1, a2, ..., an (1 ≤ ai ≤ 106) — elements of subset A. The numbers in the line follow in the ascending order.
Output
Print a single number — the maximum size of a clique in a divisibility graph for set A.
Examples
Input
8
3 4 6 8 10 18 21 24
Output
3
Note
In the first sample test a clique of size 3 is, for example, a subset of vertexes {3, 6, 18}. A clique of a larger size doesn't exist in this graph. | instruction | 0 | 103,285 | 13 | 206,570 |
Tags: dp, math, number theory
Correct Solution:
```
def clique_in_the_divisibility_graph(n, a):
MAX = 10 ** 6 + 1
L = [0] * MAX
for v in a:
L[v] = 1
for i in range(n):
if L[a[i]]:
for x in range(a[i] * 2, MAX, a[i]):
if L[x]:
L[x] = max(L[x], L[a[i]] + 1)
return max(L)
if __name__ == "__main__":
n = int(input())
a = list(map(int, input().split()))
result = clique_in_the_divisibility_graph(n, a)
print(result)
``` | output | 1 | 103,285 | 13 | 206,571 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As you must know, the maximum clique problem in an arbitrary graph is NP-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively.
Just in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are connected by an edge. In particular, an empty set of vertexes and a set consisting of a single vertex, are cliques.
Let's define a divisibility graph for a set of positive integers A = {a1, a2, ..., an} as follows. The vertices of the given graph are numbers from set A, and two numbers ai and aj (i ≠ j) are connected by an edge if and only if either ai is divisible by aj, or aj is divisible by ai.
You are given a set of non-negative integers A. Determine the size of a maximum clique in a divisibility graph for set A.
Input
The first line contains integer n (1 ≤ n ≤ 106), that sets the size of set A.
The second line contains n distinct positive integers a1, a2, ..., an (1 ≤ ai ≤ 106) — elements of subset A. The numbers in the line follow in the ascending order.
Output
Print a single number — the maximum size of a clique in a divisibility graph for set A.
Examples
Input
8
3 4 6 8 10 18 21 24
Output
3
Note
In the first sample test a clique of size 3 is, for example, a subset of vertexes {3, 6, 18}. A clique of a larger size doesn't exist in this graph. | instruction | 0 | 103,286 | 13 | 206,572 |
Tags: dp, math, number theory
Correct Solution:
```
for _ in range(1):
n=int(input())
l=list(map(int,input().split()))
dp=[0]*(10**6+1)
for i in l:
dp[i]=1
for i in l:
if dp[i]:
for x in range(i*2,10**6+1,i):
if dp[x]:
dp[x]=max(dp[x],dp[i]+1)
print(max(dp))
``` | output | 1 | 103,286 | 13 | 206,573 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As you must know, the maximum clique problem in an arbitrary graph is NP-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively.
Just in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are connected by an edge. In particular, an empty set of vertexes and a set consisting of a single vertex, are cliques.
Let's define a divisibility graph for a set of positive integers A = {a1, a2, ..., an} as follows. The vertices of the given graph are numbers from set A, and two numbers ai and aj (i ≠ j) are connected by an edge if and only if either ai is divisible by aj, or aj is divisible by ai.
You are given a set of non-negative integers A. Determine the size of a maximum clique in a divisibility graph for set A.
Input
The first line contains integer n (1 ≤ n ≤ 106), that sets the size of set A.
The second line contains n distinct positive integers a1, a2, ..., an (1 ≤ ai ≤ 106) — elements of subset A. The numbers in the line follow in the ascending order.
Output
Print a single number — the maximum size of a clique in a divisibility graph for set A.
Examples
Input
8
3 4 6 8 10 18 21 24
Output
3
Note
In the first sample test a clique of size 3 is, for example, a subset of vertexes {3, 6, 18}. A clique of a larger size doesn't exist in this graph.
Submitted Solution:
```
N = int(input())
List = [int(x) for x in input().split()]
MAX = 1000001
dp = [0] * MAX
for i in List:
dp[i] += 1
for i in range(N):
if(dp[List[i]]):
for j in range(List[i]*2,MAX, List[i]):
if(dp[j]):
dp[j] = max(dp[j], dp[List[i]]+1)
print(max(dp))
``` | instruction | 0 | 103,287 | 13 | 206,574 |
Yes | output | 1 | 103,287 | 13 | 206,575 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As you must know, the maximum clique problem in an arbitrary graph is NP-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively.
Just in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are connected by an edge. In particular, an empty set of vertexes and a set consisting of a single vertex, are cliques.
Let's define a divisibility graph for a set of positive integers A = {a1, a2, ..., an} as follows. The vertices of the given graph are numbers from set A, and two numbers ai and aj (i ≠ j) are connected by an edge if and only if either ai is divisible by aj, or aj is divisible by ai.
You are given a set of non-negative integers A. Determine the size of a maximum clique in a divisibility graph for set A.
Input
The first line contains integer n (1 ≤ n ≤ 106), that sets the size of set A.
The second line contains n distinct positive integers a1, a2, ..., an (1 ≤ ai ≤ 106) — elements of subset A. The numbers in the line follow in the ascending order.
Output
Print a single number — the maximum size of a clique in a divisibility graph for set A.
Examples
Input
8
3 4 6 8 10 18 21 24
Output
3
Note
In the first sample test a clique of size 3 is, for example, a subset of vertexes {3, 6, 18}. A clique of a larger size doesn't exist in this graph.
Submitted Solution:
```
n=int(input())
a=list(map(int,input().split()))
l=[0]*(10**6+1)
for aa in a:
l[aa]=1
for i in range(n):
if l[a[i]]:
for x in range(a[i]*2,10**6+1,a[i]):
if l[x]:
l[x]=max(l[x],l[a[i]]+1)
print(max(l))
``` | instruction | 0 | 103,288 | 13 | 206,576 |
Yes | output | 1 | 103,288 | 13 | 206,577 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As you must know, the maximum clique problem in an arbitrary graph is NP-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively.
Just in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are connected by an edge. In particular, an empty set of vertexes and a set consisting of a single vertex, are cliques.
Let's define a divisibility graph for a set of positive integers A = {a1, a2, ..., an} as follows. The vertices of the given graph are numbers from set A, and two numbers ai and aj (i ≠ j) are connected by an edge if and only if either ai is divisible by aj, or aj is divisible by ai.
You are given a set of non-negative integers A. Determine the size of a maximum clique in a divisibility graph for set A.
Input
The first line contains integer n (1 ≤ n ≤ 106), that sets the size of set A.
The second line contains n distinct positive integers a1, a2, ..., an (1 ≤ ai ≤ 106) — elements of subset A. The numbers in the line follow in the ascending order.
Output
Print a single number — the maximum size of a clique in a divisibility graph for set A.
Examples
Input
8
3 4 6 8 10 18 21 24
Output
3
Note
In the first sample test a clique of size 3 is, for example, a subset of vertexes {3, 6, 18}. A clique of a larger size doesn't exist in this graph.
Submitted Solution:
```
n = int(input())
a = [int(v) for v in input().split()]
MAX = 10**6 + 1
L = [0] * MAX
for v in a:
L[v] = 1
for i in range(n):
if L[a[i]]:
for x in range(a[i] * 2, MAX, a[i]):
if L[x]:
L[x] = max(L[x], L[a[i]] + 1)
print(max(L))
``` | instruction | 0 | 103,289 | 13 | 206,578 |
Yes | output | 1 | 103,289 | 13 | 206,579 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As you must know, the maximum clique problem in an arbitrary graph is NP-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively.
Just in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are connected by an edge. In particular, an empty set of vertexes and a set consisting of a single vertex, are cliques.
Let's define a divisibility graph for a set of positive integers A = {a1, a2, ..., an} as follows. The vertices of the given graph are numbers from set A, and two numbers ai and aj (i ≠ j) are connected by an edge if and only if either ai is divisible by aj, or aj is divisible by ai.
You are given a set of non-negative integers A. Determine the size of a maximum clique in a divisibility graph for set A.
Input
The first line contains integer n (1 ≤ n ≤ 106), that sets the size of set A.
The second line contains n distinct positive integers a1, a2, ..., an (1 ≤ ai ≤ 106) — elements of subset A. The numbers in the line follow in the ascending order.
Output
Print a single number — the maximum size of a clique in a divisibility graph for set A.
Examples
Input
8
3 4 6 8 10 18 21 24
Output
3
Note
In the first sample test a clique of size 3 is, for example, a subset of vertexes {3, 6, 18}. A clique of a larger size doesn't exist in this graph.
Submitted Solution:
```
def solve(A):
k = A[-1]
B = [0] * (k + 1)
for a in A:
B[a] += 1
C = [0] * (k + 1)
for i in range(2, n+1):
if B[i] == 0: continue
C[i] += B[i]
for j in range(2*i, k+1, i):
C[j] = max(C[j], C[i])
return max(C) + B[1]
n = int(input())
A = [int(x) for x in input().split()]
print(solve(A))
``` | instruction | 0 | 103,290 | 13 | 206,580 |
No | output | 1 | 103,290 | 13 | 206,581 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As you must know, the maximum clique problem in an arbitrary graph is NP-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively.
Just in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are connected by an edge. In particular, an empty set of vertexes and a set consisting of a single vertex, are cliques.
Let's define a divisibility graph for a set of positive integers A = {a1, a2, ..., an} as follows. The vertices of the given graph are numbers from set A, and two numbers ai and aj (i ≠ j) are connected by an edge if and only if either ai is divisible by aj, or aj is divisible by ai.
You are given a set of non-negative integers A. Determine the size of a maximum clique in a divisibility graph for set A.
Input
The first line contains integer n (1 ≤ n ≤ 106), that sets the size of set A.
The second line contains n distinct positive integers a1, a2, ..., an (1 ≤ ai ≤ 106) — elements of subset A. The numbers in the line follow in the ascending order.
Output
Print a single number — the maximum size of a clique in a divisibility graph for set A.
Examples
Input
8
3 4 6 8 10 18 21 24
Output
3
Note
In the first sample test a clique of size 3 is, for example, a subset of vertexes {3, 6, 18}. A clique of a larger size doesn't exist in this graph.
Submitted Solution:
```
a=[0]*1000001
n=int(input())
l=list(map(int,input().split()))
ma=0
for x in l:
for t in range(x,a[-1]+1,x): a[t]=max(a[t],a[x]+1)
ma=max(ma,a[x])
print(ma)
``` | instruction | 0 | 103,291 | 13 | 206,582 |
No | output | 1 | 103,291 | 13 | 206,583 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As you must know, the maximum clique problem in an arbitrary graph is NP-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively.
Just in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are connected by an edge. In particular, an empty set of vertexes and a set consisting of a single vertex, are cliques.
Let's define a divisibility graph for a set of positive integers A = {a1, a2, ..., an} as follows. The vertices of the given graph are numbers from set A, and two numbers ai and aj (i ≠ j) are connected by an edge if and only if either ai is divisible by aj, or aj is divisible by ai.
You are given a set of non-negative integers A. Determine the size of a maximum clique in a divisibility graph for set A.
Input
The first line contains integer n (1 ≤ n ≤ 106), that sets the size of set A.
The second line contains n distinct positive integers a1, a2, ..., an (1 ≤ ai ≤ 106) — elements of subset A. The numbers in the line follow in the ascending order.
Output
Print a single number — the maximum size of a clique in a divisibility graph for set A.
Examples
Input
8
3 4 6 8 10 18 21 24
Output
3
Note
In the first sample test a clique of size 3 is, for example, a subset of vertexes {3, 6, 18}. A clique of a larger size doesn't exist in this graph.
Submitted Solution:
```
a=[0]*1000001
n=int(input())
l=list(map(int,input().split()))
for x in l:
t=x
while t<1000001: a[t]=max(a[t],a[x]+1);t+=x
print(max(a))
``` | instruction | 0 | 103,292 | 13 | 206,584 |
No | output | 1 | 103,292 | 13 | 206,585 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As you must know, the maximum clique problem in an arbitrary graph is NP-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively.
Just in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are connected by an edge. In particular, an empty set of vertexes and a set consisting of a single vertex, are cliques.
Let's define a divisibility graph for a set of positive integers A = {a1, a2, ..., an} as follows. The vertices of the given graph are numbers from set A, and two numbers ai and aj (i ≠ j) are connected by an edge if and only if either ai is divisible by aj, or aj is divisible by ai.
You are given a set of non-negative integers A. Determine the size of a maximum clique in a divisibility graph for set A.
Input
The first line contains integer n (1 ≤ n ≤ 106), that sets the size of set A.
The second line contains n distinct positive integers a1, a2, ..., an (1 ≤ ai ≤ 106) — elements of subset A. The numbers in the line follow in the ascending order.
Output
Print a single number — the maximum size of a clique in a divisibility graph for set A.
Examples
Input
8
3 4 6 8 10 18 21 24
Output
3
Note
In the first sample test a clique of size 3 is, for example, a subset of vertexes {3, 6, 18}. A clique of a larger size doesn't exist in this graph.
Submitted Solution:
```
R = lambda: map(int, input().split())
R()
dp = [0] * (10 ** 6 + 1)
vd = [0] * (10 ** 6 + 1)
for x in R():
dp[x] += 1
for i in range(1, 10**6 + 1):
if not vd[i] and dp[i]:
acc = dp[i]
for j in range(i * 2, 10 ** 6 + 1, i):
if dp[j]:
acc, dp[j] = dp[j], dp[j] + acc
vd[j] = 1
print(max(dp))
``` | instruction | 0 | 103,293 | 13 | 206,586 |
No | output | 1 | 103,293 | 13 | 206,587 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image> | instruction | 0 | 103,407 | 13 | 206,814 |
Tags: constructive algorithms, trees
Correct Solution:
```
n = int(input())
if n <= 5:
print(-1)
else:
print(1, 2)
print(2, 3)
print(2, 4)
for i in range(5, n+1):
print(4, i)
for i in range(2, n+1):
print(1, i)
``` | output | 1 | 103,407 | 13 | 206,815 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image> | instruction | 0 | 103,408 | 13 | 206,816 |
Tags: constructive algorithms, trees
Correct Solution:
```
import sys
input = sys.stdin.readline
n = int(input())
if n < 6:
print(-1)
else:
l = []
o = []
x = (3+n)//2
for i in range(3,x+1):
l.append((1,i))
for i in range(x+1,n+1):
o.append((2,i))
sys.stdout.write("1"+" "+"2"+"\n")
for x in l:
sys.stdout.write(str(x[0]) + " " + str(x[1]) + "\n")
for x in o:
sys.stdout.write(str(x[0]) + " " + str(x[1]) + "\n")
sys.stdout.write("1"+" "+"2"+"\n")
p = 2
for i in range(3,n+1):
sys.stdout.write(str(p) + " " + str(i) + "\n")
p = i
``` | output | 1 | 103,408 | 13 | 206,817 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image> | instruction | 0 | 103,409 | 13 | 206,818 |
Tags: constructive algorithms, trees
Correct Solution:
```
n=int(input())
#section 1
if n<6:
print(-1)
else:
print(1,2)
print(1,3)
print(1,4)
print(2,5)
print(2,6)
for i in range(7,n+1):
print(1,i)
#section 2
for i in range(2,n+1):
print(1,i)
"""
1 2
1 3
1 4
2 5
2 6
1 7
1 8
1 2
2 3
3 4
4 5
1 6
1 7
1 8
"""
``` | output | 1 | 103,409 | 13 | 206,819 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image> | instruction | 0 | 103,410 | 13 | 206,820 |
Tags: constructive algorithms, trees
Correct Solution:
```
k = int(input())
if(k<=5):
print(-1)
for i in range(2, k+1):
print(1,i)
else:
for i in range(2, k-1):
print(1, i)
print(k-2, k-1)
print(k-2, k)
for i in range(2, k+1):
print(1,i)
``` | output | 1 | 103,410 | 13 | 206,821 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image> | instruction | 0 | 103,411 | 13 | 206,822 |
Tags: constructive algorithms, trees
Correct Solution:
```
import sys
import math
from collections import Counter
from operator import itemgetter
import queue
def IO():
sys.stdin=open("pyinput.txt", 'r')
sys.stdout=open("pyoutput.txt", 'w')
def GCD(a, b):
if(b==0): return a
else: return GCD(b, a%b)
def LCM(a, b): return a*(b//GCD(a, b))
def scan(TYPE_1, TYPE_2=0):
if(TYPE_1==int):
return map(int, sys.stdin.readline().strip().split())
elif(TYPE_1==float):
return map(float, sys.stdin.readline().strip().split())
elif(TYPE_1==list and TYPE_2==float):
return list(map(float, sys.stdin.readline().strip().split()))
elif(TYPE_1==list and TYPE_2==int):
return list(map(int, sys.stdin.readline().strip().split()))
elif(TYPE_1==str):
return sys.stdin.readline().strip()
else: print("ERROR!!!!")
def main():
n=int(input())
if(n<6):
print(-1)
else:
print(1, 2)
print(1, 3)
print(1, 4)
for i in range(5, n+1):
print(4, i)
for i in range(2, n+1):
print(1, i)
# IO()
main()
``` | output | 1 | 103,411 | 13 | 206,823 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image> | instruction | 0 | 103,412 | 13 | 206,824 |
Tags: constructive algorithms, trees
Correct Solution:
```
n = int(input())
for i in range(2,n+1):
if n <= 5:
print("-1")
break
if i < 5:
print("1",i)
else:
print("2",i)
for i in range(1,n):
print(i,i+1)
``` | output | 1 | 103,412 | 13 | 206,825 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image> | instruction | 0 | 103,413 | 13 | 206,826 |
Tags: constructive algorithms, trees
Correct Solution:
```
n = int(input())
if n < 6:
print(-1)
else:
baseans = [(1,2), (1,3), (1,4), (4,5), (4,6)]
for p in baseans:
print('%d %d' % (p[0], p[1]))
for i in range(7, n+1):
print('%d %d' % (4, i))
for i in range(2, n+1):
print('%d %d' % (1, i))
``` | output | 1 | 103,413 | 13 | 206,827 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image> | instruction | 0 | 103,414 | 13 | 206,828 |
Tags: constructive algorithms, trees
Correct Solution:
```
n = int(input());
if ( n <= 5):
print (-1);
else:
print (str(1)+ " "+str(2));
print (str(2)+" "+str(3));
print (str(2)+" "+str(4));
print (str(4)+" "+str(5));
print (str(4) + " "+str(6));
k = 5;
while ( k < n - 1 ):
print ( str(4) + " " + str(k+2) );
k+=1;
h = 0;
while ( h < n - 1 ):
print ( str(1) + " " + str(h + 2) );
h+=1;
``` | output | 1 | 103,414 | 13 | 206,829 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image>
Submitted Solution:
```
n = int(input())
if n <= 5:
print(-1)
else:
print('1 2', '1 3', '1 4', '2 5', sep = '\n')
for i in range(6, n + 1):
print('2 {0:d}'.format(i))
for i in range(2, n + 1):
print('1 {0:d}'.format(i))
``` | instruction | 0 | 103,415 | 13 | 206,830 |
Yes | output | 1 | 103,415 | 13 | 206,831 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image>
Submitted Solution:
```
n = int(input())
if n < 6:
print(-1)
else:
print(1, 2)
if (n) % 2 == 0:
for i in range(3, (n) // 2 + 2):
print(1, i)
for i in range((n) // 2 + 2, n + 1):
print(2, i)
else:
for i in range(3, (n) // 2 + 2):
print(1, i)
for i in range((n) // 2 + 2, n + 1):
print(2, i)
for i in range(2, n + 1):
print(i, i - 1)
``` | instruction | 0 | 103,416 | 13 | 206,832 |
Yes | output | 1 | 103,416 | 13 | 206,833 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image>
Submitted Solution:
```
n = int(input())
def print_wrong(l):
if l<6:
print('-1')
else:
for i in range(0, l-1):
if i % 2 == 0:
first = '1 '
else:
first = '2 '
print(first + str(i + 2))
def print_right(l):
for i in range(0, l - 1):
print('1 %s' % str(i + 2))
print_wrong(n)
print_right(n)
``` | instruction | 0 | 103,417 | 13 | 206,834 |
Yes | output | 1 | 103,417 | 13 | 206,835 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image>
Submitted Solution:
```
import math
import heapq
from queue import Queue
def main(n):
if n<6:
print(-1)
else:
print(1,2)
print(2,3)
print(2,4)
print(4,5)
print(4,6)
a=7
while a<=n:
print(2,a)
a+=1
a=2
while a<=n:
print(1,a)
a+=1
return
n=int(input())
(main(n))
``` | instruction | 0 | 103,418 | 13 | 206,836 |
Yes | output | 1 | 103,418 | 13 | 206,837 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image>
Submitted Solution:
```
n = int(input())
if n in range(2, 4+1):
print("-1")
for i in range(2, n+1):
print("1", i)
else:
# Wrong Answer
print("1 2")
p = n // 2 - 2
for i in range(p+1):
print("2", i+3)
for i in range(n-p-3):
print("1", i+p+4)
# Right Answer
for i in range(n - 1):
print("1", i + 2)
``` | instruction | 0 | 103,419 | 13 | 206,838 |
No | output | 1 | 103,419 | 13 | 206,839 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image>
Submitted Solution:
```
n = int(input())
if n <= 4:
print(-1)
else:
n3 = (n - 1) // 2
n2 = n - n3 - 1
for i in range(n2):
print(1, i+2)
for i in range(n3):
print(2, i+n2+2)
for i in range(n-1):
print(1, i+2)
``` | instruction | 0 | 103,420 | 13 | 206,840 |
No | output | 1 | 103,420 | 13 | 206,841 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.