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.
Vasya has got an undirected graph consisting of n vertices and m edges. This graph doesn't contain any self-loops or multiple edges. Self-loop is an edge connecting a vertex to itself. Multiple edges are a pair of edges such that they connect the same pair of vertices. Since the graph is undirected, the pair of edges (1, 2) and (2, 1) is considered to be multiple edges. Isolated vertex of the graph is a vertex such that there is no edge connecting this vertex to any other vertex.
Vasya wants to know the minimum and maximum possible number of isolated vertices in an undirected graph consisting of n vertices and m edges.
Input
The only line contains two integers n and m~(1 β€ n β€ 10^5, 0 β€ m β€ (n (n - 1))/(2)).
It is guaranteed that there exists a graph without any self-loops or multiple edges with such number of vertices and edges.
Output
In the only line print two numbers min and max β the minimum and maximum number of isolated vertices, respectively.
Examples
Input
4 2
Output
0 1
Input
3 1
Output
1 1
Note
In the first example it is possible to construct a graph with 0 isolated vertices: for example, it should contain edges (1, 2) and (3, 4). To get one isolated vertex, we may construct a graph with edges (1, 2) and (1, 3).
In the second example the graph will always contain exactly one isolated vertex. | instruction | 0 | 60,658 | 13 | 121,316 |
Tags: constructive algorithms, graphs
Correct Solution:
```
ls = input().split()
n = int(ls[0])
m = int(ls[1])
minv = n - (2*m)
minv = max(0, minv)
maxv = 0
for i in range(n):
if i*(i-1)/2 >= m:
maxv = n-i
break
print(minv, maxv)
``` | output | 1 | 60,658 | 13 | 121,317 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has got an undirected graph consisting of n vertices and m edges. This graph doesn't contain any self-loops or multiple edges. Self-loop is an edge connecting a vertex to itself. Multiple edges are a pair of edges such that they connect the same pair of vertices. Since the graph is undirected, the pair of edges (1, 2) and (2, 1) is considered to be multiple edges. Isolated vertex of the graph is a vertex such that there is no edge connecting this vertex to any other vertex.
Vasya wants to know the minimum and maximum possible number of isolated vertices in an undirected graph consisting of n vertices and m edges.
Input
The only line contains two integers n and m~(1 β€ n β€ 10^5, 0 β€ m β€ (n (n - 1))/(2)).
It is guaranteed that there exists a graph without any self-loops or multiple edges with such number of vertices and edges.
Output
In the only line print two numbers min and max β the minimum and maximum number of isolated vertices, respectively.
Examples
Input
4 2
Output
0 1
Input
3 1
Output
1 1
Note
In the first example it is possible to construct a graph with 0 isolated vertices: for example, it should contain edges (1, 2) and (3, 4). To get one isolated vertex, we may construct a graph with edges (1, 2) and (1, 3).
In the second example the graph will always contain exactly one isolated vertex. | instruction | 0 | 60,659 | 13 | 121,318 |
Tags: constructive algorithms, graphs
Correct Solution:
```
a,b=list(map(int,input().split()))
k=0
n=1
while(n<=b):
n=n+k
k=k+1
max=a-k
min=a-2*b
if(min<0):
min=0
print(min,max)
``` | output | 1 | 60,659 | 13 | 121,319 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has got an undirected graph consisting of n vertices and m edges. This graph doesn't contain any self-loops or multiple edges. Self-loop is an edge connecting a vertex to itself. Multiple edges are a pair of edges such that they connect the same pair of vertices. Since the graph is undirected, the pair of edges (1, 2) and (2, 1) is considered to be multiple edges. Isolated vertex of the graph is a vertex such that there is no edge connecting this vertex to any other vertex.
Vasya wants to know the minimum and maximum possible number of isolated vertices in an undirected graph consisting of n vertices and m edges.
Input
The only line contains two integers n and m~(1 β€ n β€ 10^5, 0 β€ m β€ (n (n - 1))/(2)).
It is guaranteed that there exists a graph without any self-loops or multiple edges with such number of vertices and edges.
Output
In the only line print two numbers min and max β the minimum and maximum number of isolated vertices, respectively.
Examples
Input
4 2
Output
0 1
Input
3 1
Output
1 1
Note
In the first example it is possible to construct a graph with 0 isolated vertices: for example, it should contain edges (1, 2) and (3, 4). To get one isolated vertex, we may construct a graph with edges (1, 2) and (1, 3).
In the second example the graph will always contain exactly one isolated vertex. | instruction | 0 | 60,660 | 13 | 121,320 |
Tags: constructive algorithms, graphs
Correct Solution:
```
v, e = map(int, input().split())
def minv(v, e):
if v - 2*e < 0:
return 0
else:
return v - 2*e
def maxv(v, e):
ans = 0
if e == 0:
return v
for i in range(2, v):
ans = ans + i - 1
if ans >= e:
return v - i
return 0
print(minv(v, e), maxv(v, e))
``` | output | 1 | 60,660 | 13 | 121,321 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has got an undirected graph consisting of n vertices and m edges. This graph doesn't contain any self-loops or multiple edges. Self-loop is an edge connecting a vertex to itself. Multiple edges are a pair of edges such that they connect the same pair of vertices. Since the graph is undirected, the pair of edges (1, 2) and (2, 1) is considered to be multiple edges. Isolated vertex of the graph is a vertex such that there is no edge connecting this vertex to any other vertex.
Vasya wants to know the minimum and maximum possible number of isolated vertices in an undirected graph consisting of n vertices and m edges.
Input
The only line contains two integers n and m~(1 β€ n β€ 10^5, 0 β€ m β€ (n (n - 1))/(2)).
It is guaranteed that there exists a graph without any self-loops or multiple edges with such number of vertices and edges.
Output
In the only line print two numbers min and max β the minimum and maximum number of isolated vertices, respectively.
Examples
Input
4 2
Output
0 1
Input
3 1
Output
1 1
Note
In the first example it is possible to construct a graph with 0 isolated vertices: for example, it should contain edges (1, 2) and (3, 4). To get one isolated vertex, we may construct a graph with edges (1, 2) and (1, 3).
In the second example the graph will always contain exactly one isolated vertex. | instruction | 0 | 60,661 | 13 | 121,322 |
Tags: constructive algorithms, graphs
Correct Solution:
```
import math
x=input()
x=x.split()
n=int(x[0])
m=int(x[1])
if m>=n/2:
min=0
else:
min=n-2*m
max=n-math.ceil((2*m+(2*m)**(1/2))**(1/2))
print(str(min)+" "+str(max))
``` | output | 1 | 60,661 | 13 | 121,323 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has got an undirected graph consisting of n vertices and m edges. This graph doesn't contain any self-loops or multiple edges. Self-loop is an edge connecting a vertex to itself. Multiple edges are a pair of edges such that they connect the same pair of vertices. Since the graph is undirected, the pair of edges (1, 2) and (2, 1) is considered to be multiple edges. Isolated vertex of the graph is a vertex such that there is no edge connecting this vertex to any other vertex.
Vasya wants to know the minimum and maximum possible number of isolated vertices in an undirected graph consisting of n vertices and m edges.
Input
The only line contains two integers n and m~(1 β€ n β€ 10^5, 0 β€ m β€ (n (n - 1))/(2)).
It is guaranteed that there exists a graph without any self-loops or multiple edges with such number of vertices and edges.
Output
In the only line print two numbers min and max β the minimum and maximum number of isolated vertices, respectively.
Examples
Input
4 2
Output
0 1
Input
3 1
Output
1 1
Note
In the first example it is possible to construct a graph with 0 isolated vertices: for example, it should contain edges (1, 2) and (3, 4). To get one isolated vertex, we may construct a graph with edges (1, 2) and (1, 3).
In the second example the graph will always contain exactly one isolated vertex. | instruction | 0 | 60,662 | 13 | 121,324 |
Tags: constructive algorithms, graphs
Correct Solution:
```
#constructive algorithm, graph
#Self-loop is not a cycle :xD
from math import sqrt,ceil
n,m = map(int, input().split())
Min = n - 2*m
Min = max(0,Min)
Max = int(n - ceil((1 + sqrt(1+8*m))/ 2))
Max = max(0,Max)
if(m==0):
Min = Max = n
print(Min, Max)
``` | output | 1 | 60,662 | 13 | 121,325 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has got an undirected graph consisting of n vertices and m edges. This graph doesn't contain any self-loops or multiple edges. Self-loop is an edge connecting a vertex to itself. Multiple edges are a pair of edges such that they connect the same pair of vertices. Since the graph is undirected, the pair of edges (1, 2) and (2, 1) is considered to be multiple edges. Isolated vertex of the graph is a vertex such that there is no edge connecting this vertex to any other vertex.
Vasya wants to know the minimum and maximum possible number of isolated vertices in an undirected graph consisting of n vertices and m edges.
Input
The only line contains two integers n and m~(1 β€ n β€ 10^5, 0 β€ m β€ (n (n - 1))/(2)).
It is guaranteed that there exists a graph without any self-loops or multiple edges with such number of vertices and edges.
Output
In the only line print two numbers min and max β the minimum and maximum number of isolated vertices, respectively.
Examples
Input
4 2
Output
0 1
Input
3 1
Output
1 1
Note
In the first example it is possible to construct a graph with 0 isolated vertices: for example, it should contain edges (1, 2) and (3, 4). To get one isolated vertex, we may construct a graph with edges (1, 2) and (1, 3).
In the second example the graph will always contain exactly one isolated vertex. | instruction | 0 | 60,663 | 13 | 121,326 |
Tags: constructive algorithms, graphs
Correct Solution:
```
#JMD
#Nagendra Jha-4096
import sys
import math
#import fractions
#import numpy
###File Operations###
fileoperation=0
if(fileoperation):
orig_stdout = sys.stdout
orig_stdin = sys.stdin
inputfile = open('W:/Competitive Programming/input.txt', 'r')
outputfile = open('W:/Competitive Programming/output.txt', 'w')
sys.stdin = inputfile
sys.stdout = outputfile
###Defines...###
mod=1000000007
###FUF's...###
def nospace(l):
ans=''.join(str(i) for i in l)
return ans
##### Main ####
t=1
for tt in range(t):
#n=int(input())
#a=list(map(int,sys.stdin.readline().split(' ')))
n,m= map(int, sys.stdin.readline().split(' '))
ans=0
k=0
for i in range(100000000000):
ans+=i
if(ans>=m):
k+=1
break
k+=1
if(m):
print(max(0,n-2*m),(n-k))
elif(m==0):
print(n,n)
#####File Operations#####
if(fileoperation):
sys.stdout = orig_stdout
sys.stdin = orig_stdin
inputfile.close()
outputfile.close()
``` | output | 1 | 60,663 | 13 | 121,327 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has got an undirected graph consisting of n vertices and m edges. This graph doesn't contain any self-loops or multiple edges. Self-loop is an edge connecting a vertex to itself. Multiple edges are a pair of edges such that they connect the same pair of vertices. Since the graph is undirected, the pair of edges (1, 2) and (2, 1) is considered to be multiple edges. Isolated vertex of the graph is a vertex such that there is no edge connecting this vertex to any other vertex.
Vasya wants to know the minimum and maximum possible number of isolated vertices in an undirected graph consisting of n vertices and m edges.
Input
The only line contains two integers n and m~(1 β€ n β€ 10^5, 0 β€ m β€ (n (n - 1))/(2)).
It is guaranteed that there exists a graph without any self-loops or multiple edges with such number of vertices and edges.
Output
In the only line print two numbers min and max β the minimum and maximum number of isolated vertices, respectively.
Examples
Input
4 2
Output
0 1
Input
3 1
Output
1 1
Note
In the first example it is possible to construct a graph with 0 isolated vertices: for example, it should contain edges (1, 2) and (3, 4). To get one isolated vertex, we may construct a graph with edges (1, 2) and (1, 3).
In the second example the graph will always contain exactly one isolated vertex.
Submitted Solution:
```
def poisk(x,l,r):
if l>=r-1:
return r
y=(l+r)//2
if y*(y-1)//2>=x:
r=y
else:
l=y
return poisk(x,l,r)
n,m=map(int,input().split())
min1=n-m*2
if min1<0:
min1=0
max1=n-poisk(m,-1,10**6)
print(min1,max1)
``` | instruction | 0 | 60,664 | 13 | 121,328 |
Yes | output | 1 | 60,664 | 13 | 121,329 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has got an undirected graph consisting of n vertices and m edges. This graph doesn't contain any self-loops or multiple edges. Self-loop is an edge connecting a vertex to itself. Multiple edges are a pair of edges such that they connect the same pair of vertices. Since the graph is undirected, the pair of edges (1, 2) and (2, 1) is considered to be multiple edges. Isolated vertex of the graph is a vertex such that there is no edge connecting this vertex to any other vertex.
Vasya wants to know the minimum and maximum possible number of isolated vertices in an undirected graph consisting of n vertices and m edges.
Input
The only line contains two integers n and m~(1 β€ n β€ 10^5, 0 β€ m β€ (n (n - 1))/(2)).
It is guaranteed that there exists a graph without any self-loops or multiple edges with such number of vertices and edges.
Output
In the only line print two numbers min and max β the minimum and maximum number of isolated vertices, respectively.
Examples
Input
4 2
Output
0 1
Input
3 1
Output
1 1
Note
In the first example it is possible to construct a graph with 0 isolated vertices: for example, it should contain edges (1, 2) and (3, 4). To get one isolated vertex, we may construct a graph with edges (1, 2) and (1, 3).
In the second example the graph will always contain exactly one isolated vertex.
Submitted Solution:
```
import math
import fractions
n, m = list(map(int, input().split()))
if m == 0:
print(n, n)
exit()
print(n - min(n, m*2))
if m == 1:
print(n - 2)
if m == 2:
print(n - 3)
if m > 2:
x = math.ceil(math.sqrt(m*2 + fractions.Fraction(1, 4)) - fractions.Fraction(1, 2)) + 1
print(n - x)
``` | instruction | 0 | 60,665 | 13 | 121,330 |
Yes | output | 1 | 60,665 | 13 | 121,331 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has got an undirected graph consisting of n vertices and m edges. This graph doesn't contain any self-loops or multiple edges. Self-loop is an edge connecting a vertex to itself. Multiple edges are a pair of edges such that they connect the same pair of vertices. Since the graph is undirected, the pair of edges (1, 2) and (2, 1) is considered to be multiple edges. Isolated vertex of the graph is a vertex such that there is no edge connecting this vertex to any other vertex.
Vasya wants to know the minimum and maximum possible number of isolated vertices in an undirected graph consisting of n vertices and m edges.
Input
The only line contains two integers n and m~(1 β€ n β€ 10^5, 0 β€ m β€ (n (n - 1))/(2)).
It is guaranteed that there exists a graph without any self-loops or multiple edges with such number of vertices and edges.
Output
In the only line print two numbers min and max β the minimum and maximum number of isolated vertices, respectively.
Examples
Input
4 2
Output
0 1
Input
3 1
Output
1 1
Note
In the first example it is possible to construct a graph with 0 isolated vertices: for example, it should contain edges (1, 2) and (3, 4). To get one isolated vertex, we may construct a graph with edges (1, 2) and (1, 3).
In the second example the graph will always contain exactly one isolated vertex.
Submitted Solution:
```
import math
n,m = map(int, input().split())
if(m==0):
print(n,n)
else:
mi, mx = 0,0
mi = max(0, n-(2*m))
left= 0
right = int(10e5)
while(left < right):
mid = (left+right)//2
if(mid*(mid-1)//2 > m):
right = mid
else:
left = mid+1
left-=1
if((left*(left-1)//2) < m):
left+=1
mx = max(0, n-left)
print(mi, mx)
``` | instruction | 0 | 60,666 | 13 | 121,332 |
Yes | output | 1 | 60,666 | 13 | 121,333 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has got an undirected graph consisting of n vertices and m edges. This graph doesn't contain any self-loops or multiple edges. Self-loop is an edge connecting a vertex to itself. Multiple edges are a pair of edges such that they connect the same pair of vertices. Since the graph is undirected, the pair of edges (1, 2) and (2, 1) is considered to be multiple edges. Isolated vertex of the graph is a vertex such that there is no edge connecting this vertex to any other vertex.
Vasya wants to know the minimum and maximum possible number of isolated vertices in an undirected graph consisting of n vertices and m edges.
Input
The only line contains two integers n and m~(1 β€ n β€ 10^5, 0 β€ m β€ (n (n - 1))/(2)).
It is guaranteed that there exists a graph without any self-loops or multiple edges with such number of vertices and edges.
Output
In the only line print two numbers min and max β the minimum and maximum number of isolated vertices, respectively.
Examples
Input
4 2
Output
0 1
Input
3 1
Output
1 1
Note
In the first example it is possible to construct a graph with 0 isolated vertices: for example, it should contain edges (1, 2) and (3, 4). To get one isolated vertex, we may construct a graph with edges (1, 2) and (1, 3).
In the second example the graph will always contain exactly one isolated vertex.
Submitted Solution:
```
import math
values = list(map(int, input().split(sep=' ')))
n = values[0]
m = values[1]
def count(n,m):
if(m==0):
print(str(int(n)) + " " + str(int(n)))
return
if(n==1):
maximum = 0
minimum = 0
print(str(int(minimum)) + " " + str(int(maximum)))
return
else:
if((int((1+math.sqrt(1+8*m))/2)) == (1+math.sqrt(1+8*m))/2):
maximum = n - int((1 + math.sqrt(1 + 8 * m)) / 2)
else:
maximum = n - int((1+math.sqrt(1+8*m))/2) - 1
minimum = max(n - 2*m,0)
print(str(int(minimum)) + " " + str(int(maximum)))
return
count(n,m)
``` | instruction | 0 | 60,667 | 13 | 121,334 |
Yes | output | 1 | 60,667 | 13 | 121,335 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has got an undirected graph consisting of n vertices and m edges. This graph doesn't contain any self-loops or multiple edges. Self-loop is an edge connecting a vertex to itself. Multiple edges are a pair of edges such that they connect the same pair of vertices. Since the graph is undirected, the pair of edges (1, 2) and (2, 1) is considered to be multiple edges. Isolated vertex of the graph is a vertex such that there is no edge connecting this vertex to any other vertex.
Vasya wants to know the minimum and maximum possible number of isolated vertices in an undirected graph consisting of n vertices and m edges.
Input
The only line contains two integers n and m~(1 β€ n β€ 10^5, 0 β€ m β€ (n (n - 1))/(2)).
It is guaranteed that there exists a graph without any self-loops or multiple edges with such number of vertices and edges.
Output
In the only line print two numbers min and max β the minimum and maximum number of isolated vertices, respectively.
Examples
Input
4 2
Output
0 1
Input
3 1
Output
1 1
Note
In the first example it is possible to construct a graph with 0 isolated vertices: for example, it should contain edges (1, 2) and (3, 4). To get one isolated vertex, we may construct a graph with edges (1, 2) and (1, 3).
In the second example the graph will always contain exactly one isolated vertex.
Submitted Solution:
```
n,m=list(map(int,input().split()))
min_isolated=0;
max_isolated=0;
if n-m*2>0:
min_isolated=n-m*2
if m<n-1:
max_isolated=(n-1-m)
print(min_isolated,max_isolated)
# for i in range(n-1,0,-1):
# max_isolated-=i
# if max_isolated<=0:
# m=i
``` | instruction | 0 | 60,668 | 13 | 121,336 |
No | output | 1 | 60,668 | 13 | 121,337 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has got an undirected graph consisting of n vertices and m edges. This graph doesn't contain any self-loops or multiple edges. Self-loop is an edge connecting a vertex to itself. Multiple edges are a pair of edges such that they connect the same pair of vertices. Since the graph is undirected, the pair of edges (1, 2) and (2, 1) is considered to be multiple edges. Isolated vertex of the graph is a vertex such that there is no edge connecting this vertex to any other vertex.
Vasya wants to know the minimum and maximum possible number of isolated vertices in an undirected graph consisting of n vertices and m edges.
Input
The only line contains two integers n and m~(1 β€ n β€ 10^5, 0 β€ m β€ (n (n - 1))/(2)).
It is guaranteed that there exists a graph without any self-loops or multiple edges with such number of vertices and edges.
Output
In the only line print two numbers min and max β the minimum and maximum number of isolated vertices, respectively.
Examples
Input
4 2
Output
0 1
Input
3 1
Output
1 1
Note
In the first example it is possible to construct a graph with 0 isolated vertices: for example, it should contain edges (1, 2) and (3, 4). To get one isolated vertex, we may construct a graph with edges (1, 2) and (1, 3).
In the second example the graph will always contain exactly one isolated vertex.
Submitted Solution:
```
from math import sqrt
n, m = map(int, input().split())
ans1 = max(0, n - 2 * m)
#n2 - n - 2m = 0
d = 1 + 4 * 2 * m
y = 0
if (sqrt(d) % 1 > 0):
y = 1
x = int((1 + sqrt(d)) / 2) + y
print(x)
#x = int(sqrt(2 * m)) + 1
ans2 = max(n - x, 0)
print(ans1, ans2)
``` | instruction | 0 | 60,669 | 13 | 121,338 |
No | output | 1 | 60,669 | 13 | 121,339 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has got an undirected graph consisting of n vertices and m edges. This graph doesn't contain any self-loops or multiple edges. Self-loop is an edge connecting a vertex to itself. Multiple edges are a pair of edges such that they connect the same pair of vertices. Since the graph is undirected, the pair of edges (1, 2) and (2, 1) is considered to be multiple edges. Isolated vertex of the graph is a vertex such that there is no edge connecting this vertex to any other vertex.
Vasya wants to know the minimum and maximum possible number of isolated vertices in an undirected graph consisting of n vertices and m edges.
Input
The only line contains two integers n and m~(1 β€ n β€ 10^5, 0 β€ m β€ (n (n - 1))/(2)).
It is guaranteed that there exists a graph without any self-loops or multiple edges with such number of vertices and edges.
Output
In the only line print two numbers min and max β the minimum and maximum number of isolated vertices, respectively.
Examples
Input
4 2
Output
0 1
Input
3 1
Output
1 1
Note
In the first example it is possible to construct a graph with 0 isolated vertices: for example, it should contain edges (1, 2) and (3, 4). To get one isolated vertex, we may construct a graph with edges (1, 2) and (1, 3).
In the second example the graph will always contain exactly one isolated vertex.
Submitted Solution:
```
n, m = map(int, input().split())
print(n % (m * 2), n - m - 1)
``` | instruction | 0 | 60,670 | 13 | 121,340 |
No | output | 1 | 60,670 | 13 | 121,341 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has got an undirected graph consisting of n vertices and m edges. This graph doesn't contain any self-loops or multiple edges. Self-loop is an edge connecting a vertex to itself. Multiple edges are a pair of edges such that they connect the same pair of vertices. Since the graph is undirected, the pair of edges (1, 2) and (2, 1) is considered to be multiple edges. Isolated vertex of the graph is a vertex such that there is no edge connecting this vertex to any other vertex.
Vasya wants to know the minimum and maximum possible number of isolated vertices in an undirected graph consisting of n vertices and m edges.
Input
The only line contains two integers n and m~(1 β€ n β€ 10^5, 0 β€ m β€ (n (n - 1))/(2)).
It is guaranteed that there exists a graph without any self-loops or multiple edges with such number of vertices and edges.
Output
In the only line print two numbers min and max β the minimum and maximum number of isolated vertices, respectively.
Examples
Input
4 2
Output
0 1
Input
3 1
Output
1 1
Note
In the first example it is possible to construct a graph with 0 isolated vertices: for example, it should contain edges (1, 2) and (3, 4). To get one isolated vertex, we may construct a graph with edges (1, 2) and (1, 3).
In the second example the graph will always contain exactly one isolated vertex.
Submitted Solution:
```
import math
import random
import sys
import collections
def In():
return map(int, sys.stdin.readline().split())
input = sys.stdin.readline
#
# l = [1200,1300,1400,1500,1600,1700]
# print(random.choice(l))
def isoedge():
v,e = In()
maxiso = 0
pos = 1
while True:
if pos*(pos+1)//2 > e:
maxiso = v - pos
break
pos += 1
print(max(v-(2*e),0),maxiso)
isoedge()
``` | instruction | 0 | 60,671 | 13 | 121,342 |
No | output | 1 | 60,671 | 13 | 121,343 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an undirected unweighted connected graph consisting of n vertices and m edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.
Your task is to find any spanning tree of this graph such that the degree of the first vertex (vertex with label 1 on it) is equal to D (or say that there are no such spanning trees). Recall that the degree of a vertex is the number of edges incident to it.
Input
The first line contains three integers n, m and D (2 β€ n β€ 2 β
10^5, n - 1 β€ m β€ min(2 β
10^5, (n(n-1))/(2)), 1 β€ D < n) β the number of vertices, the number of edges and required degree of the first vertex, respectively.
The following m lines denote edges: edge i is represented by a pair of integers v_i, u_i (1 β€ v_i, u_i β€ n, u_i β v_i), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair (v_i, u_i) there are no other pairs (v_i, u_i) or (u_i, v_i) in the list of edges, and for each pair (v_i, u_i) the condition v_i β u_i is satisfied.
Output
If there is no spanning tree satisfying the condition from the problem statement, print "NO" in the first line.
Otherwise print "YES" in the first line and then print n-1 lines describing the edges of a spanning tree such that the degree of the first vertex (vertex with label 1 on it) is equal to D. Make sure that the edges of the printed spanning tree form some subset of the input edges (order doesn't matter and edge (v, u) is considered the same as the edge (u, v)).
If there are multiple possible answers, print any of them.
Examples
Input
4 5 1
1 2
1 3
1 4
2 3
3 4
Output
YES
2 1
2 3
3 4
Input
4 5 3
1 2
1 3
1 4
2 3
3 4
Output
YES
1 2
1 3
4 1
Input
4 4 3
1 2
1 4
2 3
3 4
Output
NO
Note
The picture corresponding to the first and second examples: <image>
The picture corresponding to the third example: <image> | instruction | 0 | 60,673 | 13 | 121,346 |
Tags: constructive algorithms, dfs and similar, dsu, graphs, greedy
Correct Solution:
```
import sys
input = sys.stdin.readline
n,m,D=map(int,input().split())
E=[list(map(int,input().split())) for i in range(m)]
EDGELIST=[[] for i in range(n+1)]
for x,y in E:
EDGELIST[x].append(y)
EDGELIST[y].append(x)
Group=[i for i in range(n+1)]
def find(x):
while Group[x] != x:
x=Group[x]
return x
def Union(x,y):
if find(x) != find(y):
Group[find(y)]=Group[find(x)]=min(find(y),find(x))
ONE=EDGELIST[1]
for x,y in E:
if x==1 or y==1:
continue
Union(x,y)
ONEU=[find(e) for e in ONE]
if len(set(ONEU))>D or D>len(ONE):
print("NO")
sys.exit()
else:
print("YES")
USED=set()
ANS=[]
from collections import deque
QUE=deque()
check=[0]*(n+1)
check[1]=1
for j in range(len(ONE)):
if find(ONE[j]) in USED:
continue
else:
ANS.append([1,ONE[j]])
QUE.append(ONE[j])
USED.add(find(ONE[j]))
check[ONE[j]]=1
D-=1
j=0
for i in range(D):
while check[ONE[j]]==1:
j+=1
ANS.append([1,ONE[j]])
QUE.append(ONE[j])
check[ONE[j]]=1
while QUE:
x=QUE.popleft()
check[x]=1
for to in EDGELIST[x]:
if check[to]==0:
ANS.append([x,to])
QUE.append(to)
check[to]=1
#print(ANS)
for x,y in ANS:
print(x,y)
``` | output | 1 | 60,673 | 13 | 121,347 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an undirected unweighted connected graph consisting of n vertices and m edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.
Your task is to find any spanning tree of this graph such that the degree of the first vertex (vertex with label 1 on it) is equal to D (or say that there are no such spanning trees). Recall that the degree of a vertex is the number of edges incident to it.
Input
The first line contains three integers n, m and D (2 β€ n β€ 2 β
10^5, n - 1 β€ m β€ min(2 β
10^5, (n(n-1))/(2)), 1 β€ D < n) β the number of vertices, the number of edges and required degree of the first vertex, respectively.
The following m lines denote edges: edge i is represented by a pair of integers v_i, u_i (1 β€ v_i, u_i β€ n, u_i β v_i), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair (v_i, u_i) there are no other pairs (v_i, u_i) or (u_i, v_i) in the list of edges, and for each pair (v_i, u_i) the condition v_i β u_i is satisfied.
Output
If there is no spanning tree satisfying the condition from the problem statement, print "NO" in the first line.
Otherwise print "YES" in the first line and then print n-1 lines describing the edges of a spanning tree such that the degree of the first vertex (vertex with label 1 on it) is equal to D. Make sure that the edges of the printed spanning tree form some subset of the input edges (order doesn't matter and edge (v, u) is considered the same as the edge (u, v)).
If there are multiple possible answers, print any of them.
Examples
Input
4 5 1
1 2
1 3
1 4
2 3
3 4
Output
YES
2 1
2 3
3 4
Input
4 5 3
1 2
1 3
1 4
2 3
3 4
Output
YES
1 2
1 3
4 1
Input
4 4 3
1 2
1 4
2 3
3 4
Output
NO
Note
The picture corresponding to the first and second examples: <image>
The picture corresponding to the third example: <image> | instruction | 0 | 60,674 | 13 | 121,348 |
Tags: constructive algorithms, dfs and similar, dsu, graphs, greedy
Correct Solution:
```
from collections import defaultdict, deque
import sys
input = sys.stdin.readline
def get_root(s):
v = []
while not s == root[s]:
v.append(s)
s = root[s]
for i in v:
root[i] = s
return s
def unite(s, t):
root_s = get_root(s)
root_t = get_root(t)
if not root_s == root_t:
if rank[s] == rank[t]:
root[root_t] = root_s
rank[root_s] += 1
size[root_s] += size[root_t]
elif rank[s] > rank[t]:
root[root_t] = root_s
size[root_s] += size[root_t]
else:
root[root_s] = root_t
size[root_t] += size[root_s]
def same(s, t):
if get_root(s) == get_root(t):
return True
else:
return False
def bfs(s):
q = deque()
q.append(s)
anss = []
while q:
i = q.popleft()
for j in G[i]:
if not visit[j]:
ans.append((i, j))
q.append(j)
visit[j] = 1
return anss
n, m, d = map(int, input().split())
G = [[] for _ in range(n + 1)]
root = [i for i in range(n + 1)]
rank = [1 for _ in range(n + 1)]
size = [1 for _ in range(n + 1)]
u1 = []
for _ in range(m):
u, v = map(int, input().split())
if u ^ 1 and v ^ 1:
unite(u, v)
G[u].append(v)
G[v].append(u)
else:
u1.append(u ^ v ^ 1)
s = set()
r = defaultdict(lambda : -1)
for i in u1:
ri = get_root(i)
s.add(ri)
r[ri] = i
ans = "YES" if len(s) <= d and len(u1) >= d else "NO"
print(ans)
if ans == "YES":
visit = [0] * (n + 1)
visit[1] = 1
st = set()
for i in r:
st.add(r[i])
for i in u1:
if len(st) == d:
break
st.add(i)
for i in st:
visit[i] = 1
ans = []
for i in st:
ans.append((1, i))
for u, v in bfs(i):
ans.append((u, v))
for u, v in ans:
print(u, v)
``` | output | 1 | 60,674 | 13 | 121,349 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an undirected unweighted connected graph consisting of n vertices and m edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.
Your task is to find any spanning tree of this graph such that the degree of the first vertex (vertex with label 1 on it) is equal to D (or say that there are no such spanning trees). Recall that the degree of a vertex is the number of edges incident to it.
Input
The first line contains three integers n, m and D (2 β€ n β€ 2 β
10^5, n - 1 β€ m β€ min(2 β
10^5, (n(n-1))/(2)), 1 β€ D < n) β the number of vertices, the number of edges and required degree of the first vertex, respectively.
The following m lines denote edges: edge i is represented by a pair of integers v_i, u_i (1 β€ v_i, u_i β€ n, u_i β v_i), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair (v_i, u_i) there are no other pairs (v_i, u_i) or (u_i, v_i) in the list of edges, and for each pair (v_i, u_i) the condition v_i β u_i is satisfied.
Output
If there is no spanning tree satisfying the condition from the problem statement, print "NO" in the first line.
Otherwise print "YES" in the first line and then print n-1 lines describing the edges of a spanning tree such that the degree of the first vertex (vertex with label 1 on it) is equal to D. Make sure that the edges of the printed spanning tree form some subset of the input edges (order doesn't matter and edge (v, u) is considered the same as the edge (u, v)).
If there are multiple possible answers, print any of them.
Examples
Input
4 5 1
1 2
1 3
1 4
2 3
3 4
Output
YES
2 1
2 3
3 4
Input
4 5 3
1 2
1 3
1 4
2 3
3 4
Output
YES
1 2
1 3
4 1
Input
4 4 3
1 2
1 4
2 3
3 4
Output
NO
Note
The picture corresponding to the first and second examples: <image>
The picture corresponding to the third example: <image> | instruction | 0 | 60,675 | 13 | 121,350 |
Tags: constructive algorithms, dfs and similar, dsu, graphs, greedy
Correct Solution:
```
n, m, d = map(int, input().split())
g = [[] for _ in range(n + 1)]
haveOne = [False] * (n + 1)
for i in range(m):
u, v = map(int, input().split())
g[u].append(v)
g[v].append(u)
if u == 1:
haveOne[v] = True
if v == 1:
haveOne[u] = True
count = 0
group = [-1] * (n + 1)
selectedOne = []
for i in range(2, n+1):
if group[i] == -1:
# bfs
group[i] = count
useOne = False
if haveOne[i]:
selectedOne.append(i)
useOne = True
if count >= d:
count += 1
break
incount = count + 1
qu = []
qu += g[i]
while len(qu) > 0:
c = qu.pop()
if c != 1 and group[c] == -1:
if haveOne[c] and not(useOne):
selectedOne.append(c)
useOne = True
group[c] = count
qu += g[c]
count += 1
if count > d or d > len(g[1]):
print('NO')
else:
diffOne = list(set(g[1]) - set(selectedOne))
diffOne = selectedOne + diffOne
g[1] = diffOne[:d]
visited = [False] * (n + 1)
qVisit = [1]
visited[1] = True
print('YES')
while len(qVisit) > 0:
i = qVisit.pop()
for j in g[i]:
if not(visited[j]):
print(i, j)
visited[j] = True
qVisit.append(j)
``` | output | 1 | 60,675 | 13 | 121,351 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an undirected unweighted connected graph consisting of n vertices and m edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.
Your task is to find any spanning tree of this graph such that the degree of the first vertex (vertex with label 1 on it) is equal to D (or say that there are no such spanning trees). Recall that the degree of a vertex is the number of edges incident to it.
Input
The first line contains three integers n, m and D (2 β€ n β€ 2 β
10^5, n - 1 β€ m β€ min(2 β
10^5, (n(n-1))/(2)), 1 β€ D < n) β the number of vertices, the number of edges and required degree of the first vertex, respectively.
The following m lines denote edges: edge i is represented by a pair of integers v_i, u_i (1 β€ v_i, u_i β€ n, u_i β v_i), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair (v_i, u_i) there are no other pairs (v_i, u_i) or (u_i, v_i) in the list of edges, and for each pair (v_i, u_i) the condition v_i β u_i is satisfied.
Output
If there is no spanning tree satisfying the condition from the problem statement, print "NO" in the first line.
Otherwise print "YES" in the first line and then print n-1 lines describing the edges of a spanning tree such that the degree of the first vertex (vertex with label 1 on it) is equal to D. Make sure that the edges of the printed spanning tree form some subset of the input edges (order doesn't matter and edge (v, u) is considered the same as the edge (u, v)).
If there are multiple possible answers, print any of them.
Examples
Input
4 5 1
1 2
1 3
1 4
2 3
3 4
Output
YES
2 1
2 3
3 4
Input
4 5 3
1 2
1 3
1 4
2 3
3 4
Output
YES
1 2
1 3
4 1
Input
4 4 3
1 2
1 4
2 3
3 4
Output
NO
Note
The picture corresponding to the first and second examples: <image>
The picture corresponding to the third example: <image> | instruction | 0 | 60,676 | 13 | 121,352 |
Tags: constructive algorithms, dfs and similar, dsu, graphs, greedy
Correct Solution:
```
import sys
from collections import Counter, defaultdict
def i_ints():
return map(int, sys.stdin.readline().split())
n, m, D = i_ints()
E = defaultdict(set)
for i in range(m):
u, v = i_ints()
E[u].add(v)
E[v].add(u)
def append_edge(u, v):
E[u].discard(v)
E[v].discard(u)
t.add(u)
t.add(v)
te.append((u, v))
def complete_tree(u):
global too_much
todo = {u}
while todo:
u = todo.pop()
for v in list(E[u]):
if v not in t:
if v not in starts:
append_edge(u, v)
todo.add(v)
else:
if too_much > 0:
append_edge(u, v)
todo.add(v)
too_much -= 1
def print_tree():
for u, v in te:
print(u, v)
u0 = 1
t = {u0}
te = []
starts = set(E[u0])
too_much = len(starts) - D
if too_much >= 0:
for v in starts:
if v not in t:
append_edge(u0, v)
complete_tree(v)
if not too_much:
print("YES")
print_tree()
else:
print("NO")
``` | output | 1 | 60,676 | 13 | 121,353 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an undirected unweighted connected graph consisting of n vertices and m edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.
Your task is to find any spanning tree of this graph such that the degree of the first vertex (vertex with label 1 on it) is equal to D (or say that there are no such spanning trees). Recall that the degree of a vertex is the number of edges incident to it.
Input
The first line contains three integers n, m and D (2 β€ n β€ 2 β
10^5, n - 1 β€ m β€ min(2 β
10^5, (n(n-1))/(2)), 1 β€ D < n) β the number of vertices, the number of edges and required degree of the first vertex, respectively.
The following m lines denote edges: edge i is represented by a pair of integers v_i, u_i (1 β€ v_i, u_i β€ n, u_i β v_i), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair (v_i, u_i) there are no other pairs (v_i, u_i) or (u_i, v_i) in the list of edges, and for each pair (v_i, u_i) the condition v_i β u_i is satisfied.
Output
If there is no spanning tree satisfying the condition from the problem statement, print "NO" in the first line.
Otherwise print "YES" in the first line and then print n-1 lines describing the edges of a spanning tree such that the degree of the first vertex (vertex with label 1 on it) is equal to D. Make sure that the edges of the printed spanning tree form some subset of the input edges (order doesn't matter and edge (v, u) is considered the same as the edge (u, v)).
If there are multiple possible answers, print any of them.
Examples
Input
4 5 1
1 2
1 3
1 4
2 3
3 4
Output
YES
2 1
2 3
3 4
Input
4 5 3
1 2
1 3
1 4
2 3
3 4
Output
YES
1 2
1 3
4 1
Input
4 4 3
1 2
1 4
2 3
3 4
Output
NO
Note
The picture corresponding to the first and second examples: <image>
The picture corresponding to the third example: <image> | instruction | 0 | 60,677 | 13 | 121,354 |
Tags: constructive algorithms, dfs and similar, dsu, graphs, greedy
Correct Solution:
```
import sys
root = 0
n, m, D = map(int, input().split())
g = [[] for _ in range(n)]
for _ in range(m):
u, v = map(int, input().split())
g[u-1].append(v-1)
g[v-1].append(u-1)
if len(g[root]) < D:
print('NO')
sys.exit(0)
uf = [-1 for _ in range(n)]
def find(uf, u):
if uf[u] < 0:
return u
else:
ans = find(uf, uf[u])
uf[u] = ans
return ans
def merge(uf, u, v):
pu = find(uf, u)
pv = find(uf, v)
if pu == pv:
return
if uf[pu] > uf[pv]:
pu, pv = pv, pu
uf[pu] += uf[pv]
uf[pv] = pu
ans = []
in_tree = {}
for v in g[root]:
merge(uf, root, v)
for i in range(n):
for v in g[i]:
if find(uf, i) != find(uf, v):
merge(uf, i, v)
ans.append((i+1, v+1))
in_tree[(min(i, v), max(i, v))] = True
children = [[] for _ in range(n)]
par = [-1 for _ in range(n)]
def dfs(s, super_p):
st = [(s, root)]
while len(st) > 0:
u, p = st.pop()
children[super_p].append(u)
merge(par, u, super_p)
for v in g[u]:
if v != p and (min(u, v), max(u, v)) in in_tree:
st.append((v, u))
for v in g[root]:
dfs(v, v)
sz = len(g[root])
for i in range(len(g[root])):
found = False
u = g[root][i]
if sz > D:
for v in children[u]:
for w in g[v]:
if not found and w != root and find(par, w) != find(par, v) and (min(v, w), max(v, w)) not in in_tree:
sz -= 1
found = True
merge(par, v, w)
ans.append((v+1, w+1))
in_tree[(min(v, w), max(v, w))] = True
if not found:
ans.append((root+1, u+1))
if sz != D:
print('NO')
else:
print('YES')
print('\n'.join(map(lambda x: '{} {}'.format(x[0], x[1]), ans)))
``` | output | 1 | 60,677 | 13 | 121,355 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an undirected unweighted connected graph consisting of n vertices and m edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.
Your task is to find any spanning tree of this graph such that the degree of the first vertex (vertex with label 1 on it) is equal to D (or say that there are no such spanning trees). Recall that the degree of a vertex is the number of edges incident to it.
Input
The first line contains three integers n, m and D (2 β€ n β€ 2 β
10^5, n - 1 β€ m β€ min(2 β
10^5, (n(n-1))/(2)), 1 β€ D < n) β the number of vertices, the number of edges and required degree of the first vertex, respectively.
The following m lines denote edges: edge i is represented by a pair of integers v_i, u_i (1 β€ v_i, u_i β€ n, u_i β v_i), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair (v_i, u_i) there are no other pairs (v_i, u_i) or (u_i, v_i) in the list of edges, and for each pair (v_i, u_i) the condition v_i β u_i is satisfied.
Output
If there is no spanning tree satisfying the condition from the problem statement, print "NO" in the first line.
Otherwise print "YES" in the first line and then print n-1 lines describing the edges of a spanning tree such that the degree of the first vertex (vertex with label 1 on it) is equal to D. Make sure that the edges of the printed spanning tree form some subset of the input edges (order doesn't matter and edge (v, u) is considered the same as the edge (u, v)).
If there are multiple possible answers, print any of them.
Examples
Input
4 5 1
1 2
1 3
1 4
2 3
3 4
Output
YES
2 1
2 3
3 4
Input
4 5 3
1 2
1 3
1 4
2 3
3 4
Output
YES
1 2
1 3
4 1
Input
4 4 3
1 2
1 4
2 3
3 4
Output
NO
Note
The picture corresponding to the first and second examples: <image>
The picture corresponding to the third example: <image> | instruction | 0 | 60,678 | 13 | 121,356 |
Tags: constructive algorithms, dfs and similar, dsu, graphs, greedy
Correct Solution:
```
n, m, D = [int(x) for x in input().split(' ')]
G = {}
for u in range(1, n + 1):
G[u] = set()
for i in range(m):
u, v = [int(x) for x in input().split(' ')]
G[u].add(v)
G[v].add(u)
if len(G[1]) < D:
print('NO')
else:
visited = [1] + [0] * n
comp = [0] * (n + 1)
c_visited = [1] + [0] * n
a = 0
for i in G[1]:
if not visited[i]:
a += 1
comp[i] = a
visited[i] = 1
q = [i]
# bfs
while len(q) > 0:
u = q.pop(0)
for v in G[u]:
if v != 1 and not visited[v]:
q.append(v)
comp[v] = a
visited[v] = 1
if a > D:
print('NO')
else:
print('YES')
d = D
visited[1] = 2
queue = []
n_edges = 0
for v in G[1]:
if not c_visited[comp[v]]:
visited[v] = 2
d -= 1
c_visited[comp[v]] = 1
n_edges += 1
print(1, v)
queue.append(v)
if d:
for v in G[1]:
if visited[v] != 2:
visited[v] = 2
d -= 1
print(1, v)
n_edges += 1
queue.append(v)
if not d:
break
# bfs
while len(queue) > 0 and n_edges < n -1:
u = queue.pop(0)
for v in G[u]:
if visited[v] != 2:
visited[v] = 2
queue.append(v)
n_edges += 1
print(u, v)
``` | output | 1 | 60,678 | 13 | 121,357 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an undirected unweighted connected graph consisting of n vertices and m edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.
Your task is to find any spanning tree of this graph such that the degree of the first vertex (vertex with label 1 on it) is equal to D (or say that there are no such spanning trees). Recall that the degree of a vertex is the number of edges incident to it.
Input
The first line contains three integers n, m and D (2 β€ n β€ 2 β
10^5, n - 1 β€ m β€ min(2 β
10^5, (n(n-1))/(2)), 1 β€ D < n) β the number of vertices, the number of edges and required degree of the first vertex, respectively.
The following m lines denote edges: edge i is represented by a pair of integers v_i, u_i (1 β€ v_i, u_i β€ n, u_i β v_i), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair (v_i, u_i) there are no other pairs (v_i, u_i) or (u_i, v_i) in the list of edges, and for each pair (v_i, u_i) the condition v_i β u_i is satisfied.
Output
If there is no spanning tree satisfying the condition from the problem statement, print "NO" in the first line.
Otherwise print "YES" in the first line and then print n-1 lines describing the edges of a spanning tree such that the degree of the first vertex (vertex with label 1 on it) is equal to D. Make sure that the edges of the printed spanning tree form some subset of the input edges (order doesn't matter and edge (v, u) is considered the same as the edge (u, v)).
If there are multiple possible answers, print any of them.
Examples
Input
4 5 1
1 2
1 3
1 4
2 3
3 4
Output
YES
2 1
2 3
3 4
Input
4 5 3
1 2
1 3
1 4
2 3
3 4
Output
YES
1 2
1 3
4 1
Input
4 4 3
1 2
1 4
2 3
3 4
Output
NO
Note
The picture corresponding to the first and second examples: <image>
The picture corresponding to the third example: <image> | instruction | 0 | 60,679 | 13 | 121,358 |
Tags: constructive algorithms, dfs and similar, dsu, graphs, greedy
Correct Solution:
```
import sys
import math
from collections import defaultdict,deque
import heapq
def find(node,parent):
while parent[node]!=node:
node=parent[node]
return node
def union(a,b,child,parent):
#print(a,'a',b,'b')
para=find(a,parent)
parb=find(b,parent)
#print(para,'para')
ca=child[para]
cb=child[parb]
if para!=parb:
if ca>cb:
parent[parb]=para
child[para]+=child[parb]
else:
parent[para]=parb
child[parb]+=child[para]
n,m,d=map(int,sys.stdin.readline().split())
graph=defaultdict(list)
parent=[i for i in range(n+1)]
child=[1 for i in range(n+1)]
edges=[]
for i in range(m):
u,v=map(int,sys.stdin.readline().split())
graph[u].append(v)
graph[v].append(u)
#edges.append([u,v])
vis=defaultdict(int)
q=deque()
vis[1]=1
for j in graph[1]:
if vis[j]==0:
vis[j]=1
q.append(j)
#print(j,'j')
child[j]+=1
#print(child[j],'child')
while q:
cur=q.pop()
#print(cur,'cur')
for i in graph[cur]:
if vis[i]==0:
q.append(i)
if cur!=1 and i!=1:
#print(cur,'cur',i,'i')
union(cur,i,child,parent)
vis[i]=1
if len(graph[1]) < d:
print("NO")
sys.exit()
#print(graph[1],'one')
#print(parent,'parent')
#print(child,'child')
cnt=set()
for i in graph[1]:
cnt.add(find(i,parent))
#print(cnt,'cnt')
if len(cnt)>d:
print("NO")
sys.exit()
q=deque()
res=0
ans=[]
vis=defaultdict(int)
for i in cnt:
ans.append([1,i])
q.append(i)
vis[i]=1
res+=1
rem=d-res
vis[1]=1
for i in graph[1]:
if rem>0 and vis[i]==0:
vis[i]=1
q.append(i)
rem-=1
ans.append([1,i])
while q:
cur=q.popleft()
for j in graph[cur]:
if vis[j]==0:
q.append(j)
ans.append([cur,j])
vis[j]=1
print("YES")
for i in range(n-1):
print(ans[i][0],ans[i][1])
``` | output | 1 | 60,679 | 13 | 121,359 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an undirected unweighted connected graph consisting of n vertices and m edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.
Your task is to find any spanning tree of this graph such that the degree of the first vertex (vertex with label 1 on it) is equal to D (or say that there are no such spanning trees). Recall that the degree of a vertex is the number of edges incident to it.
Input
The first line contains three integers n, m and D (2 β€ n β€ 2 β
10^5, n - 1 β€ m β€ min(2 β
10^5, (n(n-1))/(2)), 1 β€ D < n) β the number of vertices, the number of edges and required degree of the first vertex, respectively.
The following m lines denote edges: edge i is represented by a pair of integers v_i, u_i (1 β€ v_i, u_i β€ n, u_i β v_i), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair (v_i, u_i) there are no other pairs (v_i, u_i) or (u_i, v_i) in the list of edges, and for each pair (v_i, u_i) the condition v_i β u_i is satisfied.
Output
If there is no spanning tree satisfying the condition from the problem statement, print "NO" in the first line.
Otherwise print "YES" in the first line and then print n-1 lines describing the edges of a spanning tree such that the degree of the first vertex (vertex with label 1 on it) is equal to D. Make sure that the edges of the printed spanning tree form some subset of the input edges (order doesn't matter and edge (v, u) is considered the same as the edge (u, v)).
If there are multiple possible answers, print any of them.
Examples
Input
4 5 1
1 2
1 3
1 4
2 3
3 4
Output
YES
2 1
2 3
3 4
Input
4 5 3
1 2
1 3
1 4
2 3
3 4
Output
YES
1 2
1 3
4 1
Input
4 4 3
1 2
1 4
2 3
3 4
Output
NO
Note
The picture corresponding to the first and second examples: <image>
The picture corresponding to the third example: <image> | instruction | 0 | 60,680 | 13 | 121,360 |
Tags: constructive algorithms, dfs and similar, dsu, graphs, greedy
Correct Solution:
```
###pyrival template for fast IO
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def graph_undirected():
n,e,d=[int(x) for x in input().split()]
graph={} #####adjecancy list using dict
for i in range(e):
vertex,neighbour=[int(x) for x in input().split()]
if vertex in graph:
graph[vertex].append(neighbour)
else:
graph[vertex]=[neighbour]
if neighbour in graph: #####for undirected part remove to get directed
graph[neighbour].append(vertex)
else:
graph[neighbour]=[vertex]
return graph,n,d
###bfs undirected graph(asumming can reach all nodes)
from collections import deque
def bfs(graph,currnode):
visited=[False for x in range(n+1)]
stack=deque();
parent=[0 for x in range(n+1)]
stack.append(currnode)
while stack:
currnode=stack.popleft()
if visited[currnode]==False:
visited[currnode]=True
for neigbour in graph[currnode]:
if visited[neigbour]==False:
visited[neigbour]=True
parent[neigbour]=currnode
stack.append(neigbour)
return parent
def bfs1(graph,currnode,visited):
stack=deque();
stack.append(currnode)
while stack:
currnode=stack.popleft()
if visited[currnode]==False:
visited[currnode]=True
for neigbour in graph[currnode]:
if visited[neigbour]==False:
visited[neigbour]=True
stack.append(neigbour)
return
graph,n,d=graph_undirected()
def solve():
if len(graph[1])<d:
print("NO")
return
##find the nodes which cant be deleted as they make graph disconected
visited=[False for x in range(n+1)]
visited[1]=True
mustchoose=set()
for node in graph[1]:
if visited[node]==False:
mustchoose.add(node)
bfs1(graph,node,visited)
if len(mustchoose)>d:
print("NO")
return
garr=[]
for i in mustchoose:
garr.append(i)
for node in graph[1]:
if len(garr)==d:
break
if node not in mustchoose:
garr.append(node)
graph[1]=garr
parent=bfs(graph,1)
print("YES")
for i in range(2,len(parent)):
sys.stdout.write(str(parent[i])+" "+str(i)+"\n")
solve()
``` | output | 1 | 60,680 | 13 | 121,361 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an undirected unweighted connected graph consisting of n vertices and m edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.
Your task is to find any spanning tree of this graph such that the degree of the first vertex (vertex with label 1 on it) is equal to D (or say that there are no such spanning trees). Recall that the degree of a vertex is the number of edges incident to it.
Input
The first line contains three integers n, m and D (2 β€ n β€ 2 β
10^5, n - 1 β€ m β€ min(2 β
10^5, (n(n-1))/(2)), 1 β€ D < n) β the number of vertices, the number of edges and required degree of the first vertex, respectively.
The following m lines denote edges: edge i is represented by a pair of integers v_i, u_i (1 β€ v_i, u_i β€ n, u_i β v_i), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair (v_i, u_i) there are no other pairs (v_i, u_i) or (u_i, v_i) in the list of edges, and for each pair (v_i, u_i) the condition v_i β u_i is satisfied.
Output
If there is no spanning tree satisfying the condition from the problem statement, print "NO" in the first line.
Otherwise print "YES" in the first line and then print n-1 lines describing the edges of a spanning tree such that the degree of the first vertex (vertex with label 1 on it) is equal to D. Make sure that the edges of the printed spanning tree form some subset of the input edges (order doesn't matter and edge (v, u) is considered the same as the edge (u, v)).
If there are multiple possible answers, print any of them.
Examples
Input
4 5 1
1 2
1 3
1 4
2 3
3 4
Output
YES
2 1
2 3
3 4
Input
4 5 3
1 2
1 3
1 4
2 3
3 4
Output
YES
1 2
1 3
4 1
Input
4 4 3
1 2
1 4
2 3
3 4
Output
NO
Note
The picture corresponding to the first and second examples: <image>
The picture corresponding to the third example: <image>
Submitted Solution:
```
[n,m,d] = [int(x) for x in input().split()]
gr = []
def read_edge():
[a,b] = [int(x) for x in input().split()]
if a == 1 or b == 1:
gr.append(a+b-1)
return [a,b]
edges = [read_edge() for i in range(m)]
par = [i for i in range(0,n+1)]
rank = [0 for i in range(0,n+1)]
def find_parent(a):
if par[a] == a:
return a
par[a] = find_parent(par[a])
return par[a]
def union_sets(a,b):
a = find_parent(a)
b = find_parent(b)
if a != b:
if rank[b] > rank[a]:
[b,a] = [a,b]
par[b] = a;
if rank[a] == rank[b]:
rank[a] += 1
return True
return False
if d > len(gr):
print("NO")
else:
for [a,b] in edges:
if a != 1 and b != 1:
union_sets(a,b)
components = sum([par[i] == i for i in range(2,n+1)])
if components > d:
print("NO")
else:
par2 = [find_parent(i) for i in range(0,n+1)]
par = [i for i in range(0,n+1)]
rank = [0 for i in range(0,n+1)]
print("YES")
s = set()
h = set()
for x in gr:
if not par2[x] in s:
s.add(par2[x])
h.add(x)
print("1 %d" % x)
union_sets(1,x)
total = components
for x in gr:
if total == d:
break
if not x in h:
print("1 %d" % x)
union_sets(1,x)
total += 1
for [a,b] in edges:
if a != 1 and b != 1:
if union_sets(a,b):
print("%d %d" % (a,b))
``` | instruction | 0 | 60,681 | 13 | 121,362 |
Yes | output | 1 | 60,681 | 13 | 121,363 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an undirected unweighted connected graph consisting of n vertices and m edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.
Your task is to find any spanning tree of this graph such that the degree of the first vertex (vertex with label 1 on it) is equal to D (or say that there are no such spanning trees). Recall that the degree of a vertex is the number of edges incident to it.
Input
The first line contains three integers n, m and D (2 β€ n β€ 2 β
10^5, n - 1 β€ m β€ min(2 β
10^5, (n(n-1))/(2)), 1 β€ D < n) β the number of vertices, the number of edges and required degree of the first vertex, respectively.
The following m lines denote edges: edge i is represented by a pair of integers v_i, u_i (1 β€ v_i, u_i β€ n, u_i β v_i), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair (v_i, u_i) there are no other pairs (v_i, u_i) or (u_i, v_i) in the list of edges, and for each pair (v_i, u_i) the condition v_i β u_i is satisfied.
Output
If there is no spanning tree satisfying the condition from the problem statement, print "NO" in the first line.
Otherwise print "YES" in the first line and then print n-1 lines describing the edges of a spanning tree such that the degree of the first vertex (vertex with label 1 on it) is equal to D. Make sure that the edges of the printed spanning tree form some subset of the input edges (order doesn't matter and edge (v, u) is considered the same as the edge (u, v)).
If there are multiple possible answers, print any of them.
Examples
Input
4 5 1
1 2
1 3
1 4
2 3
3 4
Output
YES
2 1
2 3
3 4
Input
4 5 3
1 2
1 3
1 4
2 3
3 4
Output
YES
1 2
1 3
4 1
Input
4 4 3
1 2
1 4
2 3
3 4
Output
NO
Note
The picture corresponding to the first and second examples: <image>
The picture corresponding to the third example: <image>
Submitted Solution:
```
import sys
from collections import deque
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
n,m,d = map(int,input().split())
g = [[] for i in range(n)]
for i in range(m):
a,b = map(int,input().split())
a-=1;b-=1
g[a].append(b)
g[b].append(a)
ne = 0
ans = []
mita = [0]*n
mita[ne] = -1
def dfs(x,y):
for i in g[x]:
if mita[i] == 0:
ans.append([x+1,i+1])
mita[i] = y
que.append([i,y])
for i in g[ne]:
if mita[i] != 0:
continue
mita[i] = i
que = deque()
que.append([i,i])
while que:
x,y = que.popleft()
dfs(x,y)
syo = len(set(mita))-1
if syo > d or len(g[ne]) < d:
print("NO")
exit()
print("YES")
use = set()
ki = set()
for i in g[ne]:
if mita[i] in ki:
continue
else:
ki.add(mita[i])
use.add(i)
for i in g[ne]:
if len(use) == d:
break
if i not in use:
use.add(i)
g[ne] = list(use)
ans = []
mita = [0]*n
mita[ne] = 1
def dfs(x):
for i in g[x]:
if mita[i] == 0:
ans.append([x+1,i+1])
mita[i] = 1
que.append(i)
que = deque()
que.append(ne)
while que:
dfs(que.popleft())
for i,j in ans:
print(i,j)
``` | instruction | 0 | 60,682 | 13 | 121,364 |
Yes | output | 1 | 60,682 | 13 | 121,365 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an undirected unweighted connected graph consisting of n vertices and m edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.
Your task is to find any spanning tree of this graph such that the degree of the first vertex (vertex with label 1 on it) is equal to D (or say that there are no such spanning trees). Recall that the degree of a vertex is the number of edges incident to it.
Input
The first line contains three integers n, m and D (2 β€ n β€ 2 β
10^5, n - 1 β€ m β€ min(2 β
10^5, (n(n-1))/(2)), 1 β€ D < n) β the number of vertices, the number of edges and required degree of the first vertex, respectively.
The following m lines denote edges: edge i is represented by a pair of integers v_i, u_i (1 β€ v_i, u_i β€ n, u_i β v_i), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair (v_i, u_i) there are no other pairs (v_i, u_i) or (u_i, v_i) in the list of edges, and for each pair (v_i, u_i) the condition v_i β u_i is satisfied.
Output
If there is no spanning tree satisfying the condition from the problem statement, print "NO" in the first line.
Otherwise print "YES" in the first line and then print n-1 lines describing the edges of a spanning tree such that the degree of the first vertex (vertex with label 1 on it) is equal to D. Make sure that the edges of the printed spanning tree form some subset of the input edges (order doesn't matter and edge (v, u) is considered the same as the edge (u, v)).
If there are multiple possible answers, print any of them.
Examples
Input
4 5 1
1 2
1 3
1 4
2 3
3 4
Output
YES
2 1
2 3
3 4
Input
4 5 3
1 2
1 3
1 4
2 3
3 4
Output
YES
1 2
1 3
4 1
Input
4 4 3
1 2
1 4
2 3
3 4
Output
NO
Note
The picture corresponding to the first and second examples: <image>
The picture corresponding to the third example: <image>
Submitted Solution:
```
from collections import deque
n, m, d = map(int, input().split())
g = [[] for i in range(n + 1)]
for i in range(m):
u, v = map(int, input().split())
g[u].append(v)
g[v].append(u)
def bfs(init, c, p=False):
global color
q = deque(init)
while len(q) > 0:
u = q.popleft()
for v in g[u]:
if color[v] < 0:
if p:
print(u, v)
q.append(v)
color[v] = c
color = [-1 for i in range(n + 1)]
color[1] = 0
c = 0
for x in g[1]:
if color[x] < 0:
color[x] = c
bfs([x], c)
c += 1
if len(g[1]) < d or d < c:
print('NO')
else:
is_kid = [False for x in g[1]]
kids = []
picked = [False for i in range(c)]
for i in range(len(g[1])):
x = g[1][i]
if not picked[color[x]]:
is_kid[i] = True
kids.append(x)
picked[color[x]] = True
extra = d - c
for i in range(len(g[1])):
x = g[1][i]
if extra == 0:
break
if not is_kid[i]:
is_kid[i] = True
kids.append(x)
extra -= 1
color = [-1 for i in range(n + 1)]
color[1] = 0
print('YES')
for x in kids:
print(1, x)
color[x] = 0
bfs(kids, 0, True)
``` | instruction | 0 | 60,683 | 13 | 121,366 |
Yes | output | 1 | 60,683 | 13 | 121,367 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an undirected unweighted connected graph consisting of n vertices and m edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.
Your task is to find any spanning tree of this graph such that the degree of the first vertex (vertex with label 1 on it) is equal to D (or say that there are no such spanning trees). Recall that the degree of a vertex is the number of edges incident to it.
Input
The first line contains three integers n, m and D (2 β€ n β€ 2 β
10^5, n - 1 β€ m β€ min(2 β
10^5, (n(n-1))/(2)), 1 β€ D < n) β the number of vertices, the number of edges and required degree of the first vertex, respectively.
The following m lines denote edges: edge i is represented by a pair of integers v_i, u_i (1 β€ v_i, u_i β€ n, u_i β v_i), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair (v_i, u_i) there are no other pairs (v_i, u_i) or (u_i, v_i) in the list of edges, and for each pair (v_i, u_i) the condition v_i β u_i is satisfied.
Output
If there is no spanning tree satisfying the condition from the problem statement, print "NO" in the first line.
Otherwise print "YES" in the first line and then print n-1 lines describing the edges of a spanning tree such that the degree of the first vertex (vertex with label 1 on it) is equal to D. Make sure that the edges of the printed spanning tree form some subset of the input edges (order doesn't matter and edge (v, u) is considered the same as the edge (u, v)).
If there are multiple possible answers, print any of them.
Examples
Input
4 5 1
1 2
1 3
1 4
2 3
3 4
Output
YES
2 1
2 3
3 4
Input
4 5 3
1 2
1 3
1 4
2 3
3 4
Output
YES
1 2
1 3
4 1
Input
4 4 3
1 2
1 4
2 3
3 4
Output
NO
Note
The picture corresponding to the first and second examples: <image>
The picture corresponding to the third example: <image>
Submitted Solution:
```
import collections as cc
import math as mt
import sys
I=lambda:list(map(int,input().split()))
def find(u):
while u!=parent[u]:
u=parent[u]
return u
def union(u,v):
a=find(u)
b=find(v)
if a!=b:
parent[a]=parent[b]=min(a,b)
n,m,d=I()
uu=set()
uu.add(1)
parent=[i for i in range(n+1)]
g=cc.defaultdict(list)
on=[]
tf=cc.defaultdict(int)
other=[]
for i in range(m):
x,y=sorted(I())
g[x].append(y)
g[y].append(x)
if x!=1 and y!=1:
other.append([x,y])
union(x,y)
temp=g[1]
con=[find(i) for i in set(temp)]
if len(set(con))>d or len(set(temp))<d:
print("NO")
sys.exit()
else:
print("YES")
used=cc.defaultdict(int)
ans=[]
st=cc.deque()
use=[0]*(n+1)
use[1]=1
j=0
for i in range(len(temp)):
if not used[find(temp[i])]:
used[find(temp[i])]=1
ans.append([1,temp[i]])
st.append(temp[i])
use[temp[i]]=1
d-=1
for i in range(d):
while use[temp[j]]==1:
j+=1
ans.append([1,temp[j]])
st.append(temp[j])
use[temp[j]]=1
while st:
x=st.popleft()
use[x]=1
for y in g[x]:
if not use[y]:
ans.append([x,y])
st.append(y)
use[y]=1
for i in ans:
print(*i)
``` | instruction | 0 | 60,684 | 13 | 121,368 |
Yes | output | 1 | 60,684 | 13 | 121,369 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an undirected unweighted connected graph consisting of n vertices and m edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.
Your task is to find any spanning tree of this graph such that the degree of the first vertex (vertex with label 1 on it) is equal to D (or say that there are no such spanning trees). Recall that the degree of a vertex is the number of edges incident to it.
Input
The first line contains three integers n, m and D (2 β€ n β€ 2 β
10^5, n - 1 β€ m β€ min(2 β
10^5, (n(n-1))/(2)), 1 β€ D < n) β the number of vertices, the number of edges and required degree of the first vertex, respectively.
The following m lines denote edges: edge i is represented by a pair of integers v_i, u_i (1 β€ v_i, u_i β€ n, u_i β v_i), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair (v_i, u_i) there are no other pairs (v_i, u_i) or (u_i, v_i) in the list of edges, and for each pair (v_i, u_i) the condition v_i β u_i is satisfied.
Output
If there is no spanning tree satisfying the condition from the problem statement, print "NO" in the first line.
Otherwise print "YES" in the first line and then print n-1 lines describing the edges of a spanning tree such that the degree of the first vertex (vertex with label 1 on it) is equal to D. Make sure that the edges of the printed spanning tree form some subset of the input edges (order doesn't matter and edge (v, u) is considered the same as the edge (u, v)).
If there are multiple possible answers, print any of them.
Examples
Input
4 5 1
1 2
1 3
1 4
2 3
3 4
Output
YES
2 1
2 3
3 4
Input
4 5 3
1 2
1 3
1 4
2 3
3 4
Output
YES
1 2
1 3
4 1
Input
4 4 3
1 2
1 4
2 3
3 4
Output
NO
Note
The picture corresponding to the first and second examples: <image>
The picture corresponding to the third example: <image>
Submitted Solution:
```
import collections as cc
import math as mt
import sys
I=lambda:list(map(int,input().split()))
def find(u):
while u!=parent[u]:
u=parent[u]
return u
def union(u,v):
a=find(u)
b=find(v)
if a!=b:
parent[a]=parent[b]=min(a,b)
n,m,d=I()
parent=[i for i in range(n+1)]
g=cc.defaultdict(list)
on=[]
tf=cc.defaultdict(int)
other=[]
for i in range(m):
x,y=I()
g[x].append(y)
g[y].append(x)
if x!=1 and y!=1:
other.append([x,y])
union(x,y)
temp=g[1]
con=[find(i) for i in set(temp)]
if len(set(con))>d or len(set(temp))<d:
print("NO")
sys.exit()
else:
ans=[]
dis=len(set(con))
used=cc.defaultdict(int)
us=cc.defaultdict(int)
nd=d
for i in temp:
if not used[find(i)]:
ans.append([1,i])
used[find(i)]=1
nd-=1
us[i]=1
tf[i]=1
ii=0
while nd and ii<len(temp):
if not us[temp[ii]]:
nd-=1
ans.append([1,temp[ii]])
us[ii]=1
tf[temp[ii]]=1
ii+=1
parent=[i for i in range(n+1)]
other.sort()
need=(n-1)-d
for i in other:
if find(i[0])!=find(i[1]) and need:
ans.append(i)
tf[i[0]]=1
tf[i[1]]=1
need-=1
print("YES")
for i in ans:
print(*i)
``` | instruction | 0 | 60,685 | 13 | 121,370 |
No | output | 1 | 60,685 | 13 | 121,371 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an undirected unweighted connected graph consisting of n vertices and m edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.
Your task is to find any spanning tree of this graph such that the degree of the first vertex (vertex with label 1 on it) is equal to D (or say that there are no such spanning trees). Recall that the degree of a vertex is the number of edges incident to it.
Input
The first line contains three integers n, m and D (2 β€ n β€ 2 β
10^5, n - 1 β€ m β€ min(2 β
10^5, (n(n-1))/(2)), 1 β€ D < n) β the number of vertices, the number of edges and required degree of the first vertex, respectively.
The following m lines denote edges: edge i is represented by a pair of integers v_i, u_i (1 β€ v_i, u_i β€ n, u_i β v_i), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair (v_i, u_i) there are no other pairs (v_i, u_i) or (u_i, v_i) in the list of edges, and for each pair (v_i, u_i) the condition v_i β u_i is satisfied.
Output
If there is no spanning tree satisfying the condition from the problem statement, print "NO" in the first line.
Otherwise print "YES" in the first line and then print n-1 lines describing the edges of a spanning tree such that the degree of the first vertex (vertex with label 1 on it) is equal to D. Make sure that the edges of the printed spanning tree form some subset of the input edges (order doesn't matter and edge (v, u) is considered the same as the edge (u, v)).
If there are multiple possible answers, print any of them.
Examples
Input
4 5 1
1 2
1 3
1 4
2 3
3 4
Output
YES
2 1
2 3
3 4
Input
4 5 3
1 2
1 3
1 4
2 3
3 4
Output
YES
1 2
1 3
4 1
Input
4 4 3
1 2
1 4
2 3
3 4
Output
NO
Note
The picture corresponding to the first and second examples: <image>
The picture corresponding to the third example: <image>
Submitted Solution:
```
import sys
from collections import Counter, defaultdict
def i_ints():
return map(int, sys.stdin.readline().split())
n, m, D = i_ints()
E = defaultdict(set)
for i in range(m):
u, v = i_ints()
E[u].add(v)
E[v].add(u)
def append_edge(u, v):
E[u].discard(v)
E[v].discard(u)
t.add(u)
t.add(v)
te.append((u, v))
def complete_tree(u):
global too_much
todo = {u}
while todo:
u = todo.pop()
starts = list(E[u])
for v in starts:
if v not in t:
if v not in starts:
append_edge(u, v)
todo.add(v)
else:
if too_much > 0:
append_edge(u, v)
todo.add(v)
too_much -= 1
def print_tree():
for u, v in te:
print(u, v)
u0 = 1
t = {u0}
te = []
starts = set(E[u0])
too_much = len(starts) - D
if too_much < 0:
print("NO")
else:
print("YES")
for v in starts:
if v not in t:
append_edge(u0, v)
complete_tree(v)
print_tree()
``` | instruction | 0 | 60,686 | 13 | 121,372 |
No | output | 1 | 60,686 | 13 | 121,373 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an undirected unweighted connected graph consisting of n vertices and m edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.
Your task is to find any spanning tree of this graph such that the degree of the first vertex (vertex with label 1 on it) is equal to D (or say that there are no such spanning trees). Recall that the degree of a vertex is the number of edges incident to it.
Input
The first line contains three integers n, m and D (2 β€ n β€ 2 β
10^5, n - 1 β€ m β€ min(2 β
10^5, (n(n-1))/(2)), 1 β€ D < n) β the number of vertices, the number of edges and required degree of the first vertex, respectively.
The following m lines denote edges: edge i is represented by a pair of integers v_i, u_i (1 β€ v_i, u_i β€ n, u_i β v_i), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair (v_i, u_i) there are no other pairs (v_i, u_i) or (u_i, v_i) in the list of edges, and for each pair (v_i, u_i) the condition v_i β u_i is satisfied.
Output
If there is no spanning tree satisfying the condition from the problem statement, print "NO" in the first line.
Otherwise print "YES" in the first line and then print n-1 lines describing the edges of a spanning tree such that the degree of the first vertex (vertex with label 1 on it) is equal to D. Make sure that the edges of the printed spanning tree form some subset of the input edges (order doesn't matter and edge (v, u) is considered the same as the edge (u, v)).
If there are multiple possible answers, print any of them.
Examples
Input
4 5 1
1 2
1 3
1 4
2 3
3 4
Output
YES
2 1
2 3
3 4
Input
4 5 3
1 2
1 3
1 4
2 3
3 4
Output
YES
1 2
1 3
4 1
Input
4 4 3
1 2
1 4
2 3
3 4
Output
NO
Note
The picture corresponding to the first and second examples: <image>
The picture corresponding to the third example: <image>
Submitted Solution:
```
import sys
from collections import Counter, defaultdict
def i_ints():
return map(int, sys.stdin.readline().split())
n, m, D = i_ints()
E = defaultdict(set)
for i in range(m):
u, v = i_ints()
E[u].add(v)
E[v].add(u)
def append_edge(u, v):
E[u].discard(v)
E[v].discard(u)
t.add(u)
t.add(v)
te.append((u, v))
def complete_tree(u):
global too_much
todo = {u}
while todo:
u = todo.pop()
starts = list(E[u])
for v in starts:
if v not in t:
if v not in starts:
append_edge(u, v)
todo.add(v)
else:
if too_much > 0:
append_edge(u, v)
todo.add(v)
too_much -= 1
def print_tree():
for u, v in te:
print(u, v)
u0 = 1
t = {u0}
te = []
starts = set(E[u0])
too_much = len(starts) - D
if too_much >= 0:
for v in starts:
if v not in t:
append_edge(u0, v)
complete_tree(v)
if not too_much:
print("YES")
print_tree()
else:
print("NO")
``` | instruction | 0 | 60,687 | 13 | 121,374 |
No | output | 1 | 60,687 | 13 | 121,375 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an undirected unweighted connected graph consisting of n vertices and m edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.
Your task is to find any spanning tree of this graph such that the degree of the first vertex (vertex with label 1 on it) is equal to D (or say that there are no such spanning trees). Recall that the degree of a vertex is the number of edges incident to it.
Input
The first line contains three integers n, m and D (2 β€ n β€ 2 β
10^5, n - 1 β€ m β€ min(2 β
10^5, (n(n-1))/(2)), 1 β€ D < n) β the number of vertices, the number of edges and required degree of the first vertex, respectively.
The following m lines denote edges: edge i is represented by a pair of integers v_i, u_i (1 β€ v_i, u_i β€ n, u_i β v_i), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair (v_i, u_i) there are no other pairs (v_i, u_i) or (u_i, v_i) in the list of edges, and for each pair (v_i, u_i) the condition v_i β u_i is satisfied.
Output
If there is no spanning tree satisfying the condition from the problem statement, print "NO" in the first line.
Otherwise print "YES" in the first line and then print n-1 lines describing the edges of a spanning tree such that the degree of the first vertex (vertex with label 1 on it) is equal to D. Make sure that the edges of the printed spanning tree form some subset of the input edges (order doesn't matter and edge (v, u) is considered the same as the edge (u, v)).
If there are multiple possible answers, print any of them.
Examples
Input
4 5 1
1 2
1 3
1 4
2 3
3 4
Output
YES
2 1
2 3
3 4
Input
4 5 3
1 2
1 3
1 4
2 3
3 4
Output
YES
1 2
1 3
4 1
Input
4 4 3
1 2
1 4
2 3
3 4
Output
NO
Note
The picture corresponding to the first and second examples: <image>
The picture corresponding to the third example: <image>
Submitted Solution:
```
from sys import stdin,stdout
from collections import *
from math import ceil, floor , log
st=lambda:list(stdin.readline().strip())
li=lambda:list(map(int,stdin.readline().split()))
mp=lambda:map(int,stdin.readline().split())
inp=lambda:int(stdin.readline())
pr=lambda n: stdout.write(str(n)+"\n")
mod=1000000007
INF=float('inf')
def solve():
def DFS(n):
stack=[n]
v[n]=True
while stack:
a=stack.pop()
for i in d[a]:
if not v[i]:
stack.append(i)
v[i]=True
def BFS(n):
q=deque()
v[n]=True
q.append(n)
while q:
a=q.popleft()
for i in d[a]:
if not v[i]:
print(i,a)
v[i]=True
q.append(i)
n,m,dis=mp()
d={i:[] for i in range(1,n+1)}
for i in range(m):
a,b=mp()
d[a].append(b)
d[b].append(a)
v=[False for i in range(n+1)]
v[1]=True
c=0
for i in range(2,n+1):
if not v[i]:
DFS(i)
c+=1
if c>dis or len(d[1])<dis:
pr('NO')
return
v=[False for i in range(n+1)]
v[1]=True
pr('YES')
for i in d[1]:
if not v[i]:
print(1,i)
BFS(i)
for _ in range(1):
#print('Case',str(_+1)+':')
solve()
``` | instruction | 0 | 60,688 | 13 | 121,376 |
No | output | 1 | 60,688 | 13 | 121,377 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A tree of size n is an undirected connected graph consisting of n vertices without cycles.
Consider some tree with n vertices. We call a tree invariant relative to permutation p = p1p2... pn, if for any two vertices of the tree u and v the condition holds: "vertices u and v are connected by an edge if and only if vertices pu and pv are connected by an edge".
You are given permutation p of size n. Find some tree size n, invariant relative to the given permutation.
Input
The first line contains number n (1 β€ n β€ 105) β the size of the permutation (also equal to the size of the sought tree).
The second line contains permutation pi (1 β€ pi β€ n).
Output
If the sought tree does not exist, print "NO" (without the quotes).
Otherwise, print "YES", and then print n - 1 lines, each of which contains two integers β the numbers of vertices connected by an edge of the tree you found. The vertices are numbered from 1, the order of the edges and the order of the vertices within the edges does not matter.
If there are multiple solutions, output any of them.
Examples
Input
4
4 3 2 1
Output
YES
4 1
4 2
1 3
Input
3
3 1 2
Output
NO
Note
In the first sample test a permutation transforms edge (4, 1) into edge (1, 4), edge (4, 2) into edge (1, 3) and edge (1, 3) into edge (4, 2). These edges all appear in the resulting tree.
It can be shown that in the second sample test no tree satisfies the given condition. | instruction | 0 | 61,107 | 13 | 122,214 |
Tags: constructive algorithms, dfs and similar, greedy, trees
Correct Solution:
```
n = int(input())
a = list(map(lambda x: int(x) - 1, input().split()))
p = q = -1
for i in range(n):
if a[a[i]] == i:
p, q = i, a[i]
if a[i] == i:
print('YES')
[print(i + 1, j + 1) for j in range(n) if i != j]
exit()
if p < 0 or q < 0:
print('NO')
exit()
r = [(p, q)]
v = [0] * n
v[p] = v[q] = 1
for i in range(n):
if not v[i]:
r.append((p, i))
v[i] = 1
t = 0
x = a[i]
while x != i:
r.append((p if t else q, x))
v[x] = 1
t = 1 - t
x = a[x]
if not t:
print('NO')
exit()
print('YES')
[print(x + 1, y + 1) for x, y in r]
``` | output | 1 | 61,107 | 13 | 122,215 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A tree of size n is an undirected connected graph consisting of n vertices without cycles.
Consider some tree with n vertices. We call a tree invariant relative to permutation p = p1p2... pn, if for any two vertices of the tree u and v the condition holds: "vertices u and v are connected by an edge if and only if vertices pu and pv are connected by an edge".
You are given permutation p of size n. Find some tree size n, invariant relative to the given permutation.
Input
The first line contains number n (1 β€ n β€ 105) β the size of the permutation (also equal to the size of the sought tree).
The second line contains permutation pi (1 β€ pi β€ n).
Output
If the sought tree does not exist, print "NO" (without the quotes).
Otherwise, print "YES", and then print n - 1 lines, each of which contains two integers β the numbers of vertices connected by an edge of the tree you found. The vertices are numbered from 1, the order of the edges and the order of the vertices within the edges does not matter.
If there are multiple solutions, output any of them.
Examples
Input
4
4 3 2 1
Output
YES
4 1
4 2
1 3
Input
3
3 1 2
Output
NO
Note
In the first sample test a permutation transforms edge (4, 1) into edge (1, 4), edge (4, 2) into edge (1, 3) and edge (1, 3) into edge (4, 2). These edges all appear in the resulting tree.
It can be shown that in the second sample test no tree satisfies the given condition. | instruction | 0 | 61,108 | 13 | 122,216 |
Tags: constructive algorithms, dfs and similar, greedy, trees
Correct Solution:
```
def main():
n = int(input())
a = list(map(lambda x: int(x) - 1, input().split()))
p = -1
q = -1
for i in range(n):
if a[a[i]] == i:
p, q = i, a[i]
if a[i] == i:
print('YES')
[print(i + 1, j + 1) for j in range(n) if i != j]
exit()
if p < 0 or q < 0:
print('NO'); exit()
ans = [(p, q)]
used = [False] * n
used[p] = True
used[q] = True
for i in range(n):
if used[i]: continue
ans.append((p, i))
used[i] = True
flag = False
x = a[i]
while x != i:
ans.append((p if flag else q, x))
used[x] = True
x = a[x]
flag = not flag
if not flag:
print('NO'); exit()
print('YES')
[print(x + 1, y + 1) for x, y in ans]
main()
``` | output | 1 | 61,108 | 13 | 122,217 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A tree of size n is an undirected connected graph consisting of n vertices without cycles.
Consider some tree with n vertices. We call a tree invariant relative to permutation p = p1p2... pn, if for any two vertices of the tree u and v the condition holds: "vertices u and v are connected by an edge if and only if vertices pu and pv are connected by an edge".
You are given permutation p of size n. Find some tree size n, invariant relative to the given permutation.
Input
The first line contains number n (1 β€ n β€ 105) β the size of the permutation (also equal to the size of the sought tree).
The second line contains permutation pi (1 β€ pi β€ n).
Output
If the sought tree does not exist, print "NO" (without the quotes).
Otherwise, print "YES", and then print n - 1 lines, each of which contains two integers β the numbers of vertices connected by an edge of the tree you found. The vertices are numbered from 1, the order of the edges and the order of the vertices within the edges does not matter.
If there are multiple solutions, output any of them.
Examples
Input
4
4 3 2 1
Output
YES
4 1
4 2
1 3
Input
3
3 1 2
Output
NO
Note
In the first sample test a permutation transforms edge (4, 1) into edge (1, 4), edge (4, 2) into edge (1, 3) and edge (1, 3) into edge (4, 2). These edges all appear in the resulting tree.
It can be shown that in the second sample test no tree satisfies the given condition.
Submitted Solution:
```
print(' '.join(map(str, list(map(lambda x: int(x) + 1, range(int(input())))))))
``` | instruction | 0 | 61,109 | 13 | 122,218 |
No | output | 1 | 61,109 | 13 | 122,219 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Isart and Modsart were trying to solve an interesting problem when suddenly Kasra arrived. Breathless, he asked: "Can you solve a problem I'm stuck at all day?"
We have a tree T with n vertices and m types of ice cream numerated from 1 to m. Each vertex i has a set of si types of ice cream. Vertices which have the i-th (1 β€ i β€ m) type of ice cream form a connected subgraph. We build a new graph G with m vertices. We put an edge between the v-th and the u-th (1 β€ u, v β€ m, u β v) vertices in G if and only if there exists a vertex in T that has both the v-th and the u-th types of ice cream in its set. The problem is to paint the vertices of G with minimum possible number of colors in a way that no adjacent vertices have the same color.
Please note that we consider that empty set of vertices form a connected subgraph in this problem.
As usual, Modsart don't like to abandon the previous problem, so Isart wants you to solve the new problem.
Input
The first line contains two integer n and m (1 β€ n, m β€ 3Β·105) β the number of vertices in T and the number of ice cream types.
n lines follow, the i-th of these lines contain single integer si (0 β€ si β€ 3Β·105) and then si distinct integers, each between 1 and m β the types of ice cream in the i-th vertex. The sum of si doesn't exceed 5Β·105.
n - 1 lines follow. Each of these lines describes an edge of the tree with two integers u and v (1 β€ u, v β€ n) β the indexes of connected by this edge vertices.
Output
Print single integer c in the first line β the minimum number of colors to paint the vertices in graph G.
In the second line print m integers, the i-th of which should be the color of the i-th vertex. The colors should be between 1 and c. If there are some answers, print any of them.
Examples
Input
3 3
1 1
2 2 3
1 2
1 2
2 3
Output
2
1 1 2
Input
4 5
0
1 1
1 3
3 2 4 5
2 1
3 2
4 3
Output
3
1 1 1 2 3
Note
In the first example the first type of ice cream is present in the first vertex only, so we can color it in any color. The second and the third ice cream are both presented in the second vertex, so we should paint them in different colors.
In the second example the colors of the second, the fourth and the fifth ice cream should obviously be distinct. | instruction | 0 | 61,179 | 13 | 122,358 |
Tags: constructive algorithms, dfs and similar, greedy
Correct Solution:
```
from collections import deque
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
src = 0
X = []
for u in range(n):
X.append(list(map(lambda x: int(x) - 1, input().split())))
X[u] = X[u][1:]
if len(X[u]) > len(X[src]):
src = u
E = [[] for u in range(n)]
for j in range(n - 1):
u, v = map(lambda x: int(x) - 1, input().split())
E[u].append(v)
E[v].append(u)
color = [0] * m
vis = [False] * n
used = [False] * (m + 1)
q = deque()
q.append(src)
while q:
u = q.popleft()
vis[u] = True
for x in X[u]:
used[color[x]] = True
ptr = 1
for x in X[u]:
if not color[x]:
while used[ptr]:
ptr += 1
color[x] = ptr
ptr += 1
for x in X[u]:
used[color[x]] = False
for v in E[u]:
if not vis[v]:
q.append(v)
for x in range(m):
if not color[x]:
color[x] = 1
print(max(color))
print(*color)
``` | output | 1 | 61,179 | 13 | 122,359 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Isart and Modsart were trying to solve an interesting problem when suddenly Kasra arrived. Breathless, he asked: "Can you solve a problem I'm stuck at all day?"
We have a tree T with n vertices and m types of ice cream numerated from 1 to m. Each vertex i has a set of si types of ice cream. Vertices which have the i-th (1 β€ i β€ m) type of ice cream form a connected subgraph. We build a new graph G with m vertices. We put an edge between the v-th and the u-th (1 β€ u, v β€ m, u β v) vertices in G if and only if there exists a vertex in T that has both the v-th and the u-th types of ice cream in its set. The problem is to paint the vertices of G with minimum possible number of colors in a way that no adjacent vertices have the same color.
Please note that we consider that empty set of vertices form a connected subgraph in this problem.
As usual, Modsart don't like to abandon the previous problem, so Isart wants you to solve the new problem.
Input
The first line contains two integer n and m (1 β€ n, m β€ 3Β·105) β the number of vertices in T and the number of ice cream types.
n lines follow, the i-th of these lines contain single integer si (0 β€ si β€ 3Β·105) and then si distinct integers, each between 1 and m β the types of ice cream in the i-th vertex. The sum of si doesn't exceed 5Β·105.
n - 1 lines follow. Each of these lines describes an edge of the tree with two integers u and v (1 β€ u, v β€ n) β the indexes of connected by this edge vertices.
Output
Print single integer c in the first line β the minimum number of colors to paint the vertices in graph G.
In the second line print m integers, the i-th of which should be the color of the i-th vertex. The colors should be between 1 and c. If there are some answers, print any of them.
Examples
Input
3 3
1 1
2 2 3
1 2
1 2
2 3
Output
2
1 1 2
Input
4 5
0
1 1
1 3
3 2 4 5
2 1
3 2
4 3
Output
3
1 1 1 2 3
Note
In the first example the first type of ice cream is present in the first vertex only, so we can color it in any color. The second and the third ice cream are both presented in the second vertex, so we should paint them in different colors.
In the second example the colors of the second, the fourth and the fifth ice cream should obviously be distinct.
Submitted Solution:
```
from collections import deque
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
src = 0
X = []
for u in range(n):
X.append(list(map(lambda x: int(x) - 1, input().split())))
X[u] = X[u][1:]
if len(X[u]) > len(X[src]):
src = u
E = [[] for u in range(n)]
for j in range(n - 1):
u, v = map(lambda x: int(x) - 1, input().split())
E[u].append(v)
E[v].append(u)
color = [0] * m
vis = [False] * n
used = [False] * (m + 1)
q = deque()
q.append(src)
while q:
u = q.popleft()
vis[u] = True
for x in X[u]:
used[color[x]] = True
ptr = 1
for x in X[u]:
if not color[x]:
while used[ptr]:
ptr += 1
color[x] = ptr
ptr += 1
for x in X[u]:
used[color[x]] = False
for v in E[u]:
if not vis[v]:
q.append(v)
for x in range(m):
if not color[x]:
color[x] = 1
print(len(X[src]))
print(*color)
``` | instruction | 0 | 61,180 | 13 | 122,360 |
No | output | 1 | 61,180 | 13 | 122,361 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Isart and Modsart were trying to solve an interesting problem when suddenly Kasra arrived. Breathless, he asked: "Can you solve a problem I'm stuck at all day?"
We have a tree T with n vertices and m types of ice cream numerated from 1 to m. Each vertex i has a set of si types of ice cream. Vertices which have the i-th (1 β€ i β€ m) type of ice cream form a connected subgraph. We build a new graph G with m vertices. We put an edge between the v-th and the u-th (1 β€ u, v β€ m, u β v) vertices in G if and only if there exists a vertex in T that has both the v-th and the u-th types of ice cream in its set. The problem is to paint the vertices of G with minimum possible number of colors in a way that no adjacent vertices have the same color.
Please note that we consider that empty set of vertices form a connected subgraph in this problem.
As usual, Modsart don't like to abandon the previous problem, so Isart wants you to solve the new problem.
Input
The first line contains two integer n and m (1 β€ n, m β€ 3Β·105) β the number of vertices in T and the number of ice cream types.
n lines follow, the i-th of these lines contain single integer si (0 β€ si β€ 3Β·105) and then si distinct integers, each between 1 and m β the types of ice cream in the i-th vertex. The sum of si doesn't exceed 5Β·105.
n - 1 lines follow. Each of these lines describes an edge of the tree with two integers u and v (1 β€ u, v β€ n) β the indexes of connected by this edge vertices.
Output
Print single integer c in the first line β the minimum number of colors to paint the vertices in graph G.
In the second line print m integers, the i-th of which should be the color of the i-th vertex. The colors should be between 1 and c. If there are some answers, print any of them.
Examples
Input
3 3
1 1
2 2 3
1 2
1 2
2 3
Output
2
1 1 2
Input
4 5
0
1 1
1 3
3 2 4 5
2 1
3 2
4 3
Output
3
1 1 1 2 3
Note
In the first example the first type of ice cream is present in the first vertex only, so we can color it in any color. The second and the third ice cream are both presented in the second vertex, so we should paint them in different colors.
In the second example the colors of the second, the fourth and the fifth ice cream should obviously be distinct.
Submitted Solution:
```
l = input().split()
n = int(l[0])
m = int(l[1])
icecream = []
ans = 0
for i in range(n):
l = input().split()
for j in range(0, len(l)):
l[j] = int(l[j])
icecream.append(l)
ans = max(ans, l[0])
print(ans)
col = [-1]*m
aval = [True]*ans
for i in range(n):
if icecream[i][0] == ans:
aval = [True]*ans
for j in range(1, icecream[i][0]+1):
if col[icecream[i][j]-1] == -1:
for k in range(ans):
if aval[k]:
col[icecream[i][j]-1] = k
aval[col[icecream[i][j]-1]] = False
break
else:
aval[col[icecream[i][j]-1]] = False
break
for i in range(n):
if icecream[i][0] != ans:
aval = [True]*ans
for j in range(1, icecream[i][0]+1):
if col[icecream[i][j]-1] != -1:
aval[col[icecream[i][j]-1]] = False
for j in range(1, icecream[i][0]+1):
if col[icecream[i][j]-1] == -1:
for k in range(ans):
if aval[k]:
col[icecream[i][j]-1] = k
aval[col[icecream[i][j]-1]] = False
break
for aa in col:
print(aa+1, end=' ')
``` | instruction | 0 | 61,181 | 13 | 122,362 |
No | output | 1 | 61,181 | 13 | 122,363 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Isart and Modsart were trying to solve an interesting problem when suddenly Kasra arrived. Breathless, he asked: "Can you solve a problem I'm stuck at all day?"
We have a tree T with n vertices and m types of ice cream numerated from 1 to m. Each vertex i has a set of si types of ice cream. Vertices which have the i-th (1 β€ i β€ m) type of ice cream form a connected subgraph. We build a new graph G with m vertices. We put an edge between the v-th and the u-th (1 β€ u, v β€ m, u β v) vertices in G if and only if there exists a vertex in T that has both the v-th and the u-th types of ice cream in its set. The problem is to paint the vertices of G with minimum possible number of colors in a way that no adjacent vertices have the same color.
Please note that we consider that empty set of vertices form a connected subgraph in this problem.
As usual, Modsart don't like to abandon the previous problem, so Isart wants you to solve the new problem.
Input
The first line contains two integer n and m (1 β€ n, m β€ 3Β·105) β the number of vertices in T and the number of ice cream types.
n lines follow, the i-th of these lines contain single integer si (0 β€ si β€ 3Β·105) and then si distinct integers, each between 1 and m β the types of ice cream in the i-th vertex. The sum of si doesn't exceed 5Β·105.
n - 1 lines follow. Each of these lines describes an edge of the tree with two integers u and v (1 β€ u, v β€ n) β the indexes of connected by this edge vertices.
Output
Print single integer c in the first line β the minimum number of colors to paint the vertices in graph G.
In the second line print m integers, the i-th of which should be the color of the i-th vertex. The colors should be between 1 and c. If there are some answers, print any of them.
Examples
Input
3 3
1 1
2 2 3
1 2
1 2
2 3
Output
2
1 1 2
Input
4 5
0
1 1
1 3
3 2 4 5
2 1
3 2
4 3
Output
3
1 1 1 2 3
Note
In the first example the first type of ice cream is present in the first vertex only, so we can color it in any color. The second and the third ice cream are both presented in the second vertex, so we should paint them in different colors.
In the second example the colors of the second, the fourth and the fifth ice cream should obviously be distinct.
Submitted Solution:
```
""" Created by Shahen Kosyan on 5/4/17 """
if __name__ == "__main__":
s = input()
total = 0
i = 0
while True:
if i == len(s) - 2:
if s[i] == 'a' and s[i + 1] == 'b':
s = s[0:i] + 'bba' + s[i + 2:len(s)]
total += 1
break
if s[i] == 'a' and s[i + 1] == 'b':
s = s[0:i] + 'bba' + s[i + 2:len(s)]
total += 1
i += 1
if s[i] == 'a' and s[i + 1] == 'a' and s[i + 2] == 'b':
s = s[0:i] + 'bbbba' + s[i + 3: len(s)]
total += 3
i += 2
if s[i] == 'b' and s[i + 1] == 'a' and s[i + 2] == 'b':
s = s[0:i] + 'bbba' + s[i + 3: len(s)]
total += 1
i += 2
i += 1
print(total % 1000000007)
``` | instruction | 0 | 61,182 | 13 | 122,364 |
No | output | 1 | 61,182 | 13 | 122,365 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Isart and Modsart were trying to solve an interesting problem when suddenly Kasra arrived. Breathless, he asked: "Can you solve a problem I'm stuck at all day?"
We have a tree T with n vertices and m types of ice cream numerated from 1 to m. Each vertex i has a set of si types of ice cream. Vertices which have the i-th (1 β€ i β€ m) type of ice cream form a connected subgraph. We build a new graph G with m vertices. We put an edge between the v-th and the u-th (1 β€ u, v β€ m, u β v) vertices in G if and only if there exists a vertex in T that has both the v-th and the u-th types of ice cream in its set. The problem is to paint the vertices of G with minimum possible number of colors in a way that no adjacent vertices have the same color.
Please note that we consider that empty set of vertices form a connected subgraph in this problem.
As usual, Modsart don't like to abandon the previous problem, so Isart wants you to solve the new problem.
Input
The first line contains two integer n and m (1 β€ n, m β€ 3Β·105) β the number of vertices in T and the number of ice cream types.
n lines follow, the i-th of these lines contain single integer si (0 β€ si β€ 3Β·105) and then si distinct integers, each between 1 and m β the types of ice cream in the i-th vertex. The sum of si doesn't exceed 5Β·105.
n - 1 lines follow. Each of these lines describes an edge of the tree with two integers u and v (1 β€ u, v β€ n) β the indexes of connected by this edge vertices.
Output
Print single integer c in the first line β the minimum number of colors to paint the vertices in graph G.
In the second line print m integers, the i-th of which should be the color of the i-th vertex. The colors should be between 1 and c. If there are some answers, print any of them.
Examples
Input
3 3
1 1
2 2 3
1 2
1 2
2 3
Output
2
1 1 2
Input
4 5
0
1 1
1 3
3 2 4 5
2 1
3 2
4 3
Output
3
1 1 1 2 3
Note
In the first example the first type of ice cream is present in the first vertex only, so we can color it in any color. The second and the third ice cream are both presented in the second vertex, so we should paint them in different colors.
In the second example the colors of the second, the fourth and the fifth ice cream should obviously be distinct.
Submitted Solution:
```
from collections import deque
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
src = 0
X = []
for u in range(n):
X.append(list(map(lambda x: int(x) - 1, input().split())))
X[u] = X[u][1:]
if len(X[u]) > len(X[src]):
src = u
E = [[] for u in range(n)]
for j in range(n - 1):
u, v = map(lambda x: int(x) - 1, input().split())
E[u].append(v)
E[v].append(u)
color = [0] * m
vis = [False] * n
used = [False] * (m + 1)
q = deque()
q.append(src)
while q:
u = q.popleft()
vis[u] = True
for x in X[u]:
used[color[x]] = True
ptr = 1
for x in X[u]:
if not color[x]:
while used[ptr]:
ptr += 1
color[x] = ptr
ptr += 1
for x in X[u]:
used[color[x]] = False
for v in E[u]:
if not vis[v]:
q.append(v)
print(len(X[src]))
print(*color)
``` | instruction | 0 | 61,183 | 13 | 122,366 |
No | output | 1 | 61,183 | 13 | 122,367 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence of n positive integers d1, d2, ..., dn (d1 < d2 < ... < dn). Your task is to construct an undirected graph such that:
* there are exactly dn + 1 vertices;
* there are no self-loops;
* there are no multiple edges;
* there are no more than 106 edges;
* its degree set is equal to d.
Vertices should be numbered 1 through (dn + 1).
Degree sequence is an array a with length equal to the number of vertices in a graph such that ai is the number of vertices adjacent to i-th vertex.
Degree set is a sorted in increasing order sequence of all distinct values from the degree sequence.
It is guaranteed that there exists such a graph that all the conditions hold, and it contains no more than 106 edges.
Print the resulting graph.
Input
The first line contains one integer n (1 β€ n β€ 300) β the size of the degree set.
The second line contains n integers d1, d2, ..., dn (1 β€ di β€ 1000, d1 < d2 < ... < dn) β the degree set.
Output
In the first line print one integer m (1 β€ m β€ 106) β the number of edges in the resulting graph. It is guaranteed that there exists such a graph that all the conditions hold and it contains no more than 106 edges.
Each of the next m lines should contain two integers vi and ui (1 β€ vi, ui β€ dn + 1) β the description of the i-th edge.
Examples
Input
3
2 3 4
Output
8
3 1
4 2
4 5
2 5
5 1
3 2
2 1
5 3
Input
3
1 2 3
Output
4
1 2
1 3
1 4
2 3 | instruction | 0 | 61,227 | 13 | 122,454 |
Tags: constructive algorithms, graphs, implementation
Correct Solution:
```
import sys
line=lambda:sys.stdin.buffer.readline()
n=int(line())
d=[0]+list(map(int,line().split()))
s=[]
for i in range(1,n+1):
for u in range(d[i-1]+1,d[i]+1):
for v in range(u+1,d[n-i+1]+2):
s.append((u,v))
print(len(s))
for t in s: print(*t)
``` | output | 1 | 61,227 | 13 | 122,455 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence of n positive integers d1, d2, ..., dn (d1 < d2 < ... < dn). Your task is to construct an undirected graph such that:
* there are exactly dn + 1 vertices;
* there are no self-loops;
* there are no multiple edges;
* there are no more than 106 edges;
* its degree set is equal to d.
Vertices should be numbered 1 through (dn + 1).
Degree sequence is an array a with length equal to the number of vertices in a graph such that ai is the number of vertices adjacent to i-th vertex.
Degree set is a sorted in increasing order sequence of all distinct values from the degree sequence.
It is guaranteed that there exists such a graph that all the conditions hold, and it contains no more than 106 edges.
Print the resulting graph.
Input
The first line contains one integer n (1 β€ n β€ 300) β the size of the degree set.
The second line contains n integers d1, d2, ..., dn (1 β€ di β€ 1000, d1 < d2 < ... < dn) β the degree set.
Output
In the first line print one integer m (1 β€ m β€ 106) β the number of edges in the resulting graph. It is guaranteed that there exists such a graph that all the conditions hold and it contains no more than 106 edges.
Each of the next m lines should contain two integers vi and ui (1 β€ vi, ui β€ dn + 1) β the description of the i-th edge.
Examples
Input
3
2 3 4
Output
8
3 1
4 2
4 5
2 5
5 1
3 2
2 1
5 3
Input
3
1 2 3
Output
4
1 2
1 3
1 4
2 3 | instruction | 0 | 61,228 | 13 | 122,456 |
Tags: constructive algorithms, graphs, implementation
Correct Solution:
```
def readline(): return list(map(int, input().split()))
def SolveDegreeSet(DegreSet, n):
edges = []
verticesCount = 0
if(n == 0):
verticesCount = 1
return edges, verticesCount
if(n == 1):
verticesCount = DegreSet[0]+1
#print(verticesCount)
for i in range(1,verticesCount + 1):
for j in range(i + 1,verticesCount + 1):
edges.append([i,j])
#print(edges)
return edges , verticesCount
newDegreeSet = []
for i in range(1,n - 1):
newDegreeSet.append(DegreSet[i ]- DegreSet[0])
#print(newDegreeSet)
prevSolveDegreeSet = SolveDegreeSet(newDegreeSet, n - 2)
verticesCount = prevSolveDegreeSet[1]
edges = prevSolveDegreeSet[0]
#print(edges)
#print(verticesCount)
verticesCount += (DegreSet[n-1] - DegreSet[n-2])
#print(verticesCount)
for i in range(0, DegreSet[0]):
verticesCount += 1
for j in range(1, verticesCount):
edges.append([j, verticesCount])
#print (edges)
#print(verticesCount)
return edges, verticesCount
n, = readline()
d = readline()
par = list(SolveDegreeSet(d, n))
edges = par[0]
#print( edges)
print(len(edges))
print("\n".join(map("{0[0]} {0[1]}".format, edges)))
``` | output | 1 | 61,228 | 13 | 122,457 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence of n positive integers d1, d2, ..., dn (d1 < d2 < ... < dn). Your task is to construct an undirected graph such that:
* there are exactly dn + 1 vertices;
* there are no self-loops;
* there are no multiple edges;
* there are no more than 106 edges;
* its degree set is equal to d.
Vertices should be numbered 1 through (dn + 1).
Degree sequence is an array a with length equal to the number of vertices in a graph such that ai is the number of vertices adjacent to i-th vertex.
Degree set is a sorted in increasing order sequence of all distinct values from the degree sequence.
It is guaranteed that there exists such a graph that all the conditions hold, and it contains no more than 106 edges.
Print the resulting graph.
Input
The first line contains one integer n (1 β€ n β€ 300) β the size of the degree set.
The second line contains n integers d1, d2, ..., dn (1 β€ di β€ 1000, d1 < d2 < ... < dn) β the degree set.
Output
In the first line print one integer m (1 β€ m β€ 106) β the number of edges in the resulting graph. It is guaranteed that there exists such a graph that all the conditions hold and it contains no more than 106 edges.
Each of the next m lines should contain two integers vi and ui (1 β€ vi, ui β€ dn + 1) β the description of the i-th edge.
Examples
Input
3
2 3 4
Output
8
3 1
4 2
4 5
2 5
5 1
3 2
2 1
5 3
Input
3
1 2 3
Output
4
1 2
1 3
1 4
2 3 | instruction | 0 | 61,229 | 13 | 122,458 |
Tags: constructive algorithms, graphs, implementation
Correct Solution:
```
# python3
def readline(): return list(map(int, input().split()))
def solve(d):
while d:
dn = d.pop()
if not d:
for i in range(1, dn + 1):
for j in range(i, dn + 1):
yield i, j + 1
return
else:
d1 = d.pop(0)
for i in range(1, dn + 1):
for j in range(max(dn - d1 + 1, i), dn + 1):
yield i, j + 1
d = [di - d1 for di in d]
def main():
n, = readline()
d = readline()
assert len(d) == n
edges = list(solve(d))
print(len(edges))
print("\n".join(map("{0[0]} {0[1]}".format, edges)))
main()
``` | output | 1 | 61,229 | 13 | 122,459 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence of n positive integers d1, d2, ..., dn (d1 < d2 < ... < dn). Your task is to construct an undirected graph such that:
* there are exactly dn + 1 vertices;
* there are no self-loops;
* there are no multiple edges;
* there are no more than 106 edges;
* its degree set is equal to d.
Vertices should be numbered 1 through (dn + 1).
Degree sequence is an array a with length equal to the number of vertices in a graph such that ai is the number of vertices adjacent to i-th vertex.
Degree set is a sorted in increasing order sequence of all distinct values from the degree sequence.
It is guaranteed that there exists such a graph that all the conditions hold, and it contains no more than 106 edges.
Print the resulting graph.
Input
The first line contains one integer n (1 β€ n β€ 300) β the size of the degree set.
The second line contains n integers d1, d2, ..., dn (1 β€ di β€ 1000, d1 < d2 < ... < dn) β the degree set.
Output
In the first line print one integer m (1 β€ m β€ 106) β the number of edges in the resulting graph. It is guaranteed that there exists such a graph that all the conditions hold and it contains no more than 106 edges.
Each of the next m lines should contain two integers vi and ui (1 β€ vi, ui β€ dn + 1) β the description of the i-th edge.
Examples
Input
3
2 3 4
Output
8
3 1
4 2
4 5
2 5
5 1
3 2
2 1
5 3
Input
3
1 2 3
Output
4
1 2
1 3
1 4
2 3 | instruction | 0 | 61,230 | 13 | 122,460 |
Tags: constructive algorithms, graphs, implementation
Correct Solution:
```
import sys
line=lambda:sys.stdin.buffer.readline()
n=int(line())
d=[0]+list(map(int,line().split()))
s={}
for i in range(1,n+1):
for u in range(d[i],d[i-1],-1):
for v in range(d[n-i+1]+1,u,-1):
s[(u,v)]=1
print(len(s))
for t in s: print(*t)
``` | output | 1 | 61,230 | 13 | 122,461 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence of n positive integers d1, d2, ..., dn (d1 < d2 < ... < dn). Your task is to construct an undirected graph such that:
* there are exactly dn + 1 vertices;
* there are no self-loops;
* there are no multiple edges;
* there are no more than 106 edges;
* its degree set is equal to d.
Vertices should be numbered 1 through (dn + 1).
Degree sequence is an array a with length equal to the number of vertices in a graph such that ai is the number of vertices adjacent to i-th vertex.
Degree set is a sorted in increasing order sequence of all distinct values from the degree sequence.
It is guaranteed that there exists such a graph that all the conditions hold, and it contains no more than 106 edges.
Print the resulting graph.
Input
The first line contains one integer n (1 β€ n β€ 300) β the size of the degree set.
The second line contains n integers d1, d2, ..., dn (1 β€ di β€ 1000, d1 < d2 < ... < dn) β the degree set.
Output
In the first line print one integer m (1 β€ m β€ 106) β the number of edges in the resulting graph. It is guaranteed that there exists such a graph that all the conditions hold and it contains no more than 106 edges.
Each of the next m lines should contain two integers vi and ui (1 β€ vi, ui β€ dn + 1) β the description of the i-th edge.
Examples
Input
3
2 3 4
Output
8
3 1
4 2
4 5
2 5
5 1
3 2
2 1
5 3
Input
3
1 2 3
Output
4
1 2
1 3
1 4
2 3 | instruction | 0 | 61,231 | 13 | 122,462 |
Tags: constructive algorithms, graphs, implementation
Correct Solution:
```
from sys import stdin
from sys import stdout
n = int(stdin.readline())
d = [0] + list(map(int, stdin.readline().split()))
e = []
for i in range(1, n+1):
for u in range(d[i-1]+1, d[i]+1):
for v in range(u+1, d[n-i+1]+2):
e.append([u,v])
stdout.write("{}\n".format(len(e)))
for ei in e: stdout.write("{} {}\n".format(ei[0], ei[1]))
``` | output | 1 | 61,231 | 13 | 122,463 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence of n positive integers d1, d2, ..., dn (d1 < d2 < ... < dn). Your task is to construct an undirected graph such that:
* there are exactly dn + 1 vertices;
* there are no self-loops;
* there are no multiple edges;
* there are no more than 106 edges;
* its degree set is equal to d.
Vertices should be numbered 1 through (dn + 1).
Degree sequence is an array a with length equal to the number of vertices in a graph such that ai is the number of vertices adjacent to i-th vertex.
Degree set is a sorted in increasing order sequence of all distinct values from the degree sequence.
It is guaranteed that there exists such a graph that all the conditions hold, and it contains no more than 106 edges.
Print the resulting graph.
Input
The first line contains one integer n (1 β€ n β€ 300) β the size of the degree set.
The second line contains n integers d1, d2, ..., dn (1 β€ di β€ 1000, d1 < d2 < ... < dn) β the degree set.
Output
In the first line print one integer m (1 β€ m β€ 106) β the number of edges in the resulting graph. It is guaranteed that there exists such a graph that all the conditions hold and it contains no more than 106 edges.
Each of the next m lines should contain two integers vi and ui (1 β€ vi, ui β€ dn + 1) β the description of the i-th edge.
Examples
Input
3
2 3 4
Output
8
3 1
4 2
4 5
2 5
5 1
3 2
2 1
5 3
Input
3
1 2 3
Output
4
1 2
1 3
1 4
2 3
Submitted Solution:
```
n = int(input())
D = list(map(int, input().split()))
now_v = 1
ind_d = n - 1
minus = 0
A = []
while D[ind_d] > minus:
for i in range(D[ind_d] - minus):
A.append([now_v, now_v + i + 1])
now_v += 1
ind_d -= 1
minus += 1
print(len(A))
for u, v in A:
print(u, v)
``` | instruction | 0 | 61,232 | 13 | 122,464 |
No | output | 1 | 61,232 | 13 | 122,465 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence of n positive integers d1, d2, ..., dn (d1 < d2 < ... < dn). Your task is to construct an undirected graph such that:
* there are exactly dn + 1 vertices;
* there are no self-loops;
* there are no multiple edges;
* there are no more than 106 edges;
* its degree set is equal to d.
Vertices should be numbered 1 through (dn + 1).
Degree sequence is an array a with length equal to the number of vertices in a graph such that ai is the number of vertices adjacent to i-th vertex.
Degree set is a sorted in increasing order sequence of all distinct values from the degree sequence.
It is guaranteed that there exists such a graph that all the conditions hold, and it contains no more than 106 edges.
Print the resulting graph.
Input
The first line contains one integer n (1 β€ n β€ 300) β the size of the degree set.
The second line contains n integers d1, d2, ..., dn (1 β€ di β€ 1000, d1 < d2 < ... < dn) β the degree set.
Output
In the first line print one integer m (1 β€ m β€ 106) β the number of edges in the resulting graph. It is guaranteed that there exists such a graph that all the conditions hold and it contains no more than 106 edges.
Each of the next m lines should contain two integers vi and ui (1 β€ vi, ui β€ dn + 1) β the description of the i-th edge.
Examples
Input
3
2 3 4
Output
8
3 1
4 2
4 5
2 5
5 1
3 2
2 1
5 3
Input
3
1 2 3
Output
4
1 2
1 3
1 4
2 3
Submitted Solution:
```
def readline(): return list(map(int, input().split()))
def SolveDegreeSet(DegreSet, n):
edges = []
verticesCount = 0
if(n == 0):
verticesCount = 1
return edges, verticesCount
if(n == 1):
verticesCount = DegreSet[0]+1
#print(verticesCount)
for i in range(2,verticesCount + 1):
edges.append([1,i])
#print(edges)
return edges , verticesCount
newDegreeSet = []
for i in range(1,n - 1):
newDegreeSet.append(DegreSet[i ]- DegreSet[0])
#print(newDegreeSet)
prevSolveDegreeSet = SolveDegreeSet(newDegreeSet, n - 2)
verticesCount = prevSolveDegreeSet[1]
edges = prevSolveDegreeSet[0]
#print(edges)
#print(verticesCount)
verticesCount += (DegreSet[n-1] - DegreSet[n-2])
#print(verticesCount)
for i in range(0, DegreSet[0]):
verticesCount += 1
for j in range(1, verticesCount):
edges.append([j, verticesCount])
#print (edges)
#print(verticesCount)
return edges, verticesCount
n, = readline()
d = readline()
par = list(SolveDegreeSet(d, n))
edges = par[0]
print( edges)
print(len(edges))
print("\n".join(map("{0[0]} {0[1]}".format, edges)))
``` | instruction | 0 | 61,233 | 13 | 122,466 |
No | output | 1 | 61,233 | 13 | 122,467 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence of n positive integers d1, d2, ..., dn (d1 < d2 < ... < dn). Your task is to construct an undirected graph such that:
* there are exactly dn + 1 vertices;
* there are no self-loops;
* there are no multiple edges;
* there are no more than 106 edges;
* its degree set is equal to d.
Vertices should be numbered 1 through (dn + 1).
Degree sequence is an array a with length equal to the number of vertices in a graph such that ai is the number of vertices adjacent to i-th vertex.
Degree set is a sorted in increasing order sequence of all distinct values from the degree sequence.
It is guaranteed that there exists such a graph that all the conditions hold, and it contains no more than 106 edges.
Print the resulting graph.
Input
The first line contains one integer n (1 β€ n β€ 300) β the size of the degree set.
The second line contains n integers d1, d2, ..., dn (1 β€ di β€ 1000, d1 < d2 < ... < dn) β the degree set.
Output
In the first line print one integer m (1 β€ m β€ 106) β the number of edges in the resulting graph. It is guaranteed that there exists such a graph that all the conditions hold and it contains no more than 106 edges.
Each of the next m lines should contain two integers vi and ui (1 β€ vi, ui β€ dn + 1) β the description of the i-th edge.
Examples
Input
3
2 3 4
Output
8
3 1
4 2
4 5
2 5
5 1
3 2
2 1
5 3
Input
3
1 2 3
Output
4
1 2
1 3
1 4
2 3
Submitted Solution:
```
def readline(): return list(map(int, input().split()))
def SolveDegreeSet(DegreSet, n):
edges = []
verticesCount = 0
if(n == 0):
verticesCount = 1
return edges, verticesCount
if(n == 1):
verticesCount = DegreSet[0]+1
#print(verticesCount)
for i in range(2,verticesCount + 1):
edges.append([1,i])
#print(edges)
return edges , verticesCount
newDegreeSet = []
for i in range(1,n - 1):
newDegreeSet.append(DegreSet[i ]- DegreSet[0])
#print(newDegreeSet)
prevSolveDegreeSet = SolveDegreeSet(newDegreeSet, n - 2)
verticesCount = prevSolveDegreeSet[1]
edges = prevSolveDegreeSet[0]
#print(edges)
#print(verticesCount)
verticesCount += (DegreSet[n-1] - DegreSet[n-2])
#print(verticesCount)
for i in range(0, DegreSet[0]):
verticesCount += 1
for j in range(1, verticesCount):
edges.append([j, verticesCount])
#print (edges)
#print(verticesCount)
return edges, verticesCount
n, = readline()
d = readline()
par = list(SolveDegreeSet(d, n))
edges = par[0]
#print( edges)
print(len(edges))
print("\n".join(map("{0[0]} {0[1]}".format, edges)))
``` | instruction | 0 | 61,234 | 13 | 122,468 |
No | output | 1 | 61,234 | 13 | 122,469 |
Provide a correct Python 3 solution for this coding contest problem.
We have a rooted binary tree with N vertices, where the vertices are numbered 1 to N. Vertex 1 is the root, and the parent of Vertex i (i \geq 2) is Vertex \left[ \frac{i}{2} \right].
Each vertex has one item in it. The item in Vertex i has a value of V_i and a weight of W_i. Now, process the following query Q times:
* Given are a vertex v of the tree and a positive integer L. Let us choose some (possibly none) of the items in v and the ancestors of v so that their total weight is at most L. Find the maximum possible total value of the chosen items.
Here, Vertex u is said to be an ancestor of Vertex v when u is an indirect parent of v, that is, there exists a sequence of vertices w_1,w_2,\ldots,w_k (k\geq 2) where w_1=v, w_k=u, and w_{i+1} is the parent of w_i for each i.
Constraints
* All values in input are integers.
* 1 \leq N < 2^{18}
* 1 \leq Q \leq 10^5
* 1 \leq V_i \leq 10^5
* 1 \leq W_i \leq 10^5
* For the values v and L given in each query, 1 \leq v \leq N and 1 \leq L \leq 10^5.
Input
Let v_i and L_i be the values v and L given in the i-th query. Then, Input is given from Standard Input in the following format:
N
V_1 W_1
:
V_N W_N
Q
v_1 L_1
:
v_Q L_Q
Output
For each integer i from 1 through Q, the i-th line should contain the response to the i-th query.
Examples
Input
3
1 2
2 3
3 4
3
1 1
2 5
3 5
Output
0
3
3
Input
15
123 119
129 120
132 112
126 109
118 103
115 109
102 100
130 120
105 105
132 115
104 102
107 107
127 116
121 104
121 115
8
8 234
9 244
10 226
11 227
12 240
13 237
14 206
15 227
Output
256
255
250
247
255
259
223
253 | instruction | 0 | 61,251 | 13 | 122,502 |
"Correct Solution:
```
import sys
n, *inp = map(int, sys.stdin.buffer.read().split())
vvv = [0] + inp[0:n * 2:2]
www = [0] + inp[1:n * 2:2]
weight_limit = 10 ** 5
precalc_limit = min(1 << 10, n + 1)
precalc = [[0] * (weight_limit + 1)]
for u in range(1, precalc_limit):
v = vvv[u]
w = www[u]
dp = precalc[u >> 1][:]
for x in range(weight_limit, w - 1, -1):
nv = dp[x - w] + v
if dp[x] < nv:
dp[x] = nv
precalc.append(dp)
buf = []
mp = iter(inp[n * 2 + 1:])
for u, l in zip(mp, mp):
if u < precalc_limit:
buf.append(precalc[u][l])
continue
dp_w = [0]
dp_v = [0]
while u >= precalc_limit:
v = vvv[u]
w = www[u]
for i in range(len(dp_w)):
nw = dp_w[i] + w
if nw > l:
continue
nv = dp_v[i] + v
dp_w.append(nw)
dp_v.append(nv)
u >>= 1
ans = 0
for w, v in zip(dp_w, dp_v):
nv = v + precalc[u][l - w]
if ans < nv:
ans = nv
buf.append(ans)
print('\n'.join(map(str, buf)))
``` | output | 1 | 61,251 | 13 | 122,503 |
Provide a correct Python 3 solution for this coding contest problem.
We have a rooted binary tree with N vertices, where the vertices are numbered 1 to N. Vertex 1 is the root, and the parent of Vertex i (i \geq 2) is Vertex \left[ \frac{i}{2} \right].
Each vertex has one item in it. The item in Vertex i has a value of V_i and a weight of W_i. Now, process the following query Q times:
* Given are a vertex v of the tree and a positive integer L. Let us choose some (possibly none) of the items in v and the ancestors of v so that their total weight is at most L. Find the maximum possible total value of the chosen items.
Here, Vertex u is said to be an ancestor of Vertex v when u is an indirect parent of v, that is, there exists a sequence of vertices w_1,w_2,\ldots,w_k (k\geq 2) where w_1=v, w_k=u, and w_{i+1} is the parent of w_i for each i.
Constraints
* All values in input are integers.
* 1 \leq N < 2^{18}
* 1 \leq Q \leq 10^5
* 1 \leq V_i \leq 10^5
* 1 \leq W_i \leq 10^5
* For the values v and L given in each query, 1 \leq v \leq N and 1 \leq L \leq 10^5.
Input
Let v_i and L_i be the values v and L given in the i-th query. Then, Input is given from Standard Input in the following format:
N
V_1 W_1
:
V_N W_N
Q
v_1 L_1
:
v_Q L_Q
Output
For each integer i from 1 through Q, the i-th line should contain the response to the i-th query.
Examples
Input
3
1 2
2 3
3 4
3
1 1
2 5
3 5
Output
0
3
3
Input
15
123 119
129 120
132 112
126 109
118 103
115 109
102 100
130 120
105 105
132 115
104 102
107 107
127 116
121 104
121 115
8
8 234
9 244
10 226
11 227
12 240
13 237
14 206
15 227
Output
256
255
250
247
255
259
223
253 | instruction | 0 | 61,252 | 13 | 122,504 |
"Correct Solution:
```
import sys
MX = 10**5
def main():
data = map(int, sys.stdin.buffer.read().split())
N = next(data)
H = min(N + 1, 1 << 10)
dp = [None] * H
dp[0] = [0] * (MX + 1)
IV, IW = [], []
i = 1
for v, w in zip(data, data):
if i < H:
dpi = dp[i >> 1][:]
dp[i] = dpi
for j in range(MX, w - 1, -1):
dpi[j] = max(dpi[j], dpi[j - w] + v)
else:
IV.append(v)
IW.append(w)
i += 1
if i > N:
break
Q = next(data)
for v, L in zip(data, data):
if v < H:
print(dp[v][L])
continue
item_values = []
item_weights = []
while v >= H:
item_values.append(IV[v - H])
item_weights.append(IW[v - H])
v >>= 1
l = len(item_values)
V = [0] * (1 << l)
W = [0] * (1 << l)
ans = dp[v][L]
for i in range(1, 1 << l):
msb = i & (-i)
msbb = msb.bit_length() - 1
wi = W[i - msb] + item_weights[msbb]
W[i] = wi
if wi <= L:
vi = V[i - msb] + item_values[msbb]
ans = max(ans, vi + dp[v][L - wi])
V[i] = vi
print(ans)
if __name__ == '__main__':
main()
``` | output | 1 | 61,252 | 13 | 122,505 |
Provide a correct Python 3 solution for this coding contest problem.
We have a rooted binary tree with N vertices, where the vertices are numbered 1 to N. Vertex 1 is the root, and the parent of Vertex i (i \geq 2) is Vertex \left[ \frac{i}{2} \right].
Each vertex has one item in it. The item in Vertex i has a value of V_i and a weight of W_i. Now, process the following query Q times:
* Given are a vertex v of the tree and a positive integer L. Let us choose some (possibly none) of the items in v and the ancestors of v so that their total weight is at most L. Find the maximum possible total value of the chosen items.
Here, Vertex u is said to be an ancestor of Vertex v when u is an indirect parent of v, that is, there exists a sequence of vertices w_1,w_2,\ldots,w_k (k\geq 2) where w_1=v, w_k=u, and w_{i+1} is the parent of w_i for each i.
Constraints
* All values in input are integers.
* 1 \leq N < 2^{18}
* 1 \leq Q \leq 10^5
* 1 \leq V_i \leq 10^5
* 1 \leq W_i \leq 10^5
* For the values v and L given in each query, 1 \leq v \leq N and 1 \leq L \leq 10^5.
Input
Let v_i and L_i be the values v and L given in the i-th query. Then, Input is given from Standard Input in the following format:
N
V_1 W_1
:
V_N W_N
Q
v_1 L_1
:
v_Q L_Q
Output
For each integer i from 1 through Q, the i-th line should contain the response to the i-th query.
Examples
Input
3
1 2
2 3
3 4
3
1 1
2 5
3 5
Output
0
3
3
Input
15
123 119
129 120
132 112
126 109
118 103
115 109
102 100
130 120
105 105
132 115
104 102
107 107
127 116
121 104
121 115
8
8 234
9 244
10 226
11 227
12 240
13 237
14 206
15 227
Output
256
255
250
247
255
259
223
253 | instruction | 0 | 61,253 | 13 | 122,506 |
"Correct Solution:
```
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
def merge(a, b):
res = []
i, j = 0, 0
while i < len(a) or j < len(b):
if j >= len(b) or (i < len(a) and a[i][0] < b[j][0]):
w, v = a[i]
i += 1
else:
w, v = b[j]
j += 1
if not res or res[-1][1] < v:
res.append((w, v))
elif res[-1][0] == w and res[-1][1] <= v:
res.pop()
res.append((w, v))
return res
def best(L, a, b):
res = 0
i = 0
j = len(b) - 1
while i < len(a):
w, v = a[i]
if j < 0:
break
if b[j][0] + w > L:
j -= 1
continue
res = max(res, v + b[j][1])
i += 1
return res
def main():
N = int(input())
VW = [(-1, -1)] + [list(map(int, input().split())) for _ in range(N)]
Q = int(input())
query = tuple(tuple(map(int, input().split())) for _ in range(Q))
dp = [[] for _ in range(2048)]
dp[0] = [(0, 0)]
for i in range(1, min(N + 1, 2048)):
vi, wi = VW[i]
a1 = dp[i // 2]
a2 = [(w + wi, v + vi) for w, v in a1]
dp[i] = merge(a1, a2)
for n, L in query:
a = [(0, 0)]
while n >= 2048:
vi, wi = VW[n]
b = [(w + wi, v + vi) for w, v in a]
a = merge(a, b)
n //= 2
print(best(L, a, dp[n]))
if __name__ == "__main__":
main()
``` | output | 1 | 61,253 | 13 | 122,507 |
Provide a correct Python 3 solution for this coding contest problem.
We have a rooted binary tree with N vertices, where the vertices are numbered 1 to N. Vertex 1 is the root, and the parent of Vertex i (i \geq 2) is Vertex \left[ \frac{i}{2} \right].
Each vertex has one item in it. The item in Vertex i has a value of V_i and a weight of W_i. Now, process the following query Q times:
* Given are a vertex v of the tree and a positive integer L. Let us choose some (possibly none) of the items in v and the ancestors of v so that their total weight is at most L. Find the maximum possible total value of the chosen items.
Here, Vertex u is said to be an ancestor of Vertex v when u is an indirect parent of v, that is, there exists a sequence of vertices w_1,w_2,\ldots,w_k (k\geq 2) where w_1=v, w_k=u, and w_{i+1} is the parent of w_i for each i.
Constraints
* All values in input are integers.
* 1 \leq N < 2^{18}
* 1 \leq Q \leq 10^5
* 1 \leq V_i \leq 10^5
* 1 \leq W_i \leq 10^5
* For the values v and L given in each query, 1 \leq v \leq N and 1 \leq L \leq 10^5.
Input
Let v_i and L_i be the values v and L given in the i-th query. Then, Input is given from Standard Input in the following format:
N
V_1 W_1
:
V_N W_N
Q
v_1 L_1
:
v_Q L_Q
Output
For each integer i from 1 through Q, the i-th line should contain the response to the i-th query.
Examples
Input
3
1 2
2 3
3 4
3
1 1
2 5
3 5
Output
0
3
3
Input
15
123 119
129 120
132 112
126 109
118 103
115 109
102 100
130 120
105 105
132 115
104 102
107 107
127 116
121 104
121 115
8
8 234
9 244
10 226
11 227
12 240
13 237
14 206
15 227
Output
256
255
250
247
255
259
223
253 | instruction | 0 | 61,254 | 13 | 122,508 |
"Correct Solution:
```
import sys
import bisect
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
def merge(a, b):
res = []
i, j = 0, 0
while i < len(a) or j < len(b):
if j >= len(b) or (i < len(a) and a[i][0] < b[j][0]):
w, v = a[i]
i += 1
else:
w, v = b[j]
j += 1
if not res or (res[-1][1]) < v:
res.append((w, v))
elif res[-1][0] == w and res[-1][1] <= v:
res.pop()
res.append((w, v))
return res
def best(L, a, b):
res = 0
i = 0
j = len(b) - 1
while i < len(a):
w, v = a[i]
if j < 0:
break
if b[j][0] + w > L:
j -= 1
continue
res = max(res, v + b[j][1])
i += 1
return res
def main():
N = int(input())
VW = [(-1, -1)] + [list(map(int, input().split())) for _ in range(N)]
Q = int(input())
query = tuple(tuple(map(int, input().split())) for _ in range(Q))
dp = [[] for _ in range(2048)]
dp[0] = [(0, 0)]
for i in range(1, min(N + 1, 2048)):
vi, wi = VW[i]
a1 = dp[i // 2]
a2 = [(w + wi, v + vi) for w, v in a1]
dp[i] = merge(a1, a2)
for n, L in query:
if n < 2048:
w, v = zip(*dp[n])
i = bisect.bisect_right(w, L) - 1
print(v[i])
else:
a = [(0, 0)]
while n >= 2048:
vi, wi = VW[n]
b = [(w + wi, v + vi) for w, v in a]
a = merge(a, b)
n //= 2
print(best(L, a, dp[n]))
if __name__ == "__main__":
main()
``` | output | 1 | 61,254 | 13 | 122,509 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.