description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
def fun(ino, isi, iei, level):
if isi > iei:
return None
root = Node(level[0])
if len(level) == 1:
return root
s = set()
idx = isi
while level[0] != ino[idx]:
s.add(ino[idx])
idx += 1
lols = []
lors = []
for i in level[1:]:
if i in s:
s.remove(i)
lols.append(i)
else:
lors.append(i)
root.left = fun(ino, isi, idx - 1, lols)
root.right = fun(ino, idx + 1, iei, lors)
return root
n = len(level) - 1
ans = fun(ino, 0, n, level)
def fun1(root):
if root == None:
return None
print(root.data, end=" ")
fun1(root.left)
fun1(root.right)
fun1(ans) | FUNC_DEF FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_DEF IF VAR NONE RETURN NONE EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
if len(level) == 0:
return None
if len(level) == 1:
return Node(level[0])
root_val = level[0]
ino_left = []
ino_right = []
left = True
for val in ino:
if val == root_val:
left = False
continue
if left:
ino_left.append(val)
else:
ino_right.append(val)
level_left = []
level_right = []
for val in level:
if val in ino_left:
level_left.append(val)
if val in ino_right:
level_right.append(val)
root = Node(root_val)
root.left = buildTree(level_left, ino_left)
root.right = buildTree(level_right, ino_right)
return root | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NONE IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
if not level:
return None
root = Node(level[0])
if not len(ino):
return root
in_indx = ino.index(root.data)
left_level = []
right_level = []
ino_left = ino[:in_indx]
level = level[1:]
for i in range(0, len(level)):
if level[i] in ino_left:
left_level.append(level[i])
else:
right_level.append(level[i])
root.left = buildTree(left_level, ino_left)
root.right = buildTree(right_level, ino[in_indx + 1 :])
return root | FUNC_DEF IF VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | class Node:
def __init__(self, key):
self.data = key
self.left = None
self.right = None
def construct(inorder, start, end, d):
if start > end:
return None
index = start
for j in range(start + 1, end + 1):
if d.get(inorder[j]) < d.get(inorder[index]):
index = j
root = Node(inorder[index])
root.left = construct(inorder, start, index - 1, d)
root.right = construct(inorder, index + 1, end, d)
return root
def buildTree(level, ino):
d = {}
for i, e in enumerate(level):
d[e] = i
return construct(ino, 0, len(ino) - 1, d) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
if not level:
return
root = Node(level[0])
index = 1
queue = [root]
while index < len(level):
newLevel = []
for node in queue:
if index >= len(level):
break
newNode = Node(level[index])
node.left = newNode
index += 1
newLevel.append(newNode)
if index >= len(level):
break
newNode = Node(level[index])
node.right = newNode
index += 1
newLevel.append(newNode)
queue.clear()
if newLevel:
queue.extend(newLevel)
return root | FUNC_DEF IF VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
You are given an array arr[ ] of size N consisting of only positive integers. Your task is to find the count of special numbers in the array. A number is said to be a special number if it is divisible by at least 1 other element of the array.
Example 1:
Input:
N = 3
arr[] = {2, 3, 6}
Output:
1
Explanation:
The number 6 is the only special number in the
array as it is divisible by the array elements 2
and 3. Hence, the answer is 1 in this case.
Example 2:
Input:
N = 4
arr[] = {5, 5, 5, 5}
Output:
4
Explanation:
All the array elements are special. Hence,
the answer is 4 in this case.
Your Task:
You don't need to read input or print anything. Complete the function countSpecialNumbers() which takes the integer N and the array arr[ ] as the input parameters, and returns the count of special numbers in the array.
Expected Time Complexity: O(N*root(M)) where M = max(arr[i])
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5 }
1 ≤ arr[i] ≤ 10^{6}^{ } | class Solution:
def countSpecialNumbers(self, N, arr):
n = len(arr)
arr.sort()
mx = max(arr)
c = 0
d = {}
for i in range(n):
if arr[i] not in d:
d[arr[i]] = 1
else:
d[arr[i]] += 1
s = set(arr)
ss = set()
for i in d:
if d[i] > 1:
c += d[i]
ss.add(i)
sl = list(s)
for i in sl:
j = i + i
while j < mx + 1:
if j in s and j not in ss:
c += 1
ss.add(j)
j += i
return c | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
You are given an array arr[ ] of size N consisting of only positive integers. Your task is to find the count of special numbers in the array. A number is said to be a special number if it is divisible by at least 1 other element of the array.
Example 1:
Input:
N = 3
arr[] = {2, 3, 6}
Output:
1
Explanation:
The number 6 is the only special number in the
array as it is divisible by the array elements 2
and 3. Hence, the answer is 1 in this case.
Example 2:
Input:
N = 4
arr[] = {5, 5, 5, 5}
Output:
4
Explanation:
All the array elements are special. Hence,
the answer is 4 in this case.
Your Task:
You don't need to read input or print anything. Complete the function countSpecialNumbers() which takes the integer N and the array arr[ ] as the input parameters, and returns the count of special numbers in the array.
Expected Time Complexity: O(N*root(M)) where M = max(arr[i])
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5 }
1 ≤ arr[i] ≤ 10^{6}^{ } | class Solution:
def countSpecialNumbers(self, N, arr):
c = 0
m = max(arr)
d = {}
for i in range(N):
if not arr[i] in d:
d[arr[i]] = 1
else:
d[arr[i]] += 1
arr.sort()
for j in range(N):
if d[arr[j]] > 1:
c += 1
else:
for i in range(j):
if arr[j] % arr[i] == 0:
c += 1
break
return c | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
You are given an array arr[ ] of size N consisting of only positive integers. Your task is to find the count of special numbers in the array. A number is said to be a special number if it is divisible by at least 1 other element of the array.
Example 1:
Input:
N = 3
arr[] = {2, 3, 6}
Output:
1
Explanation:
The number 6 is the only special number in the
array as it is divisible by the array elements 2
and 3. Hence, the answer is 1 in this case.
Example 2:
Input:
N = 4
arr[] = {5, 5, 5, 5}
Output:
4
Explanation:
All the array elements are special. Hence,
the answer is 4 in this case.
Your Task:
You don't need to read input or print anything. Complete the function countSpecialNumbers() which takes the integer N and the array arr[ ] as the input parameters, and returns the count of special numbers in the array.
Expected Time Complexity: O(N*root(M)) where M = max(arr[i])
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5 }
1 ≤ arr[i] ≤ 10^{6}^{ } | class Solution:
def countSpecialNumbers(self, N, arr):
m = max(arr)
fact = [(0) for i in range(m + 1)]
for i in range(N):
if fact[arr[i]] <= 1:
for j in range(arr[i], m + 1, arr[i]):
fact[j] += 1
count = 0
for i in range(N):
if fact[arr[i]] > 1:
count += 1
return count | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
You are given an array arr[ ] of size N consisting of only positive integers. Your task is to find the count of special numbers in the array. A number is said to be a special number if it is divisible by at least 1 other element of the array.
Example 1:
Input:
N = 3
arr[] = {2, 3, 6}
Output:
1
Explanation:
The number 6 is the only special number in the
array as it is divisible by the array elements 2
and 3. Hence, the answer is 1 in this case.
Example 2:
Input:
N = 4
arr[] = {5, 5, 5, 5}
Output:
4
Explanation:
All the array elements are special. Hence,
the answer is 4 in this case.
Your Task:
You don't need to read input or print anything. Complete the function countSpecialNumbers() which takes the integer N and the array arr[ ] as the input parameters, and returns the count of special numbers in the array.
Expected Time Complexity: O(N*root(M)) where M = max(arr[i])
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5 }
1 ≤ arr[i] ≤ 10^{6}^{ } | class Solution:
def countSpecialNumbers(self, N, arr):
ans = 0
special = set()
frequency = dict()
distinctnumbers = set(arr)
maximum = max(arr)
for i in distinctnumbers:
for j in range(i * 2, maximum + 1, i):
if j in distinctnumbers:
special.add(j)
for i in arr:
if i in frequency:
frequency[i] += 1
else:
frequency[i] = 1
for i in frequency.keys():
if frequency[i] > 1:
ans += frequency[i]
elif i in special:
ans += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR |
You are given an array arr[ ] of size N consisting of only positive integers. Your task is to find the count of special numbers in the array. A number is said to be a special number if it is divisible by at least 1 other element of the array.
Example 1:
Input:
N = 3
arr[] = {2, 3, 6}
Output:
1
Explanation:
The number 6 is the only special number in the
array as it is divisible by the array elements 2
and 3. Hence, the answer is 1 in this case.
Example 2:
Input:
N = 4
arr[] = {5, 5, 5, 5}
Output:
4
Explanation:
All the array elements are special. Hence,
the answer is 4 in this case.
Your Task:
You don't need to read input or print anything. Complete the function countSpecialNumbers() which takes the integer N and the array arr[ ] as the input parameters, and returns the count of special numbers in the array.
Expected Time Complexity: O(N*root(M)) where M = max(arr[i])
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5 }
1 ≤ arr[i] ≤ 10^{6}^{ } | class Solution:
def countSpecialNumbers(self, N, arr):
m = max(arr)
nums = [0] * (m + 1)
for i in range(len(arr)):
tar = arr[i]
if nums[tar] < 2:
for j in range(tar, m + 1, tar):
nums[j] += 1
ans = 0
for i in range(len(arr)):
if nums[arr[i]] > 1:
ans += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
You are given an array arr[ ] of size N consisting of only positive integers. Your task is to find the count of special numbers in the array. A number is said to be a special number if it is divisible by at least 1 other element of the array.
Example 1:
Input:
N = 3
arr[] = {2, 3, 6}
Output:
1
Explanation:
The number 6 is the only special number in the
array as it is divisible by the array elements 2
and 3. Hence, the answer is 1 in this case.
Example 2:
Input:
N = 4
arr[] = {5, 5, 5, 5}
Output:
4
Explanation:
All the array elements are special. Hence,
the answer is 4 in this case.
Your Task:
You don't need to read input or print anything. Complete the function countSpecialNumbers() which takes the integer N and the array arr[ ] as the input parameters, and returns the count of special numbers in the array.
Expected Time Complexity: O(N*root(M)) where M = max(arr[i])
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5 }
1 ≤ arr[i] ≤ 10^{6}^{ } | class Solution:
def countSpecialNumbers(self, N, arr):
m = max(arr)
l = [0] * (m + 1)
c = 0
for i in range(N):
if l[arr[i]] <= 1:
j = arr[i]
while j <= m:
l[j] += 1
j = j + arr[i]
for i in range(N):
if l[arr[i]] > 1:
c += 1
return c | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
You are given an array arr[ ] of size N consisting of only positive integers. Your task is to find the count of special numbers in the array. A number is said to be a special number if it is divisible by at least 1 other element of the array.
Example 1:
Input:
N = 3
arr[] = {2, 3, 6}
Output:
1
Explanation:
The number 6 is the only special number in the
array as it is divisible by the array elements 2
and 3. Hence, the answer is 1 in this case.
Example 2:
Input:
N = 4
arr[] = {5, 5, 5, 5}
Output:
4
Explanation:
All the array elements are special. Hence,
the answer is 4 in this case.
Your Task:
You don't need to read input or print anything. Complete the function countSpecialNumbers() which takes the integer N and the array arr[ ] as the input parameters, and returns the count of special numbers in the array.
Expected Time Complexity: O(N*root(M)) where M = max(arr[i])
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5 }
1 ≤ arr[i] ≤ 10^{6}^{ } | class Solution:
def countSpecialNumbers(self, N, arr):
maxx = max(arr)
a = [0] * (maxx + 1)
for j in arr:
if a[j] <= 1:
for k in range(j, maxx + 1, j):
a[k] += 1
return sum(a[i] > 1 for i in arr) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR VAR |
You are given an array arr[ ] of size N consisting of only positive integers. Your task is to find the count of special numbers in the array. A number is said to be a special number if it is divisible by at least 1 other element of the array.
Example 1:
Input:
N = 3
arr[] = {2, 3, 6}
Output:
1
Explanation:
The number 6 is the only special number in the
array as it is divisible by the array elements 2
and 3. Hence, the answer is 1 in this case.
Example 2:
Input:
N = 4
arr[] = {5, 5, 5, 5}
Output:
4
Explanation:
All the array elements are special. Hence,
the answer is 4 in this case.
Your Task:
You don't need to read input or print anything. Complete the function countSpecialNumbers() which takes the integer N and the array arr[ ] as the input parameters, and returns the count of special numbers in the array.
Expected Time Complexity: O(N*root(M)) where M = max(arr[i])
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5 }
1 ≤ arr[i] ≤ 10^{6}^{ } | class Solution:
def countSpecialNumbers(self, N, arr):
count_freq = dict()
max_ele = max(arr)
special = [(False) for i in range(0, max_ele + 1)]
for x in arr:
if x in count_freq:
count_freq[x] += 1
continue
for i in range(2 * x, max_ele + 1, x):
special[i] = True
count_freq[x] = 1
count = 0
for p in arr:
if special[p] or p in count_freq and count_freq[p] > 1:
count += 1
return count | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
You are given an array arr[ ] of size N consisting of only positive integers. Your task is to find the count of special numbers in the array. A number is said to be a special number if it is divisible by at least 1 other element of the array.
Example 1:
Input:
N = 3
arr[] = {2, 3, 6}
Output:
1
Explanation:
The number 6 is the only special number in the
array as it is divisible by the array elements 2
and 3. Hence, the answer is 1 in this case.
Example 2:
Input:
N = 4
arr[] = {5, 5, 5, 5}
Output:
4
Explanation:
All the array elements are special. Hence,
the answer is 4 in this case.
Your Task:
You don't need to read input or print anything. Complete the function countSpecialNumbers() which takes the integer N and the array arr[ ] as the input parameters, and returns the count of special numbers in the array.
Expected Time Complexity: O(N*root(M)) where M = max(arr[i])
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5 }
1 ≤ arr[i] ≤ 10^{6}^{ } | class Solution:
def countSpecialNumbers(self, N, arr):
max_element = max(arr)
res = [(0) for i in range(max_element + 1)]
for i in arr:
if res[i] <= 1:
for j in range(i, max_element + 1, i):
res[j] = res[j] + 1
return sum(res[i] > 1 for i in arr) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR VAR |
You are given an array arr[ ] of size N consisting of only positive integers. Your task is to find the count of special numbers in the array. A number is said to be a special number if it is divisible by at least 1 other element of the array.
Example 1:
Input:
N = 3
arr[] = {2, 3, 6}
Output:
1
Explanation:
The number 6 is the only special number in the
array as it is divisible by the array elements 2
and 3. Hence, the answer is 1 in this case.
Example 2:
Input:
N = 4
arr[] = {5, 5, 5, 5}
Output:
4
Explanation:
All the array elements are special. Hence,
the answer is 4 in this case.
Your Task:
You don't need to read input or print anything. Complete the function countSpecialNumbers() which takes the integer N and the array arr[ ] as the input parameters, and returns the count of special numbers in the array.
Expected Time Complexity: O(N*root(M)) where M = max(arr[i])
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5 }
1 ≤ arr[i] ≤ 10^{6}^{ } | class Solution:
def countSpecialNumbers(self, N, arr):
arr.sort()
di = {}
for x in arr:
di[x] = 0
for x in arr:
di[x] = di[x] + 1
count = 0
for i in range(len(arr)):
if di[arr[i]] > 1:
count = count + 1
else:
flag = 0
for j in range(i):
if arr[i] % arr[j] == 0:
flag = 1
break
if flag == 1:
count = count + 1
return count | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
You are given an array arr[ ] of size N consisting of only positive integers. Your task is to find the count of special numbers in the array. A number is said to be a special number if it is divisible by at least 1 other element of the array.
Example 1:
Input:
N = 3
arr[] = {2, 3, 6}
Output:
1
Explanation:
The number 6 is the only special number in the
array as it is divisible by the array elements 2
and 3. Hence, the answer is 1 in this case.
Example 2:
Input:
N = 4
arr[] = {5, 5, 5, 5}
Output:
4
Explanation:
All the array elements are special. Hence,
the answer is 4 in this case.
Your Task:
You don't need to read input or print anything. Complete the function countSpecialNumbers() which takes the integer N and the array arr[ ] as the input parameters, and returns the count of special numbers in the array.
Expected Time Complexity: O(N*root(M)) where M = max(arr[i])
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5 }
1 ≤ arr[i] ≤ 10^{6}^{ } | class Solution:
def countSpecialNumbers(self, N, arr):
freq = {}
uniq = set()
maximum = 0
for i in range(N):
freq[arr[i]] = freq.get(arr[i], 0) + 1
uniq.add(arr[i])
maximum = max(maximum, arr[i])
special = set()
for z in uniq:
for i in range(2 * z, maximum + 1, z):
if i in uniq:
special.add(i)
ans = 0
for x in freq.items():
if x[1] > 1:
ans += x[1]
elif x[0] in special:
ans += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER RETURN VAR |
You are given an array arr[ ] of size N consisting of only positive integers. Your task is to find the count of special numbers in the array. A number is said to be a special number if it is divisible by at least 1 other element of the array.
Example 1:
Input:
N = 3
arr[] = {2, 3, 6}
Output:
1
Explanation:
The number 6 is the only special number in the
array as it is divisible by the array elements 2
and 3. Hence, the answer is 1 in this case.
Example 2:
Input:
N = 4
arr[] = {5, 5, 5, 5}
Output:
4
Explanation:
All the array elements are special. Hence,
the answer is 4 in this case.
Your Task:
You don't need to read input or print anything. Complete the function countSpecialNumbers() which takes the integer N and the array arr[ ] as the input parameters, and returns the count of special numbers in the array.
Expected Time Complexity: O(N*root(M)) where M = max(arr[i])
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5 }
1 ≤ arr[i] ≤ 10^{6}^{ } | class Solution:
def countSpecialNumbers(self, N, arr):
ma = max(arr)
mark = [-1] * (ma + 1)
for a in arr:
if mark[a] == -1:
mark[a] = 1
for i in range(a + a, ma + 1, a):
mark[i] = 0
elif mark[a] == 1:
mark[a] = 0
ans = 0
for a in arr:
if mark[a] == 0:
ans += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR NUMBER RETURN VAR |
You are given an array arr[ ] of size N consisting of only positive integers. Your task is to find the count of special numbers in the array. A number is said to be a special number if it is divisible by at least 1 other element of the array.
Example 1:
Input:
N = 3
arr[] = {2, 3, 6}
Output:
1
Explanation:
The number 6 is the only special number in the
array as it is divisible by the array elements 2
and 3. Hence, the answer is 1 in this case.
Example 2:
Input:
N = 4
arr[] = {5, 5, 5, 5}
Output:
4
Explanation:
All the array elements are special. Hence,
the answer is 4 in this case.
Your Task:
You don't need to read input or print anything. Complete the function countSpecialNumbers() which takes the integer N and the array arr[ ] as the input parameters, and returns the count of special numbers in the array.
Expected Time Complexity: O(N*root(M)) where M = max(arr[i])
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5 }
1 ≤ arr[i] ≤ 10^{6}^{ } | class Solution:
def countSpecialNumbers(self, N, arr):
d = {}
count = 0
for i in arr:
d[i] = 1 + d.get(i, 0)
arr.sort()
j = 0
while j < N:
if d[arr[j]] > 1:
count = count + 1
else:
for z in range(j):
if arr[j] % arr[z] == 0:
count = count + 1
break
j = j + 1
return count | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
You are given an array arr[ ] of size N consisting of only positive integers. Your task is to find the count of special numbers in the array. A number is said to be a special number if it is divisible by at least 1 other element of the array.
Example 1:
Input:
N = 3
arr[] = {2, 3, 6}
Output:
1
Explanation:
The number 6 is the only special number in the
array as it is divisible by the array elements 2
and 3. Hence, the answer is 1 in this case.
Example 2:
Input:
N = 4
arr[] = {5, 5, 5, 5}
Output:
4
Explanation:
All the array elements are special. Hence,
the answer is 4 in this case.
Your Task:
You don't need to read input or print anything. Complete the function countSpecialNumbers() which takes the integer N and the array arr[ ] as the input parameters, and returns the count of special numbers in the array.
Expected Time Complexity: O(N*root(M)) where M = max(arr[i])
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5 }
1 ≤ arr[i] ≤ 10^{6}^{ } | class Solution:
def countSpecialNumbers(self, N, arr):
count = 0
len1 = len(arr)
duplicate = {}
count = {}
for i in range(len1):
if arr[i] not in duplicate:
for j in range(len1):
if i != j and arr[i] % arr[j] == 0:
if arr[i] in count:
count[arr[i]] += 1
else:
count[arr[i]] = 1
break
duplicate[arr[i]] = 1
elif arr[i] in count:
count[arr[i]] += 1
return sum(count.values()) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR |
You are given an array arr[ ] of size N consisting of only positive integers. Your task is to find the count of special numbers in the array. A number is said to be a special number if it is divisible by at least 1 other element of the array.
Example 1:
Input:
N = 3
arr[] = {2, 3, 6}
Output:
1
Explanation:
The number 6 is the only special number in the
array as it is divisible by the array elements 2
and 3. Hence, the answer is 1 in this case.
Example 2:
Input:
N = 4
arr[] = {5, 5, 5, 5}
Output:
4
Explanation:
All the array elements are special. Hence,
the answer is 4 in this case.
Your Task:
You don't need to read input or print anything. Complete the function countSpecialNumbers() which takes the integer N and the array arr[ ] as the input parameters, and returns the count of special numbers in the array.
Expected Time Complexity: O(N*root(M)) where M = max(arr[i])
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5 }
1 ≤ arr[i] ≤ 10^{6}^{ } | class Solution:
def countSpecialNumbers(self, N, arr):
freq = {}
special = set()
unique = set()
maxi = 0
for i in range(N):
if arr[i] in freq:
freq[arr[i]] += 1
else:
freq[arr[i]] = 1
unique.add(arr[i])
maxi = max(maxi, arr[i])
for x in unique:
for i in range(2 * x, maxi + 1, x):
if i in unique:
special.add(i)
ans = 0
for x in freq:
if freq[x] > 1:
ans += freq[x]
elif x in special:
ans += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR |
You are given an array arr[ ] of size N consisting of only positive integers. Your task is to find the count of special numbers in the array. A number is said to be a special number if it is divisible by at least 1 other element of the array.
Example 1:
Input:
N = 3
arr[] = {2, 3, 6}
Output:
1
Explanation:
The number 6 is the only special number in the
array as it is divisible by the array elements 2
and 3. Hence, the answer is 1 in this case.
Example 2:
Input:
N = 4
arr[] = {5, 5, 5, 5}
Output:
4
Explanation:
All the array elements are special. Hence,
the answer is 4 in this case.
Your Task:
You don't need to read input or print anything. Complete the function countSpecialNumbers() which takes the integer N and the array arr[ ] as the input parameters, and returns the count of special numbers in the array.
Expected Time Complexity: O(N*root(M)) where M = max(arr[i])
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5 }
1 ≤ arr[i] ≤ 10^{6}^{ } | class Solution:
def countSpecialNumbers(self, N, arr):
count = 0
arr.sort()
d = {x: (0) for x in arr}
for i in arr:
d[i] += 1
for i in range(N):
if d[arr[i]] > 1:
count += 1
else:
for j in range(i):
if arr[i] % arr[j] == 0:
count += 1
break
return count | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
You are given an array arr[ ] of size N consisting of only positive integers. Your task is to find the count of special numbers in the array. A number is said to be a special number if it is divisible by at least 1 other element of the array.
Example 1:
Input:
N = 3
arr[] = {2, 3, 6}
Output:
1
Explanation:
The number 6 is the only special number in the
array as it is divisible by the array elements 2
and 3. Hence, the answer is 1 in this case.
Example 2:
Input:
N = 4
arr[] = {5, 5, 5, 5}
Output:
4
Explanation:
All the array elements are special. Hence,
the answer is 4 in this case.
Your Task:
You don't need to read input or print anything. Complete the function countSpecialNumbers() which takes the integer N and the array arr[ ] as the input parameters, and returns the count of special numbers in the array.
Expected Time Complexity: O(N*root(M)) where M = max(arr[i])
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5 }
1 ≤ arr[i] ≤ 10^{6}^{ } | class Solution:
def countSpecialNumbers(self, N, arr):
m = max(arr)
freq = [(0) for _ in range(m + 1)]
for i in arr:
if freq[i] <= 1:
for j in range(i, m + 1, i):
freq[j] += 1
count = sum(1 for i in arr if freq[i] > 1)
return count | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR |
You are given an array arr[ ] of size N consisting of only positive integers. Your task is to find the count of special numbers in the array. A number is said to be a special number if it is divisible by at least 1 other element of the array.
Example 1:
Input:
N = 3
arr[] = {2, 3, 6}
Output:
1
Explanation:
The number 6 is the only special number in the
array as it is divisible by the array elements 2
and 3. Hence, the answer is 1 in this case.
Example 2:
Input:
N = 4
arr[] = {5, 5, 5, 5}
Output:
4
Explanation:
All the array elements are special. Hence,
the answer is 4 in this case.
Your Task:
You don't need to read input or print anything. Complete the function countSpecialNumbers() which takes the integer N and the array arr[ ] as the input parameters, and returns the count of special numbers in the array.
Expected Time Complexity: O(N*root(M)) where M = max(arr[i])
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5 }
1 ≤ arr[i] ≤ 10^{6}^{ } | class Solution:
def countSpecialNumbers(self, N, arr):
cnt = 0
d = {}
maxi = max(arr)
for i in range(N):
curr = arr[i]
if d.get(curr, 0) < 2:
for j in range(curr, maxi + 1, curr):
d[j] = 1 + d.get(j, 0)
for i in range(N):
curr = arr[i]
if d[curr] > 1:
cnt += 1
return cnt | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER RETURN VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | for i in range(int(input())):
b = []
cnt = 0
a = int(input())
for i in range(a):
c = list(map(int, input().split()))
b.append(c)
d = [[b[k][j] for k in range(a)] for j in range(a)]
for i in range(a):
if 0 in b[i] and 0 in d[i]:
cnt += 1
else:
cnt = 0
if cnt == a:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | t = int(input())
while t:
t -= 1
n = int(input())
a = []
for i in range(0, n):
b = [int(x) for x in input().split()]
a.append(b)
flag = 0
col = 0
for i in range(0, n):
prod = 1
for j in range(0, n):
prod = prod * a[i][j]
if prod != 0:
flag = 1
break
for j in range(0, n):
prod_col = 1
for i in range(0, n):
prod_col = prod_col * a[i][j]
if prod_col != 0:
col += 1
break
if flag == 0 and col == 0:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | n = int(input())
while n > 0:
x = int(input())
a = []
for i in range(0, x):
w = input()
w = w.split(" ")
a.append(w)
z = 0
y = 0
u = 0
v = 0
for i in range(0, x):
for j in range(0, x):
if a[i][j] == "0":
z = z + 1
break
for i in range(0, x):
for j in range(0, x):
if a[j][i] == "0":
u = u + 1
break
if z + u == 2 * x:
print("YES")
else:
print("NO")
n = n - 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | t = int(input())
for _ in range(t):
n = int(input())
list1 = []
row = []
col = []
for i in range(n):
list2 = list(map(int, input().split()))
list1.append(list2)
for j in range(n):
if list1[i][j] == 0:
row.append(i)
col.append(j)
if len(set(row)) == n and len(set(col)) == n:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | def tranpose(a, n):
b = []
for i in range(n):
r = []
for j in range(n):
r.append(a[j][i])
b.append(r)
for i in range(n):
if 0 not in a[i]:
return False
for i in range(n):
if 0 not in b[i]:
return False
return True
for _ in range(int(input())):
n = int(input())
a = []
for i in range(n):
r = list(map(int, input().split()))
a.append(r)
if tranpose(a, n):
print("YES")
else:
print("NO") | FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF NUMBER VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF NUMBER VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | for _ in range(int(input())):
n = int(input())
mat = []
for ___ in range(n):
temp = list(map(int, input().split()))
mat.append(temp)
ans = True
for i in range(n):
row, col = False, False
if not ans:
break
for j in range(n):
if mat[i][j] == 0:
row = True
if mat[j][i] == 0:
col = True
if not (row and col):
ans = False
break
if ans:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | for _ in range(int(input())):
n = int(input())
col = set()
row = []
flag = 1
for i in range(n):
row.append(list(map(int, input().split())))
for i in range(n):
if flag:
flag = 0
for j in range(n):
if row[i][j] == 0:
col.add(j + 1)
flag = 1
else:
break
print("YES" if flag and sum(list(col)) == n * (n + 1) // 2 else "NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER STRING STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | for t in range(int(input())):
n = int(input())
l = []
flag = 0
for x in range(n):
a = list(map(int, input().split()))
l.append(a)
for i in l[x]:
if i == 0:
flag += 1
break
s = []
for x in range(n):
for y in range(n):
if l[x][y] == 0:
s.append(y)
if len(set(s)) == n and flag == n:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | for _ in range(int(input())):
n = int(input())
arr = []
for i in range(n):
arr.append(list(map(int, input().split())))
count = 0
col = [[arr[j][i] for j in range(n)] for i in range(n)]
for i in range(n):
if 0 in arr[i] and 0 in col[i]:
count += 1
if n == count:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | tests = int(input())
for i in range(tests):
n = int(input())
ans = "YES"
arr = []
for j in range(n):
sub_arr = list(map(int, input().split()))
arr.append(sub_arr)
minimum = min(sub_arr)
if minimum:
ans = "NO"
col = 0
while ans == "YES" and col < n:
minimum = arr[0][col]
for j in range(n):
minimum = min(minimum, arr[j][col])
if minimum:
ans = "NO"
col += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR STRING VAR VAR ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR ASSIGN VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | t = input()
for c in range(0, int(t)):
n = int(input())
mat = []
z = 0
for i in range(n):
a = list(map(int, input().split()))
mat.append(a)
if 0 not in mat[i]:
z = 1
f = 0
if z == 0:
for j in range(n):
f = 0
for i in range(n):
if mat[i][j] == 0:
f = 1
break
if f == 0:
break
if f == 1:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | for i in range(int(input())):
n = int(input())
list2 = [[] for x in range(n)]
okay = 1
for j in range(n):
list1 = list(map(int, input().split()))
z = 0
for s in list1:
list2[z].append(s)
z += 1
if 0 not in list1:
okay = 0
if okay == 0:
print("NO")
else:
for y in list2:
if 0 not in y:
okay = 0
if okay == 0:
print("NO")
else:
print("YES") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR VAR IF NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | for _ in range(int(input())):
n = int(input())
rows = []
for i in range(n):
rows.append([int(x) for x in input().split()])
columns = []
for i in range(n):
col = []
for j in range(n):
col.append(rows[j][i])
columns.append(col)
p = True
for i in rows:
if 0 not in i:
p = False
break
for i in columns:
if 0 not in i:
p = False
break
print("YES" if p else "NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR IF NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | for _ in range(int(input())):
N = int(input())
A = []
invalidated = False
for _ in range(N):
if invalidated:
input()
continue
T = [int(s) for s in input().split()]
if 0 not in T:
invalidated = True
continue
A.append(T)
if not invalidated:
for i in range(N):
if all(A[j][i] for j in range(N)):
invalidated = True
break
print("NO" if invalidated else "YES") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | def check_column(m, temp):
f = True
for i in range(temp):
k = False
for j in range(temp):
if m[j][i] == 0:
k = True
if not k:
f = False
break
return f
for _ in range(int(input())):
n = int(input())
m = []
flag = True
for i in range(n):
a = list(map(int, input().split()))
if 0 not in a:
flag = False
m.append(a)
if not flag:
print("NO")
elif check_column(m, n):
print("YES")
else:
print("NO") | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | for _ in range(int(input())):
n = int(input())
a = []
for k in range(n):
row = list(map(int, input().split()))
a.append(row)
col = []
row = []
for i in range(n):
for j in range(n):
if a[i][j] == 0:
if i not in col:
col.append(i)
if j not in row:
row.append(j)
if len(col) == n and len(row) == n:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | def solve(num, matrix):
col = []
row = []
ans = None
for i in range(num):
for j in range(num):
if matrix[i][j] == 0:
if i not in col:
col.append(i)
if j not in row:
row.append(j)
if len(col) == num and len(row) == num:
ans = "YES"
else:
ans = "NO"
return ans
for _ in range(int(input())):
n = int(input())
matrix = []
for _ in range(n):
columns = list(map(int, input().split()))
matrix.append(columns)
print(solve(n, matrix)) | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | t = int(input())
for _ in range(t):
n = int(input())
llst = []
flagr = 1
cc = []
for i in range(n):
lst = list(map(int, input().split()))
if flagr:
if 0 not in lst:
flagr = 0
llst.append(lst)
if len(cc) == 0:
for j in lst:
cc.append([j])
else:
for j in range(n):
cc[j].append(lst[j])
flagc = 1
for i in cc:
if 0 not in i:
flagc = 0
break
if flagr and flagc:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR IF NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | import sys
input = sys.stdin.readline
def rii():
return range(int(input().strip()))
def ii():
return int(input().strip())
def mii():
return map(int, input().strip().split(" "))
def lmii():
return list(map(int, input().strip().split(" ")))
def si():
return str(input().strip())
success = "YES"
failure = "NO"
def solve(*args):
n, matx = args
cols = [1] * n
for l in matx:
try:
ll = l.index(0)
cols = [min(x, y) for x, y in zip(cols, l)]
except ValueError:
return failure
return success if max(cols) == 0 else failure
def do_codechef():
for t in rii():
n = ii()
matx = list()
for _ in range(n):
a = lmii()
matx.append(a)
print(solve(n, matx))
do_codechef()
exit() | IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | for _ in range(int(input())):
n = int(input())
matrix = list()
flag = 0
for i in range(n):
li = list(map(int, input().split()))
if 0 not in li:
flag = -1
res = [i for i, value in enumerate(li) if i not in matrix and value == 0]
if len(res) > 0:
matrix.extend(res)
if len(matrix) == n and flag == 0:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | for tc in range(int(input())):
poss = True
n = int(input())
rows = []
cols = []
for i in range(n):
rows.append(list(map(int, input().split())))
for j in range(n):
col = [rows[i][j] for i in range(n)]
cols.append(col)
for row in rows:
if 0 not in row:
poss = False
for col in cols:
if 0 not in col:
poss = False
print("YES" if poss else "NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR IF NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | for _ in range(int(input())):
n = int(input())
arr = []
q, r = [], []
l = 0
for i in range(n):
s = list(map(int, input().split()))
arr.append(s)
for i in range(n):
m = 1
x = list(arr[i])
for i in range(n):
if x[i] == 0:
q.append(i)
m = m * 0
l = l + m
for i in range(n):
r.append(i)
q = list(set(q))
q.sort()
if q == r and l == 0:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR LIST LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | t = int(input())
while t != 0:
n = int(input())
ans = []
for i in range(1, n + 1):
l = list(map(int, input().split()))
for j in range(1, n + 1):
x = l[j - 1]
if x == 0:
if ans.count(i) == 0:
ans.append(i)
if ans.count(-1 * j) == 0:
ans.append(-1 * j)
if len(ans) == 2 * n:
print("YES")
else:
print("NO")
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | t = int(input())
while t > 0:
t -= 1
n = int(input())
rows, cols = [0] * n, [0] * n
for r in range(n):
arr = list(map(int, input().split(" ")))
for index, item in enumerate(arr):
if item == 0:
rows[r] = 1
cols[index] = 1
if 0 not in rows and 0 not in cols:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | T = int(input())
for x in range(T):
n = int(input())
a = []
for i in range(n):
a.append(list(map(int, input().split())))
row = set()
col = set()
for i in range(len(a)):
for j in range(len(a)):
if a[i][j] == 0:
row.add(i)
col.add(j)
if len(row) == n and len(col) == n:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | T = int(input())
for i in range(0, T):
a = int(input())
mt = []
for j in range(0, a):
b = input().split()
mt.append(b)
doit = True
for j in range(0, a):
for k in range(0, a):
if mt[j][k] == "0":
break
else:
doit = False
break
if doit:
for j in range(0, a):
for k in range(0, a):
if mt[k][j] == "0":
break
else:
doit = False
break
if doit:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | for _ in range(int(input())):
n = int(input())
flag = 1
matrix = []
for _ in range(n):
matrix.append(list(map(int, input().split())))
for row in matrix:
if 0 not in row:
print("NO")
flag = 0
break
if flag == 0:
continue
trans = [([0] * n) for _ in range(n)]
for i in range(n):
for j in range(n):
trans[i][j] = matrix[j][i]
for row in trans:
if 0 not in row:
print("NO")
flag = 0
break
if flag == 1:
print("YES") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | for _ in range(int(input())):
n = int(input())
matrix = [(0) for i in range(n)]
count = [(0) for i in range(n)]
for i in range(n):
row = list(map(int, input().split()))
for j, e in enumerate(row):
if e == 0:
matrix[j] = 1
count[i] = 1
print("YES" if sum(matrix) == sum(count) == n else "NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR STRING STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | def hung(a, n):
for i in range(n):
if 0 not in a[i]:
return "NO"
for i in range(n):
mn = a[0][i]
for j in range(n):
mn = min(mn, a[j][i])
if mn != 0:
return "NO"
return "YES"
for i in range(int(input())):
n = int(input())
a = []
for i in range(n):
s = list(map(int, input().split()))
a.append(s)
print(hung(a, n)) | FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF NUMBER VAR VAR RETURN STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | def checkHungarian(m, n):
column = []
row = []
for i in range(n):
for j in range(n):
if m[i][j] == 0:
if i not in row:
row.append(i)
if j not in column:
column.append(j)
if len(column) == n and len(row) == n:
print("YES")
else:
print("NO")
for _ in range(int(input())):
n = int(input())
ls = [1] * n
for i in range(n):
a = list(map(int, input().split()))[:n]
ls[i] = a
checkHungarian(ls, n) | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | for _ in range(int(input())):
n = int(input())
a = []
for i in range(n):
a.append(list(map(int, input().split())))
b = []
for i in range(n):
b.append([0] * n)
for i in range(n):
for j in range(n):
b[j][i] = a[i][j]
t = True
for i in a:
if min(i) == 0:
pass
else:
t = False
break
for i in b:
if min(i) == 0:
pass
else:
t = False
break
if t:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | for _ in range(int(input())):
lst = []
for _ in range(int(input())):
lst.append(list(map(int, input().split(" "))))
lst1 = []
for ele in lst:
a = min(ele)
lst1.append([(i - a) for i in ele])
for i in range(len(lst1)):
m = 10**3
for ele in lst1:
if ele[i] < m:
m = ele[i]
for ele in lst1:
ele[i] = ele[i] - m
if lst == lst1:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | def solve():
n = int(input())
a = [0] * n
for i in range(n):
a[i] = [int(_) for _ in input().split()]
row = [10000] * n
col = [10000] * n
for i in range(n):
for j in range(n):
row[i] = min(row[i], a[i][j])
col[j] = min(col[j], a[i][j])
for i in range(n):
if row[i] != 0 or col[i] != 0:
return "NO"
return "YES"
t = int(input())
while t:
t -= 1
print(solve()) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | for i in range(int(input())):
N = int(input())
lst = []
for j in range(N):
k = input()
kk = k.split()
small = []
for m in range(N):
small.append(int(kk[m]))
lst.append(small.copy())
valid = True
for n in range(N):
count = 0
for t in range(N):
if lst[n][t] == 0:
count += 1
if count == 0:
valid = False
break
for n in range(N):
count = 0
for t in range(N):
if lst[t][n] == 0:
count += 1
if count == 0:
valid = False
break
if valid == True:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | for _ in range(int(input())):
n = int(input())
cntRow, cntCol = set(), set()
mat = []
for _ in range(n):
mat.append(list(map(int, input().split())))
for i in range(n):
for j in range(n):
if not mat[i][j]:
cntRow.add(i)
cntCol.add(j)
if cntRow == cntCol and len(cntRow) == n:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | def column(matrix, p):
return [row[p] for row in matrix]
t = int(input())
for i in range(t):
n = int(input())
entries = []
flag = True
for j in range(n):
entries.append(list(map(int, input().split())))
for row in entries:
if 0 in row:
continue
else:
flag = False
break
for k in range(n):
if 0 in column(entries, k):
continue
else:
flag = False
break
if flag is False:
print("NO")
else:
print("YES") | FUNC_DEF RETURN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | t = int(input())
for i in range(t):
n = int(input())
zero = "YES"
count = []
for j in range(n):
s = [int(x) for x in input().split()]
c = s.count(0)
if c == 0:
zero = "NO"
for i in range(c):
count.append(s.index(0) + i)
s.remove(0)
if zero == "NO":
print("NO")
elif len(set(count)) == n:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | t = int(input())
for i in range(t):
n = int(input())
a = []
c = 0
c1 = 0
for i in range(n):
a.append(list(map(int, input().split())))
for i in range(n):
if min(a[i]) == 0:
c += 1
for i in range(n):
b = []
for j in range(n):
b.append(a[j][i])
if min(b) == 0:
c1 += 1
if c == n and c1 == n:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | for _ in range(int(input())):
n = int(input())
l1 = []
for i in range(n):
l1.append([int(x) for x in input().split(" ")])
flag = 1
for i in range(n):
for j in range(n):
if l1[i][j] == 0:
break
if j == n - 1 and l1[i][j] != 0:
flag = 0
if flag == 0:
break
if flag == 1:
for i in range(n):
for j in range(n):
if l1[j][i] == 0:
break
if j == n - 1 and l1[j][i] != 0:
flag = 0
if flag == 0:
break
if flag == 0:
print("NO")
else:
print("YES") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.
Chef is studying computer science. Tomorrow, he has an exam on algorithms and data structures. One of the algorithms he is required to study is the Hungarian algorithm (you do not have to know this algorithm to solve this problem).
In the Hungarian algorithm, you start with a matrix of positive integers with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). The algorithm proceeds in steps.
In the first step, for each row of the matrix, you should find the smallest element in this row (let's denote it by $A$) and subtract $A$ from each element of the matrix in this row.
In the second step, for each column of the matrix, you should find the smallest element in this column (let's denote it by $A$) and subtract $A$ from each element in this column. The remaining steps of the algorithm are not relevant in this problem.
Currently, Chef is practicing implementation of the first two steps of the algorithm. He wants you to check if he is doing these two steps correctly or not. You are given a matrix with $N$ rows and $N$ columns; let's denote the element of this matrix in the $i$-th row and $j$-th column by $a_{i, j}$. Determine if this matrix can be the result of applying the first two steps of the Hungarian algorithm on some matrix.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of the following $N$ lines contains $N$ space-separated integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, N}$.
------ Output ------
For each test case, print a single line containing the string "YES" (without quotes) if the given matrix can be a result of the first two steps or "NO" (without quotes) if it cannot.
------ Constraints ------
$1 ≤ T ≤ 10,000$
$2 ≤ N ≤ 500$
$0 ≤ a_{i, j} ≤ 10^{3}$ for each valid $i, j$
the sum of $N^{2}$ over all test cases does not exceed $2 \cdot 10^{6}$
----- Sample Input 1 ------
5
3
0 0 4
3 0 1
0 1 0
2
0 5
3 2
2
1 3
5 0
3
0 3 4
4 0 5
6 5 0
4
3 0 2 4
0 2 3 5
3 4 3 0
3 4 0 3
----- Sample Output 1 ------
YES
NO
NO
YES
YES
----- explanation 1 ------
Example case 1: Let's consider the matrix
3 1 5
7 2 3
5 4 3
and apply the first two steps of the Hungarian algorithm on it.
In the first step:
- the smallest integer in row $1$ is $1$
- the smallest integer in row $2$ is $2$
- the smallest integer in row $3$ is $3$
After subtracting, the matrix becomes
2 0 4
5 0 1
2 1 0
and the second step is performed on this matrix. In the second step:
- the smallest integer in column $1$ is $2$
- the smallest integer in column $2$ is $0$
- the smallest integer in column $3$ is $0$
After subtracting, the resulting matrix is
0 0 4
3 0 1
0 1 0
and this is the matrix given on the input, so the answer is YES.
Example case 2: It is impossible to find a matrix such that after applying the first two steps of the Hungarian algorithm, it becomes the matrix given on the input, so the answer is NO. | for _ in range(int(input())):
n = int(input())
arr = []
column = []
for i in range(n):
arr.append([int(m) for m in input().split()])
for k in range(n):
column.append([arr[j][k] for j in range(n)])
flag = 1
for a in arr:
if 0 not in a:
flag = 0
break
else:
continue
if flag == 1:
for c in column:
if 0 not in c:
flag = 0
else:
continue
if flag == 1:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR VAR IF NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
def four_div_sum(num):
divs = set()
for i in range(1, floor(sqrt(num)) + 1):
if num % i == 0:
divs.update({i, num // i})
if len(divs) > 4:
return 0
return sum(divs) if len(divs) == 4 else 0
return sum(four_div_sum(num) for num in nums) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
def findiv(num):
res = 0
cnt = 0
for i in range(1, int(num**0.5) + 1):
if not num % i:
if i * i == num:
cnt += 1
res += i
else:
cnt += 2
res += i
res += num // i
return res if cnt == 4 else 0
res = 0
for num in nums:
res += findiv(num)
return res | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR RETURN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
def count_divisors(x):
num_divisors = 0
sum_divisors = 0
if sqrt(x) == int(sqrt(x)):
num_divisors += 1
sum_divisors += sqrt(x)
for i in range(1, ceil(sqrt(x))):
if x % i == 0:
num_divisors += 2
sum_divisors += i + x // i
return sum_divisors if num_divisors == 4 else 0
return sum([count_divisors(x) for x in nums]) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR RETURN VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
res = 0
for num in nums:
curr = 0
div_sum = 0
for i in range(1, int(sqrt(num)) + 1):
if num % i == 0:
curr += 2
if i == num // i:
div_sum -= i
curr -= 1
div_sum += i
div_sum += num // i
if curr == 4:
res += div_sum
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR RETURN VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
ans = 0
for val in nums:
P = self.check(val)
if P:
ans += sum(P)
return ans
def check(self, n):
L = [n]
count = 1
if n != 1:
L.append(1)
count += 1
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
L.append(i)
count += 1
if n / i != float(i):
L.append(n // i)
count += 1
if count > 4:
return None
if count != 4:
return None
return L | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER RETURN NONE IF VAR NUMBER RETURN NONE RETURN VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def find_factors(self, n):
factors = []
i = 1
j = n
while True:
if i * j == n:
factors.append(i)
if i == j:
break
factors.append(j)
i += 1
j = n // i
if i > j:
break
return factors
def sumFourDivisors(self, nums: List[int]) -> int:
d = 0
for i in nums:
f = self.find_factors(i)
if len(f) == 4:
d += f[0] + f[1] + f[2] + f[3]
return d | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
maxim = max(nums)
total = 0
for k in range(len(nums)):
num_div = 0
index = 2
div = []
curr_val = nums[k]
if abs(int(sqrt(curr_val)) - sqrt(curr_val)) > 10**-12:
while index <= int(sqrt(curr_val)):
if curr_val % index == 0:
div.append(index)
div.append(nums[k] / index)
if len(div) > 2:
break
index += 1
if len(div) == 2:
total = total + sum(div) + 1 + nums[k]
return int(total) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER WHILE VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
z = 0
for num in nums:
i = 1
res = []
while i * i <= num:
if num % i == 0:
res.append(i)
i += 1
if len(res) == 2:
lin = [(num // j) for j in res]
final = list(set(res + lin))
if len(final) == 4:
z += sum(final)
return max(0, z) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def divisors(self, n, c={}):
if n in c:
return c[n]
d = []
for i in range(1, int(sqrt(n) + 1)):
if n % i == 0:
d.append(i)
j = n // i
if j != i:
d.append(j)
if len(d) > 4:
break
if len(d) == 4:
s = sum(d)
c.update({n: s})
return s
else:
c.update({n: 0})
return 0
def sumFourDivisors(self, nums: List[int]) -> int:
return sum(self.divisors(x) for x in nums) | CLASS_DEF FUNC_DEF DICT IF VAR VAR RETURN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR DICT VAR VAR RETURN VAR EXPR FUNC_CALL VAR DICT VAR NUMBER RETURN NUMBER FUNC_DEF VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
def findFactors(num):
res = set()
for i in range(int(num**0.5) + 1):
if num % (i + 1) == 0:
res.add(i + 1)
res.add(num // (i + 1))
if len(res) > 4:
break
if len(res) == 4:
return sum(res)
else:
return 0
output = 0
for num in nums:
temp = findFactors(num)
output += temp
return output | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
res = 0
@lru_cache(None)
def divisors(v):
res = []
for i in range(1, ceil(sqrt(v)) + 2):
if len(res) > 4:
return 0
if not v % i:
res += (i,)
if v // i > i:
res += (v // i,)
else:
break
res = set(res)
return sum(res) if len(res) == 4 else 0
for v in nums:
res += divisors(v)
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR NONE FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
range_6 = list(range(6))
result = 0
for num in nums:
if num in range_6:
pass
else:
pivot = int(num**0.5)
temp = [1, num]
len_t = 2
for i in range(2, pivot + 1):
divisor, rem = divmod(num, i)
if not rem:
if i == divisor:
len_t = 0
break
temp += [i, divisor]
len_t += 2
if len_t > 4:
break
if len_t == 4:
result += sum(temp)
return result | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR IF VAR VAR ASSIGN VAR NUMBER VAR LIST VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
def div4(i):
if i <= 5:
return set()
else:
count = {1, i}
for j in range(2, int(math.sqrt(i)) + 1):
if i % j == 0:
count.update({j, i / j})
if len(count) > 4:
return count
return count
count = 0
for i in nums:
s = div4(i)
if len(s) == 4:
count += sum(s)
return int(count) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def divisors(self, n):
for i in range(1, int(sqrt(n) + 1)):
if n % i == 0:
yield i
j = n // i
if j != i:
yield j
def sumFourDivisors(self, nums: List[int]) -> int:
s = 0
for n in nums:
l = list(self.divisors(n))
if len(l) == 4:
s += sum(l)
return s | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
return sum(contr(n) for n in nums)
def contr(n):
p = None
if n**0.5 % 1 == 0:
return 0
for i in range(2, math.ceil(n**0.5)):
if n % i == 0:
if p is None:
p = i
else:
return 0
if p is None:
return 0
return 1 + p + n // p + n | CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NONE IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR NONE ASSIGN VAR VAR RETURN NUMBER IF VAR NONE RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
total = 0
pSieve = [(0) for k in range(10**5 + 1)]
for k in range(2, len(pSieve)):
if pSieve[k] == 1:
continue
pSieve[k + k :: k] = [1] * ((len(pSieve) - 1) // k - 1)
for num in nums:
if num == 1 or pSieve[num] == 0 or sqrt(num) == int(sqrt(num)):
continue
k = 2
while num % k != 0:
k += 1
if num == k**3 or pSieve[num // k] == 0:
total += 1 + num + k + num // k
return total | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP LIST NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR VAR RETURN VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
ret = 0
for num in nums:
sqrt = int(math.sqrt(num))
if sqrt * sqrt == num:
continue
divSum = 0
count = 0
for i in range(1, sqrt + 1):
if num % i == 0:
divSum += i + num // i
count += 1
if count > 2:
break
if count == 2:
ret += divSum
return ret | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR VAR RETURN VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
ret = 0
for num in nums:
divs = set()
i = 1
while i**2 <= num:
if not num % i:
divs.add(i)
divs.add(num // i)
if len(divs) > 4:
break
i += 1
if len(divs) == 4:
ret += sum(divs)
return ret | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
ans = 0
for n in nums:
sq = floor(n**0.5)
if sq * sq == n:
continue
divs = 2
divsum = 1 + n
for i in range(sq, 1, -1):
if n % i == 0:
divs += 2
divsum += i + n // i
if divs > 4:
break
if divs == 4:
ans += divsum
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR NUMBER VAR VAR RETURN VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def find_divisors(self, num):
cnt = 0
run_sum = num + 1
for i in range(2, int(num**0.5) + 1):
if i * i == num:
return 0
if cnt > 1:
return 0
if not num % i:
run_sum += num // i + i
cnt += 1
return run_sum if cnt == 1 else 0
def sumFourDivisors(self, nums: List[int]) -> int:
cnt = 0
for i in nums:
cnt += self.find_divisors(i)
return cnt | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR NUMBER VAR NUMBER FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
def helper(n):
if int(math.sqrt(n)) * int(math.sqrt(n)) == n:
return 0
summary = 1 + n
count = 2
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
summary += n // i + i
count += 2
if count > 4:
break
if count == 4:
return summary
else:
return 0
res = 0
for n in nums:
res += helper(n)
return res | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER RETURN VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
n = 400
prime = [(True) for i in range(n + 1)]
p = 2
while p * p <= n:
if prime[p] == True:
for i in range(p * p, n + 1, p):
prime[i] = False
p += 1
prime[0] = False
prime[1] = False
prime_set = [p for p in range(n + 1) if prime[p]]
cnt = 0
for i in nums:
if i == 0:
continue
for p in prime_set:
if i % p == 0:
r = i // p
if r == p or r == 1:
break
r_prime = True
for q in prime_set:
if r % q == 0:
if r != q:
r_prime = False
break
if r_prime:
cnt += (p + 1) * (r + 1)
break
for i in nums:
p = int(i ** (1 / 3) + 0.5)
if prime[p] and p ** 3 == i:
cnt += (p * i - 1) // (p - 1)
print(i, p, p**2, p)
return cnt | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def getDivisors(self, x):
if x == 1:
return [1]
out = []
bound = int(sqrt(x)) + 1
for i in range(1, bound):
if x % i == 0:
out.append(i)
if x // i != i:
out.append(x // i)
if len(out) > 4:
break
return out
def sumFourDivisors(self, nums: List[int]) -> int:
divisors = {}
sum_four = 0
for x in nums:
if x in divisors:
if len(divisors[x]) == 4:
sum_four += sum(divisors[x])
else:
x_div = self.getDivisors(x)
if len(x_div) == 4:
sum_four += sum(x_div)
divisors[x] = x_div
return sum_four | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
ans = 0
for num in nums:
divisor = 0
a = 2
upperLimit = int(num**0.5)
if upperLimit**2 == num:
continue
upperLimit += 1
subAns = 1 + num
while a < upperLimit:
if num % a == 0:
if divisor == 0:
divisor += 1
subAns += a + num // a
else:
break
upperLimit = min(upperLimit, num // a)
a += 1
else:
if divisor == 1:
ans += subAns
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR WHILE VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR RETURN VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
ret = 0
for num in nums:
divs = self.divisors(num)
if len(divs) == 4:
ret += sum(divs)
return ret
def divisors(self, num):
ret = []
for i in range(1, int(num**0.5) + 1):
if num % i == 0:
ret += [i]
if num // i != i:
ret += [num // i]
return ret | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR LIST VAR IF BIN_OP VAR VAR VAR VAR LIST BIN_OP VAR VAR RETURN VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
divs = dict()
for v in nums:
divs.setdefault(v, [0, []])
divs[v][0] += 1
n = max(nums)
sieve = (1 + n) * [0]
for i in range(2, 1 + n):
j = i
while j <= n:
sieve[j] += 1
if j in divs:
divs[j][1].append(i)
j += i
return sum(
[
(freq * (1 + sum(cur_div)))
for k, (freq, cur_div) in list(divs.items())
if len(cur_div) == 3
]
) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR LIST NUMBER LIST VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
def findFactors(num):
if num == 0:
return 0
res = set()
for i in range(int(num**0.5) + 1):
if num % (i + 1) == 0:
res.add(i + 1)
res.add(num // (i + 1))
return [len(res), sum(res)]
output = 0
for num in nums:
c, sm = findFactors(num)
if c == 4:
output += sm
return output | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR RETURN VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
def findfactors(n):
f = []
for i in range(1, int(n**0.5) + 1):
if n % i == 0:
f.append(i)
if i != n // i:
f.append(n // i)
return sum(f) if len(f) == 4 else 0
return sum([findfactors(x) for x in nums]) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
def getDivisors(k):
count, second = 0, 0
for i in range(2, int(sqrt(k)) + 1):
if k % i == 0:
count += 1
if count > 1 or i * i == k:
return [0]
second = k // i
if count == 1:
return [1, second, k // second, k]
else:
return [0]
total = 0
for num in nums:
total += sum(getDivisors(num))
return total | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR VAR RETURN LIST NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN LIST NUMBER VAR BIN_OP VAR VAR VAR RETURN LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
ans = 0
for n in nums:
tmp = set([1, n])
for d in range(2, ceil(sqrt(n)) + 1):
if n % d == 0:
tmp.add(d)
tmp.add(n // d)
if len(tmp) == 4:
ans += sum(tmp)
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
ans = 0
for num in nums:
out = []
for i in range(1, int(num**0.5) + 1):
a, b = divmod(num, i)
if b == 0:
if a == i:
out.append(a)
else:
out.extend([a, i])
if len(out) > 4:
break
if len(out) == 4:
ans += sum(out)
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
summ = 0
for num in nums:
if num > 1:
summ += self.divisors(num)
return summ
def divisors(self, num):
visited_factors = set()
visited_factors.add(1)
visited_factors.add(num)
factors = 2
summ = 1 + num
for i in range(2, int(num**0.5) + 1):
if not num % i and num % i not in visited_factors:
visited_factors.add(i)
summ += i
factors += 1
secondHalf = num // i
if secondHalf not in visited_factors:
visited_factors.add(secondHalf)
factors += 1
summ += secondHalf
if factors == 4:
return summ
return 0 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER RETURN VAR RETURN NUMBER |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
def get_divs(num):
divs = []
for i in range(1, int(sqrt(num)) + 1):
if not num % i:
divs.append(i)
if i != int(num / i):
divs.append(int(num / i))
if len(divs) > 4:
return None
if len(divs) < 4:
return None
return sum(divs)
ans = 0
for item in nums:
divs = get_divs(item)
if divs:
ans += divs
return ans | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NONE IF FUNC_CALL VAR VAR NUMBER RETURN NONE RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
def isPrime(n):
if n <= 1:
return False
if n <= 3:
return True
if n & 1 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 and n % (i + 2) == 0:
return False
i += 6
return True
res = 0
c = 0
temp = set()
for i in nums:
for j in range(1, int(i**0.5) + 1):
if i % j == 0:
temp.add(j)
temp.add(i // j)
if i // j != j:
c += 2
else:
c += 1
res += sum(temp) if c == 4 else 0
temp = set()
c = 0
return res | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER RETURN VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
def div_num(x):
ans, ssum = 2, x + 1
for i in range(2, int(x**0.5) + 1):
if x % i == 0:
ans += 1 + (i * i != x)
ssum += i + x // i if i * i != x else i
return ans == 4, ssum
res = 0
for x in nums:
flag, ssum = div_num(x)
if flag == 1:
res += ssum
return res | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR RETURN VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR RETURN VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
factors_cache = {}
def get_factors(num):
if num in factors_cache:
return factors_cache[num]
else:
factors = set([1, num])
for potential_divisor in range(2, math.ceil(math.sqrt(num))):
if num % potential_divisor == 0:
factors = factors.union(get_factors(potential_divisor))
factors = factors.union(get_factors(num // potential_divisor))
if len(factors) > 4:
break
factors_cache[num] = factors
return factors
running_sum = 0
for num in nums:
factors = get_factors(num)
if len(factors) == 4:
running_sum += sum(factors)
return running_sum | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
If there is no such integer in the array, return 0.
Example 1:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has 4 divisors: 1, 3, 7, 21
4 has 3 divisors: 1, 2, 4
7 has 2 divisors: 1, 7
The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5 | class Solution:
def sumFourDivisors(self, nums: List[int]) -> int:
def NOD(x):
divisor = set([1, x])
for i in range(2, int(x**0.5) + 1):
if not x % i:
divisor.add(i)
divisor.add(x // i)
return divisor
ans = []
for num in nums:
divisor = NOD(num)
if len(divisor) == 4:
ans.append(divisor)
return sum([sum(i) for i in ans]) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.