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 |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
DZY loves Physics, and he enjoys calculating density.
Almost everything has density, even a graph. We define the density of a non-directed graph (nodes and edges of the graph have some values) as follows:
<image> where v is the sum of the values of the nodes, e is the sum of the values of the edges.
Once DZY got a graph G, now he wants to find a connected induced subgraph G' of the graph, such that the density of G' is as large as possible.
An induced subgraph G'(V', E') of a graph G(V, E) is a graph that satisfies:
* <image>;
* edge <image> if and only if <image>, and edge <image>;
* the value of an edge in G' is the same as the value of the corresponding edge in G, so as the value of a node.
Help DZY to find the induced subgraph with maximum density. Note that the induced subgraph you choose must be connected.
<image>
Input
The first line contains two space-separated integers n (1 ≤ n ≤ 500), <image>. Integer n represents the number of nodes of the graph G, m represents the number of edges.
The second line contains n space-separated integers xi (1 ≤ xi ≤ 106), where xi represents the value of the i-th node. Consider the graph nodes are numbered from 1 to n.
Each of the next m lines contains three space-separated integers ai, bi, ci (1 ≤ ai < bi ≤ n; 1 ≤ ci ≤ 103), denoting an edge between node ai and bi with value ci. The graph won't contain multiple edges.
Output
Output a real number denoting the answer, with an absolute or relative error of at most 10 - 9.
Examples
Input
1 0
1
Output
0.000000000000000
Input
2 1
1 2
1 2 1
Output
3.000000000000000
Input
5 6
13 56 73 98 17
1 2 56
1 3 29
1 4 42
2 3 95
2 4 88
3 4 63
Output
2.965517241379311
Note
In the first sample, you can only choose an empty subgraph, or the subgraph containing only node 1.
In the second sample, choosing the whole graph is optimal.
Submitted Solution:
```
n,m=map(int,input().split());a=list(map(int,input().split()));o=0.0
for i in range(m):
x,y,c=map(int,input().split());o=max(o,(a[x-1]+a[y-1])/c)
print(o)
``` | instruction | 0 | 44,940 | 13 | 89,880 |
Yes | output | 1 | 44,940 | 13 | 89,881 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
DZY loves Physics, and he enjoys calculating density.
Almost everything has density, even a graph. We define the density of a non-directed graph (nodes and edges of the graph have some values) as follows:
<image> where v is the sum of the values of the nodes, e is the sum of the values of the edges.
Once DZY got a graph G, now he wants to find a connected induced subgraph G' of the graph, such that the density of G' is as large as possible.
An induced subgraph G'(V', E') of a graph G(V, E) is a graph that satisfies:
* <image>;
* edge <image> if and only if <image>, and edge <image>;
* the value of an edge in G' is the same as the value of the corresponding edge in G, so as the value of a node.
Help DZY to find the induced subgraph with maximum density. Note that the induced subgraph you choose must be connected.
<image>
Input
The first line contains two space-separated integers n (1 ≤ n ≤ 500), <image>. Integer n represents the number of nodes of the graph G, m represents the number of edges.
The second line contains n space-separated integers xi (1 ≤ xi ≤ 106), where xi represents the value of the i-th node. Consider the graph nodes are numbered from 1 to n.
Each of the next m lines contains three space-separated integers ai, bi, ci (1 ≤ ai < bi ≤ n; 1 ≤ ci ≤ 103), denoting an edge between node ai and bi with value ci. The graph won't contain multiple edges.
Output
Output a real number denoting the answer, with an absolute or relative error of at most 10 - 9.
Examples
Input
1 0
1
Output
0.000000000000000
Input
2 1
1 2
1 2 1
Output
3.000000000000000
Input
5 6
13 56 73 98 17
1 2 56
1 3 29
1 4 42
2 3 95
2 4 88
3 4 63
Output
2.965517241379311
Note
In the first sample, you can only choose an empty subgraph, or the subgraph containing only node 1.
In the second sample, choosing the whole graph is optimal.
Submitted Solution:
```
n,m=map(int,input().split())
ans=0
a=list(map(int,input().split()))
for i in range(m):
x,y,z=map(int,input().split())
ans=max(ans,(a[x-1]+a[y-1])/z)
print(ans)
``` | instruction | 0 | 44,941 | 13 | 89,882 |
Yes | output | 1 | 44,941 | 13 | 89,883 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
DZY loves Physics, and he enjoys calculating density.
Almost everything has density, even a graph. We define the density of a non-directed graph (nodes and edges of the graph have some values) as follows:
<image> where v is the sum of the values of the nodes, e is the sum of the values of the edges.
Once DZY got a graph G, now he wants to find a connected induced subgraph G' of the graph, such that the density of G' is as large as possible.
An induced subgraph G'(V', E') of a graph G(V, E) is a graph that satisfies:
* <image>;
* edge <image> if and only if <image>, and edge <image>;
* the value of an edge in G' is the same as the value of the corresponding edge in G, so as the value of a node.
Help DZY to find the induced subgraph with maximum density. Note that the induced subgraph you choose must be connected.
<image>
Input
The first line contains two space-separated integers n (1 ≤ n ≤ 500), <image>. Integer n represents the number of nodes of the graph G, m represents the number of edges.
The second line contains n space-separated integers xi (1 ≤ xi ≤ 106), where xi represents the value of the i-th node. Consider the graph nodes are numbered from 1 to n.
Each of the next m lines contains three space-separated integers ai, bi, ci (1 ≤ ai < bi ≤ n; 1 ≤ ci ≤ 103), denoting an edge between node ai and bi with value ci. The graph won't contain multiple edges.
Output
Output a real number denoting the answer, with an absolute or relative error of at most 10 - 9.
Examples
Input
1 0
1
Output
0.000000000000000
Input
2 1
1 2
1 2 1
Output
3.000000000000000
Input
5 6
13 56 73 98 17
1 2 56
1 3 29
1 4 42
2 3 95
2 4 88
3 4 63
Output
2.965517241379311
Note
In the first sample, you can only choose an empty subgraph, or the subgraph containing only node 1.
In the second sample, choosing the whole graph is optimal.
Submitted Solution:
```
n, m = input().split()
a = [0] + list(map(float, input().split()))
ans = 0
for _ in range(int(m)):
u, v, c = input().split()
ans = max(ans, (a[int(u)] + a[int(v)]) / float(c))
print(ans)
``` | instruction | 0 | 44,942 | 13 | 89,884 |
Yes | output | 1 | 44,942 | 13 | 89,885 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ilya is very fond of graphs, especially trees. During his last trip to the forest Ilya found a very interesting tree rooted at vertex 1. There is an integer number written on each vertex of the tree; the number written on vertex i is equal to ai.
Ilya believes that the beauty of the vertex x is the greatest common divisor of all numbers written on the vertices on the path from the root to x, including this vertex itself. In addition, Ilya can change the number in one arbitrary vertex to 0 or leave all vertices unchanged. Now for each vertex Ilya wants to know the maximum possible beauty it can have.
For each vertex the answer must be considered independently.
The beauty of the root equals to number written on it.
Input
First line contains one integer number n — the number of vertices in tree (1 ≤ n ≤ 2·105).
Next line contains n integer numbers ai (1 ≤ i ≤ n, 1 ≤ ai ≤ 2·105).
Each of next n - 1 lines contains two integer numbers x and y (1 ≤ x, y ≤ n, x ≠ y), which means that there is an edge (x, y) in the tree.
Output
Output n numbers separated by spaces, where i-th number equals to maximum possible beauty of vertex i.
Examples
Input
2
6 2
1 2
Output
6 6
Input
3
6 2 3
1 2
1 3
Output
6 6 6
Input
1
10
Output
10
Submitted Solution:
```
def gcd(a, b):
while b:
a, b = b, a%b
return a
n = int(input())
A = list(map(int, input().split()))
adj = [-1 for i in range(n)]
for i in range(n-1):
x, y = map(int, input().split())
x, y = min(x, y), max(x, y)
adj[y-1] = x-1
gs = []
for v in range(n):
parent = adj[v]
vs = [v]
while parent != -1:
vs.append(parent)
parent = adj[parent]
vs = sorted(vs, key=lambda x: A[x], reverse=True)
if len(vs) > 1:
g = 0
for i in range(len(vs)-1):
g = gcd(A[vs[i]], g)
gs.append(g)
else:
gs.append(A[v])
print(*gs)
``` | instruction | 0 | 45,095 | 13 | 90,190 |
No | output | 1 | 45,095 | 13 | 90,191 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ilya is very fond of graphs, especially trees. During his last trip to the forest Ilya found a very interesting tree rooted at vertex 1. There is an integer number written on each vertex of the tree; the number written on vertex i is equal to ai.
Ilya believes that the beauty of the vertex x is the greatest common divisor of all numbers written on the vertices on the path from the root to x, including this vertex itself. In addition, Ilya can change the number in one arbitrary vertex to 0 or leave all vertices unchanged. Now for each vertex Ilya wants to know the maximum possible beauty it can have.
For each vertex the answer must be considered independently.
The beauty of the root equals to number written on it.
Input
First line contains one integer number n — the number of vertices in tree (1 ≤ n ≤ 2·105).
Next line contains n integer numbers ai (1 ≤ i ≤ n, 1 ≤ ai ≤ 2·105).
Each of next n - 1 lines contains two integer numbers x and y (1 ≤ x, y ≤ n, x ≠ y), which means that there is an edge (x, y) in the tree.
Output
Output n numbers separated by spaces, where i-th number equals to maximum possible beauty of vertex i.
Examples
Input
2
6 2
1 2
Output
6 6
Input
3
6 2 3
1 2
1 3
Output
6 6 6
Input
1
10
Output
10
Submitted Solution:
```
import math
from functools import reduce
num_vertices = int(input())
edges = []
vertex_numbers = [int(i) for i in input().split(" ")]
for i in range(num_vertices-1):
edge = [int(i) for i in input().split(" ")]
edges.append((edge[0], edge[1]))
class tree_node():
def __init__(self, id, number):
self.id = id
self.number = number
self.parent = None
def addParent(self, node):
self.parent = node
def getParent(self):
return self.parent
def getId(self):
return self.id
def getNumber(self):
return self.number
def getVerticesToRoot(self):
if self.id == 1: return [self]
return [self] + self.parent.getVerticesToRoot()
nodes = dict()
for i in range(1, num_vertices+1):
nodes[i] = tree_node(i, vertex_numbers[i-1])
for edge in edges:
child = nodes[edge[1]]
parent = nodes[edge[0]]
child.addParent(parent)
output = []
def gcd_list(ls):
return reduce(lambda x, y: math.gcd(x,y), ls)
for i in range(1, num_vertices+1):
nodepath = nodes[i].getVerticesToRoot()
vals = [n.getNumber() for n in nodepath]
if len(vals) == 1: output.append(vals[0])
else:
sublists = []
print(vals)
for i in range(0, len(vals)):
sublists.append(vals[:i] + vals[i+1:])
sublists = [gcd_list(sl) for sl in sublists]
print(sublists)
max_gcd = max(gcd_list(vals), max(sublists))
output.append(max_gcd)
print(" ".join(str(s) for s in output))
``` | instruction | 0 | 45,096 | 13 | 90,192 |
No | output | 1 | 45,096 | 13 | 90,193 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ilya is very fond of graphs, especially trees. During his last trip to the forest Ilya found a very interesting tree rooted at vertex 1. There is an integer number written on each vertex of the tree; the number written on vertex i is equal to ai.
Ilya believes that the beauty of the vertex x is the greatest common divisor of all numbers written on the vertices on the path from the root to x, including this vertex itself. In addition, Ilya can change the number in one arbitrary vertex to 0 or leave all vertices unchanged. Now for each vertex Ilya wants to know the maximum possible beauty it can have.
For each vertex the answer must be considered independently.
The beauty of the root equals to number written on it.
Input
First line contains one integer number n — the number of vertices in tree (1 ≤ n ≤ 2·105).
Next line contains n integer numbers ai (1 ≤ i ≤ n, 1 ≤ ai ≤ 2·105).
Each of next n - 1 lines contains two integer numbers x and y (1 ≤ x, y ≤ n, x ≠ y), which means that there is an edge (x, y) in the tree.
Output
Output n numbers separated by spaces, where i-th number equals to maximum possible beauty of vertex i.
Examples
Input
2
6 2
1 2
Output
6 6
Input
3
6 2 3
1 2
1 3
Output
6 6 6
Input
1
10
Output
10
Submitted Solution:
```
def dfs(u):
for v in gr[u]:
if cost[v] > beauty[u] and u != 0:
beauty[v] = cost[v]
else:
beauty[v] = beauty[u]
dfs(v)
n = int(input())
cost = list(map(int, input().split()))
gr = [[] for i in range(n)]
for i in range(n - 1):
f, t = map(int, input().split())
gr[f - 1].append(t - 1)
beauty = [0 for i in range(n)]
beauty[0] = cost[0]
dfs(0)
print(*beauty)
``` | instruction | 0 | 45,097 | 13 | 90,194 |
No | output | 1 | 45,097 | 13 | 90,195 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ilya is very fond of graphs, especially trees. During his last trip to the forest Ilya found a very interesting tree rooted at vertex 1. There is an integer number written on each vertex of the tree; the number written on vertex i is equal to ai.
Ilya believes that the beauty of the vertex x is the greatest common divisor of all numbers written on the vertices on the path from the root to x, including this vertex itself. In addition, Ilya can change the number in one arbitrary vertex to 0 or leave all vertices unchanged. Now for each vertex Ilya wants to know the maximum possible beauty it can have.
For each vertex the answer must be considered independently.
The beauty of the root equals to number written on it.
Input
First line contains one integer number n — the number of vertices in tree (1 ≤ n ≤ 2·105).
Next line contains n integer numbers ai (1 ≤ i ≤ n, 1 ≤ ai ≤ 2·105).
Each of next n - 1 lines contains two integer numbers x and y (1 ≤ x, y ≤ n, x ≠ y), which means that there is an edge (x, y) in the tree.
Output
Output n numbers separated by spaces, where i-th number equals to maximum possible beauty of vertex i.
Examples
Input
2
6 2
1 2
Output
6 6
Input
3
6 2 3
1 2
1 3
Output
6 6 6
Input
1
10
Output
10
Submitted Solution:
```
from math import gcd
n = int(input())
a = list(map(int, input().split(" ")))
dp = [[0, 0] for i in range(n)]
allmul = [0 for i in range(n)]
dp[0][0] = 0
dp[0][1] = a[0]
allmul[0] = a[0]
edges = [[]]*n
for i in range(n-1):
x, y = map(int, input().split(" "))
edges[x-1].append(y-1)
stack = [0]
while stack:
curr = stack[0]
stack = stack[1:]
for child in edges[curr]:
allmul[child] = gcd(allmul[curr],a[child])
dp[child][0] = allmul[curr]
dp[child][1] = max(gcd(a[child],dp[curr][0]), gcd(a[child],dp[curr][1]))
for i in dp:
print(max(i[0], i[1]), end=' ')
print()
``` | instruction | 0 | 45,098 | 13 | 90,196 |
No | output | 1 | 45,098 | 13 | 90,197 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a complete undirected graph with n vertices. A number ai is assigned to each vertex, and the weight of an edge between vertices i and j is equal to ai xor aj.
Calculate the weight of the minimum spanning tree in this graph.
Input
The first line contains n (1 ≤ n ≤ 200000) — the number of vertices in the graph.
The second line contains n integers a1, a2, ..., an (0 ≤ ai < 230) — the numbers assigned to the vertices.
Output
Print one number — the weight of the minimum spanning tree in the graph.
Examples
Input
5
1 2 3 4 5
Output
8
Input
4
1 2 3 4
Output
8
Submitted Solution:
```
print("8")
``` | instruction | 0 | 45,115 | 13 | 90,230 |
No | output | 1 | 45,115 | 13 | 90,231 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a complete undirected graph with n vertices. A number ai is assigned to each vertex, and the weight of an edge between vertices i and j is equal to ai xor aj.
Calculate the weight of the minimum spanning tree in this graph.
Input
The first line contains n (1 ≤ n ≤ 200000) — the number of vertices in the graph.
The second line contains n integers a1, a2, ..., an (0 ≤ ai < 230) — the numbers assigned to the vertices.
Output
Print one number — the weight of the minimum spanning tree in the graph.
Examples
Input
5
1 2 3 4 5
Output
8
Input
4
1 2 3 4
Output
8
Submitted Solution:
```
# -*- coding: utf-8 -*-
import math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys
"""
created by shhuan at 2017/11/10 01:15
"""
N = int(input())
A = [int(x) for x in input().split()]
#
# N = 200000
# A = [random.randint(2**10, 2**30) for _ in range(N)]
t0 = time.time()
trie = {}
for v in A:
t = trie
for i in range(2, -1, -1):
if (v >> i) & 1 == 1:
if 1 not in t:
t[1] = {}
t = t[1]
else:
if 0 not in t:
t[0] = {}
t = t[0]
t['#'] = v
def min_xor_trie(a, b):
if not a or not b:
return float('inf')
if '#' in a and '#' in b:
return a['#'] ^ b['#']
ans = min(min_xor_trie(a.get(0, None), b.get(0, None)), min_xor_trie(a.get(1, None), b.get(1, None)))
if ans == float('inf'):
return min(min_xor_trie(a.get(0, None), b.get(1, None)), min_xor_trie(a.get(1, None), b.get(0, None)))
return ans
def dfs_trie(trie):
if not trie:
return 0
if 0 in trie and 1 in trie:
a = trie[0]
b = trie[1]
xor = min_xor_trie(a, b)
return dfs_trie(a) + dfs_trie(b) + (0 if xor == float('inf') else xor)
elif 1 in trie:
return dfs_trie(trie[1])
elif 0 in trie:
return dfs_trie(trie[0])
else:
return 0
def min_xor(a, b, bi):
if not a or not b:
return float('inf')
if bi < 0:
return min([u^v for u in a for v in b] or [float('inf')])
aa = [v for v in a if (v >> bi) & 1 == 1]
ab = [v for v in a if (v >> bi) & 1 == 0]
ba = [v for v in b if (v >> bi) & 1 == 1]
bb = [v for v in b if (v >> bi) & 1 == 0]
ans = min(min_xor(aa, ba, bi-1), min_xor(ab, bb, bi-1))
if ans == float('inf'):
# print(a, b, bi, min([u ^ v for u in a for v in b] or [float('inf')]))
return min([u ^ v for u in a for v in b] or [float('inf')])
# print(a, b, bi, ans)
return ans
def dfs(bi, vals):
if bi < 0 or not vals:
return 0
a = [v for v in vals if (v >> bi) & 1 == 1]
b = [v for v in vals if (v >> bi) & 1 == 0]
xor = min_xor(a, b, bi-1)
return dfs(bi-1, a) + dfs(bi-1, b) + (xor if xor != float('inf') else 0)
# print(dfs(31, A))
print(dfs_trie(trie))
# print(time.time() - t0)
``` | instruction | 0 | 45,116 | 13 | 90,232 |
No | output | 1 | 45,116 | 13 | 90,233 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a complete undirected graph with n vertices. A number ai is assigned to each vertex, and the weight of an edge between vertices i and j is equal to ai xor aj.
Calculate the weight of the minimum spanning tree in this graph.
Input
The first line contains n (1 ≤ n ≤ 200000) — the number of vertices in the graph.
The second line contains n integers a1, a2, ..., an (0 ≤ ai < 230) — the numbers assigned to the vertices.
Output
Print one number — the weight of the minimum spanning tree in the graph.
Examples
Input
5
1 2 3 4 5
Output
8
Input
4
1 2 3 4
Output
8
Submitted Solution:
```
from collections import defaultdict
class Graph:
def init(self,vertices):
self.V=vertices
self.graph=[]
def addEdge(self, u, v, w):
self.graph.append([u, v, w])
def find(self, parent, i):
if parent[i] == i:
return i
parent[i]=self.find(parent, parent[i])
return parent[i]
def mst(self):
ans=i=e=0
self.graph=sorted(self.graph,key=lambda item:item[2])
parent=[]
for node in range(self.V):
parent.append(node)
while e<self.V-1:
u,v,w=self.graph[i]
i+=1
x = self.find(parent, u)
y = self.find(parent, v)
if x==y:continue
e+=1
ans+=w
parent[x]=y
return ans
n=int(input())
p=list(map(int,input().split()))
g=Graph()
g.init(n)
for i in range(0,n):
for j in range(i+1,n):
g.addEdge(i,j,(i+1)^(j+1))
#print(g.graph)
print(g.mst())
``` | instruction | 0 | 45,117 | 13 | 90,234 |
No | output | 1 | 45,117 | 13 | 90,235 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a complete undirected graph with n vertices. A number ai is assigned to each vertex, and the weight of an edge between vertices i and j is equal to ai xor aj.
Calculate the weight of the minimum spanning tree in this graph.
Input
The first line contains n (1 ≤ n ≤ 200000) — the number of vertices in the graph.
The second line contains n integers a1, a2, ..., an (0 ≤ ai < 230) — the numbers assigned to the vertices.
Output
Print one number — the weight of the minimum spanning tree in the graph.
Examples
Input
5
1 2 3 4 5
Output
8
Input
4
1 2 3 4
Output
8
Submitted Solution:
```
print(8,"")
``` | instruction | 0 | 45,118 | 13 | 90,236 |
No | output | 1 | 45,118 | 13 | 90,237 |
Provide a correct Python 3 solution for this coding contest problem.
Given is a tree G with N vertices. The vertices are numbered 1 through N, and the i-th edge connects Vertex a_i and Vertex b_i.
Consider painting the edges in G with some number of colors. We want to paint them so that, for each vertex, the colors of the edges incident to that vertex are all different.
Among the colorings satisfying the condition above, construct one that uses the minimum number of colors.
Constraints
* 2 \le N \le 10^5
* 1 \le a_i \lt b_i \le N
* All values in input are integers.
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}
Output
Print N lines.
The first line should contain K, the number of colors used.
The (i+1)-th line (1 \le i \le N-1) should contain c_i, the integer representing the color of the i-th edge, where 1 \le c_i \le K must hold.
If there are multiple colorings with the minimum number of colors that satisfy the condition, printing any of them will be accepted.
Examples
Input
3
1 2
2 3
Output
2
1
2
Input
8
1 2
2 3
2 4
2 5
4 7
5 6
6 8
Output
4
1
2
3
4
1
1
2
Input
6
1 2
1 3
1 4
1 5
1 6
Output
5
1
2
3
4
5 | instruction | 0 | 45,191 | 13 | 90,382 |
"Correct Solution:
```
def p_d():
n = int(input())
t = [[] for _ in range(n)]
p = [-1] * n
for i in range(n - 1):
a, b = map(int, input().split())
t[a - 1].append((i, b - 1))
p[b - 1] = a - 1
q = [(0, -1)]
ans = [-1] * (n - 1)
while q:
x, c0 = q.pop()
c = 1
for i, y in t[x]:
if c == c0:
c += 1
ans[i] = c
q.append((y, c))
c += 1
print(max(ans))
for a in ans:
print(a)
p_d()
``` | output | 1 | 45,191 | 13 | 90,383 |
Provide a correct Python 3 solution for this coding contest problem.
Given is a tree G with N vertices. The vertices are numbered 1 through N, and the i-th edge connects Vertex a_i and Vertex b_i.
Consider painting the edges in G with some number of colors. We want to paint them so that, for each vertex, the colors of the edges incident to that vertex are all different.
Among the colorings satisfying the condition above, construct one that uses the minimum number of colors.
Constraints
* 2 \le N \le 10^5
* 1 \le a_i \lt b_i \le N
* All values in input are integers.
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}
Output
Print N lines.
The first line should contain K, the number of colors used.
The (i+1)-th line (1 \le i \le N-1) should contain c_i, the integer representing the color of the i-th edge, where 1 \le c_i \le K must hold.
If there are multiple colorings with the minimum number of colors that satisfy the condition, printing any of them will be accepted.
Examples
Input
3
1 2
2 3
Output
2
1
2
Input
8
1 2
2 3
2 4
2 5
4 7
5 6
6 8
Output
4
1
2
3
4
1
1
2
Input
6
1 2
1 3
1 4
1 5
1 6
Output
5
1
2
3
4
5 | instruction | 0 | 45,192 | 13 | 90,384 |
"Correct Solution:
```
from collections import deque
def main():
N, *AB = map(int, open(0).read().split())
A, B = AB[::2], AB[1::2]
E = [[] for _ in range(N + 1)]
for a, b in zip(A, B):
E[a].append(b)
Q = deque([1])
C = [0] * (N + 1)
while Q:
v = Q.popleft()
c = 0
for u in E[v]:
c += 1 + (c + 1 == C[v])
C[u] = c
Q.append(u)
print(max(C))
for b in B:
print(C[b])
main()
``` | output | 1 | 45,192 | 13 | 90,385 |
Provide a correct Python 3 solution for this coding contest problem.
Given is a tree G with N vertices. The vertices are numbered 1 through N, and the i-th edge connects Vertex a_i and Vertex b_i.
Consider painting the edges in G with some number of colors. We want to paint them so that, for each vertex, the colors of the edges incident to that vertex are all different.
Among the colorings satisfying the condition above, construct one that uses the minimum number of colors.
Constraints
* 2 \le N \le 10^5
* 1 \le a_i \lt b_i \le N
* All values in input are integers.
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}
Output
Print N lines.
The first line should contain K, the number of colors used.
The (i+1)-th line (1 \le i \le N-1) should contain c_i, the integer representing the color of the i-th edge, where 1 \le c_i \le K must hold.
If there are multiple colorings with the minimum number of colors that satisfy the condition, printing any of them will be accepted.
Examples
Input
3
1 2
2 3
Output
2
1
2
Input
8
1 2
2 3
2 4
2 5
4 7
5 6
6 8
Output
4
1
2
3
4
1
1
2
Input
6
1 2
1 3
1 4
1 5
1 6
Output
5
1
2
3
4
5 | instruction | 0 | 45,193 | 13 | 90,386 |
"Correct Solution:
```
# -*- coding: utf-8 -*-
n = int(input())
ab = [list(map(int,input().split())) for i in range(n-1)]
ans = [0] * (n-1)
abi = [[] for _ in range(n+1)]
#再起のlimitを上げる
import sys
sys.setrecursionlimit(4100000)
for i,(a,b) in enumerate(ab):
abi[a].append((i,b))
abi[b].append((i,a))
def dfs(v,parent=-1,color=-1):
k=1
for i in abi[v]:
if parent==i[1]:continue
if(k==color):k+=1
ans[i[0]]=k
k+=1
dfs(i[1],v,ans[i[0]])
dfs(1)
print(max(ans))
for i in ans:
print(i)
``` | output | 1 | 45,193 | 13 | 90,387 |
Provide a correct Python 3 solution for this coding contest problem.
Given is a tree G with N vertices. The vertices are numbered 1 through N, and the i-th edge connects Vertex a_i and Vertex b_i.
Consider painting the edges in G with some number of colors. We want to paint them so that, for each vertex, the colors of the edges incident to that vertex are all different.
Among the colorings satisfying the condition above, construct one that uses the minimum number of colors.
Constraints
* 2 \le N \le 10^5
* 1 \le a_i \lt b_i \le N
* All values in input are integers.
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}
Output
Print N lines.
The first line should contain K, the number of colors used.
The (i+1)-th line (1 \le i \le N-1) should contain c_i, the integer representing the color of the i-th edge, where 1 \le c_i \le K must hold.
If there are multiple colorings with the minimum number of colors that satisfy the condition, printing any of them will be accepted.
Examples
Input
3
1 2
2 3
Output
2
1
2
Input
8
1 2
2 3
2 4
2 5
4 7
5 6
6 8
Output
4
1
2
3
4
1
1
2
Input
6
1 2
1 3
1 4
1 5
1 6
Output
5
1
2
3
4
5 | instruction | 0 | 45,194 | 13 | 90,388 |
"Correct Solution:
```
n = int(input())
g = [[] for i in range(n)]
for i in range(n-1):
a,b = map(int,input().split())
g[a-1].append([b-1,i])
g[b-1].append([a-1,i])
from collections import deque
d = deque([(0,-1)])
ans = [0 for i in range(n-1)]
while d:
a,noc = d.popleft()
c = 1 if noc != 1 else 2
for b,i in g[a]:
if ans[i] == 0:
ans[i] = c
d.append((b,c))
else:
continue
c += 1 if c+1 != noc else 2
print(max(ans))
for i in range(n-1):
print(ans[i])
``` | output | 1 | 45,194 | 13 | 90,389 |
Provide a correct Python 3 solution for this coding contest problem.
Given is a tree G with N vertices. The vertices are numbered 1 through N, and the i-th edge connects Vertex a_i and Vertex b_i.
Consider painting the edges in G with some number of colors. We want to paint them so that, for each vertex, the colors of the edges incident to that vertex are all different.
Among the colorings satisfying the condition above, construct one that uses the minimum number of colors.
Constraints
* 2 \le N \le 10^5
* 1 \le a_i \lt b_i \le N
* All values in input are integers.
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}
Output
Print N lines.
The first line should contain K, the number of colors used.
The (i+1)-th line (1 \le i \le N-1) should contain c_i, the integer representing the color of the i-th edge, where 1 \le c_i \le K must hold.
If there are multiple colorings with the minimum number of colors that satisfy the condition, printing any of them will be accepted.
Examples
Input
3
1 2
2 3
Output
2
1
2
Input
8
1 2
2 3
2 4
2 5
4 7
5 6
6 8
Output
4
1
2
3
4
1
1
2
Input
6
1 2
1 3
1 4
1 5
1 6
Output
5
1
2
3
4
5 | instruction | 0 | 45,195 | 13 | 90,390 |
"Correct Solution:
```
import sys
input = sys.stdin.readline
n = int(input())
l=[[]]
nn = []
for _ in range(n+1):
l.append([])
for i in range(n-1):
a,b = map(int,input().split())
l[a].append(b)
l[b].append(a)
nn.append(b)
ml = list(map(len,l))
m = max(ml)
co = []
for i in range(n+1):
co.append( set(range(1,ml[i]+1) ))
col = [0]*(n+1)
col[1] = 1
for i in range(1,n+1):
for la in l[i]:
if col[la] ==0:
col[la] = co[i].pop()
co[la].discard(col[la])
print(m)
for i in range(n-1):
print(col[nn[i]])
``` | output | 1 | 45,195 | 13 | 90,391 |
Provide a correct Python 3 solution for this coding contest problem.
Given is a tree G with N vertices. The vertices are numbered 1 through N, and the i-th edge connects Vertex a_i and Vertex b_i.
Consider painting the edges in G with some number of colors. We want to paint them so that, for each vertex, the colors of the edges incident to that vertex are all different.
Among the colorings satisfying the condition above, construct one that uses the minimum number of colors.
Constraints
* 2 \le N \le 10^5
* 1 \le a_i \lt b_i \le N
* All values in input are integers.
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}
Output
Print N lines.
The first line should contain K, the number of colors used.
The (i+1)-th line (1 \le i \le N-1) should contain c_i, the integer representing the color of the i-th edge, where 1 \le c_i \le K must hold.
If there are multiple colorings with the minimum number of colors that satisfy the condition, printing any of them will be accepted.
Examples
Input
3
1 2
2 3
Output
2
1
2
Input
8
1 2
2 3
2 4
2 5
4 7
5 6
6 8
Output
4
1
2
3
4
1
1
2
Input
6
1 2
1 3
1 4
1 5
1 6
Output
5
1
2
3
4
5 | instruction | 0 | 45,196 | 13 | 90,392 |
"Correct Solution:
```
import sys
sys.setrecursionlimit(500000)
N = int(input())
E = [[] for _ in range(N+1)]
for i in range(N-1):
a,b = map(int,input().split())
E[a].append((b,i))
E[b].append((a,i))
#最大の次数を見つける
K = max(len(e) for e in E)
print(K)
ans = [-1] * (N-1)
def dfs(v=1,p=0,p_col=-1):
col = 1
for u, idx in E[v]:
if u != p:
if col == p_col:
col += 1
ans[idx] = col
dfs(u,v,col)
col += 1
dfs()
print("\n".join(map(str,ans)))
``` | output | 1 | 45,196 | 13 | 90,393 |
Provide a correct Python 3 solution for this coding contest problem.
Given is a tree G with N vertices. The vertices are numbered 1 through N, and the i-th edge connects Vertex a_i and Vertex b_i.
Consider painting the edges in G with some number of colors. We want to paint them so that, for each vertex, the colors of the edges incident to that vertex are all different.
Among the colorings satisfying the condition above, construct one that uses the minimum number of colors.
Constraints
* 2 \le N \le 10^5
* 1 \le a_i \lt b_i \le N
* All values in input are integers.
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}
Output
Print N lines.
The first line should contain K, the number of colors used.
The (i+1)-th line (1 \le i \le N-1) should contain c_i, the integer representing the color of the i-th edge, where 1 \le c_i \le K must hold.
If there are multiple colorings with the minimum number of colors that satisfy the condition, printing any of them will be accepted.
Examples
Input
3
1 2
2 3
Output
2
1
2
Input
8
1 2
2 3
2 4
2 5
4 7
5 6
6 8
Output
4
1
2
3
4
1
1
2
Input
6
1 2
1 3
1 4
1 5
1 6
Output
5
1
2
3
4
5 | instruction | 0 | 45,197 | 13 | 90,394 |
"Correct Solution:
```
import sys
sys.setrecursionlimit(10**8)
n=int(input())
AB=[list(map(int,input().split())) for _ in range(n-1)]
path=[[] for _ in range(n)]
for i,(a,b) in enumerate(AB):
a,b=a-1,b-1
path[a].append((b,i))
path[b].append((a,i))
visited=[0]*n
ans=[None]*(n-1)
def dfs(v,c=-1):
nc=1
for to,i in path[v]:
if visited[to]: continue
visited[v]=1
if nc==c: nc+=1
ans[i]=nc
dfs(to,nc)
nc+=1
dfs(0)
print(max(ans))
print(*ans,sep='\n')
``` | output | 1 | 45,197 | 13 | 90,395 |
Provide a correct Python 3 solution for this coding contest problem.
Given is a tree G with N vertices. The vertices are numbered 1 through N, and the i-th edge connects Vertex a_i and Vertex b_i.
Consider painting the edges in G with some number of colors. We want to paint them so that, for each vertex, the colors of the edges incident to that vertex are all different.
Among the colorings satisfying the condition above, construct one that uses the minimum number of colors.
Constraints
* 2 \le N \le 10^5
* 1 \le a_i \lt b_i \le N
* All values in input are integers.
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}
Output
Print N lines.
The first line should contain K, the number of colors used.
The (i+1)-th line (1 \le i \le N-1) should contain c_i, the integer representing the color of the i-th edge, where 1 \le c_i \le K must hold.
If there are multiple colorings with the minimum number of colors that satisfy the condition, printing any of them will be accepted.
Examples
Input
3
1 2
2 3
Output
2
1
2
Input
8
1 2
2 3
2 4
2 5
4 7
5 6
6 8
Output
4
1
2
3
4
1
1
2
Input
6
1 2
1 3
1 4
1 5
1 6
Output
5
1
2
3
4
5 | instruction | 0 | 45,198 | 13 | 90,396 |
"Correct Solution:
```
# 2019/11/24
import sys
sys.setrecursionlimit(10**9)
n=int(input())
ab=[]
edge=[[] for _ in range(n)]
for i in range(n-1):
a,b=map(int,input().split())
a,b=a-1,b-1
ab.append((a,b))
edge[a].append(b)
ans={}
def solve(p,c):
global ans
cnt=1
for vtx in edge[p]:
while c==cnt:
cnt+=1
ans[(p,vtx)]=cnt
solve(vtx,cnt)
cnt+=1
solve(0,0)
print(max(ans.values()))
for e in ab:
print(ans[e])
``` | output | 1 | 45,198 | 13 | 90,397 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given is a tree G with N vertices. The vertices are numbered 1 through N, and the i-th edge connects Vertex a_i and Vertex b_i.
Consider painting the edges in G with some number of colors. We want to paint them so that, for each vertex, the colors of the edges incident to that vertex are all different.
Among the colorings satisfying the condition above, construct one that uses the minimum number of colors.
Constraints
* 2 \le N \le 10^5
* 1 \le a_i \lt b_i \le N
* All values in input are integers.
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}
Output
Print N lines.
The first line should contain K, the number of colors used.
The (i+1)-th line (1 \le i \le N-1) should contain c_i, the integer representing the color of the i-th edge, where 1 \le c_i \le K must hold.
If there are multiple colorings with the minimum number of colors that satisfy the condition, printing any of them will be accepted.
Examples
Input
3
1 2
2 3
Output
2
1
2
Input
8
1 2
2 3
2 4
2 5
4 7
5 6
6 8
Output
4
1
2
3
4
1
1
2
Input
6
1 2
1 3
1 4
1 5
1 6
Output
5
1
2
3
4
5
Submitted Solution:
```
import sys
sys.setrecursionlimit(9**9)
n=int(input())
T=[[] for _ in range(n)]
for i in range(n-1):
a,b=map(int,input().split())
T[a-1].append([b-1,i])
C=[0]*(n-1)
def f(i,r):
c=1
for (x,y) in T[i]:
c+=(c==r)
C[y]=c
f(x,c)
c+=1
f(0,0)
print(max(C))
for c in C:
print(c)
``` | instruction | 0 | 45,199 | 13 | 90,398 |
Yes | output | 1 | 45,199 | 13 | 90,399 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given is a tree G with N vertices. The vertices are numbered 1 through N, and the i-th edge connects Vertex a_i and Vertex b_i.
Consider painting the edges in G with some number of colors. We want to paint them so that, for each vertex, the colors of the edges incident to that vertex are all different.
Among the colorings satisfying the condition above, construct one that uses the minimum number of colors.
Constraints
* 2 \le N \le 10^5
* 1 \le a_i \lt b_i \le N
* All values in input are integers.
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}
Output
Print N lines.
The first line should contain K, the number of colors used.
The (i+1)-th line (1 \le i \le N-1) should contain c_i, the integer representing the color of the i-th edge, where 1 \le c_i \le K must hold.
If there are multiple colorings with the minimum number of colors that satisfy the condition, printing any of them will be accepted.
Examples
Input
3
1 2
2 3
Output
2
1
2
Input
8
1 2
2 3
2 4
2 5
4 7
5 6
6 8
Output
4
1
2
3
4
1
1
2
Input
6
1 2
1 3
1 4
1 5
1 6
Output
5
1
2
3
4
5
Submitted Solution:
```
from collections import deque
N = int(input())
G = [[] for _ in range(N)]
for i in range(N-1):
a, b = map(int, input().split())
a, b = a-1, b-1
G[a].append([b, i])
G[b].append([a, i])
ans = [0]*(N-1)
q = deque([(0, -1)])
while q:
v, pc = q.popleft()
c = 1 if pc != 1 else 2
for nv, i in G[v]:
if ans[i] == 0:
ans[i] = c
q.append((nv, c))
c += 1 if c+1 != pc else 2
print(max(ans))
print(*ans, sep="\n")
``` | instruction | 0 | 45,200 | 13 | 90,400 |
Yes | output | 1 | 45,200 | 13 | 90,401 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given is a tree G with N vertices. The vertices are numbered 1 through N, and the i-th edge connects Vertex a_i and Vertex b_i.
Consider painting the edges in G with some number of colors. We want to paint them so that, for each vertex, the colors of the edges incident to that vertex are all different.
Among the colorings satisfying the condition above, construct one that uses the minimum number of colors.
Constraints
* 2 \le N \le 10^5
* 1 \le a_i \lt b_i \le N
* All values in input are integers.
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}
Output
Print N lines.
The first line should contain K, the number of colors used.
The (i+1)-th line (1 \le i \le N-1) should contain c_i, the integer representing the color of the i-th edge, where 1 \le c_i \le K must hold.
If there are multiple colorings with the minimum number of colors that satisfy the condition, printing any of them will be accepted.
Examples
Input
3
1 2
2 3
Output
2
1
2
Input
8
1 2
2 3
2 4
2 5
4 7
5 6
6 8
Output
4
1
2
3
4
1
1
2
Input
6
1 2
1 3
1 4
1 5
1 6
Output
5
1
2
3
4
5
Submitted Solution:
```
import sys
input = sys.stdin.readline
N=int(input());AB=[tuple(map(int,input().split())) for _ in range(1,N)]
co=[0 for _ in range(N)]
prev=0
color=1
for a,b in sorted(AB):
if prev!=a:
color=1
if co[a-1]==color:
color+=1
co[b-1]=color
prev=a
color+=1
print(max(co))
for _,b in AB:
print(co[b-1])
``` | instruction | 0 | 45,201 | 13 | 90,402 |
Yes | output | 1 | 45,201 | 13 | 90,403 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given is a tree G with N vertices. The vertices are numbered 1 through N, and the i-th edge connects Vertex a_i and Vertex b_i.
Consider painting the edges in G with some number of colors. We want to paint them so that, for each vertex, the colors of the edges incident to that vertex are all different.
Among the colorings satisfying the condition above, construct one that uses the minimum number of colors.
Constraints
* 2 \le N \le 10^5
* 1 \le a_i \lt b_i \le N
* All values in input are integers.
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}
Output
Print N lines.
The first line should contain K, the number of colors used.
The (i+1)-th line (1 \le i \le N-1) should contain c_i, the integer representing the color of the i-th edge, where 1 \le c_i \le K must hold.
If there are multiple colorings with the minimum number of colors that satisfy the condition, printing any of them will be accepted.
Examples
Input
3
1 2
2 3
Output
2
1
2
Input
8
1 2
2 3
2 4
2 5
4 7
5 6
6 8
Output
4
1
2
3
4
1
1
2
Input
6
1 2
1 3
1 4
1 5
1 6
Output
5
1
2
3
4
5
Submitted Solution:
```
f=lambda:map(int,input().split())
n=int(input())
g=[[] for _ in range(n)]
for i in range(n-1):
a,b=f()
g[a-1]+=[(b-1,i)]
g[b-1]+=[(a-1,i)]
l=[0]*(n-1)
u=[0]*n
q=[(0,0)]
while q:
v,s=q.pop()
u[v]=1
t=1
for c,i in g[v]:
if u[c]: continue
if t==s: t+=1
l[i]=t
q+=[(c,t)]
t+=1
print(max(l))
for i in l: print(i)
``` | instruction | 0 | 45,202 | 13 | 90,404 |
Yes | output | 1 | 45,202 | 13 | 90,405 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given is a tree G with N vertices. The vertices are numbered 1 through N, and the i-th edge connects Vertex a_i and Vertex b_i.
Consider painting the edges in G with some number of colors. We want to paint them so that, for each vertex, the colors of the edges incident to that vertex are all different.
Among the colorings satisfying the condition above, construct one that uses the minimum number of colors.
Constraints
* 2 \le N \le 10^5
* 1 \le a_i \lt b_i \le N
* All values in input are integers.
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}
Output
Print N lines.
The first line should contain K, the number of colors used.
The (i+1)-th line (1 \le i \le N-1) should contain c_i, the integer representing the color of the i-th edge, where 1 \le c_i \le K must hold.
If there are multiple colorings with the minimum number of colors that satisfy the condition, printing any of them will be accepted.
Examples
Input
3
1 2
2 3
Output
2
1
2
Input
8
1 2
2 3
2 4
2 5
4 7
5 6
6 8
Output
4
1
2
3
4
1
1
2
Input
6
1 2
1 3
1 4
1 5
1 6
Output
5
1
2
3
4
5
Submitted Solution:
```
from collections import deque
n=int(input())
G=[[]for _ in range(n)]
V=[False]*n
for i in range(n-1):
a,b=map(int,input().split())
G[a-1].append(b-1)
G[b-1].append(a-1)
k=0
for i in range(n):
k=max(k,len(G[i]))
H=[0]*(n-1)
que=deque([0])
while que:
s=que.popleft()
if V[s]==False:
V[s]=True
for ni in G[s]:
print(ni)
que.append(ni)
``` | instruction | 0 | 45,203 | 13 | 90,406 |
No | output | 1 | 45,203 | 13 | 90,407 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given is a tree G with N vertices. The vertices are numbered 1 through N, and the i-th edge connects Vertex a_i and Vertex b_i.
Consider painting the edges in G with some number of colors. We want to paint them so that, for each vertex, the colors of the edges incident to that vertex are all different.
Among the colorings satisfying the condition above, construct one that uses the minimum number of colors.
Constraints
* 2 \le N \le 10^5
* 1 \le a_i \lt b_i \le N
* All values in input are integers.
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}
Output
Print N lines.
The first line should contain K, the number of colors used.
The (i+1)-th line (1 \le i \le N-1) should contain c_i, the integer representing the color of the i-th edge, where 1 \le c_i \le K must hold.
If there are multiple colorings with the minimum number of colors that satisfy the condition, printing any of them will be accepted.
Examples
Input
3
1 2
2 3
Output
2
1
2
Input
8
1 2
2 3
2 4
2 5
4 7
5 6
6 8
Output
4
1
2
3
4
1
1
2
Input
6
1 2
1 3
1 4
1 5
1 6
Output
5
1
2
3
4
5
Submitted Solution:
```
import sys
from fractions import gcd
from itertools import permutations, combinations, accumulate
from collections import deque
from heapq import heappush, heappop, heapify
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
def main():
n = int(input())
a= [0] * (n-1)
b = [0] * (n-1)
g = [[] for _ in range(n+1)]
c = [[0]*(n+1) for _ in range(n+1)]
cn = 0
for i in range(n-1):
a[i], b[i] = map(int, input().split())
g[a[i]].append(b[i])
for i in range(1, n+1):
cc = [_ for _ in range(1, n) if _ not in c[i]]
heapify(cc)
for j in g[i]:
c[i][j] = c[j][i] = heappop(cc)
cn = max(cn, c[i][j])
print(cn)
for p, q in zip(a, b):
print(c[p][q])
if __name__ == '__main__':
main()
``` | instruction | 0 | 45,204 | 13 | 90,408 |
No | output | 1 | 45,204 | 13 | 90,409 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given is a tree G with N vertices. The vertices are numbered 1 through N, and the i-th edge connects Vertex a_i and Vertex b_i.
Consider painting the edges in G with some number of colors. We want to paint them so that, for each vertex, the colors of the edges incident to that vertex are all different.
Among the colorings satisfying the condition above, construct one that uses the minimum number of colors.
Constraints
* 2 \le N \le 10^5
* 1 \le a_i \lt b_i \le N
* All values in input are integers.
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}
Output
Print N lines.
The first line should contain K, the number of colors used.
The (i+1)-th line (1 \le i \le N-1) should contain c_i, the integer representing the color of the i-th edge, where 1 \le c_i \le K must hold.
If there are multiple colorings with the minimum number of colors that satisfy the condition, printing any of them will be accepted.
Examples
Input
3
1 2
2 3
Output
2
1
2
Input
8
1 2
2 3
2 4
2 5
4 7
5 6
6 8
Output
4
1
2
3
4
1
1
2
Input
6
1 2
1 3
1 4
1 5
1 6
Output
5
1
2
3
4
5
Submitted Solution:
```
#import numpy as np
def search_color(edge,next_edge,color_check):
ind=color_check[edge].index(True)
color_check[edge][ind]=False
color_check[next_edge][ind]=False
return ind
#bfs内で処理すると使い回しが効かないのでsearch_color とか幅探索後の具体的な処理は別個にしたほうがいい
def bfs(s,n,node):
side_num_arr=list(map(lambda x :len(x),node))
l=max(side_num_arr)
print(l)
queue=[-1 for _ in range(n)]
now=0
queue[now]=s
color_check=[[True for _ in range(l)] for _ in range(n)]
color=[-1 for _ in range(n-1)]
last_ind=1
while last_ind<n:
if side_num_arr[queue[now]]<=0:
now+=1
continue
for i in node[queue[now]]:
edge=i[0]
ind=i[1]
if side_num_arr[edge]<=0:
continue
side_num_arr[edge]-=1
side_num_arr[queue[now]]-=1
queue[last_ind]=edge
last_ind+=1
color[ind]=search_color(queue[now],edge,color_check)
now+=1
return color
n=int(input())
side=[list(map(int,input().split())) for _ in range(n-1)]
node=[[] for _ in range(n)]
ans=[0 for _ in range(n-1)]
for i in range(n-1):
u=side[i][0]-1
v=side[i][1]-1
node[u].append([v,i])
node[v].append([u,i])
#print(node)
ans=bfs(0,n,node)
for i in ans:
print(i+1)
``` | instruction | 0 | 45,205 | 13 | 90,410 |
No | output | 1 | 45,205 | 13 | 90,411 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given is a tree G with N vertices. The vertices are numbered 1 through N, and the i-th edge connects Vertex a_i and Vertex b_i.
Consider painting the edges in G with some number of colors. We want to paint them so that, for each vertex, the colors of the edges incident to that vertex are all different.
Among the colorings satisfying the condition above, construct one that uses the minimum number of colors.
Constraints
* 2 \le N \le 10^5
* 1 \le a_i \lt b_i \le N
* All values in input are integers.
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}
Output
Print N lines.
The first line should contain K, the number of colors used.
The (i+1)-th line (1 \le i \le N-1) should contain c_i, the integer representing the color of the i-th edge, where 1 \le c_i \le K must hold.
If there are multiple colorings with the minimum number of colors that satisfy the condition, printing any of them will be accepted.
Examples
Input
3
1 2
2 3
Output
2
1
2
Input
8
1 2
2 3
2 4
2 5
4 7
5 6
6 8
Output
4
1
2
3
4
1
1
2
Input
6
1 2
1 3
1 4
1 5
1 6
Output
5
1
2
3
4
5
Submitted Solution:
```
import collections
N = int(input())
ab = [list(map(int, input().split())) for _ in range(N-1)]
a = []
b = []
for i in range(N-1):
a.append(ab[i][0])
for i in range(N-1):
b.append(ab[i][1])
ab_max = []
for i in range(N-1):
ab_max.append(a[i])
ab_max.append(b[i])
c = collections.Counter(ab_max)
ans = max(c.values())
print(ans)
l = [i for i in range(1, N+1)]
ndic = {}
for i in range(1, N+1):
ndic[i] = l[:]
for i in range(N-1):
p = max(ndic[a[i]][0], ndic[b[i]][0])
print(p)
ndic[a[i]].remove(p)
ndic[b[i]].remove(p)
``` | instruction | 0 | 45,206 | 13 | 90,412 |
No | output | 1 | 45,206 | 13 | 90,413 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees!
Recently he found a binary search tree and instinctively nibbled all of its edges, hence messing up the vertices. Dima knows that if Andrew, who has been thoroughly assembling the tree for a long time, comes home and sees his creation demolished, he'll get extremely upset.
To not let that happen, Dima has to recover the binary search tree. Luckily, he noticed that any two vertices connected by a direct edge had their greatest common divisor value exceed 1.
Help Dima construct such a binary search tree or determine that it's impossible. The definition and properties of a binary search tree can be found [here.](https://en.wikipedia.org/wiki/Binary_search_tree)
Input
The first line contains the number of vertices n (2 ≤ n ≤ 700).
The second line features n distinct integers a_i (2 ≤ a_i ≤ 10^9) — the values of vertices in ascending order.
Output
If it is possible to reassemble the binary search tree, such that the greatest common divisor of any two vertices connected by the edge is greater than 1, print "Yes" (quotes for clarity).
Otherwise, print "No" (quotes for clarity).
Examples
Input
6
3 6 9 18 36 108
Output
Yes
Input
2
7 17
Output
No
Input
9
4 8 10 12 15 18 33 44 81
Output
Yes
Note
The picture below illustrates one of the possible trees for the first example.
<image>
The picture below illustrates one of the possible trees for the third example.
<image> | instruction | 0 | 45,394 | 13 | 90,788 |
Tags: brute force, dp, math, number theory, trees
Correct Solution:
```
from math import gcd
import random,time,sys
input=sys.stdin.buffer.readline
def main():
n=int(input())
a=list(map(int,input().split()))
#a=[2*random.randint(1,10**9) for i in range(n)]
start=time.time()
a+=[0]
GCD=[0 for i in range(n+1)]
for i in range(n+1):
for j in range(n+1):
if gcd(a[i],a[j])>1:
GCD[i]+=1<<j
check1=[1<<j for j in range(n+1)]
check2=[1<<j for j in range(n+1)]
for i in range(n):
check1[0]|=int(bool(check1[0]&check2[i]&GCD[i+1]))*(1<<(i+1))
for j in range(1,n-i):
check1[j]|=int(bool(check1[j]&check2[i+j]&GCD[j+i+1]))*(1<<(i+j+1))
check2[j+i]|=int(bool(check1[j]&check2[i+j]&GCD[j-1]))*(1<<(j-1))
ans=bool(check1[0]&check2[n-1])
print("Yes" if ans else "No")
#print(time.time()-start)
if __name__=="__main__":
main()
``` | output | 1 | 45,394 | 13 | 90,789 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees!
Recently he found a binary search tree and instinctively nibbled all of its edges, hence messing up the vertices. Dima knows that if Andrew, who has been thoroughly assembling the tree for a long time, comes home and sees his creation demolished, he'll get extremely upset.
To not let that happen, Dima has to recover the binary search tree. Luckily, he noticed that any two vertices connected by a direct edge had their greatest common divisor value exceed 1.
Help Dima construct such a binary search tree or determine that it's impossible. The definition and properties of a binary search tree can be found [here.](https://en.wikipedia.org/wiki/Binary_search_tree)
Input
The first line contains the number of vertices n (2 ≤ n ≤ 700).
The second line features n distinct integers a_i (2 ≤ a_i ≤ 10^9) — the values of vertices in ascending order.
Output
If it is possible to reassemble the binary search tree, such that the greatest common divisor of any two vertices connected by the edge is greater than 1, print "Yes" (quotes for clarity).
Otherwise, print "No" (quotes for clarity).
Examples
Input
6
3 6 9 18 36 108
Output
Yes
Input
2
7 17
Output
No
Input
9
4 8 10 12 15 18 33 44 81
Output
Yes
Note
The picture below illustrates one of the possible trees for the first example.
<image>
The picture below illustrates one of the possible trees for the third example.
<image> | instruction | 0 | 45,395 | 13 | 90,790 |
Tags: brute force, dp, math, number theory, trees
Correct Solution:
```
from sys import stdin
from math import gcd
n=int(stdin.readline())
a=[int(x) for x in stdin.readline().split()]
c = []
ld=[]
rd=[]
def check(l, r, e):
if r == l: return c[l][e] > 0
if e < l and ld[l][r-l] != 0:
return ld[l][r-l] == 1
elif e > r and rd[l][r-l] != 0:
return rd[l][r-l] == 1
for i in range(l, r+1):
if c[i][e]>0:
if i==l or check(l, i-1, i):
if i==r or check(i+1, r, i):
if e < l:
ld[l][r-l] = 1
else:
rd[l][r-l] = 1
return True
if e < l:
ld[l][r - l] = -1
else:
rd[l][r - l] = -1
return False
for i in range(n):
c.append([0]*n)
ld.append([0]*n)
rd.append([0] * n)
for i in range(n):
for j in range(i+1,n):
if gcd(a[i],a[j]) > 1:
c[i][j] = c[j][i] = 1
ans=False
for i in range(n):
if i == 0 or check(0, i - 1, i):
if i == n-1 or check(i + 1, n-1, i):
ans = True
break
if ans:
print("Yes")
else:
print("No")
``` | output | 1 | 45,395 | 13 | 90,791 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees!
Recently he found a binary search tree and instinctively nibbled all of its edges, hence messing up the vertices. Dima knows that if Andrew, who has been thoroughly assembling the tree for a long time, comes home and sees his creation demolished, he'll get extremely upset.
To not let that happen, Dima has to recover the binary search tree. Luckily, he noticed that any two vertices connected by a direct edge had their greatest common divisor value exceed 1.
Help Dima construct such a binary search tree or determine that it's impossible. The definition and properties of a binary search tree can be found [here.](https://en.wikipedia.org/wiki/Binary_search_tree)
Input
The first line contains the number of vertices n (2 ≤ n ≤ 700).
The second line features n distinct integers a_i (2 ≤ a_i ≤ 10^9) — the values of vertices in ascending order.
Output
If it is possible to reassemble the binary search tree, such that the greatest common divisor of any two vertices connected by the edge is greater than 1, print "Yes" (quotes for clarity).
Otherwise, print "No" (quotes for clarity).
Examples
Input
6
3 6 9 18 36 108
Output
Yes
Input
2
7 17
Output
No
Input
9
4 8 10 12 15 18 33 44 81
Output
Yes
Note
The picture below illustrates one of the possible trees for the first example.
<image>
The picture below illustrates one of the possible trees for the third example.
<image> | instruction | 0 | 45,396 | 13 | 90,792 |
Tags: brute force, dp, math, number theory, trees
Correct Solution:
```
from math import gcd
import random,time,sys
input=sys.stdin.buffer.readline
def main():
n=int(input())
a=list(map(int,input().split()))
#a=[2*random.randint(1,10**9) for i in range(n)]
start=time.time()
a+=[0]
dp=[[False for j in range(n)] for i in range(n)]
GCD=[0 for i in range(n+1)]
for i in range(n+1):
for j in range(n+1):
if gcd(a[i],a[j])>1:
GCD[i]+=1<<j
check1=[1<<j for j in range(n+1)]
check2=[1<<j for j in range(n+1)]
for i in range(n):
for j in range(n-i):
dp[j][i+j]=bool(check1[j]&check2[i+j])
check1[j]|=int(bool(check1[j]&check2[i+j]&GCD[j+i+1]))*(1<<(i+j+1))
if j!=0:
check2[j+i]|=int(bool(check1[j]&check2[i+j]&GCD[j-1]))*(1<<(j-1))
print("Yes" if dp[0][n-1] else "No")
#print(time.time()-start)
if __name__=="__main__":
main()
``` | output | 1 | 45,396 | 13 | 90,793 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees!
Recently he found a binary search tree and instinctively nibbled all of its edges, hence messing up the vertices. Dima knows that if Andrew, who has been thoroughly assembling the tree for a long time, comes home and sees his creation demolished, he'll get extremely upset.
To not let that happen, Dima has to recover the binary search tree. Luckily, he noticed that any two vertices connected by a direct edge had their greatest common divisor value exceed 1.
Help Dima construct such a binary search tree or determine that it's impossible. The definition and properties of a binary search tree can be found [here.](https://en.wikipedia.org/wiki/Binary_search_tree)
Input
The first line contains the number of vertices n (2 ≤ n ≤ 700).
The second line features n distinct integers a_i (2 ≤ a_i ≤ 10^9) — the values of vertices in ascending order.
Output
If it is possible to reassemble the binary search tree, such that the greatest common divisor of any two vertices connected by the edge is greater than 1, print "Yes" (quotes for clarity).
Otherwise, print "No" (quotes for clarity).
Examples
Input
6
3 6 9 18 36 108
Output
Yes
Input
2
7 17
Output
No
Input
9
4 8 10 12 15 18 33 44 81
Output
Yes
Note
The picture below illustrates one of the possible trees for the first example.
<image>
The picture below illustrates one of the possible trees for the third example.
<image> | instruction | 0 | 45,397 | 13 | 90,794 |
Tags: brute force, dp, math, number theory, trees
Correct Solution:
```
from math import gcd
import random,time,sys
input=sys.stdin.buffer.readline
def main():
n=int(input())
a=list(map(int,input().split()))
#a=[2*random.randint(1,10**9) for i in range(n)]
start=time.time()
GCD=[1<<i for i in range(n+1)]
for i in range(n):
for j in range(i):
if gcd(a[i],a[j])>1:
GCD[i]+=1<<j
GCD[j]+=1<<i
check1=[1<<j for j in range(n+1)]
check2=[1<<j for j in range(n+1)]
for i in range(n):
check1[0]|=int(bool(check1[0]&check2[i]&GCD[i+1]))*(1<<(i+1))
for j in range(1,n-i):
check1[j]|=int(bool(check1[j]&check2[i+j]&GCD[j+i+1]))*(1<<(i+j+1))
check2[j+i]|=int(bool(check1[j]&check2[i+j]&GCD[j-1]))*(1<<(j-1))
ans=bool(check1[0]&check2[n-1])
print("Yes" if ans else "No")
#print(time.time()-start)
if __name__=="__main__":
main()
``` | output | 1 | 45,397 | 13 | 90,795 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees!
Recently he found a binary search tree and instinctively nibbled all of its edges, hence messing up the vertices. Dima knows that if Andrew, who has been thoroughly assembling the tree for a long time, comes home and sees his creation demolished, he'll get extremely upset.
To not let that happen, Dima has to recover the binary search tree. Luckily, he noticed that any two vertices connected by a direct edge had their greatest common divisor value exceed 1.
Help Dima construct such a binary search tree or determine that it's impossible. The definition and properties of a binary search tree can be found [here.](https://en.wikipedia.org/wiki/Binary_search_tree)
Input
The first line contains the number of vertices n (2 ≤ n ≤ 700).
The second line features n distinct integers a_i (2 ≤ a_i ≤ 10^9) — the values of vertices in ascending order.
Output
If it is possible to reassemble the binary search tree, such that the greatest common divisor of any two vertices connected by the edge is greater than 1, print "Yes" (quotes for clarity).
Otherwise, print "No" (quotes for clarity).
Examples
Input
6
3 6 9 18 36 108
Output
Yes
Input
2
7 17
Output
No
Input
9
4 8 10 12 15 18 33 44 81
Output
Yes
Note
The picture below illustrates one of the possible trees for the first example.
<image>
The picture below illustrates one of the possible trees for the third example.
<image>
Submitted Solution:
```
from sys import stdin
from math import gcd
n=int(stdin.readline())
a=[int(x) for x in stdin.readline().split()]
c = []
ld=[]
rd=[]
def check(l, r, e):
if r == l: return c[l][e] > 0
if e < l and ld[l][r-l] != 0:
return ld[l][r-l] == 1
elif e > r and rd[l][r-l] != 0:
return rd[l][r-l] == 1
for i in range(l,r+1):
if c[i][e]>0:
if i==l or check(l, i-1, i):
if i==r or check(i+1, r, i):
if e < l:
ld[l][r-l] = 1
else:
rd[l][r-l] = 1
return True
if e < l:
ld[l][r - l] = -1
else:
rd[l][r - l] = -1
return True
for i in range(n):
c.append([0]*n)
ld.append([0]*n)
rd.append([0] * n)
for i in range(n):
for j in range(i+1,n):
if gcd(a[i],a[j]) > 1:
c[i][j] = c[j][i] = 1
ans=False
for i in range(n):
if i == 0 or check(0, i - 1, i):
if i == n-1 or check(i + 1, n-1, i):
ans = True
break
if ans:
print("Yes")
else:
print("No")
``` | instruction | 0 | 45,398 | 13 | 90,796 |
No | output | 1 | 45,398 | 13 | 90,797 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees!
Recently he found a binary search tree and instinctively nibbled all of its edges, hence messing up the vertices. Dima knows that if Andrew, who has been thoroughly assembling the tree for a long time, comes home and sees his creation demolished, he'll get extremely upset.
To not let that happen, Dima has to recover the binary search tree. Luckily, he noticed that any two vertices connected by a direct edge had their greatest common divisor value exceed 1.
Help Dima construct such a binary search tree or determine that it's impossible. The definition and properties of a binary search tree can be found [here.](https://en.wikipedia.org/wiki/Binary_search_tree)
Input
The first line contains the number of vertices n (2 ≤ n ≤ 700).
The second line features n distinct integers a_i (2 ≤ a_i ≤ 10^9) — the values of vertices in ascending order.
Output
If it is possible to reassemble the binary search tree, such that the greatest common divisor of any two vertices connected by the edge is greater than 1, print "Yes" (quotes for clarity).
Otherwise, print "No" (quotes for clarity).
Examples
Input
6
3 6 9 18 36 108
Output
Yes
Input
2
7 17
Output
No
Input
9
4 8 10 12 15 18 33 44 81
Output
Yes
Note
The picture below illustrates one of the possible trees for the first example.
<image>
The picture below illustrates one of the possible trees for the third example.
<image>
Submitted Solution:
```
def nod(x, y):
if (y == 0):
return x
return nod(y, x % y)
n = int(input())
arr = [int(i) for i in input().split()]
dp = [[int(i < j) for j in range(n + 1)] for i in range(n + 1)]
for i in range(n):
dp[i][i] = arr[i]
for i in range(2, n + 1):
for j in range(n - i + 1):
l = j
r = i + j - 1
for k in range(l, r + 1):
if (nod(arr[k], dp[l][k - 1]) != -1 and nod(arr[k], dp[k + 1][r]) != -1):
dp[l][r] *= arr[k];
if (dp[0][n - 1] == 1):
print("No")
else:
print("Yes")
``` | instruction | 0 | 45,399 | 13 | 90,798 |
No | output | 1 | 45,399 | 13 | 90,799 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees!
Recently he found a binary search tree and instinctively nibbled all of its edges, hence messing up the vertices. Dima knows that if Andrew, who has been thoroughly assembling the tree for a long time, comes home and sees his creation demolished, he'll get extremely upset.
To not let that happen, Dima has to recover the binary search tree. Luckily, he noticed that any two vertices connected by a direct edge had their greatest common divisor value exceed 1.
Help Dima construct such a binary search tree or determine that it's impossible. The definition and properties of a binary search tree can be found [here.](https://en.wikipedia.org/wiki/Binary_search_tree)
Input
The first line contains the number of vertices n (2 ≤ n ≤ 700).
The second line features n distinct integers a_i (2 ≤ a_i ≤ 10^9) — the values of vertices in ascending order.
Output
If it is possible to reassemble the binary search tree, such that the greatest common divisor of any two vertices connected by the edge is greater than 1, print "Yes" (quotes for clarity).
Otherwise, print "No" (quotes for clarity).
Examples
Input
6
3 6 9 18 36 108
Output
Yes
Input
2
7 17
Output
No
Input
9
4 8 10 12 15 18 33 44 81
Output
Yes
Note
The picture below illustrates one of the possible trees for the first example.
<image>
The picture below illustrates one of the possible trees for the third example.
<image>
Submitted Solution:
```
def gcd(a,b):
while b!=0:
t=int(a)
a=int(b)
b=t%a
return int(a)
n=int(input())
s=list(map(int,input().split()))
d=dict()
for i in s:
d.update({i:False})
dp=[s[0]]
while len(dp)>0:
aux=[]
for i in dp:
if not d[i]:
d[i]=True
for j in s:
if not d[j] and gcd(i,j)>1:
aux.append(j)
dp=aux.copy()
t=True
for i in d:
if d[i]==False:
t=False
break
if t:
print("YES")
else:
print("NO")
``` | instruction | 0 | 45,400 | 13 | 90,800 |
No | output | 1 | 45,400 | 13 | 90,801 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees!
Recently he found a binary search tree and instinctively nibbled all of its edges, hence messing up the vertices. Dima knows that if Andrew, who has been thoroughly assembling the tree for a long time, comes home and sees his creation demolished, he'll get extremely upset.
To not let that happen, Dima has to recover the binary search tree. Luckily, he noticed that any two vertices connected by a direct edge had their greatest common divisor value exceed 1.
Help Dima construct such a binary search tree or determine that it's impossible. The definition and properties of a binary search tree can be found [here.](https://en.wikipedia.org/wiki/Binary_search_tree)
Input
The first line contains the number of vertices n (2 ≤ n ≤ 700).
The second line features n distinct integers a_i (2 ≤ a_i ≤ 10^9) — the values of vertices in ascending order.
Output
If it is possible to reassemble the binary search tree, such that the greatest common divisor of any two vertices connected by the edge is greater than 1, print "Yes" (quotes for clarity).
Otherwise, print "No" (quotes for clarity).
Examples
Input
6
3 6 9 18 36 108
Output
Yes
Input
2
7 17
Output
No
Input
9
4 8 10 12 15 18 33 44 81
Output
Yes
Note
The picture below illustrates one of the possible trees for the first example.
<image>
The picture below illustrates one of the possible trees for the third example.
<image>
Submitted Solution:
```
#!/usr/bin/python3
import array
import math
import sys
def inp():
return sys.stdin.readline().rstrip()
def solve(N, A):
G = [bytearray(N) for _ in range(N)]
for i in range(N):
for j in range(i + 1, N):
if math.gcd(A[i], A[j]) > 1:
G[i][j] = 1
G[j][i] = 1
lb = array.array('H', [i for i in range(N)])
rb = array.array('H', [i + 1 for i in range(N)])
pi = 0
for i in range(1, N):
if G[pi][i] == 0:
for j in range(pi, i):
lb[j] = min(lb[j], pi)
rb[j] = max(rb[j], i)
pi = i
for j in range(pi, N):
lb[j] = min(lb[j], pi)
rb[j] = max(rb[j], N)
while True:
changed = False
for i in range(N):
for j in range(lb[i] - 1, -1, -1):
if G[i][j] == 0:
continue
if rb[j] >= i and lb[i] > lb[j]:
lb[i] = lb[j]
changed = True
for j in range(rb[i], N):
if G[i][j] == 0:
continue
if lb[j] <= i + 1 and rb[i] < rb[j]:
rb[i] = rb[j]
changed = True
if lb[i] == 0 and rb[i] == N:
return True
if not changed:
break
return False
def main():
N = int(inp())
A = [int(e) for e in inp().split()]
assert len(A) == N
print('Yes' if solve(N, A) else 'No')
if __name__ == '__main__':
main()
``` | instruction | 0 | 45,401 | 13 | 90,802 |
No | output | 1 | 45,401 | 13 | 90,803 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a graph with 3 ⋅ n vertices and m edges. You are to find a matching of n edges, or an independent set of n vertices.
A set of edges is called a matching if no two edges share an endpoint.
A set of vertices is called an independent set if no two vertices are connected with an edge.
Input
The first line contains a single integer T ≥ 1 — the number of graphs you need to process. The description of T graphs follows.
The first line of description of a single graph contains two integers n and m, where 3 ⋅ n is the number of vertices, and m is the number of edges in the graph (1 ≤ n ≤ 10^{5}, 0 ≤ m ≤ 5 ⋅ 10^{5}).
Each of the next m lines contains two integers v_i and u_i (1 ≤ v_i, u_i ≤ 3 ⋅ n), meaning that there is an edge between vertices v_i and u_i.
It is guaranteed that there are no self-loops and no multiple edges in the graph.
It is guaranteed that the sum of all n over all graphs in a single test does not exceed 10^{5}, and the sum of all m over all graphs in a single test does not exceed 5 ⋅ 10^{5}.
Output
Print your answer for each of the T graphs. Output your answer for a single graph in the following format.
If you found a matching of size n, on the first line print "Matching" (without quotes), and on the second line print n integers — the indices of the edges in the matching. The edges are numbered from 1 to m in the input order.
If you found an independent set of size n, on the first line print "IndSet" (without quotes), and on the second line print n integers — the indices of the vertices in the independent set.
If there is no matching and no independent set of the specified size, print "Impossible" (without quotes).
You can print edges and vertices in any order.
If there are several solutions, print any. In particular, if there are both a matching of size n, and an independent set of size n, then you should print exactly one of such matchings or exactly one of such independent sets.
Example
Input
4
1 2
1 3
1 2
1 2
1 3
1 2
2 5
1 2
3 1
1 4
5 1
1 6
2 15
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6
Output
Matching
2
IndSet
1
IndSet
2 4
Matching
1 15
Note
The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer.
The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly n.
The fourth graph does not have an independent set of size 2, but there is a matching of size 2. | instruction | 0 | 45,464 | 13 | 90,928 |
Tags: constructive algorithms, graphs, greedy, sortings
Correct Solution:
```
def naiveSolve(n):
return
def main():
# Try to make the matching by taking as many random edges as possible.
# Any vertex that is excluded will either be connected to nothing or to
# a vertex in the matching. Hence it cannot be connected to another
# vertex outside the matching and has to form an independent set.
# If there are x matching edges, there will be 2*x vertices in the matching
# and 3*n-2*x vertices outside the matching (i.e. in the independent set).
# If x>=n, we have >=n matching edges. Else, we have > n vertices in the independent set.
t=int(input())
allans=[]
for _ in range(t):
n,m=readIntArr()
edges=[]
for _ in range(m):
u,v=readIntArr()
edges.append([u,v])
visited=[False for _ in range(3*n+1)]
matching=[]
for i,[u,v] in enumerate(edges):
if visited[u]==False and visited[v]==False:
matching.append(i+1)
visited[u]=visited[v]=True
if len(matching)>=n:
allans.append(['Matching'])
allans.append(matching[:n])
else:
ind=[]
for i in range(1,3*n+1):
if visited[i]==False:
ind.append(i)
if len(ind)==n:
break
allans.append(['IndSet'])
allans.append(ind)
multiLineArrayOfArraysPrint(allans)
return
import sys
input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok)
# input=lambda: sys.stdin.readline().rstrip("\r\n") #FOR READING STRING/TEXT INPUTS.
def oneLineArrayPrint(arr):
print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
# def readFloatArr():
# return [float(x) for x in input().split()]
def makeArr(defaultValFactory,dimensionArr): # eg. makeArr(lambda:0,[n,m])
dv=defaultValFactory;da=dimensionArr
if len(da)==1:return [dv() for _ in range(da[0])]
else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
def queryInteractive(l,r):
print('? {} {}'.format(l,r))
sys.stdout.flush()
return int(input())
def answerInteractive(x):
print('! {}'.format(x))
sys.stdout.flush()
inf=float('inf')
MOD=10**9+7
# MOD=998244353
from math import gcd,floor,ceil
for _abc in range(1):
main()
``` | output | 1 | 45,464 | 13 | 90,929 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a graph with 3 ⋅ n vertices and m edges. You are to find a matching of n edges, or an independent set of n vertices.
A set of edges is called a matching if no two edges share an endpoint.
A set of vertices is called an independent set if no two vertices are connected with an edge.
Input
The first line contains a single integer T ≥ 1 — the number of graphs you need to process. The description of T graphs follows.
The first line of description of a single graph contains two integers n and m, where 3 ⋅ n is the number of vertices, and m is the number of edges in the graph (1 ≤ n ≤ 10^{5}, 0 ≤ m ≤ 5 ⋅ 10^{5}).
Each of the next m lines contains two integers v_i and u_i (1 ≤ v_i, u_i ≤ 3 ⋅ n), meaning that there is an edge between vertices v_i and u_i.
It is guaranteed that there are no self-loops and no multiple edges in the graph.
It is guaranteed that the sum of all n over all graphs in a single test does not exceed 10^{5}, and the sum of all m over all graphs in a single test does not exceed 5 ⋅ 10^{5}.
Output
Print your answer for each of the T graphs. Output your answer for a single graph in the following format.
If you found a matching of size n, on the first line print "Matching" (without quotes), and on the second line print n integers — the indices of the edges in the matching. The edges are numbered from 1 to m in the input order.
If you found an independent set of size n, on the first line print "IndSet" (without quotes), and on the second line print n integers — the indices of the vertices in the independent set.
If there is no matching and no independent set of the specified size, print "Impossible" (without quotes).
You can print edges and vertices in any order.
If there are several solutions, print any. In particular, if there are both a matching of size n, and an independent set of size n, then you should print exactly one of such matchings or exactly one of such independent sets.
Example
Input
4
1 2
1 3
1 2
1 2
1 3
1 2
2 5
1 2
3 1
1 4
5 1
1 6
2 15
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6
Output
Matching
2
IndSet
1
IndSet
2 4
Matching
1 15
Note
The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer.
The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly n.
The fourth graph does not have an independent set of size 2, but there is a matching of size 2. | instruction | 0 | 45,465 | 13 | 90,930 |
Tags: constructive algorithms, graphs, greedy, sortings
Correct Solution:
```
import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
t = int(input())
for _ in range(t):
n, m = map(int, input().split())
edge = []
for i in range(m):
u, v = map(int, input().split())
u, v = u-1, v-1
edge.append((u, v))
used = set()
M = set()
for i, (u, v) in enumerate(edge):
if u not in used and v not in used:
M.add(i+1)
used.add(u)
used.add(v)
if len(M) >= n:
M = list(M)
M = M[0:n]
print('Matching')
print(*M)
continue
S = []
for i in range(3*n):
if i not in used:
S.append(i+1)
if len(S) == n:
break
if len(S) == n:
print('IndSet')
print(*S)
continue
print('Impossible')
``` | output | 1 | 45,465 | 13 | 90,931 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a graph with 3 ⋅ n vertices and m edges. You are to find a matching of n edges, or an independent set of n vertices.
A set of edges is called a matching if no two edges share an endpoint.
A set of vertices is called an independent set if no two vertices are connected with an edge.
Input
The first line contains a single integer T ≥ 1 — the number of graphs you need to process. The description of T graphs follows.
The first line of description of a single graph contains two integers n and m, where 3 ⋅ n is the number of vertices, and m is the number of edges in the graph (1 ≤ n ≤ 10^{5}, 0 ≤ m ≤ 5 ⋅ 10^{5}).
Each of the next m lines contains two integers v_i and u_i (1 ≤ v_i, u_i ≤ 3 ⋅ n), meaning that there is an edge between vertices v_i and u_i.
It is guaranteed that there are no self-loops and no multiple edges in the graph.
It is guaranteed that the sum of all n over all graphs in a single test does not exceed 10^{5}, and the sum of all m over all graphs in a single test does not exceed 5 ⋅ 10^{5}.
Output
Print your answer for each of the T graphs. Output your answer for a single graph in the following format.
If you found a matching of size n, on the first line print "Matching" (without quotes), and on the second line print n integers — the indices of the edges in the matching. The edges are numbered from 1 to m in the input order.
If you found an independent set of size n, on the first line print "IndSet" (without quotes), and on the second line print n integers — the indices of the vertices in the independent set.
If there is no matching and no independent set of the specified size, print "Impossible" (without quotes).
You can print edges and vertices in any order.
If there are several solutions, print any. In particular, if there are both a matching of size n, and an independent set of size n, then you should print exactly one of such matchings or exactly one of such independent sets.
Example
Input
4
1 2
1 3
1 2
1 2
1 3
1 2
2 5
1 2
3 1
1 4
5 1
1 6
2 15
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6
Output
Matching
2
IndSet
1
IndSet
2 4
Matching
1 15
Note
The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer.
The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly n.
The fourth graph does not have an independent set of size 2, but there is a matching of size 2. | instruction | 0 | 45,466 | 13 | 90,932 |
Tags: constructive algorithms, graphs, greedy, sortings
Correct Solution:
```
from sys import stdin
input = stdin.readline
T = int(input())
for _ in range(T):
n, m = [int(i) for i in input().split()]
ind_edg_v = [True]*(3*n+1)
ind_edg_e = [0]*n
num_edg = 0
for j in range(m):
edge_0, edge_1 = [int(i) for i in input().split()]
if num_edg < n:
if ind_edg_v[edge_0] and ind_edg_v[edge_1]:
ind_edg_e[num_edg] = j+1
ind_edg_v[edge_0], ind_edg_v[edge_1] = False, False
num_edg += 1
if num_edg == n:
print("Matching")
print(' '.join([str(i) for i in ind_edg_e]))
else:
print("IndSet")
vertex = 0
for i in range(n):
vertex += 1
while not ind_edg_v[vertex]:
vertex += 1
print(vertex, end = ' ')
print()
``` | output | 1 | 45,466 | 13 | 90,933 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a graph with 3 ⋅ n vertices and m edges. You are to find a matching of n edges, or an independent set of n vertices.
A set of edges is called a matching if no two edges share an endpoint.
A set of vertices is called an independent set if no two vertices are connected with an edge.
Input
The first line contains a single integer T ≥ 1 — the number of graphs you need to process. The description of T graphs follows.
The first line of description of a single graph contains two integers n and m, where 3 ⋅ n is the number of vertices, and m is the number of edges in the graph (1 ≤ n ≤ 10^{5}, 0 ≤ m ≤ 5 ⋅ 10^{5}).
Each of the next m lines contains two integers v_i and u_i (1 ≤ v_i, u_i ≤ 3 ⋅ n), meaning that there is an edge between vertices v_i and u_i.
It is guaranteed that there are no self-loops and no multiple edges in the graph.
It is guaranteed that the sum of all n over all graphs in a single test does not exceed 10^{5}, and the sum of all m over all graphs in a single test does not exceed 5 ⋅ 10^{5}.
Output
Print your answer for each of the T graphs. Output your answer for a single graph in the following format.
If you found a matching of size n, on the first line print "Matching" (without quotes), and on the second line print n integers — the indices of the edges in the matching. The edges are numbered from 1 to m in the input order.
If you found an independent set of size n, on the first line print "IndSet" (without quotes), and on the second line print n integers — the indices of the vertices in the independent set.
If there is no matching and no independent set of the specified size, print "Impossible" (without quotes).
You can print edges and vertices in any order.
If there are several solutions, print any. In particular, if there are both a matching of size n, and an independent set of size n, then you should print exactly one of such matchings or exactly one of such independent sets.
Example
Input
4
1 2
1 3
1 2
1 2
1 3
1 2
2 5
1 2
3 1
1 4
5 1
1 6
2 15
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6
Output
Matching
2
IndSet
1
IndSet
2 4
Matching
1 15
Note
The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer.
The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly n.
The fourth graph does not have an independent set of size 2, but there is a matching of size 2. | instruction | 0 | 45,467 | 13 | 90,934 |
Tags: constructive algorithms, graphs, greedy, sortings
Correct Solution:
```
from sys import stdin
input = stdin.readline
def solve_graph():
n,m = map(int, input().split())
included = [False] + ([True]*(3*n))
matching =[]
for i in range(1,m+1):
e1, e2 = map(int, input().split())
if included[e1] and included[e2]:
matching.append(i)
included[e1] = False
included[e2] = False
if len(matching) >= n:
print("Matching")
print(*matching[:n])
return
intset = [x for (x,b) in enumerate(included) if b]
print("IndSet")
print(*(list(intset)[:n]))
t = int(input())
for _ in range(t):
solve_graph()
``` | output | 1 | 45,467 | 13 | 90,935 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a graph with 3 ⋅ n vertices and m edges. You are to find a matching of n edges, or an independent set of n vertices.
A set of edges is called a matching if no two edges share an endpoint.
A set of vertices is called an independent set if no two vertices are connected with an edge.
Input
The first line contains a single integer T ≥ 1 — the number of graphs you need to process. The description of T graphs follows.
The first line of description of a single graph contains two integers n and m, where 3 ⋅ n is the number of vertices, and m is the number of edges in the graph (1 ≤ n ≤ 10^{5}, 0 ≤ m ≤ 5 ⋅ 10^{5}).
Each of the next m lines contains two integers v_i and u_i (1 ≤ v_i, u_i ≤ 3 ⋅ n), meaning that there is an edge between vertices v_i and u_i.
It is guaranteed that there are no self-loops and no multiple edges in the graph.
It is guaranteed that the sum of all n over all graphs in a single test does not exceed 10^{5}, and the sum of all m over all graphs in a single test does not exceed 5 ⋅ 10^{5}.
Output
Print your answer for each of the T graphs. Output your answer for a single graph in the following format.
If you found a matching of size n, on the first line print "Matching" (without quotes), and on the second line print n integers — the indices of the edges in the matching. The edges are numbered from 1 to m in the input order.
If you found an independent set of size n, on the first line print "IndSet" (without quotes), and on the second line print n integers — the indices of the vertices in the independent set.
If there is no matching and no independent set of the specified size, print "Impossible" (without quotes).
You can print edges and vertices in any order.
If there are several solutions, print any. In particular, if there are both a matching of size n, and an independent set of size n, then you should print exactly one of such matchings or exactly one of such independent sets.
Example
Input
4
1 2
1 3
1 2
1 2
1 3
1 2
2 5
1 2
3 1
1 4
5 1
1 6
2 15
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6
Output
Matching
2
IndSet
1
IndSet
2 4
Matching
1 15
Note
The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer.
The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly n.
The fourth graph does not have an independent set of size 2, but there is a matching of size 2. | instruction | 0 | 45,468 | 13 | 90,936 |
Tags: constructive algorithms, graphs, greedy, sortings
Correct Solution:
```
from sys import stdin
input = stdin.readline
myint = int
def solve_graph():
n,m = map(myint, input().split())
included = [False] + ([True]*(3*n))
matching =[]
for i in range(1,m+1):
e1, e2 = map(myint, input().split())
if included[e1] and included[e2]:
matching.append(i)
included[e1] = False
included[e2] = False
if len(matching) >= n:
print("Matching")
print(*matching[:n])
return
intset = [x for (x,b) in enumerate(included) if b]
print("IndSet")
print(*(list(intset)[:n]))
t = myint(input())
for _ in range(t):
solve_graph()
``` | output | 1 | 45,468 | 13 | 90,937 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a graph with 3 ⋅ n vertices and m edges. You are to find a matching of n edges, or an independent set of n vertices.
A set of edges is called a matching if no two edges share an endpoint.
A set of vertices is called an independent set if no two vertices are connected with an edge.
Input
The first line contains a single integer T ≥ 1 — the number of graphs you need to process. The description of T graphs follows.
The first line of description of a single graph contains two integers n and m, where 3 ⋅ n is the number of vertices, and m is the number of edges in the graph (1 ≤ n ≤ 10^{5}, 0 ≤ m ≤ 5 ⋅ 10^{5}).
Each of the next m lines contains two integers v_i and u_i (1 ≤ v_i, u_i ≤ 3 ⋅ n), meaning that there is an edge between vertices v_i and u_i.
It is guaranteed that there are no self-loops and no multiple edges in the graph.
It is guaranteed that the sum of all n over all graphs in a single test does not exceed 10^{5}, and the sum of all m over all graphs in a single test does not exceed 5 ⋅ 10^{5}.
Output
Print your answer for each of the T graphs. Output your answer for a single graph in the following format.
If you found a matching of size n, on the first line print "Matching" (without quotes), and on the second line print n integers — the indices of the edges in the matching. The edges are numbered from 1 to m in the input order.
If you found an independent set of size n, on the first line print "IndSet" (without quotes), and on the second line print n integers — the indices of the vertices in the independent set.
If there is no matching and no independent set of the specified size, print "Impossible" (without quotes).
You can print edges and vertices in any order.
If there are several solutions, print any. In particular, if there are both a matching of size n, and an independent set of size n, then you should print exactly one of such matchings or exactly one of such independent sets.
Example
Input
4
1 2
1 3
1 2
1 2
1 3
1 2
2 5
1 2
3 1
1 4
5 1
1 6
2 15
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6
Output
Matching
2
IndSet
1
IndSet
2 4
Matching
1 15
Note
The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer.
The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly n.
The fourth graph does not have an independent set of size 2, but there is a matching of size 2. | instruction | 0 | 45,469 | 13 | 90,938 |
Tags: constructive algorithms, graphs, greedy, sortings
Correct Solution:
```
import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
n, m = map(int, input().split())
v = set(range(1, 3 * n + 1))
e = []
for i in range(1, m + 1):
a, b = map(int, input().split())
if a in v and b in v:
e.append(i)
v.remove(a)
v.remove(b)
if len(e) >= n:
print('Matching')
print(*e[:n])
else:
print('IndSet')
print(*list(v)[:n])
``` | output | 1 | 45,469 | 13 | 90,939 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a graph with 3 ⋅ n vertices and m edges. You are to find a matching of n edges, or an independent set of n vertices.
A set of edges is called a matching if no two edges share an endpoint.
A set of vertices is called an independent set if no two vertices are connected with an edge.
Input
The first line contains a single integer T ≥ 1 — the number of graphs you need to process. The description of T graphs follows.
The first line of description of a single graph contains two integers n and m, where 3 ⋅ n is the number of vertices, and m is the number of edges in the graph (1 ≤ n ≤ 10^{5}, 0 ≤ m ≤ 5 ⋅ 10^{5}).
Each of the next m lines contains two integers v_i and u_i (1 ≤ v_i, u_i ≤ 3 ⋅ n), meaning that there is an edge between vertices v_i and u_i.
It is guaranteed that there are no self-loops and no multiple edges in the graph.
It is guaranteed that the sum of all n over all graphs in a single test does not exceed 10^{5}, and the sum of all m over all graphs in a single test does not exceed 5 ⋅ 10^{5}.
Output
Print your answer for each of the T graphs. Output your answer for a single graph in the following format.
If you found a matching of size n, on the first line print "Matching" (without quotes), and on the second line print n integers — the indices of the edges in the matching. The edges are numbered from 1 to m in the input order.
If you found an independent set of size n, on the first line print "IndSet" (without quotes), and on the second line print n integers — the indices of the vertices in the independent set.
If there is no matching and no independent set of the specified size, print "Impossible" (without quotes).
You can print edges and vertices in any order.
If there are several solutions, print any. In particular, if there are both a matching of size n, and an independent set of size n, then you should print exactly one of such matchings or exactly one of such independent sets.
Example
Input
4
1 2
1 3
1 2
1 2
1 3
1 2
2 5
1 2
3 1
1 4
5 1
1 6
2 15
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6
Output
Matching
2
IndSet
1
IndSet
2 4
Matching
1 15
Note
The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer.
The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly n.
The fourth graph does not have an independent set of size 2, but there is a matching of size 2. | instruction | 0 | 45,470 | 13 | 90,940 |
Tags: constructive algorithms, graphs, greedy, sortings
Correct Solution:
```
import sys
input = sys.stdin.readline
T=int(input())
for testcases in range(T):
n,m=map(int,input().split())
EDGE=[[0,0]]+[list(map(int,input().split())) for i in range(m)]
USED=[0]*(3*n+1)
count=0
ANS=[]
for i in range(1,m+1):
x,y=EDGE[i]
if USED[x]==0 and USED[y]==0:
count+=1
ANS.append(i)
USED[x]=1
USED[y]=1
if count==n:
print("Matching")
print(*ANS)
break
else:
ANS=[]
count=0
for i in range(1,3*n+1):
if USED[i]==0:
count+=1
ANS.append(i)
if count==n:
print("IndSet")
print(*ANS)
break
``` | output | 1 | 45,470 | 13 | 90,941 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a graph with 3 ⋅ n vertices and m edges. You are to find a matching of n edges, or an independent set of n vertices.
A set of edges is called a matching if no two edges share an endpoint.
A set of vertices is called an independent set if no two vertices are connected with an edge.
Input
The first line contains a single integer T ≥ 1 — the number of graphs you need to process. The description of T graphs follows.
The first line of description of a single graph contains two integers n and m, where 3 ⋅ n is the number of vertices, and m is the number of edges in the graph (1 ≤ n ≤ 10^{5}, 0 ≤ m ≤ 5 ⋅ 10^{5}).
Each of the next m lines contains two integers v_i and u_i (1 ≤ v_i, u_i ≤ 3 ⋅ n), meaning that there is an edge between vertices v_i and u_i.
It is guaranteed that there are no self-loops and no multiple edges in the graph.
It is guaranteed that the sum of all n over all graphs in a single test does not exceed 10^{5}, and the sum of all m over all graphs in a single test does not exceed 5 ⋅ 10^{5}.
Output
Print your answer for each of the T graphs. Output your answer for a single graph in the following format.
If you found a matching of size n, on the first line print "Matching" (without quotes), and on the second line print n integers — the indices of the edges in the matching. The edges are numbered from 1 to m in the input order.
If you found an independent set of size n, on the first line print "IndSet" (without quotes), and on the second line print n integers — the indices of the vertices in the independent set.
If there is no matching and no independent set of the specified size, print "Impossible" (without quotes).
You can print edges and vertices in any order.
If there are several solutions, print any. In particular, if there are both a matching of size n, and an independent set of size n, then you should print exactly one of such matchings or exactly one of such independent sets.
Example
Input
4
1 2
1 3
1 2
1 2
1 3
1 2
2 5
1 2
3 1
1 4
5 1
1 6
2 15
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6
Output
Matching
2
IndSet
1
IndSet
2 4
Matching
1 15
Note
The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer.
The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly n.
The fourth graph does not have an independent set of size 2, but there is a matching of size 2. | instruction | 0 | 45,471 | 13 | 90,942 |
Tags: constructive algorithms, graphs, greedy, sortings
Correct Solution:
```
'''
cho n và m:
có 3*n đỉnh và m mối quan hệ giữa các đỉnh
kết hợp:
tập hợp các cạnh không có chung điểm cuối
độc lập:
tập hợp các điểm mà không có bất kì điểm nào nằm trong chung trong một cạnh
output:
in ra nếu có tập có độ lớn n thỏa kết hợp hoặc độc lập
nếu có hai kết quả in ra bất kì kết quả nào cũng được.
'''
from sys import stdin
input=stdin.readline
t=int(input())
for k in range(t):
n,m=map(int,input().split(' '))
a=[i for i in range(1,n+1)]
qq=set(range(1,3*n+1))
e=[]
for i in range(1,m+1):
a1,a2=map(int,input().split(' '))
if a1 in qq and a2 in qq:
e.append(i)
qq.remove(a1)
qq.remove(a2)
if(len(qq)>=n):
print('IndSet')
print(*list(qq)[:n])
else:
print('Matching')
print(*e[:n])
``` | output | 1 | 45,471 | 13 | 90,943 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a graph with 3 ⋅ n vertices and m edges. You are to find a matching of n edges, or an independent set of n vertices.
A set of edges is called a matching if no two edges share an endpoint.
A set of vertices is called an independent set if no two vertices are connected with an edge.
Input
The first line contains a single integer T ≥ 1 — the number of graphs you need to process. The description of T graphs follows.
The first line of description of a single graph contains two integers n and m, where 3 ⋅ n is the number of vertices, and m is the number of edges in the graph (1 ≤ n ≤ 10^{5}, 0 ≤ m ≤ 5 ⋅ 10^{5}).
Each of the next m lines contains two integers v_i and u_i (1 ≤ v_i, u_i ≤ 3 ⋅ n), meaning that there is an edge between vertices v_i and u_i.
It is guaranteed that there are no self-loops and no multiple edges in the graph.
It is guaranteed that the sum of all n over all graphs in a single test does not exceed 10^{5}, and the sum of all m over all graphs in a single test does not exceed 5 ⋅ 10^{5}.
Output
Print your answer for each of the T graphs. Output your answer for a single graph in the following format.
If you found a matching of size n, on the first line print "Matching" (without quotes), and on the second line print n integers — the indices of the edges in the matching. The edges are numbered from 1 to m in the input order.
If you found an independent set of size n, on the first line print "IndSet" (without quotes), and on the second line print n integers — the indices of the vertices in the independent set.
If there is no matching and no independent set of the specified size, print "Impossible" (without quotes).
You can print edges and vertices in any order.
If there are several solutions, print any. In particular, if there are both a matching of size n, and an independent set of size n, then you should print exactly one of such matchings or exactly one of such independent sets.
Example
Input
4
1 2
1 3
1 2
1 2
1 3
1 2
2 5
1 2
3 1
1 4
5 1
1 6
2 15
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6
Output
Matching
2
IndSet
1
IndSet
2 4
Matching
1 15
Note
The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer.
The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly n.
The fourth graph does not have an independent set of size 2, but there is a matching of size 2.
Submitted Solution:
```
from sys import stdin
from sys import setrecursionlimit as SRL; SRL(10**7)
rd = stdin.readline
rrd = lambda: map(int, rd().strip().split())
t = int(input())
while t:
n,m = map(int,rd().split())
out = [0] * (3 * n + 1)
mans = []
nans = []
for i in range(1,m+1):
u,v = map(int,rd().split())
if not out[u] and not out[v]:
out[u] = out[v] = 1
mans.append(i)
if len(mans) >= n:
print("Matching")
print(*mans[:n])
else:
print("IndSet")
for i in range(1,1+3*n):
if not out[i]:
nans.append(i)
if len(nans) >= n:
break
print(*nans)
t-=1
``` | instruction | 0 | 45,472 | 13 | 90,944 |
Yes | output | 1 | 45,472 | 13 | 90,945 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a graph with 3 ⋅ n vertices and m edges. You are to find a matching of n edges, or an independent set of n vertices.
A set of edges is called a matching if no two edges share an endpoint.
A set of vertices is called an independent set if no two vertices are connected with an edge.
Input
The first line contains a single integer T ≥ 1 — the number of graphs you need to process. The description of T graphs follows.
The first line of description of a single graph contains two integers n and m, where 3 ⋅ n is the number of vertices, and m is the number of edges in the graph (1 ≤ n ≤ 10^{5}, 0 ≤ m ≤ 5 ⋅ 10^{5}).
Each of the next m lines contains two integers v_i and u_i (1 ≤ v_i, u_i ≤ 3 ⋅ n), meaning that there is an edge between vertices v_i and u_i.
It is guaranteed that there are no self-loops and no multiple edges in the graph.
It is guaranteed that the sum of all n over all graphs in a single test does not exceed 10^{5}, and the sum of all m over all graphs in a single test does not exceed 5 ⋅ 10^{5}.
Output
Print your answer for each of the T graphs. Output your answer for a single graph in the following format.
If you found a matching of size n, on the first line print "Matching" (without quotes), and on the second line print n integers — the indices of the edges in the matching. The edges are numbered from 1 to m in the input order.
If you found an independent set of size n, on the first line print "IndSet" (without quotes), and on the second line print n integers — the indices of the vertices in the independent set.
If there is no matching and no independent set of the specified size, print "Impossible" (without quotes).
You can print edges and vertices in any order.
If there are several solutions, print any. In particular, if there are both a matching of size n, and an independent set of size n, then you should print exactly one of such matchings or exactly one of such independent sets.
Example
Input
4
1 2
1 3
1 2
1 2
1 3
1 2
2 5
1 2
3 1
1 4
5 1
1 6
2 15
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6
Output
Matching
2
IndSet
1
IndSet
2 4
Matching
1 15
Note
The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer.
The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly n.
The fourth graph does not have an independent set of size 2, but there is a matching of size 2.
Submitted Solution:
```
import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
N, M = map(int, input().split())
X = [[] for i in range(3*N)]
for i in range(M):
x, y = map(int, input().split())
x, y = min(x,y), max(x,y)
X[x-1].append((y-1, i+1))
MAT = []
IND = []
DONE = [0] * 3*N
for i in range(3*N):
if DONE[i]: continue
for j, ind in X[i]:
if DONE[j] == 0:
MAT.append(ind)
DONE[i] = 1
DONE[j] = 1
break
else:
IND.append(i+1)
if len(MAT) >= N:
print("Matching")
print(*MAT[:N])
else:
print("IndSet")
print(*IND[:N])
``` | instruction | 0 | 45,473 | 13 | 90,946 |
Yes | output | 1 | 45,473 | 13 | 90,947 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a graph with 3 ⋅ n vertices and m edges. You are to find a matching of n edges, or an independent set of n vertices.
A set of edges is called a matching if no two edges share an endpoint.
A set of vertices is called an independent set if no two vertices are connected with an edge.
Input
The first line contains a single integer T ≥ 1 — the number of graphs you need to process. The description of T graphs follows.
The first line of description of a single graph contains two integers n and m, where 3 ⋅ n is the number of vertices, and m is the number of edges in the graph (1 ≤ n ≤ 10^{5}, 0 ≤ m ≤ 5 ⋅ 10^{5}).
Each of the next m lines contains two integers v_i and u_i (1 ≤ v_i, u_i ≤ 3 ⋅ n), meaning that there is an edge between vertices v_i and u_i.
It is guaranteed that there are no self-loops and no multiple edges in the graph.
It is guaranteed that the sum of all n over all graphs in a single test does not exceed 10^{5}, and the sum of all m over all graphs in a single test does not exceed 5 ⋅ 10^{5}.
Output
Print your answer for each of the T graphs. Output your answer for a single graph in the following format.
If you found a matching of size n, on the first line print "Matching" (without quotes), and on the second line print n integers — the indices of the edges in the matching. The edges are numbered from 1 to m in the input order.
If you found an independent set of size n, on the first line print "IndSet" (without quotes), and on the second line print n integers — the indices of the vertices in the independent set.
If there is no matching and no independent set of the specified size, print "Impossible" (without quotes).
You can print edges and vertices in any order.
If there are several solutions, print any. In particular, if there are both a matching of size n, and an independent set of size n, then you should print exactly one of such matchings or exactly one of such independent sets.
Example
Input
4
1 2
1 3
1 2
1 2
1 3
1 2
2 5
1 2
3 1
1 4
5 1
1 6
2 15
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6
Output
Matching
2
IndSet
1
IndSet
2 4
Matching
1 15
Note
The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer.
The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly n.
The fourth graph does not have an independent set of size 2, but there is a matching of size 2.
Submitted Solution:
```
# inspired by https://codeforces.com/contest/1198/submission/58086142
def main():
T = int(input())
v_cover = set()
for _ in range(T):
n, m = map(int, input().split())
v_count = 3*n
v_cover.clear()
i_cover = []
for k in range(m):
edge = list(map(int, input().split()))
if edge[0] not in v_cover and edge[1] not in v_cover:
v_cover.add(edge[0])
v_cover.add(edge[1])
i_cover.append(k+1)
if len(i_cover) == n:
print('Matching')
print(*i_cover[:n])
for _ in range(k+1, m):
input()
break
if len(i_cover) < n:
#v_independent = set(range(1, v_count+1)) - v_cover
v_independent = []
for vert in range(1, 3*n+1):
if vert not in v_cover:
v_independent.append(vert)
if len(v_independent) == n:
print('IndSet')
#print(*random.sample(v_independent, n))
print(*v_independent)
break
if len(v_independent) < n:
print('Impossible')
# preamble
import sys
input=sys.stdin.readline
# main call
if __name__== "__main__":
main()
``` | instruction | 0 | 45,474 | 13 | 90,948 |
Yes | output | 1 | 45,474 | 13 | 90,949 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a graph with 3 ⋅ n vertices and m edges. You are to find a matching of n edges, or an independent set of n vertices.
A set of edges is called a matching if no two edges share an endpoint.
A set of vertices is called an independent set if no two vertices are connected with an edge.
Input
The first line contains a single integer T ≥ 1 — the number of graphs you need to process. The description of T graphs follows.
The first line of description of a single graph contains two integers n and m, where 3 ⋅ n is the number of vertices, and m is the number of edges in the graph (1 ≤ n ≤ 10^{5}, 0 ≤ m ≤ 5 ⋅ 10^{5}).
Each of the next m lines contains two integers v_i and u_i (1 ≤ v_i, u_i ≤ 3 ⋅ n), meaning that there is an edge between vertices v_i and u_i.
It is guaranteed that there are no self-loops and no multiple edges in the graph.
It is guaranteed that the sum of all n over all graphs in a single test does not exceed 10^{5}, and the sum of all m over all graphs in a single test does not exceed 5 ⋅ 10^{5}.
Output
Print your answer for each of the T graphs. Output your answer for a single graph in the following format.
If you found a matching of size n, on the first line print "Matching" (without quotes), and on the second line print n integers — the indices of the edges in the matching. The edges are numbered from 1 to m in the input order.
If you found an independent set of size n, on the first line print "IndSet" (without quotes), and on the second line print n integers — the indices of the vertices in the independent set.
If there is no matching and no independent set of the specified size, print "Impossible" (without quotes).
You can print edges and vertices in any order.
If there are several solutions, print any. In particular, if there are both a matching of size n, and an independent set of size n, then you should print exactly one of such matchings or exactly one of such independent sets.
Example
Input
4
1 2
1 3
1 2
1 2
1 3
1 2
2 5
1 2
3 1
1 4
5 1
1 6
2 15
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6
Output
Matching
2
IndSet
1
IndSet
2 4
Matching
1 15
Note
The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer.
The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly n.
The fourth graph does not have an independent set of size 2, but there is a matching of size 2.
Submitted Solution:
```
# inspired by https://codeforces.com/contest/1198/submission/58086142
# Use PyPy 3.6! It has JIT compiler!
def main():
T = int(input())
v_cover = set()
for _ in range(T):
n, m = map(int, input().split())
v_count = 3*n
v_cover.clear()
i_cover = []
for k in range(m):
edge = list(map(int, input().split()))
if edge[0] not in v_cover and edge[1] not in v_cover:
v_cover.add(edge[0])
v_cover.add(edge[1])
i_cover.append(k+1)
if len(i_cover) == n:
print('Matching')
print(*i_cover[:n])
for _ in range(k+1, m):
input()
break
if len(i_cover) < n:
#v_independent = set(range(1, v_count+1)) - v_cover
v_independent = []
for vert in range(1, 3*n+1):
if vert not in v_cover:
v_independent.append(vert)
if len(v_independent) == n:
print('IndSet')
#print(*random.sample(v_independent, n))
print(*v_independent)
break
if len(v_independent) < n:
print('Impossible')
# preamble
import sys
input=sys.stdin.readline
# main call
if __name__== "__main__":
main()
``` | instruction | 0 | 45,475 | 13 | 90,950 |
Yes | output | 1 | 45,475 | 13 | 90,951 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a graph with 3 ⋅ n vertices and m edges. You are to find a matching of n edges, or an independent set of n vertices.
A set of edges is called a matching if no two edges share an endpoint.
A set of vertices is called an independent set if no two vertices are connected with an edge.
Input
The first line contains a single integer T ≥ 1 — the number of graphs you need to process. The description of T graphs follows.
The first line of description of a single graph contains two integers n and m, where 3 ⋅ n is the number of vertices, and m is the number of edges in the graph (1 ≤ n ≤ 10^{5}, 0 ≤ m ≤ 5 ⋅ 10^{5}).
Each of the next m lines contains two integers v_i and u_i (1 ≤ v_i, u_i ≤ 3 ⋅ n), meaning that there is an edge between vertices v_i and u_i.
It is guaranteed that there are no self-loops and no multiple edges in the graph.
It is guaranteed that the sum of all n over all graphs in a single test does not exceed 10^{5}, and the sum of all m over all graphs in a single test does not exceed 5 ⋅ 10^{5}.
Output
Print your answer for each of the T graphs. Output your answer for a single graph in the following format.
If you found a matching of size n, on the first line print "Matching" (without quotes), and on the second line print n integers — the indices of the edges in the matching. The edges are numbered from 1 to m in the input order.
If you found an independent set of size n, on the first line print "IndSet" (without quotes), and on the second line print n integers — the indices of the vertices in the independent set.
If there is no matching and no independent set of the specified size, print "Impossible" (without quotes).
You can print edges and vertices in any order.
If there are several solutions, print any. In particular, if there are both a matching of size n, and an independent set of size n, then you should print exactly one of such matchings or exactly one of such independent sets.
Example
Input
4
1 2
1 3
1 2
1 2
1 3
1 2
2 5
1 2
3 1
1 4
5 1
1 6
2 15
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6
Output
Matching
2
IndSet
1
IndSet
2 4
Matching
1 15
Note
The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer.
The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly n.
The fourth graph does not have an independent set of size 2, but there is a matching of size 2.
Submitted Solution:
```
T = int(input())
for _ in range(T):
n, m = map(int, input().split())
v = [True] * (3 * n + 1)
e = [0] * n
ptr = 0
ok = False
for i in range(1, m + 1):
a, b = map(int, input().split())
if v[a] and v[b]:
e[ptr] = i
ptr += 1
v[a] = False
v[b] = False
if ptr == n:
print('Matching')
print(*e)
ok = True
break
if ok:
continue
print('IndSet')
cnt = 0
for i in range(1, n * 3 + 1):
if v[i]:
print(i, end=' ')
cnt += 1
if cnt == n:
print()
break
``` | instruction | 0 | 45,476 | 13 | 90,952 |
No | output | 1 | 45,476 | 13 | 90,953 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a graph with 3 ⋅ n vertices and m edges. You are to find a matching of n edges, or an independent set of n vertices.
A set of edges is called a matching if no two edges share an endpoint.
A set of vertices is called an independent set if no two vertices are connected with an edge.
Input
The first line contains a single integer T ≥ 1 — the number of graphs you need to process. The description of T graphs follows.
The first line of description of a single graph contains two integers n and m, where 3 ⋅ n is the number of vertices, and m is the number of edges in the graph (1 ≤ n ≤ 10^{5}, 0 ≤ m ≤ 5 ⋅ 10^{5}).
Each of the next m lines contains two integers v_i and u_i (1 ≤ v_i, u_i ≤ 3 ⋅ n), meaning that there is an edge between vertices v_i and u_i.
It is guaranteed that there are no self-loops and no multiple edges in the graph.
It is guaranteed that the sum of all n over all graphs in a single test does not exceed 10^{5}, and the sum of all m over all graphs in a single test does not exceed 5 ⋅ 10^{5}.
Output
Print your answer for each of the T graphs. Output your answer for a single graph in the following format.
If you found a matching of size n, on the first line print "Matching" (without quotes), and on the second line print n integers — the indices of the edges in the matching. The edges are numbered from 1 to m in the input order.
If you found an independent set of size n, on the first line print "IndSet" (without quotes), and on the second line print n integers — the indices of the vertices in the independent set.
If there is no matching and no independent set of the specified size, print "Impossible" (without quotes).
You can print edges and vertices in any order.
If there are several solutions, print any. In particular, if there are both a matching of size n, and an independent set of size n, then you should print exactly one of such matchings or exactly one of such independent sets.
Example
Input
4
1 2
1 3
1 2
1 2
1 3
1 2
2 5
1 2
3 1
1 4
5 1
1 6
2 15
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6
Output
Matching
2
IndSet
1
IndSet
2 4
Matching
1 15
Note
The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer.
The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly n.
The fourth graph does not have an independent set of size 2, but there is a matching of size 2.
Submitted Solution:
```
from sys import stdin, stdout
def input():
return next(stdin)
def solve_graph():
n,m = map(int, input().split())
adj = [set() for _ in range(n*3)]
if m >= 2 * n * (2 * n - 1):
excluded = set()
matching =[]
for i in range(m):
e1, e2 = map(int, input().split())
if e1 not in excluded and e2 not in excluded:
matching.append(i)
if len(matching) == n:
for _ in range(i + 1, m):
input()
print("Matching")
print(*map((lambda x: x + 1), matching))
return
excluded.add(e1)
excluded.add(e2)
else:
for i in range(m):
e1, e2 = map(int, input().split())
adj[e1-1].add(e2-1)
adj[e2-1].add(e1-1)
adj = list(enumerate(adj))
adj.sort(key=(lambda x: len(x[1])))
excluded = set()
indset = []
for v,a in adj:
if v not in excluded:
indset.append(v)
if len(indset) == n:
print("IndSet")
print(*map((lambda x: x + 1), indset))
return
excluded |= a
print("Impossible")
t = int(input())
for _ in range(t):
solve_graph()
``` | instruction | 0 | 45,477 | 13 | 90,954 |
No | output | 1 | 45,477 | 13 | 90,955 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a graph with 3 ⋅ n vertices and m edges. You are to find a matching of n edges, or an independent set of n vertices.
A set of edges is called a matching if no two edges share an endpoint.
A set of vertices is called an independent set if no two vertices are connected with an edge.
Input
The first line contains a single integer T ≥ 1 — the number of graphs you need to process. The description of T graphs follows.
The first line of description of a single graph contains two integers n and m, where 3 ⋅ n is the number of vertices, and m is the number of edges in the graph (1 ≤ n ≤ 10^{5}, 0 ≤ m ≤ 5 ⋅ 10^{5}).
Each of the next m lines contains two integers v_i and u_i (1 ≤ v_i, u_i ≤ 3 ⋅ n), meaning that there is an edge between vertices v_i and u_i.
It is guaranteed that there are no self-loops and no multiple edges in the graph.
It is guaranteed that the sum of all n over all graphs in a single test does not exceed 10^{5}, and the sum of all m over all graphs in a single test does not exceed 5 ⋅ 10^{5}.
Output
Print your answer for each of the T graphs. Output your answer for a single graph in the following format.
If you found a matching of size n, on the first line print "Matching" (without quotes), and on the second line print n integers — the indices of the edges in the matching. The edges are numbered from 1 to m in the input order.
If you found an independent set of size n, on the first line print "IndSet" (without quotes), and on the second line print n integers — the indices of the vertices in the independent set.
If there is no matching and no independent set of the specified size, print "Impossible" (without quotes).
You can print edges and vertices in any order.
If there are several solutions, print any. In particular, if there are both a matching of size n, and an independent set of size n, then you should print exactly one of such matchings or exactly one of such independent sets.
Example
Input
4
1 2
1 3
1 2
1 2
1 3
1 2
2 5
1 2
3 1
1 4
5 1
1 6
2 15
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6
Output
Matching
2
IndSet
1
IndSet
2 4
Matching
1 15
Note
The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer.
The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly n.
The fourth graph does not have an independent set of size 2, but there is a matching of size 2.
Submitted Solution:
```
from sys import stdin
input = stdin.readline
def solve_graph():
n,m = map(int, input().split())
included = [True]*(3*n+1)
matching =[]
for i in range(m):
e1, e2 = map(int, input().split())
if included[e1] and included[e2]:
matching.append(i)
if len(matching) == n:
for _ in range(i + 1, m):
input()
print("Matching")
print(*map((lambda x: x + 1), matching))
return
included[e1] = False
included[e2] = False
intset = [x for (x,b) in enumerate(included, start=1) if b]
print("IndSet")
print(*(list(intset)[:n]))
t = int(input())
for _ in range(t):
solve_graph()
``` | instruction | 0 | 45,478 | 13 | 90,956 |
No | output | 1 | 45,478 | 13 | 90,957 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.