description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def solve(self, n, p, a, b, d): adj = [] exist = [False] * (n + 1) for i in range(n + 1): adj.append([]) for i in range(p): exist[a[i]] = True exist[b[i]] = True adj[a[i]].append(b[i]) adj[a[i]].append(d[i]) indeg = [0] * (n + 1) for i in range(len(adj)): if len(adj[i]) > 0: j, wt = adj[i] indeg[j] = 1 queue = [] for i in range(len(indeg)): if indeg[i] == 0 and exist[i]: queue.append(i) def traverse(node, minn): if len(adj[node]) == 0: ans[-1].append(node) ans[-1].append(minn) elif visited[node] == False: visited[node] = True nbr, wt = adj[node] if minn > wt: minn = wt traverse(nbr, minn) ans = [] visited = [False] * (n + 1) nop = len(queue) while queue: curr = queue.pop() ans.append([]) ans[-1].append(curr) traverse(curr, float("inf")) ans.sort(key=lambda x: x[0]) return ans if __name__ == "__main__": t = int(input()) for _ in range(t): n, p = map(int, input().strip().split()) a = [] b = [] d = [] for i in range(p): x, y, z = map(int, input().strip().split()) a.append(x) b.append(y) d.append(z) ob = Solution() ans = ob.solve(n, p, a, b, d) print(len(ans)) for i in ans: print(str(i[0]) + " " + str(i[1]) + " " + str(i[2]))
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: e = 0 m = -1 def solve(self, n, p, a, b, d): inarr = [0] * 21 outarr = [0] * 21 g = {} vis = [0] * (n + 1) ans = [] for i in range(0, p): inarr[b[i]] = 1 outarr[a[i]] = 1 g[a[i]] = [b[i], d[i]] for i in range(1, n + 1): if inarr[i] == 0 and outarr[i] == 1 and vis[i] == 0: s = i self.e = -1 self.m = 1000 self.dfs(s, self.e, self.m, g, vis) ans.append([s, self.e, self.m]) return ans def dfs(self, s, e, m, g, vis): vis[s] = 1 if s in g: node = g[s][0] if vis[node] == 0: self.e = g[s][0] self.m = min(m, g[s][1]) self.dfs(g[s][0], self.e, self.m, g, vis)
CLASS_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR LIST VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def solve(self, n, p, a, b, d): adj = [[] for _ in range(n + 1)] s = set() ans = [] for i in range(p): adj[a[i]].append([b[i], d[i]]) s.add(a[i]) for i in range(p): if b[i] in s: s.remove(b[i]) def dfs(u, start, mi, ans): if len(adj[u]) == 0: ans.append([start, u, mi]) return for v, w in adj[u]: dfs(v, start, min(mi, w), ans) return for u in s: dfs(u, u, float("inf"), ans) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR RETURN FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR RETURN VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def solve(self, n, p, a, b, d): def dfs(i, g, mv, e, vis, ans): vis[i] = 1 if len(g[i]) != 0 and vis[g[i][0]] == 0: e = g[i][0] mv = min(g[i][1], mv) dfs(g[i][0], g, mv, e, vis, ans) else: ans.append([s, e, mv]) inp = [0] * (n + 1) out = [0] * (n + 1) vis = [0] * (n + 1) g = [[]] * (n + 1) ans = [] for i in range(p): inp[b[i]] = 1 out[a[i]] = 1 g[a[i]] = [b[i], d[i]] for i in range(n + 1): if inp[i] == 0 and out[i] == 1 and vis[i] == 0: s = i e = 0 mv = float("inf") dfs(i, g, mv, e, vis, ans) return ans
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST LIST BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR LIST VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
import sys class Solution: def solve(self, n, p, out, inc, dia): incoming_set = set() for ele in inc: incoming_set.add(ele) connections = {} for i, ele in enumerate(out): connections[ele] = inc[i], dia[i] ans = [] for ele in out: if ele not in incoming_set: tap, dia = self.get_min_diameter(ele, connections) ans.append([ele, tap, dia]) return sorted(ans, key=lambda x: x[0]) def get_min_diameter(self, tank, connections): pipe = sys.maxsize while tank in connections: tank, d = connections[tank] pipe = min(pipe, d) return tank, pipe if __name__ == "__main__": t = int(input()) for _ in range(t): n, p = map(int, input().strip().split()) a = [] b = [] d = [] for i in range(p): x, y, z = map(int, input().strip().split()) a.append(x) b.append(y) d.append(z) ob = Solution() ans = ob.solve(n, p, a, b, d) print(len(ans)) for i in ans: print(str(i[0]) + " " + str(i[1]) + " " + str(i[2]))
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def __init__(self): self.ans = 0 def dfs(self, i, s, t, e): global ans if s[i] == 0: return i if t[i] < self.ans: self.ans = t[i] return self.dfs(s[i], s, t, e) def solve(self, n, p, a, b, d): s = [0] * (n + 1) e = [0] * (n + 1) t = [0] * (n + 1) for i in range(p): s[a[i]] = b[i] t[a[i]] = d[i] e[b[i]] = a[i] output = [] for i in range(1, n + 1): m = [] if e[i] == 0 and s[i]: self.ans = 1000000000 end = self.dfs(i, s, t, e) m.append(i) m.append(end) m.append(self.ans) output.append(m) return output
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR NUMBER RETURN VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST IF VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def solve(self, n, p, a, b, d): index = 0 while index < len(a): for ind, i in enumerate(a): if i == b[index]: td = d[ind] if d[ind] < d[index] else d[index] b[index] = b[ind] d[index] = td a = a[0:ind] + a[ind + 1 :] b = b[0:ind] + b[ind + 1 :] d = d[0:ind] + d[ind + 1 :] break else: index = index + 1 r = [] for i in range(len(a)): r.append([a[i], b[i], d[i]]) r = sorted(r, key=lambda x: x[0]) return r
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def solve(self, n, p, a, b, d): g = [[] for i in range(n + 1)] deg = [(0) for i in range(n + 1)] for i in range(p): g[a[i]].append([b[i], d[i]]) deg[a[i]] += 1 deg[b[i]] -= 1 vis = [(False) for i in range(n + 1)] ans = [] for i in range(1, n + 1): if vis[i] == False and deg[i] == 1: tab = [0] dia = [999] self.dfs(g, vis, i, n, tab, dia) ans.append([i, tab[0], dia[0]]) return ans def dfs(self, g, vis, node, n, tab, dia): vis[node] = True for curr in g[node]: v, wt = curr[0], curr[1] dia[0] = min(dia[0], wt) if len(g[v]) == 0: tab[0] = v return if vis[v] == False: self.dfs(g, vis, v, n, tab, dia)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR LIST VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR RETURN IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: x = 11111111 def dfs(self, ind, out, d): global x if out[ind] == 0: return ind if d[ind] < x: x = d[ind] return self.dfs(out[ind], out, d) def solve(self, n, p, a, b, c): global x inp = [(0) for i in range(n + 1)] out = [(0) for i in range(n + 1)] d = [(0) for i in range(n + 1)] ans = [] for i in range(p): t = a[i] y = b[i] u = c[i] out[t] = y inp[y] = t d[t] = u for i in range(1, n + 1): if inp[i] == 0 and out[i]: x = 12546357 en = self.dfs(i, out, d) ans.append([i, en, x]) return ans
CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR NUMBER RETURN VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR RETURN VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Graph: def __init__(self): self.graph = {} def add_pipe(self, house_a, house_b, distance): self.graph[house_a] = house_b, distance def utils_DFS(self, key, minimum): if not self.graph.get(key): return key if minimum[0] > self.graph[key][1]: minimum[0] = self.graph[key][1] return self.utils_DFS(self.graph[key][0], minimum) class Solution: def solve(self, n, p, a, b, d): g1 = Graph() for i in range(p): g1.add_pipe(a[i], b[i], d[i]) ref = {} for h_b in b: ref[h_b] = 1 res = [] a.sort() for h_a in a: if not ref.get(h_a): minimum = [float("inf")] h_b = g1.utils_DFS(h_a, minimum) res.append((h_a, h_b, minimum[0])) return res
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR RETURN VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Graph: def __init__(self): self.NODE_COUNT = 0 self.EDGE_COUNT = 0 self.ADJ_LIST = dict() self.EDGE_DICT = dict() self.IN_DEGREE = dict() def addNode(self, node): if node not in self.ADJ_LIST: self.ADJ_LIST[node] = list() self.NODE_COUNT += 1 self.IN_DEGREE[node] = 0 def addEdge(self, node1, node2, weight): if node1 not in self.ADJ_LIST: self.addNode(node1) if node2 not in self.ADJ_LIST: self.addNode(node2) self.ADJ_LIST[node1].append(node2) self.EDGE_COUNT += 1 self.EDGE_DICT[node2] = weight self.IN_DEGREE[node2] += 1 def dfsUtil(self): op = list() visitedDict = {node: (False) for node in self.ADJ_LIST} getZeroIndegree = list() for node in self.IN_DEGREE: if self.IN_DEGREE[node] == 0: getZeroIndegree.append(node) for node in getZeroIndegree: if visitedDict[node] == False: temp = [node] minDiameter = float("inf") self.dfs(node, visitedDict, temp, minDiameter) op.append(temp) return op def dfs(self, node, visitedDict, temp, minDiameter): visitedDict[node] = True for neighbourNode in self.ADJ_LIST[node]: if visitedDict[neighbourNode] == False: minDiameter = min(minDiameter, self.EDGE_DICT[neighbourNode]) self.dfs(neighbourNode, visitedDict, temp, minDiameter) if len(self.ADJ_LIST[node]) == 0: temp.append(node) temp.append(minDiameter) class Solution: def solve(self, n, p, a, b, d): graph = Graph() for index in range(p): graph.addEdge(a[index], b[index], d[index]) return graph.dfsUtil()
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def solve(self, n, p, a, b, d): pipe = [] tank = [] for i in b: if i not in a: pipe.append(i) for j in a: if j not in b: tank.append(j) tank.sort() hashtable = {} for i in range(len(a)): hashtable[a[i]] = [b[i], d[i]] res = [] ans = [] for i in tank: ans.append(i) temp = hashtable[i] min_dia = temp[1] while temp[0] not in pipe: temp = hashtable[temp[0]] min_dia = min(min_dia, temp[1]) ans.append(temp[0]) ans.append(min_dia) res.append(ans) ans = [] return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR LIST VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST RETURN VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def solve(self, n, p, a, b, d): self.visited = [False] * (n + 1) connect_lines = [] self.a = a self.b = b self.d = d min_dis = [] res = [] for i in range(1, n + 1): if i not in self.b: current_streak = [] self.min_d = float("inf") self.DFS(i, current_streak) connect_lines.append(current_streak) min_dis.append(self.min_d) for i in range(len(connect_lines)): if min_dis[i] != float("inf") and len(connect_lines[i]) != 1: start = connect_lines[i][0] end = connect_lines[i][len(connect_lines[i]) - 1] dis = min_dis[i] curr_ans = [] curr_ans.append(start) curr_ans.append(end) curr_ans.append(dis) res.append(curr_ans) return res def DFS(self, i, current_streak): self.visited[i] == True current_streak.append(i) for j in range(len(self.a)): if self.visited[j] == False and self.a[j] == i: if self.d[j] < self.min_d: self.min_d = self.d[j] self.DFS(self.b[j], current_streak)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF EXPR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def solve(self, n, p, a, b, d): def dfs(node, start, adj, ans, d): if len(adj[node]) == 0: ans.append([start, node, d]) return for [i, j] in adj[node]: dfs(i, start, adj, ans, min(d, j)) return s = set() ans = [] adj = [[] for i in range(n + 1)] for i in range(p): adj[a[i]].append([b[i], d[i]]) s.add(a[i]) for i in b: if i in s: s.remove(i) for j in s: dfs(j, j, adj, ans, float("inf")) return ans
CLASS_DEF FUNC_DEF FUNC_DEF IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR RETURN FOR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR STRING RETURN VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def solve(self, n, p, a, b, d): visited = [(0) for _ in range(n + 1)] adj = [(-1) for _ in range(n + 1)] inc = [(0) for _ in range(n + 1)] outg = [(0) for _ in range(n + 1)] for ai, bi, di in zip(a, b, d): adj[ai] = bi, di outg[ai] += 1 inc[bi] += 1 def recur_comp_cost(node, min_so_far): visited[node] = 1 if adj[node] != -1 and not visited[adj[node][0]]: cost = adj[node][1] neigh = adj[node][0] return recur_comp_cost(neigh, min(cost, min_so_far)) return node, min_so_far ans = [] for node in range(1, n + 1): if not visited[node] and adj[node] != -1 and inc[node] == 0: temp = [node] tap, min_cost = recur_comp_cost(node, adj[node][1]) temp.append(tap) temp.append(min_cost) ans.append(temp) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def solve(self, n, p, a, b, d): stack = [] start = {} end = {} diameter = {} ans = [] temp = [] for i in range(p): start[a[i]] = b[i] end[b[i]] = a[i] diameter[a[i]] = d[i] for i in range(1, n + 1): if i not in end and i in start: stack.append(start[i]) s = i d = diameter[i] e = 0 while stack: x = stack.pop() if x not in start: e = x else: if diameter[x] < d: d = diameter[x] stack.append(start[x]) ans.append([s, e, d]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR RETURN VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def solve(self, n, p, a, b, d): adj = dict() check = set(b) for i in range(len(d)): adj[a[i]] = b[i], d[i] def dfs(v): nonlocal adj, path, visited path += [v] visited.add(v) if v in adj: dfs(adj[v][0]) ans = [] for i in range(1, n + 1): visited = set() if i not in check and i not in visited and i in adj: path = [] dfs(i) fin = 10**10 for i in path[:-1]: fin = min(fin, adj[i][1]) x = [path[0], path[-1], fin] ans.append(x) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR FUNC_DEF VAR LIST VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def solve(self, n, p, a, b, d): visited = set() ans = [] inlet = [0] * (n + 1) outlet = [0] * (n + 1) graph = [[] for i in range(n + 1)] for i in range(0, p): outlet[a[i]] = 1 inlet[b[i]] = 1 graph[a[i]].append([b[i], d[i]]) def dfs(node, end, min_dia): visited.add(node) for neigh in graph[node]: if neigh[0] not in visited: min_dia[0] = min(min_dia[0], neigh[1]) end[0] = neigh[0] i = neigh[0] dfs(i, end, min_dia) return for i in range(1, n + 1): if inlet[i] == 0 and outlet[i] == 1 and i not in visited: start = i end = [-1] min_dia = [10000000000.0] dfs(i, end, min_dia) ans.append([start, end[0], min_dia[0]]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR LIST VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR NUMBER RETURN VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def solve(self, n, p, a, b, d): df = {} db = {} dv = {} for i in range(p): df[a[i]] = b[i] db[b[i]] = a[i] if a[i] not in dv: dv[a[i]] = d[i] else: dv[a[i]] = min(dv[a[i]], d[i]) if b[i] not in dv: dv[b[i]] = d[i] else: dv[b[i]] = min(dv[b[i]], d[i]) def pro(i, dic, val, vis): if i not in dic: vis[i] = 0 return i vis[i] = 0 res = pro(dic[i], dic, val, vis) mini = min(val[dic[i]], val[i]) val[i] = mini return res vis = [1] * (n + 1) res = [] for i in range(1, n + 1): if vis[i] and i in dv and i not in db: tres = [] tres.append(i) tres.append(pro(i, df, dv, vis)) tres.append(dv[i]) res.append(tres) return res
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def solve(self, n, p, a, b, d): graph = dict() for src, dst, diam in zip(a, b, d): graph[src] = dst, diam startNodes = sorted(list(set(a).difference(set(b)))) res = [] for node in startNodes: v = node minDiam = float("inf") while True: minDiam = min(graph[v][1], minDiam) v = graph[v][0] if v not in graph.keys(): break res.append((node, v, minDiam)) return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def dfs(self, i, adj): self.vis[i] = 1 if len(adj[i]) == 0: self.dis = min(self.dis, self.dic[i + 1]) self.fin = i return for j in adj[i]: if self.vis[j] == 0: self.dis = min(self.dis, self.dic[j + 1]) self.dfs(j, adj) def solve(self, n, p, a, b, d): ans = [] st = [i for i in a if i not in b] self.dic = {} for i in range(p): self.dic[b[i]] = d[i] adj = [[] for i in range(n)] for i in range(p): adj[a[i] - 1].append(b[i] - 1) self.vis = [0] * n st.sort() for i in st: i -= 1 if self.vis[i] == 0: self.fin = -1 self.dis = 100 self.dfs(i, adj) ans.append([i + 1, self.fin + 1, self.dis]) return ans if __name__ == "__main__": t = int(input()) for _ in range(t): n, p = map(int, input().strip().split()) a = [] b = [] d = [] for i in range(p): x, y, z = map(int, input().strip().split()) a.append(x) b.append(y) d.append(z) ob = Solution() ans = ob.solve(n, p, a, b, d) print(len(ans)) for i in ans: print(str(i[0]) + " " + str(i[1]) + " " + str(i[2]))
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def solve(self, n, p, a, b, d): ax = {} bx = {} for i in range(p): ax[a[i]] = i bx[b[i]] = i start = [] for i in a: if i not in bx: start.append(i) ans = [] for i in start: root = i index = ax[i] mn = d[index] while True: index = ax[i] mn = min(d[index], mn) if b[index] not in ax: ans.append([root, b[index], mn]) break else: i = b[index] ans.sort() return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR WHILE NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def solve(self, n, p, a, b, d): global ans def dfs(w): global ans if out_h[w] == 0: return w if wt_h[w] < ans: ans = wt_h[w] return dfs(out_h[w]) in_h = [0] * 1000 out_h = [0] * 1000 wt_h = [0] * 1000 l1 = [] l2 = [] for i in range(p): from_h = a[i] to_h = b[i] p_h = d[i] out_h[from_h] = to_h wt_h[from_h] = p_h in_h[to_h] = from_h for j in range(1, n + 1): l1 = [] if in_h[j] == 0 and out_h[j]: ans = 1000000000 w = dfs(j) l1.append(j) l1.append(w) l1.append(ans) l2.append(l1) return l2
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR NUMBER RETURN VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST IF VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def solve(self, n, p, a, b, d): vis = [0] * (n + 1) out = [0] * (n + 1) adj = {} for i in range(len(a)): adj[a[i]] = [b[i], d[i]] out[b[i]] = 1 def dfs(i): vis[i] = 1 if adj[i][0] in adj: val = dfs(adj[i][0]) return [val[0], min(val[1], adj[i][1])] else: return adj[i] ans = [] for i in range(1, n + 1): if i in adj and out[i] == 0: if vis[i] != 1: val = dfs(i) ans.append([i, val[0], val[1]]) return ans if __name__ == "__main__": t = int(input()) for _ in range(t): n, p = map(int, input().strip().split()) a = [] b = [] d = [] for i in range(p): x, y, z = map(int, input().strip().split()) a.append(x) b.append(y) d.append(z) ob = Solution() ans = ob.solve(n, p, a, b, d) print(len(ans)) for i in ans: print(str(i[0]) + " " + str(i[1]) + " " + str(i[2]))
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR LIST VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER RETURN LIST VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR NUMBER RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def solve(self, n, p, a, b, d): tanks, nex = [True] * (n + 1), [None] * (n + 1) for src, dst, dia in zip(a, b, d): nex[src] = dst, dia tanks[dst] = False ans = [] for src in range(1, n + 1): if not tanks[src]: continue dst = src mn = 10**9 while nex[dst] is not None: dst, dia = nex[dst] mn = min(mn, dia) if dst != src: ans.append((src, dst, mn)) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NONE BIN_OP VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR NONE ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def solve(self, n, p, a, b, d): visited = [False] * n adj = [None] * n ind = [0] * n out = [0] * n for i in range(p): adj[a[i] - 1] = b[i] - 1, d[i] ind[b[i] - 1] += 1 out[a[i] - 1] += 1 start = [] res = [] for v in range(n): if ind[v] == 0 and out[v] == 1: start.append(v) for s in start: q = s pd = 1000 while adj[q]: nxt, di = adj[q] q = nxt pd = min(pd, di) res.append([s + 1, q + 1, pd]) return res
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def solve(self, n, p, a, b, d): def dfs(w, ans, start, end, wt): if end[w] == 0: return w if wt[w] < ans[0]: ans[0] = wt[w] return dfs(end[w], ans, start, end, wt) start = [(0) for i in range(n + 1)] end = [(0) for i in range(n + 1)] wt = [(0) for i in range(n + 1)] for i in range(p): start[b[i]] = a[i] end[a[i]] = b[i] wt[a[i]] = d[i] res = [] for i in range(1, n + 1): if start[i] == 0 and end[i]: ans = [float("inf")] w = dfs(i, ans, start, end, wt) res += [[i, w, ans[0]]] return res
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR NUMBER RETURN VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR LIST FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR LIST LIST VAR VAR VAR NUMBER RETURN VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
INT_MAX = 2**32 def dfs(arr, start, tmp, visited): visited[start] = 1 w, c = arr[start] if w != -1 and visited[w] == 0: mw = min(tmp[0], c) tmp[0] = mw tmp[1] = w dfs(arr, w, tmp, visited) class Solution: def solve(self, n, p, a, b, d): ans = [] arr = [(-1, -1) for i in range(n + 1)] visited = [(0) for i in range(n + 1)] ina = [(0) for i in range(n + 1)] out = [(0) for i in range(n + 1)] for i in range(p): out[a[i]] = 1 ina[b[i]] = 1 arr[a[i]] = b[i], d[i] for i in range(1, n + 1): if ina[i] == 0 and out[i] != 0 and visited[i] == 0: start = i tmp = [INT_MAX, -1] dfs(arr, start, tmp, visited) ans.append([start, tmp[1], tmp[0]]) return ans if __name__ == "__main__": t = int(input()) for _ in range(t): n, p = map(int, input().strip().split()) a = [] b = [] d = [] for i in range(p): x, y, z = map(int, input().strip().split()) a.append(x) b.append(y) d.append(z) ob = Solution() ans = ob.solve(n, p, a, b, d) print(len(ans)) for i in ans: print(str(i[0]) + " " + str(i[1]) + " " + str(i[2]))
ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR NUMBER RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
from sys import maxsize class Solution: def solve(self, n, p, a, b, d): adj = [(-1) for i in range(n + 1)] ind = [(0) for i in range(n + 1)] for i in range(p): adj[a[i]] = [b[i], d[i]] ind[b[i]] += 1 def f(cn, tans): if adj[cn] == -1: return cn tans[0] = min(tans[0], adj[cn][1]) temp = f(adj[cn][0], tans) return temp ans = [] from sys import maxsize for i in range(1, n + 1): if ind[i] == 0 and adj[i] != -1: a1 = i tans = [maxsize] a2 = f(i, tans) a3 = tans[0] ans.append([a1, a2, a3]) ans.sort(key=lambda x: x[0]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR LIST VAR VAR VAR VAR VAR VAR VAR NUMBER FUNC_DEF IF VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def solve(self, n, p, a, b, d): ans = [[a[0], b[0], d[0]]] for i in range(1, len(a)): flag = 0 pos = -1 for j in range(len(ans)): if a[i] == ans[j][1]: flag += 1 if flag == 2: ans[pos][0] = ans[j][0] ans[pos][2] = min(ans[j][2], ans[pos][2]) ans.pop(j) break pos = j ans[j][1] = b[i] ans[j][2] = min(ans[j][2], d[i]) if b[i] == ans[j][0]: flag += 1 if flag == 2: ans[pos][1] = ans[j][1] ans[pos][2] = min(ans[j][2], ans[pos][2]) ans.pop(j) break pos = j ans[j][0] = a[i] ans[j][2] = min(ans[j][2], d[i]) if flag == 0: ans.append([a[i], b[i], d[i]]) return sorted(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def find(self, k, wt, end): temp = end[k] ans = wt[k] while end[temp] >= 0: ans = min(ans, wt[temp]) temp = end[temp] return temp, ans def solve(self, n, p, a, b, d): end = [-1] * (n + 1) start = [-1] * (n + 1) wt = [-1] * (n + 1) for i in range(p): end[a[i]] = b[i] start[b[i]] = a[i] wt[a[i]] = d[i] sol = [] for i in range(1, n + 1): if start[i] == -1 and end[i] >= 1: j, ans = self.find(i, wt, end) sol.append([i, j, ans]) return sol
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR RETURN VAR
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def solve(self, n, p, a, b, d): adjList = dict() inRight = set(b) possibleRoots = list() for i in range(p): adjList[a[i]] = b[i], d[i] if a[i] not in inRight: possibleRoots.append(a[i]) possibleRoots.sort() tot = list() stack = list() for i in range(len(possibleRoots)): stack.append(possibleRoots[i]) minRad = 100000000000000 temp = [possibleRoots[i]] while stack: popped = stack.pop() if not popped in adjList: continue stack.append(adjList[popped][0]) minRad = min(minRad, adjList[popped][1]) temp.extend([popped, minRad]) tot.append(temp) return tot if __name__ == "__main__": t = int(input()) for _ in range(t): n, p = map(int, input().strip().split()) a = [] b = [] d = [] for i in range(p): x, y, z = map(int, input().strip().split()) a.append(x) b.append(y) d.append(z) ob = Solution() ans = ob.solve(n, p, a, b, d) print(len(ans)) for i in ans: print(str(i[0]) + " " + str(i[1]) + " " + str(i[2]))
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
def dfs(w, cd, wt, ans): if cd[w] == 0: return w, ans if wt[w] < ans: ans = wt[w] return dfs(cd[w], cd, wt, ans) class Solution: def solve(self, n, p, a, b, d): cd = [(0) for i in range(n + 1)] rd = [(0) for i in range(n + 1)] wt = [(-1) for i in range(n + 1)] for i in range(0, len(a)): cd[a[i]] = b[i] rd[b[i]] = a[i] wt[a[i]] = d[i] res = [] for i in range(1, n + 1): if rd[i] == 0 and cd[i] != 0: p = dfs(i, cd, wt, 1000000) res.append((i, p[0], p[1])) return sorted(res, key=lambda x: x[0])
FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines. 1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof. 2. Every house with only one incoming and no outgoing pipe gets a tap. The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi. Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible. Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them. Example 1: Input: n = 9, p = 6 a[] = {7,5,4,2,9,3} b[] = {4,9,6,8,7,1} d[] = {98,72,10,22,17,66} Output: 3 2 8 22 3 1 66 5 6 10 Explanation: Connected components are 3->1, 5->9->7->4->6 and 2->8. Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10. Your Task: You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number). Expected Time Complexity: O(n+p) Expected Auxiliary Space: O(n+p) Constraints: 1<=n<=20 1<=p<=50 1<=a[i],b[i]<=20 1<=d[i]<=100
class Solution: def solve(self, n, p, a, b, d): i = 0 while i < p: flag = 0 for j in range(p): if i != j and a[j] != 0: if a[i] == b[j]: a[i] = a[j] a[j] = 0 b[j] = 0 flag = 1 d[i] = min(d[i], d[j]) break elif b[i] == a[j]: b[i] = b[j] a[j] = 0 b[j] = 0 flag = 1 d[i] = min(d[i], d[j]) break if flag == 0: i += 1 rt = [] for i in range(p): if a[i] != 0 and a[i] != b[i]: rt.append([a[i], b[i], d[i]]) return sorted(rt)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR
We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have an array A = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef","vyz"]. Suppose we chose a set of deletion indices D such that after deletions, the final array has its elements in lexicographic order (A[0] <= A[1] <= A[2] ... <= A[A.length - 1]). Return the minimum possible value of D.length.   Example 1: Input: ["ca","bb","ac"] Output: 1 Explanation: After deleting the first column, A = ["a", "b", "c"]. Now A is in lexicographic order (ie. A[0] <= A[1] <= A[2]). We require at least 1 deletion since initially A was not in lexicographic order, so the answer is 1. Example 2: Input: ["xc","yb","za"] Output: 0 Explanation: A is already in lexicographic order, so we don't need to delete anything. Note that the rows of A are not necessarily in lexicographic order: ie. it is NOT necessarily true that (A[0][0] <= A[0][1] <= ...) Example 3: Input: ["zyx","wvu","tsr"] Output: 3 Explanation: We have to delete every column.   Note: 1 <= A.length <= 100 1 <= A[i].length <= 100
class Solution: def minDeletionSize(self, A: List[str]) -> int: L = len(A) N = len(A[0]) sort_groups = [[i for i in range(L)]] count = 0 for i in range(N): new_groups = [] in_order = True for group in sort_groups: new_group = [] curr_ord = 0 for g_idx in group: char = A[g_idx][i] if ord(char) > curr_ord: if curr_ord != 0: new_groups.append(new_group) new_group = [g_idx] curr_ord = ord(char) elif ord(char) == curr_ord: new_group.append(g_idx) else: in_order = False break if not in_order: break if new_group != []: new_groups.append(new_group) if in_order: sort_groups = new_groups else: count += 1 return count
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR LIST EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR
We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have an array A = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef","vyz"]. Suppose we chose a set of deletion indices D such that after deletions, the final array has its elements in lexicographic order (A[0] <= A[1] <= A[2] ... <= A[A.length - 1]). Return the minimum possible value of D.length.   Example 1: Input: ["ca","bb","ac"] Output: 1 Explanation: After deleting the first column, A = ["a", "b", "c"]. Now A is in lexicographic order (ie. A[0] <= A[1] <= A[2]). We require at least 1 deletion since initially A was not in lexicographic order, so the answer is 1. Example 2: Input: ["xc","yb","za"] Output: 0 Explanation: A is already in lexicographic order, so we don't need to delete anything. Note that the rows of A are not necessarily in lexicographic order: ie. it is NOT necessarily true that (A[0][0] <= A[0][1] <= ...) Example 3: Input: ["zyx","wvu","tsr"] Output: 3 Explanation: We have to delete every column.   Note: 1 <= A.length <= 100 1 <= A[i].length <= 100
class Solution: def minDeletionSize(self, A): m, n = len(A), len(A[0]) first = [False] * m res = 0 for j in range(n): for i in range(1, m): if not first[i] and A[i][j] < A[i - 1][j]: res += 1 break else: for i in range(1, m): if A[i][j] > A[i - 1][j]: first[i] = True return res
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER RETURN VAR
We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have an array A = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef","vyz"]. Suppose we chose a set of deletion indices D such that after deletions, the final array has its elements in lexicographic order (A[0] <= A[1] <= A[2] ... <= A[A.length - 1]). Return the minimum possible value of D.length.   Example 1: Input: ["ca","bb","ac"] Output: 1 Explanation: After deleting the first column, A = ["a", "b", "c"]. Now A is in lexicographic order (ie. A[0] <= A[1] <= A[2]). We require at least 1 deletion since initially A was not in lexicographic order, so the answer is 1. Example 2: Input: ["xc","yb","za"] Output: 0 Explanation: A is already in lexicographic order, so we don't need to delete anything. Note that the rows of A are not necessarily in lexicographic order: ie. it is NOT necessarily true that (A[0][0] <= A[0][1] <= ...) Example 3: Input: ["zyx","wvu","tsr"] Output: 3 Explanation: We have to delete every column.   Note: 1 <= A.length <= 100 1 <= A[i].length <= 100
class Solution: def minDeletionSize(self, words: List[str]) -> int: n = len(words) if n == 0: return 0 w = len(words[0]) intervals = [[0, n]] del_cols = 0 for col in range(w): next_intervals = [] del_this_col = 0 for start, end in intervals: cprev = words[start][col] iprev = start for i in range(start + 1, end): c = words[i][col] if c > cprev: next_intervals.append([iprev, i]) cprev = c iprev = i elif c < cprev: del_this_col = 1 del_cols += 1 break pass if del_this_col: break next_intervals.append([iprev, end]) pass if not del_this_col: intervals = next_intervals pass return del_cols
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR ASSIGN VAR VAR RETURN VAR VAR
We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have an array A = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef","vyz"]. Suppose we chose a set of deletion indices D such that after deletions, the final array has its elements in lexicographic order (A[0] <= A[1] <= A[2] ... <= A[A.length - 1]). Return the minimum possible value of D.length.   Example 1: Input: ["ca","bb","ac"] Output: 1 Explanation: After deleting the first column, A = ["a", "b", "c"]. Now A is in lexicographic order (ie. A[0] <= A[1] <= A[2]). We require at least 1 deletion since initially A was not in lexicographic order, so the answer is 1. Example 2: Input: ["xc","yb","za"] Output: 0 Explanation: A is already in lexicographic order, so we don't need to delete anything. Note that the rows of A are not necessarily in lexicographic order: ie. it is NOT necessarily true that (A[0][0] <= A[0][1] <= ...) Example 3: Input: ["zyx","wvu","tsr"] Output: 3 Explanation: We have to delete every column.   Note: 1 <= A.length <= 100 1 <= A[i].length <= 100
class Solution: def isArrSorted(self, a) -> tuple((bool, bool)): is_exp_sorted = True is_eq_sorted = True for i, l in enumerate(a): if i == len(a) - 1: break if not l < a[i + 1]: is_exp_sorted = False if not l <= a[i + 1]: is_eq_sorted = False return is_exp_sorted, is_eq_sorted def minDeletionSize(self, A: List[str]) -> int: deletion_count = 0 built_A = [""] * len(A) for i in range(0, len(A[0])): letters_at_i = [s[i] for s in A] is_exp_sorted, is_eq_sorted = self.isArrSorted(letters_at_i) if is_exp_sorted: break test_A = [(built_A[oi] + o[i]) for oi, o in enumerate(A)] if list(sorted(test_A)) == test_A: built_A = test_A continue else: deletion_count += 1 return deletion_count
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR
We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have an array A = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef","vyz"]. Suppose we chose a set of deletion indices D such that after deletions, the final array has its elements in lexicographic order (A[0] <= A[1] <= A[2] ... <= A[A.length - 1]). Return the minimum possible value of D.length.   Example 1: Input: ["ca","bb","ac"] Output: 1 Explanation: After deleting the first column, A = ["a", "b", "c"]. Now A is in lexicographic order (ie. A[0] <= A[1] <= A[2]). We require at least 1 deletion since initially A was not in lexicographic order, so the answer is 1. Example 2: Input: ["xc","yb","za"] Output: 0 Explanation: A is already in lexicographic order, so we don't need to delete anything. Note that the rows of A are not necessarily in lexicographic order: ie. it is NOT necessarily true that (A[0][0] <= A[0][1] <= ...) Example 3: Input: ["zyx","wvu","tsr"] Output: 3 Explanation: We have to delete every column.   Note: 1 <= A.length <= 100 1 <= A[i].length <= 100
class Solution: def checkLexOrder(self, A: List[str]): if A == sorted(A): return True else: return False def findDelIndices(self, A: List[str]): iter_ = len(A[0]) to_be_del = [] temp = [""] * len(A) for i in range(iter_): if self.checkLexOrder([x[i] for x in A]) == True: temp = [(temp[j] + A[j][i]) for j in range(len(A))] else: temp_ = [(temp[j] + A[j][i]) for j in range(len(A))] if self.checkLexOrder(temp_) == True: temp = [(temp[j] + A[j][i]) for j in range(len(A))] else: to_be_del.append(i) return len(to_be_del) def minDeletionSize(self, A: List[str]) -> int: if self.checkLexOrder(A): return 0 else: return self.findDelIndices(A)
CLASS_DEF FUNC_DEF VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR
We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have an array A = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef","vyz"]. Suppose we chose a set of deletion indices D such that after deletions, the final array has its elements in lexicographic order (A[0] <= A[1] <= A[2] ... <= A[A.length - 1]). Return the minimum possible value of D.length.   Example 1: Input: ["ca","bb","ac"] Output: 1 Explanation: After deleting the first column, A = ["a", "b", "c"]. Now A is in lexicographic order (ie. A[0] <= A[1] <= A[2]). We require at least 1 deletion since initially A was not in lexicographic order, so the answer is 1. Example 2: Input: ["xc","yb","za"] Output: 0 Explanation: A is already in lexicographic order, so we don't need to delete anything. Note that the rows of A are not necessarily in lexicographic order: ie. it is NOT necessarily true that (A[0][0] <= A[0][1] <= ...) Example 3: Input: ["zyx","wvu","tsr"] Output: 3 Explanation: We have to delete every column.   Note: 1 <= A.length <= 100 1 <= A[i].length <= 100
class Solution: def minDeletionSize(self, A: List[str]) -> int: def isSorted(arr, i, j): return all(arr[k] <= arr[k + 1] for k in range(i, j)) ans = 0 ranges = [[0, len(A) - 1]] for col in zip(*A): if not ranges: break if all(isSorted(col, i, j) for i, j in ranges): tmp = [] for i, j in ranges: start = i for k in range(i, j + 1): if col[k] != col[start]: if k - start > 1: tmp.append([start, k - 1]) start = k if j + 1 - start > 1: tmp.append([start, j]) start = k ranges[:] = tmp else: ans += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR
We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have an array A = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef","vyz"]. Suppose we chose a set of deletion indices D such that after deletions, the final array has its elements in lexicographic order (A[0] <= A[1] <= A[2] ... <= A[A.length - 1]). Return the minimum possible value of D.length.   Example 1: Input: ["ca","bb","ac"] Output: 1 Explanation: After deleting the first column, A = ["a", "b", "c"]. Now A is in lexicographic order (ie. A[0] <= A[1] <= A[2]). We require at least 1 deletion since initially A was not in lexicographic order, so the answer is 1. Example 2: Input: ["xc","yb","za"] Output: 0 Explanation: A is already in lexicographic order, so we don't need to delete anything. Note that the rows of A are not necessarily in lexicographic order: ie. it is NOT necessarily true that (A[0][0] <= A[0][1] <= ...) Example 3: Input: ["zyx","wvu","tsr"] Output: 3 Explanation: We have to delete every column.   Note: 1 <= A.length <= 100 1 <= A[i].length <= 100
class Solution: def minDeletionSize(self, A: List[str]) -> int: D = set() while True: changes = False last_str = "" for i, ch in enumerate(A[0]): if i not in D: last_str += ch for x in A[1:]: this_str = "" this_idx = [] for i, ch in enumerate(x): if i not in D: this_str += ch this_idx.append(i) while this_str < last_str: for i in range(len(this_str)): if this_str[i] < last_str[i]: D.add(this_idx[i]) this_idx = this_idx[:i] + this_idx[i + 1 :] this_str = this_str[:i] + this_str[i + 1 :] last_str = last_str[:i] + last_str[i + 1 :] changes = True break last_str = this_str if not changes: break return len(D)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR FOR VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR RETURN FUNC_CALL VAR VAR VAR
Soroush and Keshi each have a labeled and rooted tree on n vertices. Both of their trees are rooted from vertex 1. Soroush and Keshi used to be at war. After endless decades of fighting, they finally became allies to prepare a Codeforces round. To celebrate this fortunate event, they decided to make a memorial graph on n vertices. They add an edge between vertices u and v in the memorial graph if both of the following conditions hold: * One of u or v is the ancestor of the other in Soroush's tree. * Neither of u or v is the ancestor of the other in Keshi's tree. Here vertex u is considered ancestor of vertex v, if u lies on the path from 1 (the root) to the v. Popping out of nowhere, Mashtali tried to find the maximum clique in the memorial graph for no reason. He failed because the graph was too big. Help Mashtali by finding the size of the maximum clique in the memorial graph. As a reminder, clique is a subset of vertices of the graph, each two of which are connected by an edge. Input The first line contains an integer t (1≤ t≤ 3 ⋅ 10^5) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer n (2≤ n≤ 3 ⋅ 10^5). The second line of each test case contains n-1 integers a_2, …, a_n (1 ≤ a_i < i), a_i being the parent of the vertex i in Soroush's tree. The third line of each test case contains n-1 integers b_2, …, b_n (1 ≤ b_i < i), b_i being the parent of the vertex i in Keshi's tree. It is guaranteed that the given graphs are trees. It is guaranteed that the sum of n over all test cases doesn't exceed 3 ⋅ 10^5. Output For each test case print a single integer — the size of the maximum clique in the memorial graph. Example Input 4 4 1 2 3 1 2 3 5 1 2 3 4 1 1 1 1 6 1 1 1 1 2 1 2 1 2 2 7 1 1 3 4 4 5 1 2 1 4 2 5 Output 1 4 1 3 Note In the first and third test cases, you can pick any vertex. In the second test case, one of the maximum cliques is \{2, 3, 4, 5\}. In the fourth test case, one of the maximum cliques is \{3, 4, 6\}.
import sys input = sys.stdin.buffer.readline class bit: def __init__(self, n): self.n = n self.a = [0] * (n + 1) def add(self, idx, val): idx += 1 while idx < self.n: self.a[idx] += val idx += idx & -idx def sumr(self, idx): idx += 1 tot = 0 while idx: tot += self.a[idx] idx -= idx & -idx return tot def sum(self, l, r): return self.sumr(r) - self.sumr(l - 1) def add(i): bit2.add(tin[i], 1) bit1.add(tin[i], i) bit1.add(tout[i] + 1, -i) def remove(i): bit2.add(tin[i], -1) bit1.add(tin[i], -i) bit1.add(tout[i] + 1, i) for _ in range(int(input())): n = int(input()) adj = [[[] for i in range(n + 1)] for j in range(2)] for k in range(2): a = list(map(int, input().split())) for i in range(2, n + 1): adj[k][a[i - 2]].append(i) ans = 0 tin = [0] * (n + 1) tout = [0] * (n + 1) t = -1 s = [1] visited = [0] * (n + 1) while s: c = s[-1] if not visited[c]: t += 1 tin[c] = t visited[c] = 1 for ne in adj[1][c]: if not visited[ne]: s.append(ne) else: tout[c] = t s.pop() curr_mx = 0 bit1 = bit(n + 1) bit2 = bit(n + 1) ops = [] s = [1] visited = [0] * (n + 1) while s: c = s[-1] if not visited[c]: visited[c] = 1 u = bit1.sumr(tin[c]) if u: remove(u) add(c) ops.append([u, c]) elif bit2.sum(tin[c], tout[c]): ops.append([0, 0]) else: add(c) curr_mx += 1 ops.append([0, c]) ans = max(ans, curr_mx) for ne in adj[0][c]: if not visited[ne]: s.append(ne) else: u, v = ops.pop() if u: add(u) curr_mx += 1 if v: remove(v) curr_mx -= 1 s.pop() print(ans)
IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR 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 BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL 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 EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
In the gym, Chef prefers to lift at least $W$ grams during a bench press and if that's impossible, Chef considers his workout to be incomplete and feels bad. The rod weighs $W_{r}$ grams and there are $N$ other weights lying on the floor that weigh $w_{1}, w_{2}, ..., w_{N}$ grams. To maintain balance and to ensure that there is no unnecessary load due to torque, it's important that the weights added to the left side are symmetric to the weights added to the right side. It is not required to use all of the weights. It is also not required to use any weights at all, if Chef feels satisfied lifting only the rod. For example: - $1$ $2$ $2$ $1$ $|$Rod Center$|$ $1$ $1$ $1$ $3$ is a wrong configuration, but - $1$ $2$ $3$ $1$ $|$Rod Center$|$ $1$ $3$ $2$ $1$ is a right configuration. Find whether Chef will be able to collect the required weights to feel satisfied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains two lines of input. The first line contains three space-separated integers $N, W, W_{r}$. The second line contains $N$ space-separated integers $w_{1}, w_{2}, \ldots, w_{N}$. ------ Output ------ For each test case, output the answer in a single line: "YES" if Chef will be satisfied after his workout and "NO" if not (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ W ≤ 2\cdot 10^{5}$ $1 ≤ w_{i} ≤ 10^{5}$ $1 ≤ W_{r} ≤ 2\cdot 10^{4}$ ------ Subtasks ------ Subtask #1 (30 points): $w_{i} = 1$ for all valid $i$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 3 2 5 10 2 2 7 100 50 100 10 10 10 10 10 90 6 100 40 10 10 10 10 10 10 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: Since the weight of the rod is at least the required weight to be lifted, Chef will feel satisfied after the workout. Test case 2: The configuration having maximum weight is: $10$ $10$ $|$Rod Center$|$ $10$ $10$ So the maximum total weight Chef can lift is $50 + 4 \cdot 10 = 90$ which is less than the required amount to get satisfied. Test case 3: The configuration having maximum weight is: $10$ $10$ $10$ $|$Rod Center$|$ $10$ $10$ $10$ So the maximum total weight Chef can lift is $40 + 6\cdot 10 = 100$ which is equal to the required amount to get satisfied.
for _ in range(int(input())): n, w, wr = map(int, input().split()) arr = list(map(int, input().split())) if w <= wr: print("YES") else: d = dict() for i in arr: if i in d: d[i] += 1 else: d[i] = 1 for i in d: if d[i] >= 2: wr += d[i] // 2 * 2 * i if wr >= w: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
In the gym, Chef prefers to lift at least $W$ grams during a bench press and if that's impossible, Chef considers his workout to be incomplete and feels bad. The rod weighs $W_{r}$ grams and there are $N$ other weights lying on the floor that weigh $w_{1}, w_{2}, ..., w_{N}$ grams. To maintain balance and to ensure that there is no unnecessary load due to torque, it's important that the weights added to the left side are symmetric to the weights added to the right side. It is not required to use all of the weights. It is also not required to use any weights at all, if Chef feels satisfied lifting only the rod. For example: - $1$ $2$ $2$ $1$ $|$Rod Center$|$ $1$ $1$ $1$ $3$ is a wrong configuration, but - $1$ $2$ $3$ $1$ $|$Rod Center$|$ $1$ $3$ $2$ $1$ is a right configuration. Find whether Chef will be able to collect the required weights to feel satisfied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains two lines of input. The first line contains three space-separated integers $N, W, W_{r}$. The second line contains $N$ space-separated integers $w_{1}, w_{2}, \ldots, w_{N}$. ------ Output ------ For each test case, output the answer in a single line: "YES" if Chef will be satisfied after his workout and "NO" if not (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ W ≤ 2\cdot 10^{5}$ $1 ≤ w_{i} ≤ 10^{5}$ $1 ≤ W_{r} ≤ 2\cdot 10^{4}$ ------ Subtasks ------ Subtask #1 (30 points): $w_{i} = 1$ for all valid $i$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 3 2 5 10 2 2 7 100 50 100 10 10 10 10 10 90 6 100 40 10 10 10 10 10 10 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: Since the weight of the rod is at least the required weight to be lifted, Chef will feel satisfied after the workout. Test case 2: The configuration having maximum weight is: $10$ $10$ $|$Rod Center$|$ $10$ $10$ So the maximum total weight Chef can lift is $50 + 4 \cdot 10 = 90$ which is less than the required amount to get satisfied. Test case 3: The configuration having maximum weight is: $10$ $10$ $10$ $|$Rod Center$|$ $10$ $10$ $10$ So the maximum total weight Chef can lift is $40 + 6\cdot 10 = 100$ which is equal to the required amount to get satisfied.
t = int(input()) for i in range(t): n, wt, wr = map(int, input().split()) lst = sorted(list(map(int, input().split()))) res = wr i = 0 while i < n - 1: if lst[i] == lst[i + 1]: res += 2 * lst[i] i += 2 else: i += 1 if res >= wt: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
In the gym, Chef prefers to lift at least $W$ grams during a bench press and if that's impossible, Chef considers his workout to be incomplete and feels bad. The rod weighs $W_{r}$ grams and there are $N$ other weights lying on the floor that weigh $w_{1}, w_{2}, ..., w_{N}$ grams. To maintain balance and to ensure that there is no unnecessary load due to torque, it's important that the weights added to the left side are symmetric to the weights added to the right side. It is not required to use all of the weights. It is also not required to use any weights at all, if Chef feels satisfied lifting only the rod. For example: - $1$ $2$ $2$ $1$ $|$Rod Center$|$ $1$ $1$ $1$ $3$ is a wrong configuration, but - $1$ $2$ $3$ $1$ $|$Rod Center$|$ $1$ $3$ $2$ $1$ is a right configuration. Find whether Chef will be able to collect the required weights to feel satisfied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains two lines of input. The first line contains three space-separated integers $N, W, W_{r}$. The second line contains $N$ space-separated integers $w_{1}, w_{2}, \ldots, w_{N}$. ------ Output ------ For each test case, output the answer in a single line: "YES" if Chef will be satisfied after his workout and "NO" if not (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ W ≤ 2\cdot 10^{5}$ $1 ≤ w_{i} ≤ 10^{5}$ $1 ≤ W_{r} ≤ 2\cdot 10^{4}$ ------ Subtasks ------ Subtask #1 (30 points): $w_{i} = 1$ for all valid $i$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 3 2 5 10 2 2 7 100 50 100 10 10 10 10 10 90 6 100 40 10 10 10 10 10 10 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: Since the weight of the rod is at least the required weight to be lifted, Chef will feel satisfied after the workout. Test case 2: The configuration having maximum weight is: $10$ $10$ $|$Rod Center$|$ $10$ $10$ So the maximum total weight Chef can lift is $50 + 4 \cdot 10 = 90$ which is less than the required amount to get satisfied. Test case 3: The configuration having maximum weight is: $10$ $10$ $10$ $|$Rod Center$|$ $10$ $10$ $10$ So the maximum total weight Chef can lift is $40 + 6\cdot 10 = 100$ which is equal to the required amount to get satisfied.
t = int(input()) while t: t -= 1 n, w, wr = map(int, input().split()) a = list(map(int, input().split())) d = {} for item in a: if item not in d.keys(): d[item] = 0 d[item] += 1 a = list(set(a)) for item in a: if d[item] > 1: wr += item * 2 * (d[item] // 2) if wr >= w: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
In the gym, Chef prefers to lift at least $W$ grams during a bench press and if that's impossible, Chef considers his workout to be incomplete and feels bad. The rod weighs $W_{r}$ grams and there are $N$ other weights lying on the floor that weigh $w_{1}, w_{2}, ..., w_{N}$ grams. To maintain balance and to ensure that there is no unnecessary load due to torque, it's important that the weights added to the left side are symmetric to the weights added to the right side. It is not required to use all of the weights. It is also not required to use any weights at all, if Chef feels satisfied lifting only the rod. For example: - $1$ $2$ $2$ $1$ $|$Rod Center$|$ $1$ $1$ $1$ $3$ is a wrong configuration, but - $1$ $2$ $3$ $1$ $|$Rod Center$|$ $1$ $3$ $2$ $1$ is a right configuration. Find whether Chef will be able to collect the required weights to feel satisfied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains two lines of input. The first line contains three space-separated integers $N, W, W_{r}$. The second line contains $N$ space-separated integers $w_{1}, w_{2}, \ldots, w_{N}$. ------ Output ------ For each test case, output the answer in a single line: "YES" if Chef will be satisfied after his workout and "NO" if not (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ W ≤ 2\cdot 10^{5}$ $1 ≤ w_{i} ≤ 10^{5}$ $1 ≤ W_{r} ≤ 2\cdot 10^{4}$ ------ Subtasks ------ Subtask #1 (30 points): $w_{i} = 1$ for all valid $i$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 3 2 5 10 2 2 7 100 50 100 10 10 10 10 10 90 6 100 40 10 10 10 10 10 10 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: Since the weight of the rod is at least the required weight to be lifted, Chef will feel satisfied after the workout. Test case 2: The configuration having maximum weight is: $10$ $10$ $|$Rod Center$|$ $10$ $10$ So the maximum total weight Chef can lift is $50 + 4 \cdot 10 = 90$ which is less than the required amount to get satisfied. Test case 3: The configuration having maximum weight is: $10$ $10$ $10$ $|$Rod Center$|$ $10$ $10$ $10$ So the maximum total weight Chef can lift is $40 + 6\cdot 10 = 100$ which is equal to the required amount to get satisfied.
for _ in range(int(input())): l = str(input()).split() N = int(l[0]) W = int(l[1]) Wr = int(l[2]) a = str(input()).split() a = list(map(int, a)) m = {} for i in range(N): m[a[i]] = m.get(a[i], 0) + 1 s = 0 for i in m: s += i * (m[i] // 2) s *= 2 if s + Wr >= W: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
In the gym, Chef prefers to lift at least $W$ grams during a bench press and if that's impossible, Chef considers his workout to be incomplete and feels bad. The rod weighs $W_{r}$ grams and there are $N$ other weights lying on the floor that weigh $w_{1}, w_{2}, ..., w_{N}$ grams. To maintain balance and to ensure that there is no unnecessary load due to torque, it's important that the weights added to the left side are symmetric to the weights added to the right side. It is not required to use all of the weights. It is also not required to use any weights at all, if Chef feels satisfied lifting only the rod. For example: - $1$ $2$ $2$ $1$ $|$Rod Center$|$ $1$ $1$ $1$ $3$ is a wrong configuration, but - $1$ $2$ $3$ $1$ $|$Rod Center$|$ $1$ $3$ $2$ $1$ is a right configuration. Find whether Chef will be able to collect the required weights to feel satisfied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains two lines of input. The first line contains three space-separated integers $N, W, W_{r}$. The second line contains $N$ space-separated integers $w_{1}, w_{2}, \ldots, w_{N}$. ------ Output ------ For each test case, output the answer in a single line: "YES" if Chef will be satisfied after his workout and "NO" if not (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ W ≤ 2\cdot 10^{5}$ $1 ≤ w_{i} ≤ 10^{5}$ $1 ≤ W_{r} ≤ 2\cdot 10^{4}$ ------ Subtasks ------ Subtask #1 (30 points): $w_{i} = 1$ for all valid $i$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 3 2 5 10 2 2 7 100 50 100 10 10 10 10 10 90 6 100 40 10 10 10 10 10 10 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: Since the weight of the rod is at least the required weight to be lifted, Chef will feel satisfied after the workout. Test case 2: The configuration having maximum weight is: $10$ $10$ $|$Rod Center$|$ $10$ $10$ So the maximum total weight Chef can lift is $50 + 4 \cdot 10 = 90$ which is less than the required amount to get satisfied. Test case 3: The configuration having maximum weight is: $10$ $10$ $10$ $|$Rod Center$|$ $10$ $10$ $10$ So the maximum total weight Chef can lift is $40 + 6\cdot 10 = 100$ which is equal to the required amount to get satisfied.
for i in range(int(input())): N, w, W = map(int, input().split()) arr = list(map(int, input().split())) flag = 0 d = {} for j in range(len(arr)): if arr[j] in d: d[arr[j]] += 1 else: d[arr[j]] = 1 if w <= W: print("YES") else: max_wt = W for j in d.keys(): if d[j] % 2 == 0: max_wt += d[j] * j else: max_wt += (d[j] - 1) * j if max_wt >= w: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
In the gym, Chef prefers to lift at least $W$ grams during a bench press and if that's impossible, Chef considers his workout to be incomplete and feels bad. The rod weighs $W_{r}$ grams and there are $N$ other weights lying on the floor that weigh $w_{1}, w_{2}, ..., w_{N}$ grams. To maintain balance and to ensure that there is no unnecessary load due to torque, it's important that the weights added to the left side are symmetric to the weights added to the right side. It is not required to use all of the weights. It is also not required to use any weights at all, if Chef feels satisfied lifting only the rod. For example: - $1$ $2$ $2$ $1$ $|$Rod Center$|$ $1$ $1$ $1$ $3$ is a wrong configuration, but - $1$ $2$ $3$ $1$ $|$Rod Center$|$ $1$ $3$ $2$ $1$ is a right configuration. Find whether Chef will be able to collect the required weights to feel satisfied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains two lines of input. The first line contains three space-separated integers $N, W, W_{r}$. The second line contains $N$ space-separated integers $w_{1}, w_{2}, \ldots, w_{N}$. ------ Output ------ For each test case, output the answer in a single line: "YES" if Chef will be satisfied after his workout and "NO" if not (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ W ≤ 2\cdot 10^{5}$ $1 ≤ w_{i} ≤ 10^{5}$ $1 ≤ W_{r} ≤ 2\cdot 10^{4}$ ------ Subtasks ------ Subtask #1 (30 points): $w_{i} = 1$ for all valid $i$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 3 2 5 10 2 2 7 100 50 100 10 10 10 10 10 90 6 100 40 10 10 10 10 10 10 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: Since the weight of the rod is at least the required weight to be lifted, Chef will feel satisfied after the workout. Test case 2: The configuration having maximum weight is: $10$ $10$ $|$Rod Center$|$ $10$ $10$ So the maximum total weight Chef can lift is $50 + 4 \cdot 10 = 90$ which is less than the required amount to get satisfied. Test case 3: The configuration having maximum weight is: $10$ $10$ $10$ $|$Rod Center$|$ $10$ $10$ $10$ So the maximum total weight Chef can lift is $40 + 6\cdot 10 = 100$ which is equal to the required amount to get satisfied.
for t in range(int(input())): n, w, r = map(int, input().split()) l = [int(x) for x in input().split()] if w - r <= 0: print("YES") else: w -= r d = {} for x in l: if x in d: d[x] += 1 else: d[x] = 1 for x, y in d.items(): if y % 2 == 0: w -= y * x if w <= 0: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
In the gym, Chef prefers to lift at least $W$ grams during a bench press and if that's impossible, Chef considers his workout to be incomplete and feels bad. The rod weighs $W_{r}$ grams and there are $N$ other weights lying on the floor that weigh $w_{1}, w_{2}, ..., w_{N}$ grams. To maintain balance and to ensure that there is no unnecessary load due to torque, it's important that the weights added to the left side are symmetric to the weights added to the right side. It is not required to use all of the weights. It is also not required to use any weights at all, if Chef feels satisfied lifting only the rod. For example: - $1$ $2$ $2$ $1$ $|$Rod Center$|$ $1$ $1$ $1$ $3$ is a wrong configuration, but - $1$ $2$ $3$ $1$ $|$Rod Center$|$ $1$ $3$ $2$ $1$ is a right configuration. Find whether Chef will be able to collect the required weights to feel satisfied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains two lines of input. The first line contains three space-separated integers $N, W, W_{r}$. The second line contains $N$ space-separated integers $w_{1}, w_{2}, \ldots, w_{N}$. ------ Output ------ For each test case, output the answer in a single line: "YES" if Chef will be satisfied after his workout and "NO" if not (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ W ≤ 2\cdot 10^{5}$ $1 ≤ w_{i} ≤ 10^{5}$ $1 ≤ W_{r} ≤ 2\cdot 10^{4}$ ------ Subtasks ------ Subtask #1 (30 points): $w_{i} = 1$ for all valid $i$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 3 2 5 10 2 2 7 100 50 100 10 10 10 10 10 90 6 100 40 10 10 10 10 10 10 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: Since the weight of the rod is at least the required weight to be lifted, Chef will feel satisfied after the workout. Test case 2: The configuration having maximum weight is: $10$ $10$ $|$Rod Center$|$ $10$ $10$ So the maximum total weight Chef can lift is $50 + 4 \cdot 10 = 90$ which is less than the required amount to get satisfied. Test case 3: The configuration having maximum weight is: $10$ $10$ $10$ $|$Rod Center$|$ $10$ $10$ $10$ So the maximum total weight Chef can lift is $40 + 6\cdot 10 = 100$ which is equal to the required amount to get satisfied.
for _ in range(int(input())): n, w, wr = [int(a) for a in input().split()] arr = [int(a) for a in input().split()] d = {} for l in arr: if l in d: d[l] += 1 else: d[l] = 1 c = 0 for l in d: c = c + d[l] // 2 * 2 * l if c + wr >= w: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
In the gym, Chef prefers to lift at least $W$ grams during a bench press and if that's impossible, Chef considers his workout to be incomplete and feels bad. The rod weighs $W_{r}$ grams and there are $N$ other weights lying on the floor that weigh $w_{1}, w_{2}, ..., w_{N}$ grams. To maintain balance and to ensure that there is no unnecessary load due to torque, it's important that the weights added to the left side are symmetric to the weights added to the right side. It is not required to use all of the weights. It is also not required to use any weights at all, if Chef feels satisfied lifting only the rod. For example: - $1$ $2$ $2$ $1$ $|$Rod Center$|$ $1$ $1$ $1$ $3$ is a wrong configuration, but - $1$ $2$ $3$ $1$ $|$Rod Center$|$ $1$ $3$ $2$ $1$ is a right configuration. Find whether Chef will be able to collect the required weights to feel satisfied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains two lines of input. The first line contains three space-separated integers $N, W, W_{r}$. The second line contains $N$ space-separated integers $w_{1}, w_{2}, \ldots, w_{N}$. ------ Output ------ For each test case, output the answer in a single line: "YES" if Chef will be satisfied after his workout and "NO" if not (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ W ≤ 2\cdot 10^{5}$ $1 ≤ w_{i} ≤ 10^{5}$ $1 ≤ W_{r} ≤ 2\cdot 10^{4}$ ------ Subtasks ------ Subtask #1 (30 points): $w_{i} = 1$ for all valid $i$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 3 2 5 10 2 2 7 100 50 100 10 10 10 10 10 90 6 100 40 10 10 10 10 10 10 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: Since the weight of the rod is at least the required weight to be lifted, Chef will feel satisfied after the workout. Test case 2: The configuration having maximum weight is: $10$ $10$ $|$Rod Center$|$ $10$ $10$ So the maximum total weight Chef can lift is $50 + 4 \cdot 10 = 90$ which is less than the required amount to get satisfied. Test case 3: The configuration having maximum weight is: $10$ $10$ $10$ $|$Rod Center$|$ $10$ $10$ $10$ So the maximum total weight Chef can lift is $40 + 6\cdot 10 = 100$ which is equal to the required amount to get satisfied.
def Satisfaction(N, W, Wr, W_list): if N <= 1 and Wr == W: return "YES" elif N <= 1 and Wr != W: return "NO" elif sum(W_list) + Wr < W: return "NO" else: p = W - Wr j = 0 while j < N - 1: if p <= 0: return "YES" if j < N - 1 and W_list[j] == W_list[j + 1]: p = p - 2 * W_list[j] j += 2 else: j += 1 if p <= 0: return "YES" if j >= N - 1 and p > 0: return "NO" T = int(input()) for i in range(T): N, W, Wr = map(int, input().split()) W_list = list(map(int, input().split())) W_list = sorted(W_list) result = Satisfaction(N, W, Wr, W_list) print(result)
FUNC_DEF IF VAR NUMBER VAR VAR RETURN STRING IF VAR NUMBER VAR VAR RETURN STRING IF BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN STRING IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN STRING IF VAR BIN_OP VAR NUMBER VAR NUMBER RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
In the gym, Chef prefers to lift at least $W$ grams during a bench press and if that's impossible, Chef considers his workout to be incomplete and feels bad. The rod weighs $W_{r}$ grams and there are $N$ other weights lying on the floor that weigh $w_{1}, w_{2}, ..., w_{N}$ grams. To maintain balance and to ensure that there is no unnecessary load due to torque, it's important that the weights added to the left side are symmetric to the weights added to the right side. It is not required to use all of the weights. It is also not required to use any weights at all, if Chef feels satisfied lifting only the rod. For example: - $1$ $2$ $2$ $1$ $|$Rod Center$|$ $1$ $1$ $1$ $3$ is a wrong configuration, but - $1$ $2$ $3$ $1$ $|$Rod Center$|$ $1$ $3$ $2$ $1$ is a right configuration. Find whether Chef will be able to collect the required weights to feel satisfied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains two lines of input. The first line contains three space-separated integers $N, W, W_{r}$. The second line contains $N$ space-separated integers $w_{1}, w_{2}, \ldots, w_{N}$. ------ Output ------ For each test case, output the answer in a single line: "YES" if Chef will be satisfied after his workout and "NO" if not (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ W ≤ 2\cdot 10^{5}$ $1 ≤ w_{i} ≤ 10^{5}$ $1 ≤ W_{r} ≤ 2\cdot 10^{4}$ ------ Subtasks ------ Subtask #1 (30 points): $w_{i} = 1$ for all valid $i$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 3 2 5 10 2 2 7 100 50 100 10 10 10 10 10 90 6 100 40 10 10 10 10 10 10 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: Since the weight of the rod is at least the required weight to be lifted, Chef will feel satisfied after the workout. Test case 2: The configuration having maximum weight is: $10$ $10$ $|$Rod Center$|$ $10$ $10$ So the maximum total weight Chef can lift is $50 + 4 \cdot 10 = 90$ which is less than the required amount to get satisfied. Test case 3: The configuration having maximum weight is: $10$ $10$ $10$ $|$Rod Center$|$ $10$ $10$ $10$ So the maximum total weight Chef can lift is $40 + 6\cdot 10 = 100$ which is equal to the required amount to get satisfied.
for i in range(int(input())): n, w, wr = map(int, input().split()) k = list(map(int, input().split())) d = {} c = wr for h in k: if h in d: d[h] += 1 else: d[h] = 1 for t in d: if d[t] % 2 == 0: c += d[t] * t if c >= w: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
In the gym, Chef prefers to lift at least $W$ grams during a bench press and if that's impossible, Chef considers his workout to be incomplete and feels bad. The rod weighs $W_{r}$ grams and there are $N$ other weights lying on the floor that weigh $w_{1}, w_{2}, ..., w_{N}$ grams. To maintain balance and to ensure that there is no unnecessary load due to torque, it's important that the weights added to the left side are symmetric to the weights added to the right side. It is not required to use all of the weights. It is also not required to use any weights at all, if Chef feels satisfied lifting only the rod. For example: - $1$ $2$ $2$ $1$ $|$Rod Center$|$ $1$ $1$ $1$ $3$ is a wrong configuration, but - $1$ $2$ $3$ $1$ $|$Rod Center$|$ $1$ $3$ $2$ $1$ is a right configuration. Find whether Chef will be able to collect the required weights to feel satisfied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains two lines of input. The first line contains three space-separated integers $N, W, W_{r}$. The second line contains $N$ space-separated integers $w_{1}, w_{2}, \ldots, w_{N}$. ------ Output ------ For each test case, output the answer in a single line: "YES" if Chef will be satisfied after his workout and "NO" if not (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ W ≤ 2\cdot 10^{5}$ $1 ≤ w_{i} ≤ 10^{5}$ $1 ≤ W_{r} ≤ 2\cdot 10^{4}$ ------ Subtasks ------ Subtask #1 (30 points): $w_{i} = 1$ for all valid $i$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 3 2 5 10 2 2 7 100 50 100 10 10 10 10 10 90 6 100 40 10 10 10 10 10 10 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: Since the weight of the rod is at least the required weight to be lifted, Chef will feel satisfied after the workout. Test case 2: The configuration having maximum weight is: $10$ $10$ $|$Rod Center$|$ $10$ $10$ So the maximum total weight Chef can lift is $50 + 4 \cdot 10 = 90$ which is less than the required amount to get satisfied. Test case 3: The configuration having maximum weight is: $10$ $10$ $10$ $|$Rod Center$|$ $10$ $10$ $10$ So the maximum total weight Chef can lift is $40 + 6\cdot 10 = 100$ which is equal to the required amount to get satisfied.
class Solution: def func(self, arr, n, w, wr): if w - wr <= 0: return "YES" remain_weight = w - wr wd = dict() for wi in arr: if wi not in wd.keys(): wd[wi] = 1 else: wd[wi] += 1 for weight, count in wd.items(): if count % 2 == 0: remain_weight -= weight * count if remain_weight <= 0: return "YES" else: return "NO" t = int(input()) for _ in range(t): n, w, wr = map(int, input().strip().split()) arr = list(map(int, input().strip().split())) ob = Solution() ans = ob.func(arr, n, w, wr) print(ans)
CLASS_DEF FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
In the gym, Chef prefers to lift at least $W$ grams during a bench press and if that's impossible, Chef considers his workout to be incomplete and feels bad. The rod weighs $W_{r}$ grams and there are $N$ other weights lying on the floor that weigh $w_{1}, w_{2}, ..., w_{N}$ grams. To maintain balance and to ensure that there is no unnecessary load due to torque, it's important that the weights added to the left side are symmetric to the weights added to the right side. It is not required to use all of the weights. It is also not required to use any weights at all, if Chef feels satisfied lifting only the rod. For example: - $1$ $2$ $2$ $1$ $|$Rod Center$|$ $1$ $1$ $1$ $3$ is a wrong configuration, but - $1$ $2$ $3$ $1$ $|$Rod Center$|$ $1$ $3$ $2$ $1$ is a right configuration. Find whether Chef will be able to collect the required weights to feel satisfied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains two lines of input. The first line contains three space-separated integers $N, W, W_{r}$. The second line contains $N$ space-separated integers $w_{1}, w_{2}, \ldots, w_{N}$. ------ Output ------ For each test case, output the answer in a single line: "YES" if Chef will be satisfied after his workout and "NO" if not (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ W ≤ 2\cdot 10^{5}$ $1 ≤ w_{i} ≤ 10^{5}$ $1 ≤ W_{r} ≤ 2\cdot 10^{4}$ ------ Subtasks ------ Subtask #1 (30 points): $w_{i} = 1$ for all valid $i$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 3 2 5 10 2 2 7 100 50 100 10 10 10 10 10 90 6 100 40 10 10 10 10 10 10 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: Since the weight of the rod is at least the required weight to be lifted, Chef will feel satisfied after the workout. Test case 2: The configuration having maximum weight is: $10$ $10$ $|$Rod Center$|$ $10$ $10$ So the maximum total weight Chef can lift is $50 + 4 \cdot 10 = 90$ which is less than the required amount to get satisfied. Test case 3: The configuration having maximum weight is: $10$ $10$ $10$ $|$Rod Center$|$ $10$ $10$ $10$ So the maximum total weight Chef can lift is $40 + 6\cdot 10 = 100$ which is equal to the required amount to get satisfied.
try: for _ in range(int(input())): n, w, r = map(int, input().split()) li = list(map(int, input().split())) li.sort() c = 1 result = 0 for i in range(n - 1): if li[i] == li[i + 1]: c += 1 else: result = result + 2 * (c // 2) * li[i] c = 1 result = result + 2 * (c // 2) * li[n - 1] + r if result >= w: print("YES") else: print("NO") except: pass
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
In the gym, Chef prefers to lift at least $W$ grams during a bench press and if that's impossible, Chef considers his workout to be incomplete and feels bad. The rod weighs $W_{r}$ grams and there are $N$ other weights lying on the floor that weigh $w_{1}, w_{2}, ..., w_{N}$ grams. To maintain balance and to ensure that there is no unnecessary load due to torque, it's important that the weights added to the left side are symmetric to the weights added to the right side. It is not required to use all of the weights. It is also not required to use any weights at all, if Chef feels satisfied lifting only the rod. For example: - $1$ $2$ $2$ $1$ $|$Rod Center$|$ $1$ $1$ $1$ $3$ is a wrong configuration, but - $1$ $2$ $3$ $1$ $|$Rod Center$|$ $1$ $3$ $2$ $1$ is a right configuration. Find whether Chef will be able to collect the required weights to feel satisfied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains two lines of input. The first line contains three space-separated integers $N, W, W_{r}$. The second line contains $N$ space-separated integers $w_{1}, w_{2}, \ldots, w_{N}$. ------ Output ------ For each test case, output the answer in a single line: "YES" if Chef will be satisfied after his workout and "NO" if not (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ W ≤ 2\cdot 10^{5}$ $1 ≤ w_{i} ≤ 10^{5}$ $1 ≤ W_{r} ≤ 2\cdot 10^{4}$ ------ Subtasks ------ Subtask #1 (30 points): $w_{i} = 1$ for all valid $i$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 3 2 5 10 2 2 7 100 50 100 10 10 10 10 10 90 6 100 40 10 10 10 10 10 10 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: Since the weight of the rod is at least the required weight to be lifted, Chef will feel satisfied after the workout. Test case 2: The configuration having maximum weight is: $10$ $10$ $|$Rod Center$|$ $10$ $10$ So the maximum total weight Chef can lift is $50 + 4 \cdot 10 = 90$ which is less than the required amount to get satisfied. Test case 3: The configuration having maximum weight is: $10$ $10$ $10$ $|$Rod Center$|$ $10$ $10$ $10$ So the maximum total weight Chef can lift is $40 + 6\cdot 10 = 100$ which is equal to the required amount to get satisfied.
def Test(n, w, wr, arr): if wr >= w: return "YES" else: arr.sort() data = {} for i in range(n): if arr[i] in data: data[arr[i]] += 1 else: data[arr[i]] = 1 ans = 0 for k, v in data.items(): if v == 1: continue elif v % 2 != 0: while True: if v % 2 == 0: break v -= 1 ans += v * int(k) else: ans += v * int(k) if ans + wr >= w: return "YES" return "NO" for _ in range(int(input())): n, w, wr = map(int, input().split()) arr = list(map(int, input().split())) print(Test(n, w, wr, arr))
FUNC_DEF IF VAR VAR RETURN STRING EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER WHILE NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
In the gym, Chef prefers to lift at least $W$ grams during a bench press and if that's impossible, Chef considers his workout to be incomplete and feels bad. The rod weighs $W_{r}$ grams and there are $N$ other weights lying on the floor that weigh $w_{1}, w_{2}, ..., w_{N}$ grams. To maintain balance and to ensure that there is no unnecessary load due to torque, it's important that the weights added to the left side are symmetric to the weights added to the right side. It is not required to use all of the weights. It is also not required to use any weights at all, if Chef feels satisfied lifting only the rod. For example: - $1$ $2$ $2$ $1$ $|$Rod Center$|$ $1$ $1$ $1$ $3$ is a wrong configuration, but - $1$ $2$ $3$ $1$ $|$Rod Center$|$ $1$ $3$ $2$ $1$ is a right configuration. Find whether Chef will be able to collect the required weights to feel satisfied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains two lines of input. The first line contains three space-separated integers $N, W, W_{r}$. The second line contains $N$ space-separated integers $w_{1}, w_{2}, \ldots, w_{N}$. ------ Output ------ For each test case, output the answer in a single line: "YES" if Chef will be satisfied after his workout and "NO" if not (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ W ≤ 2\cdot 10^{5}$ $1 ≤ w_{i} ≤ 10^{5}$ $1 ≤ W_{r} ≤ 2\cdot 10^{4}$ ------ Subtasks ------ Subtask #1 (30 points): $w_{i} = 1$ for all valid $i$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 3 2 5 10 2 2 7 100 50 100 10 10 10 10 10 90 6 100 40 10 10 10 10 10 10 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: Since the weight of the rod is at least the required weight to be lifted, Chef will feel satisfied after the workout. Test case 2: The configuration having maximum weight is: $10$ $10$ $|$Rod Center$|$ $10$ $10$ So the maximum total weight Chef can lift is $50 + 4 \cdot 10 = 90$ which is less than the required amount to get satisfied. Test case 3: The configuration having maximum weight is: $10$ $10$ $10$ $|$Rod Center$|$ $10$ $10$ $10$ So the maximum total weight Chef can lift is $40 + 6\cdot 10 = 100$ which is equal to the required amount to get satisfied.
T = int(input()) for _ in range(T): n, w1, w2 = map(int, input().split()) L = list(map(int, input().split())) if w2 >= w1: print("YES") else: S = set(L) M = list(S) d = {} for i in range(len(M)): d[M[i]] = 0 L.sort() for i in range(n): d[L[i]] += 1 w = w2 i = 0 while w < w1 and i < len(M): a = M[i] if d[a] % 2 == 0: w += a * d[a] i += 1 if w >= w1: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
In the gym, Chef prefers to lift at least $W$ grams during a bench press and if that's impossible, Chef considers his workout to be incomplete and feels bad. The rod weighs $W_{r}$ grams and there are $N$ other weights lying on the floor that weigh $w_{1}, w_{2}, ..., w_{N}$ grams. To maintain balance and to ensure that there is no unnecessary load due to torque, it's important that the weights added to the left side are symmetric to the weights added to the right side. It is not required to use all of the weights. It is also not required to use any weights at all, if Chef feels satisfied lifting only the rod. For example: - $1$ $2$ $2$ $1$ $|$Rod Center$|$ $1$ $1$ $1$ $3$ is a wrong configuration, but - $1$ $2$ $3$ $1$ $|$Rod Center$|$ $1$ $3$ $2$ $1$ is a right configuration. Find whether Chef will be able to collect the required weights to feel satisfied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains two lines of input. The first line contains three space-separated integers $N, W, W_{r}$. The second line contains $N$ space-separated integers $w_{1}, w_{2}, \ldots, w_{N}$. ------ Output ------ For each test case, output the answer in a single line: "YES" if Chef will be satisfied after his workout and "NO" if not (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ W ≤ 2\cdot 10^{5}$ $1 ≤ w_{i} ≤ 10^{5}$ $1 ≤ W_{r} ≤ 2\cdot 10^{4}$ ------ Subtasks ------ Subtask #1 (30 points): $w_{i} = 1$ for all valid $i$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 3 2 5 10 2 2 7 100 50 100 10 10 10 10 10 90 6 100 40 10 10 10 10 10 10 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: Since the weight of the rod is at least the required weight to be lifted, Chef will feel satisfied after the workout. Test case 2: The configuration having maximum weight is: $10$ $10$ $|$Rod Center$|$ $10$ $10$ So the maximum total weight Chef can lift is $50 + 4 \cdot 10 = 90$ which is less than the required amount to get satisfied. Test case 3: The configuration having maximum weight is: $10$ $10$ $10$ $|$Rod Center$|$ $10$ $10$ $10$ So the maximum total weight Chef can lift is $40 + 6\cdot 10 = 100$ which is equal to the required amount to get satisfied.
for _ in range(int(input())): useless, Wgoal, Wrod = list(map(int, input().split())) weights = list(map(int, input().split())) Wgoal -= Wrod Num_dups = {} if Wgoal <= 0: print("YES") else: for i in range(len(weights)): try: Num_dups[weights[i]] += 1 if Num_dups[weights[i]] == 2: Num_dups[weights[i]] = 0 Wgoal -= 2 * weights[i] if Wgoal <= 0: print("YES") break except: Num_dups[weights[i]] = 1 if i == len(weights) - 1: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR ASSIGN VAR DICT IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING
In the gym, Chef prefers to lift at least $W$ grams during a bench press and if that's impossible, Chef considers his workout to be incomplete and feels bad. The rod weighs $W_{r}$ grams and there are $N$ other weights lying on the floor that weigh $w_{1}, w_{2}, ..., w_{N}$ grams. To maintain balance and to ensure that there is no unnecessary load due to torque, it's important that the weights added to the left side are symmetric to the weights added to the right side. It is not required to use all of the weights. It is also not required to use any weights at all, if Chef feels satisfied lifting only the rod. For example: - $1$ $2$ $2$ $1$ $|$Rod Center$|$ $1$ $1$ $1$ $3$ is a wrong configuration, but - $1$ $2$ $3$ $1$ $|$Rod Center$|$ $1$ $3$ $2$ $1$ is a right configuration. Find whether Chef will be able to collect the required weights to feel satisfied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains two lines of input. The first line contains three space-separated integers $N, W, W_{r}$. The second line contains $N$ space-separated integers $w_{1}, w_{2}, \ldots, w_{N}$. ------ Output ------ For each test case, output the answer in a single line: "YES" if Chef will be satisfied after his workout and "NO" if not (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ W ≤ 2\cdot 10^{5}$ $1 ≤ w_{i} ≤ 10^{5}$ $1 ≤ W_{r} ≤ 2\cdot 10^{4}$ ------ Subtasks ------ Subtask #1 (30 points): $w_{i} = 1$ for all valid $i$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 3 2 5 10 2 2 7 100 50 100 10 10 10 10 10 90 6 100 40 10 10 10 10 10 10 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: Since the weight of the rod is at least the required weight to be lifted, Chef will feel satisfied after the workout. Test case 2: The configuration having maximum weight is: $10$ $10$ $|$Rod Center$|$ $10$ $10$ So the maximum total weight Chef can lift is $50 + 4 \cdot 10 = 90$ which is less than the required amount to get satisfied. Test case 3: The configuration having maximum weight is: $10$ $10$ $10$ $|$Rod Center$|$ $10$ $10$ $10$ So the maximum total weight Chef can lift is $40 + 6\cdot 10 = 100$ which is equal to the required amount to get satisfied.
for _ in range(int(input())): l = str(input()).split() n = int(l[0]) w = int(l[1]) wr = int(l[2]) a = str(input()).split() a = list(map(int, a)) freq = {} for i in a: if i in freq: freq[i] += 1 else: freq[i] = 1 if wr >= w: print("YES") else: max_wt = wr for i in freq.keys(): if freq[i] % 2 == 0: max_wt += i * freq[i] else: max_wt += i * (freq[i] - 1) if max_wt >= w: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
In the gym, Chef prefers to lift at least $W$ grams during a bench press and if that's impossible, Chef considers his workout to be incomplete and feels bad. The rod weighs $W_{r}$ grams and there are $N$ other weights lying on the floor that weigh $w_{1}, w_{2}, ..., w_{N}$ grams. To maintain balance and to ensure that there is no unnecessary load due to torque, it's important that the weights added to the left side are symmetric to the weights added to the right side. It is not required to use all of the weights. It is also not required to use any weights at all, if Chef feels satisfied lifting only the rod. For example: - $1$ $2$ $2$ $1$ $|$Rod Center$|$ $1$ $1$ $1$ $3$ is a wrong configuration, but - $1$ $2$ $3$ $1$ $|$Rod Center$|$ $1$ $3$ $2$ $1$ is a right configuration. Find whether Chef will be able to collect the required weights to feel satisfied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains two lines of input. The first line contains three space-separated integers $N, W, W_{r}$. The second line contains $N$ space-separated integers $w_{1}, w_{2}, \ldots, w_{N}$. ------ Output ------ For each test case, output the answer in a single line: "YES" if Chef will be satisfied after his workout and "NO" if not (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ W ≤ 2\cdot 10^{5}$ $1 ≤ w_{i} ≤ 10^{5}$ $1 ≤ W_{r} ≤ 2\cdot 10^{4}$ ------ Subtasks ------ Subtask #1 (30 points): $w_{i} = 1$ for all valid $i$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 3 2 5 10 2 2 7 100 50 100 10 10 10 10 10 90 6 100 40 10 10 10 10 10 10 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: Since the weight of the rod is at least the required weight to be lifted, Chef will feel satisfied after the workout. Test case 2: The configuration having maximum weight is: $10$ $10$ $|$Rod Center$|$ $10$ $10$ So the maximum total weight Chef can lift is $50 + 4 \cdot 10 = 90$ which is less than the required amount to get satisfied. Test case 3: The configuration having maximum weight is: $10$ $10$ $10$ $|$Rod Center$|$ $10$ $10$ $10$ So the maximum total weight Chef can lift is $40 + 6\cdot 10 = 100$ which is equal to the required amount to get satisfied.
for _ in range(int(input())): n, w, wrod = map(int, input().split()) weights = [int(x) for x in input().split()] if wrod >= w: print("YES") else: wmap = {} wsum = wrod ans = "NO" for i in weights: wmap[i] = 1 + wmap.get(i, 0) for k, v in wmap.items(): possibleToAdd = wmap[k] // 2 wsum += 2 * k * possibleToAdd if wsum >= w: ans = "YES" break print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR DICT ASSIGN VAR VAR ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
In the gym, Chef prefers to lift at least $W$ grams during a bench press and if that's impossible, Chef considers his workout to be incomplete and feels bad. The rod weighs $W_{r}$ grams and there are $N$ other weights lying on the floor that weigh $w_{1}, w_{2}, ..., w_{N}$ grams. To maintain balance and to ensure that there is no unnecessary load due to torque, it's important that the weights added to the left side are symmetric to the weights added to the right side. It is not required to use all of the weights. It is also not required to use any weights at all, if Chef feels satisfied lifting only the rod. For example: - $1$ $2$ $2$ $1$ $|$Rod Center$|$ $1$ $1$ $1$ $3$ is a wrong configuration, but - $1$ $2$ $3$ $1$ $|$Rod Center$|$ $1$ $3$ $2$ $1$ is a right configuration. Find whether Chef will be able to collect the required weights to feel satisfied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains two lines of input. The first line contains three space-separated integers $N, W, W_{r}$. The second line contains $N$ space-separated integers $w_{1}, w_{2}, \ldots, w_{N}$. ------ Output ------ For each test case, output the answer in a single line: "YES" if Chef will be satisfied after his workout and "NO" if not (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ W ≤ 2\cdot 10^{5}$ $1 ≤ w_{i} ≤ 10^{5}$ $1 ≤ W_{r} ≤ 2\cdot 10^{4}$ ------ Subtasks ------ Subtask #1 (30 points): $w_{i} = 1$ for all valid $i$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 3 2 5 10 2 2 7 100 50 100 10 10 10 10 10 90 6 100 40 10 10 10 10 10 10 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: Since the weight of the rod is at least the required weight to be lifted, Chef will feel satisfied after the workout. Test case 2: The configuration having maximum weight is: $10$ $10$ $|$Rod Center$|$ $10$ $10$ So the maximum total weight Chef can lift is $50 + 4 \cdot 10 = 90$ which is less than the required amount to get satisfied. Test case 3: The configuration having maximum weight is: $10$ $10$ $10$ $|$Rod Center$|$ $10$ $10$ $10$ So the maximum total weight Chef can lift is $40 + 6\cdot 10 = 100$ which is equal to the required amount to get satisfied.
import sys def _input(): return sys.stdin.readline().strip() def main(): for _ in range(int(_input())): n, w, wr = map(int, _input().split()) lst = list(map(int, _input().split())) freq = {} for i in lst: if i not in freq: freq[i] = 1 else: freq[i] += 1 for lol in freq: if freq[lol] & 1: wr += (freq[lol] - 1) * lol else: wr += freq[lol] * lol if wr >= w: print("YES") else: print("NO") main()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
In the gym, Chef prefers to lift at least $W$ grams during a bench press and if that's impossible, Chef considers his workout to be incomplete and feels bad. The rod weighs $W_{r}$ grams and there are $N$ other weights lying on the floor that weigh $w_{1}, w_{2}, ..., w_{N}$ grams. To maintain balance and to ensure that there is no unnecessary load due to torque, it's important that the weights added to the left side are symmetric to the weights added to the right side. It is not required to use all of the weights. It is also not required to use any weights at all, if Chef feels satisfied lifting only the rod. For example: - $1$ $2$ $2$ $1$ $|$Rod Center$|$ $1$ $1$ $1$ $3$ is a wrong configuration, but - $1$ $2$ $3$ $1$ $|$Rod Center$|$ $1$ $3$ $2$ $1$ is a right configuration. Find whether Chef will be able to collect the required weights to feel satisfied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains two lines of input. The first line contains three space-separated integers $N, W, W_{r}$. The second line contains $N$ space-separated integers $w_{1}, w_{2}, \ldots, w_{N}$. ------ Output ------ For each test case, output the answer in a single line: "YES" if Chef will be satisfied after his workout and "NO" if not (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ W ≤ 2\cdot 10^{5}$ $1 ≤ w_{i} ≤ 10^{5}$ $1 ≤ W_{r} ≤ 2\cdot 10^{4}$ ------ Subtasks ------ Subtask #1 (30 points): $w_{i} = 1$ for all valid $i$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 3 2 5 10 2 2 7 100 50 100 10 10 10 10 10 90 6 100 40 10 10 10 10 10 10 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: Since the weight of the rod is at least the required weight to be lifted, Chef will feel satisfied after the workout. Test case 2: The configuration having maximum weight is: $10$ $10$ $|$Rod Center$|$ $10$ $10$ So the maximum total weight Chef can lift is $50 + 4 \cdot 10 = 90$ which is less than the required amount to get satisfied. Test case 3: The configuration having maximum weight is: $10$ $10$ $10$ $|$Rod Center$|$ $10$ $10$ $10$ So the maximum total weight Chef can lift is $40 + 6\cdot 10 = 100$ which is equal to the required amount to get satisfied.
for _ in range(0, int(input())): x = 0 chef_lst = [int(i) for i in input().split()] bench_weight = [int(i) for i in input().split()] if chef_lst[1] <= chef_lst[2]: print("YES") continue bench_weight = sorted(bench_weight) new_weight = [] for i in range(0, len(bench_weight) - 1, 2): if bench_weight[i] == bench_weight[i + 1]: new_weight.append(bench_weight[i]) score = int(chef_lst[2]) for i in range(0, len(new_weight)): score += 2 * int(new_weight[i]) if score >= int(chef_lst[1]): print("YES") x = 1 break if x == 1: continue print("NO")
FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING
In the gym, Chef prefers to lift at least $W$ grams during a bench press and if that's impossible, Chef considers his workout to be incomplete and feels bad. The rod weighs $W_{r}$ grams and there are $N$ other weights lying on the floor that weigh $w_{1}, w_{2}, ..., w_{N}$ grams. To maintain balance and to ensure that there is no unnecessary load due to torque, it's important that the weights added to the left side are symmetric to the weights added to the right side. It is not required to use all of the weights. It is also not required to use any weights at all, if Chef feels satisfied lifting only the rod. For example: - $1$ $2$ $2$ $1$ $|$Rod Center$|$ $1$ $1$ $1$ $3$ is a wrong configuration, but - $1$ $2$ $3$ $1$ $|$Rod Center$|$ $1$ $3$ $2$ $1$ is a right configuration. Find whether Chef will be able to collect the required weights to feel satisfied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains two lines of input. The first line contains three space-separated integers $N, W, W_{r}$. The second line contains $N$ space-separated integers $w_{1}, w_{2}, \ldots, w_{N}$. ------ Output ------ For each test case, output the answer in a single line: "YES" if Chef will be satisfied after his workout and "NO" if not (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ W ≤ 2\cdot 10^{5}$ $1 ≤ w_{i} ≤ 10^{5}$ $1 ≤ W_{r} ≤ 2\cdot 10^{4}$ ------ Subtasks ------ Subtask #1 (30 points): $w_{i} = 1$ for all valid $i$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 3 2 5 10 2 2 7 100 50 100 10 10 10 10 10 90 6 100 40 10 10 10 10 10 10 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: Since the weight of the rod is at least the required weight to be lifted, Chef will feel satisfied after the workout. Test case 2: The configuration having maximum weight is: $10$ $10$ $|$Rod Center$|$ $10$ $10$ So the maximum total weight Chef can lift is $50 + 4 \cdot 10 = 90$ which is less than the required amount to get satisfied. Test case 3: The configuration having maximum weight is: $10$ $10$ $10$ $|$Rod Center$|$ $10$ $10$ $10$ So the maximum total weight Chef can lift is $40 + 6\cdot 10 = 100$ which is equal to the required amount to get satisfied.
for test in range(int(input())): n, w, wr = map(int, input().split()) arr = [int(char) for char in input().split()] weight_required = w - wr dict = {} if weight_required <= 0: print("YES") else: for i in range(n): if arr[i] not in dict: dict[arr[i]] = 1 else: dict[arr[i]] += 1 weight = 0 for word in dict: weight += 2 * (word * (dict[word] // 2)) wr += weight if wr >= w: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR DICT IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
In the gym, Chef prefers to lift at least $W$ grams during a bench press and if that's impossible, Chef considers his workout to be incomplete and feels bad. The rod weighs $W_{r}$ grams and there are $N$ other weights lying on the floor that weigh $w_{1}, w_{2}, ..., w_{N}$ grams. To maintain balance and to ensure that there is no unnecessary load due to torque, it's important that the weights added to the left side are symmetric to the weights added to the right side. It is not required to use all of the weights. It is also not required to use any weights at all, if Chef feels satisfied lifting only the rod. For example: - $1$ $2$ $2$ $1$ $|$Rod Center$|$ $1$ $1$ $1$ $3$ is a wrong configuration, but - $1$ $2$ $3$ $1$ $|$Rod Center$|$ $1$ $3$ $2$ $1$ is a right configuration. Find whether Chef will be able to collect the required weights to feel satisfied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains two lines of input. The first line contains three space-separated integers $N, W, W_{r}$. The second line contains $N$ space-separated integers $w_{1}, w_{2}, \ldots, w_{N}$. ------ Output ------ For each test case, output the answer in a single line: "YES" if Chef will be satisfied after his workout and "NO" if not (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ W ≤ 2\cdot 10^{5}$ $1 ≤ w_{i} ≤ 10^{5}$ $1 ≤ W_{r} ≤ 2\cdot 10^{4}$ ------ Subtasks ------ Subtask #1 (30 points): $w_{i} = 1$ for all valid $i$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 3 2 5 10 2 2 7 100 50 100 10 10 10 10 10 90 6 100 40 10 10 10 10 10 10 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: Since the weight of the rod is at least the required weight to be lifted, Chef will feel satisfied after the workout. Test case 2: The configuration having maximum weight is: $10$ $10$ $|$Rod Center$|$ $10$ $10$ So the maximum total weight Chef can lift is $50 + 4 \cdot 10 = 90$ which is less than the required amount to get satisfied. Test case 3: The configuration having maximum weight is: $10$ $10$ $10$ $|$Rod Center$|$ $10$ $10$ $10$ So the maximum total weight Chef can lift is $40 + 6\cdot 10 = 100$ which is equal to the required amount to get satisfied.
t = int(input()) for _ in range(t): n, w, wr = list(map(int, input().split())) wn = list(map(int, input().split())) if wr >= w: print("YES") else: rw = w - wr wn.sort() i = n - 1 while i > 0 and rw > 0: if wn[i] == wn[i - 1]: rw -= wn[i] * 2 i -= 2 else: i -= 1 if rw <= 0: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
In the gym, Chef prefers to lift at least $W$ grams during a bench press and if that's impossible, Chef considers his workout to be incomplete and feels bad. The rod weighs $W_{r}$ grams and there are $N$ other weights lying on the floor that weigh $w_{1}, w_{2}, ..., w_{N}$ grams. To maintain balance and to ensure that there is no unnecessary load due to torque, it's important that the weights added to the left side are symmetric to the weights added to the right side. It is not required to use all of the weights. It is also not required to use any weights at all, if Chef feels satisfied lifting only the rod. For example: - $1$ $2$ $2$ $1$ $|$Rod Center$|$ $1$ $1$ $1$ $3$ is a wrong configuration, but - $1$ $2$ $3$ $1$ $|$Rod Center$|$ $1$ $3$ $2$ $1$ is a right configuration. Find whether Chef will be able to collect the required weights to feel satisfied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains two lines of input. The first line contains three space-separated integers $N, W, W_{r}$. The second line contains $N$ space-separated integers $w_{1}, w_{2}, \ldots, w_{N}$. ------ Output ------ For each test case, output the answer in a single line: "YES" if Chef will be satisfied after his workout and "NO" if not (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ W ≤ 2\cdot 10^{5}$ $1 ≤ w_{i} ≤ 10^{5}$ $1 ≤ W_{r} ≤ 2\cdot 10^{4}$ ------ Subtasks ------ Subtask #1 (30 points): $w_{i} = 1$ for all valid $i$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 3 2 5 10 2 2 7 100 50 100 10 10 10 10 10 90 6 100 40 10 10 10 10 10 10 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: Since the weight of the rod is at least the required weight to be lifted, Chef will feel satisfied after the workout. Test case 2: The configuration having maximum weight is: $10$ $10$ $|$Rod Center$|$ $10$ $10$ So the maximum total weight Chef can lift is $50 + 4 \cdot 10 = 90$ which is less than the required amount to get satisfied. Test case 3: The configuration having maximum weight is: $10$ $10$ $10$ $|$Rod Center$|$ $10$ $10$ $10$ So the maximum total weight Chef can lift is $40 + 6\cdot 10 = 100$ which is equal to the required amount to get satisfied.
for x in range(int(input())): n, w, wr = map(int, input().split()) li = list(map(int, input().split())) flag = 0 if wr >= w: flag = 1 else: w -= wr count = 0 li = sorted(li) for i in range(len(li)): count += 1 if i == len(li) - 1 or li[i] != li[i + 1]: if count % 2 != 0: count -= 1 w -= count * li[i] count = 0 if w <= 0: flag = 1 break if flag == 0: print("NO") else: print("YES")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
In the gym, Chef prefers to lift at least $W$ grams during a bench press and if that's impossible, Chef considers his workout to be incomplete and feels bad. The rod weighs $W_{r}$ grams and there are $N$ other weights lying on the floor that weigh $w_{1}, w_{2}, ..., w_{N}$ grams. To maintain balance and to ensure that there is no unnecessary load due to torque, it's important that the weights added to the left side are symmetric to the weights added to the right side. It is not required to use all of the weights. It is also not required to use any weights at all, if Chef feels satisfied lifting only the rod. For example: - $1$ $2$ $2$ $1$ $|$Rod Center$|$ $1$ $1$ $1$ $3$ is a wrong configuration, but - $1$ $2$ $3$ $1$ $|$Rod Center$|$ $1$ $3$ $2$ $1$ is a right configuration. Find whether Chef will be able to collect the required weights to feel satisfied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains two lines of input. The first line contains three space-separated integers $N, W, W_{r}$. The second line contains $N$ space-separated integers $w_{1}, w_{2}, \ldots, w_{N}$. ------ Output ------ For each test case, output the answer in a single line: "YES" if Chef will be satisfied after his workout and "NO" if not (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ W ≤ 2\cdot 10^{5}$ $1 ≤ w_{i} ≤ 10^{5}$ $1 ≤ W_{r} ≤ 2\cdot 10^{4}$ ------ Subtasks ------ Subtask #1 (30 points): $w_{i} = 1$ for all valid $i$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 3 2 5 10 2 2 7 100 50 100 10 10 10 10 10 90 6 100 40 10 10 10 10 10 10 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: Since the weight of the rod is at least the required weight to be lifted, Chef will feel satisfied after the workout. Test case 2: The configuration having maximum weight is: $10$ $10$ $|$Rod Center$|$ $10$ $10$ So the maximum total weight Chef can lift is $50 + 4 \cdot 10 = 90$ which is less than the required amount to get satisfied. Test case 3: The configuration having maximum weight is: $10$ $10$ $10$ $|$Rod Center$|$ $10$ $10$ $10$ So the maximum total weight Chef can lift is $40 + 6\cdot 10 = 100$ which is equal to the required amount to get satisfied.
for z in range(int(input())): n, w, wr = [int(x) for x in input().split()] a = [int(x) for x in input().split()] if wr >= w: print("YES") continue w -= wr d = {} for x in a: if d.get(x) is None: d[x] = 0 d[x] += 1 b = 0 for x in d.keys(): if d[x] % 2 == 0: b += x * d[x] else: b += x * (d[x] - 1) if b >= w: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR DICT FOR VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
In the gym, Chef prefers to lift at least $W$ grams during a bench press and if that's impossible, Chef considers his workout to be incomplete and feels bad. The rod weighs $W_{r}$ grams and there are $N$ other weights lying on the floor that weigh $w_{1}, w_{2}, ..., w_{N}$ grams. To maintain balance and to ensure that there is no unnecessary load due to torque, it's important that the weights added to the left side are symmetric to the weights added to the right side. It is not required to use all of the weights. It is also not required to use any weights at all, if Chef feels satisfied lifting only the rod. For example: - $1$ $2$ $2$ $1$ $|$Rod Center$|$ $1$ $1$ $1$ $3$ is a wrong configuration, but - $1$ $2$ $3$ $1$ $|$Rod Center$|$ $1$ $3$ $2$ $1$ is a right configuration. Find whether Chef will be able to collect the required weights to feel satisfied. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains two lines of input. The first line contains three space-separated integers $N, W, W_{r}$. The second line contains $N$ space-separated integers $w_{1}, w_{2}, \ldots, w_{N}$. ------ Output ------ For each test case, output the answer in a single line: "YES" if Chef will be satisfied after his workout and "NO" if not (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 10^{5}$ $1 ≤ W ≤ 2\cdot 10^{5}$ $1 ≤ w_{i} ≤ 10^{5}$ $1 ≤ W_{r} ≤ 2\cdot 10^{4}$ ------ Subtasks ------ Subtask #1 (30 points): $w_{i} = 1$ for all valid $i$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 3 2 5 10 2 2 7 100 50 100 10 10 10 10 10 90 6 100 40 10 10 10 10 10 10 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: Since the weight of the rod is at least the required weight to be lifted, Chef will feel satisfied after the workout. Test case 2: The configuration having maximum weight is: $10$ $10$ $|$Rod Center$|$ $10$ $10$ So the maximum total weight Chef can lift is $50 + 4 \cdot 10 = 90$ which is less than the required amount to get satisfied. Test case 3: The configuration having maximum weight is: $10$ $10$ $10$ $|$Rod Center$|$ $10$ $10$ $10$ So the maximum total weight Chef can lift is $40 + 6\cdot 10 = 100$ which is equal to the required amount to get satisfied.
for _ in range(int(input())): n, w, wr = [int(i) for i in input().split()] d = {} for i in input().split(): i = int(i) if i in d.keys(): d[i] = d[i] + 1 else: d[i] = 1 for i in d.keys(): if d[i] % 2: d[i] -= 1 l = list(d.items()) for ind, i in enumerate(l): if i[1] == 0: l.pop(ind) sum = 0 for i in l: sum += i[0] * i[1] if w > wr + sum: print("NO") else: print("YES")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Multiset —is a set of numbers in which there can be equal elements, and the order of the numbers does not matter. Two multisets are equal when each value occurs the same number of times. For example, the multisets $\{2,2,4\}$ and $\{2,4,2\}$ are equal, but the multisets $\{1,2,2\}$ and $\{1,1,2\}$ — are not. You are given two multisets $a$ and $b$, each consisting of $n$ integers. In a single operation, any element of the $b$ multiset can be doubled or halved (rounded down). In other words, you have one of the following operations available for an element $x$ of the $b$ multiset: replace $x$ with $x \cdot 2$, or replace $x$ with $\lfloor \frac{x}{2} \rfloor$ (round down). Note that you cannot change the elements of the $a$ multiset. See if you can make the multiset $b$ become equal to the multiset $a$ in an arbitrary number of operations (maybe $0$). For example, if $n = 4$, $a = \{4, 24, 5, 2\}$, $b = \{4, 1, 6, 11\}$, then the answer is yes. We can proceed as follows: Replace $1$ with $1 \cdot 2 = 2$. We get $b = \{4, 2, 6, 11\}$. Replace $11$ with $\lfloor \frac{11}{2} \rfloor = 5$. We get $b = \{4, 2, 6, 5\}$. Replace $6$ with $6 \cdot 2 = 12$. We get $b = \{4, 2, 12, 5\}$. Replace $12$ with $12 \cdot 2 = 24$. We get $b = \{4, 2, 24, 5\}$. Got equal multisets $a = \{4, 24, 5, 2\}$ and $b = \{4, 2, 24, 5\}$. -----Input----- The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) —the number of test cases. Each test case consists of three lines. The first line of the test case contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) —the number of elements in the multisets $a$ and $b$. The second line gives $n$ integers: $a_1, a_2, \dots, a_n$ ($1 \le a_1 \le a_2 \le \dots \le a_n \le 10^9$) —the elements of the multiset $a$. Note that the elements may be equal. The third line contains $n$ integers: $b_1, b_2, \dots, b_n$ ($1 \le b_1 \le b_2 \le \dots \le b_n \le 10^9$) — elements of the multiset $b$. Note that the elements may be equal. It is guaranteed that the sum of $n$ values over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print on a separate line: YES if you can make the multiset $b$ become equal to $a$, NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive answer). -----Examples----- Input 5 4 2 4 5 24 1 4 6 11 3 1 4 17 4 5 31 5 4 7 10 13 14 2 14 14 26 42 5 2 2 4 4 4 28 46 62 71 98 6 1 2 10 16 64 80 20 43 60 74 85 99 Output YES NO YES YES YES -----Note----- The first example is explained in the statement. In the second example, it is impossible to get the value $31$ from the numbers of the multiset $b$ by available operations. In the third example, we can proceed as follows: Replace $2$ with $2 \cdot 2 = 4$. We get $b = \{4, 14, 14, 26, 42\}$. Replace $14$ with $\lfloor \frac{14}{2} \rfloor = 7$. We get $b = \{4, 7, 14, 26, 42\}$. Replace $26$ with $\lfloor \frac{26}{2} \rfloor = 13$. We get $b = \{4, 7, 14, 13, 42\}$. Replace $42$ with $\lfloor \frac{42}{2} \rfloor = 21$. We get $b = \{4, 7, 14, 13, 21\}$. Replace $21$ with $\lfloor \frac{21}{2} \rfloor = 10$. We get $b = \{4, 7, 14, 13, 10\}$. Got equal multisets $a = \{4, 7, 10, 13, 14\}$ and $b = \{4, 7, 14, 13, 10\}$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) for i in range(n): while a[i] % 2 == 0: a[i] //= 2 x = {} for i in a: if i in x: x[i] += 1 else: x[i] = 1 ans = 1 for i in b: while not i in x or x[i] == 0: i //= 2 if i == 0: ans = 0 break if i == 0: break x[i] -= 1 if ans == 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Multiset —is a set of numbers in which there can be equal elements, and the order of the numbers does not matter. Two multisets are equal when each value occurs the same number of times. For example, the multisets $\{2,2,4\}$ and $\{2,4,2\}$ are equal, but the multisets $\{1,2,2\}$ and $\{1,1,2\}$ — are not. You are given two multisets $a$ and $b$, each consisting of $n$ integers. In a single operation, any element of the $b$ multiset can be doubled or halved (rounded down). In other words, you have one of the following operations available for an element $x$ of the $b$ multiset: replace $x$ with $x \cdot 2$, or replace $x$ with $\lfloor \frac{x}{2} \rfloor$ (round down). Note that you cannot change the elements of the $a$ multiset. See if you can make the multiset $b$ become equal to the multiset $a$ in an arbitrary number of operations (maybe $0$). For example, if $n = 4$, $a = \{4, 24, 5, 2\}$, $b = \{4, 1, 6, 11\}$, then the answer is yes. We can proceed as follows: Replace $1$ with $1 \cdot 2 = 2$. We get $b = \{4, 2, 6, 11\}$. Replace $11$ with $\lfloor \frac{11}{2} \rfloor = 5$. We get $b = \{4, 2, 6, 5\}$. Replace $6$ with $6 \cdot 2 = 12$. We get $b = \{4, 2, 12, 5\}$. Replace $12$ with $12 \cdot 2 = 24$. We get $b = \{4, 2, 24, 5\}$. Got equal multisets $a = \{4, 24, 5, 2\}$ and $b = \{4, 2, 24, 5\}$. -----Input----- The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) —the number of test cases. Each test case consists of three lines. The first line of the test case contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) —the number of elements in the multisets $a$ and $b$. The second line gives $n$ integers: $a_1, a_2, \dots, a_n$ ($1 \le a_1 \le a_2 \le \dots \le a_n \le 10^9$) —the elements of the multiset $a$. Note that the elements may be equal. The third line contains $n$ integers: $b_1, b_2, \dots, b_n$ ($1 \le b_1 \le b_2 \le \dots \le b_n \le 10^9$) — elements of the multiset $b$. Note that the elements may be equal. It is guaranteed that the sum of $n$ values over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print on a separate line: YES if you can make the multiset $b$ become equal to $a$, NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive answer). -----Examples----- Input 5 4 2 4 5 24 1 4 6 11 3 1 4 17 4 5 31 5 4 7 10 13 14 2 14 14 26 42 5 2 2 4 4 4 28 46 62 71 98 6 1 2 10 16 64 80 20 43 60 74 85 99 Output YES NO YES YES YES -----Note----- The first example is explained in the statement. In the second example, it is impossible to get the value $31$ from the numbers of the multiset $b$ by available operations. In the third example, we can proceed as follows: Replace $2$ with $2 \cdot 2 = 4$. We get $b = \{4, 14, 14, 26, 42\}$. Replace $14$ with $\lfloor \frac{14}{2} \rfloor = 7$. We get $b = \{4, 7, 14, 26, 42\}$. Replace $26$ with $\lfloor \frac{26}{2} \rfloor = 13$. We get $b = \{4, 7, 14, 13, 42\}$. Replace $42$ with $\lfloor \frac{42}{2} \rfloor = 21$. We get $b = \{4, 7, 14, 13, 21\}$. Replace $21$ with $\lfloor \frac{21}{2} \rfloor = 10$. We get $b = \{4, 7, 14, 13, 10\}$. Got equal multisets $a = \{4, 7, 10, 13, 14\}$ and $b = \{4, 7, 14, 13, 10\}$.
for _ in range(int(input())): n = int(input()) nums = {} ans = "YES" for i in list(map(int, input().split())): while not i % 2: i //= 2 nums[i] = nums[i] + 1 if i in nums else 1 for i in list(map(int, input().split())): while i > 0: if i in nums and nums[i] > 0: nums[i] -= 1 break i //= 2 else: ans = "NO" break print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
Multiset —is a set of numbers in which there can be equal elements, and the order of the numbers does not matter. Two multisets are equal when each value occurs the same number of times. For example, the multisets $\{2,2,4\}$ and $\{2,4,2\}$ are equal, but the multisets $\{1,2,2\}$ and $\{1,1,2\}$ — are not. You are given two multisets $a$ and $b$, each consisting of $n$ integers. In a single operation, any element of the $b$ multiset can be doubled or halved (rounded down). In other words, you have one of the following operations available for an element $x$ of the $b$ multiset: replace $x$ with $x \cdot 2$, or replace $x$ with $\lfloor \frac{x}{2} \rfloor$ (round down). Note that you cannot change the elements of the $a$ multiset. See if you can make the multiset $b$ become equal to the multiset $a$ in an arbitrary number of operations (maybe $0$). For example, if $n = 4$, $a = \{4, 24, 5, 2\}$, $b = \{4, 1, 6, 11\}$, then the answer is yes. We can proceed as follows: Replace $1$ with $1 \cdot 2 = 2$. We get $b = \{4, 2, 6, 11\}$. Replace $11$ with $\lfloor \frac{11}{2} \rfloor = 5$. We get $b = \{4, 2, 6, 5\}$. Replace $6$ with $6 \cdot 2 = 12$. We get $b = \{4, 2, 12, 5\}$. Replace $12$ with $12 \cdot 2 = 24$. We get $b = \{4, 2, 24, 5\}$. Got equal multisets $a = \{4, 24, 5, 2\}$ and $b = \{4, 2, 24, 5\}$. -----Input----- The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) —the number of test cases. Each test case consists of three lines. The first line of the test case contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) —the number of elements in the multisets $a$ and $b$. The second line gives $n$ integers: $a_1, a_2, \dots, a_n$ ($1 \le a_1 \le a_2 \le \dots \le a_n \le 10^9$) —the elements of the multiset $a$. Note that the elements may be equal. The third line contains $n$ integers: $b_1, b_2, \dots, b_n$ ($1 \le b_1 \le b_2 \le \dots \le b_n \le 10^9$) — elements of the multiset $b$. Note that the elements may be equal. It is guaranteed that the sum of $n$ values over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print on a separate line: YES if you can make the multiset $b$ become equal to $a$, NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive answer). -----Examples----- Input 5 4 2 4 5 24 1 4 6 11 3 1 4 17 4 5 31 5 4 7 10 13 14 2 14 14 26 42 5 2 2 4 4 4 28 46 62 71 98 6 1 2 10 16 64 80 20 43 60 74 85 99 Output YES NO YES YES YES -----Note----- The first example is explained in the statement. In the second example, it is impossible to get the value $31$ from the numbers of the multiset $b$ by available operations. In the third example, we can proceed as follows: Replace $2$ with $2 \cdot 2 = 4$. We get $b = \{4, 14, 14, 26, 42\}$. Replace $14$ with $\lfloor \frac{14}{2} \rfloor = 7$. We get $b = \{4, 7, 14, 26, 42\}$. Replace $26$ with $\lfloor \frac{26}{2} \rfloor = 13$. We get $b = \{4, 7, 14, 13, 42\}$. Replace $42$ with $\lfloor \frac{42}{2} \rfloor = 21$. We get $b = \{4, 7, 14, 13, 21\}$. Replace $21$ with $\lfloor \frac{21}{2} \rfloor = 10$. We get $b = \{4, 7, 14, 13, 10\}$. Got equal multisets $a = \{4, 7, 10, 13, 14\}$ and $b = \{4, 7, 14, 13, 10\}$.
for _ in range(int(input())): n = int(input()) a = [int(x) for x in input().split(" ")] b = [int(x) for x in input().split(" ")] mp = {} for i in a: t = i while t % 2 == 0: t = t // 2 if t not in mp.keys(): mp[t] = 0 mp[t] += 1 for j in b: t = j flag = 0 while t: if t in mp.keys() and mp[t] > 0: mp[t] -= 1 break t = t // 2 if sum(mp.values()) == 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 VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Multiset —is a set of numbers in which there can be equal elements, and the order of the numbers does not matter. Two multisets are equal when each value occurs the same number of times. For example, the multisets $\{2,2,4\}$ and $\{2,4,2\}$ are equal, but the multisets $\{1,2,2\}$ and $\{1,1,2\}$ — are not. You are given two multisets $a$ and $b$, each consisting of $n$ integers. In a single operation, any element of the $b$ multiset can be doubled or halved (rounded down). In other words, you have one of the following operations available for an element $x$ of the $b$ multiset: replace $x$ with $x \cdot 2$, or replace $x$ with $\lfloor \frac{x}{2} \rfloor$ (round down). Note that you cannot change the elements of the $a$ multiset. See if you can make the multiset $b$ become equal to the multiset $a$ in an arbitrary number of operations (maybe $0$). For example, if $n = 4$, $a = \{4, 24, 5, 2\}$, $b = \{4, 1, 6, 11\}$, then the answer is yes. We can proceed as follows: Replace $1$ with $1 \cdot 2 = 2$. We get $b = \{4, 2, 6, 11\}$. Replace $11$ with $\lfloor \frac{11}{2} \rfloor = 5$. We get $b = \{4, 2, 6, 5\}$. Replace $6$ with $6 \cdot 2 = 12$. We get $b = \{4, 2, 12, 5\}$. Replace $12$ with $12 \cdot 2 = 24$. We get $b = \{4, 2, 24, 5\}$. Got equal multisets $a = \{4, 24, 5, 2\}$ and $b = \{4, 2, 24, 5\}$. -----Input----- The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) —the number of test cases. Each test case consists of three lines. The first line of the test case contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) —the number of elements in the multisets $a$ and $b$. The second line gives $n$ integers: $a_1, a_2, \dots, a_n$ ($1 \le a_1 \le a_2 \le \dots \le a_n \le 10^9$) —the elements of the multiset $a$. Note that the elements may be equal. The third line contains $n$ integers: $b_1, b_2, \dots, b_n$ ($1 \le b_1 \le b_2 \le \dots \le b_n \le 10^9$) — elements of the multiset $b$. Note that the elements may be equal. It is guaranteed that the sum of $n$ values over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print on a separate line: YES if you can make the multiset $b$ become equal to $a$, NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive answer). -----Examples----- Input 5 4 2 4 5 24 1 4 6 11 3 1 4 17 4 5 31 5 4 7 10 13 14 2 14 14 26 42 5 2 2 4 4 4 28 46 62 71 98 6 1 2 10 16 64 80 20 43 60 74 85 99 Output YES NO YES YES YES -----Note----- The first example is explained in the statement. In the second example, it is impossible to get the value $31$ from the numbers of the multiset $b$ by available operations. In the third example, we can proceed as follows: Replace $2$ with $2 \cdot 2 = 4$. We get $b = \{4, 14, 14, 26, 42\}$. Replace $14$ with $\lfloor \frac{14}{2} \rfloor = 7$. We get $b = \{4, 7, 14, 26, 42\}$. Replace $26$ with $\lfloor \frac{26}{2} \rfloor = 13$. We get $b = \{4, 7, 14, 13, 42\}$. Replace $42$ with $\lfloor \frac{42}{2} \rfloor = 21$. We get $b = \{4, 7, 14, 13, 21\}$. Replace $21$ with $\lfloor \frac{21}{2} \rfloor = 10$. We get $b = \{4, 7, 14, 13, 10\}$. Got equal multisets $a = \{4, 7, 10, 13, 14\}$ and $b = \{4, 7, 14, 13, 10\}$.
import sys def solve(): inp = sys.stdin.readline n = int(inp()) d = dict() i = 0 for x in map(int, inp().split()): while x % 2 == 0: x //= 2 d[x] = d.get(x, 0) + 1 i += 1 for x in map(int, inp().split()): while x > 0: if d.get(x, 0) > 0: d[x] -= 1 break x //= 2 else: print("NO") return print("YES") def main(): for i in range(int(sys.stdin.readline())): solve() main()
IMPORT FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
s = input() stack = [] for i in s: if not stack: stack.append(i) continue f = 0 while stack: if stack[-1] == i: f = 1 stack.pop() else: break if not f: stack.append(i) if stack: print("No") else: print("Yes")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR IF VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
initial = list(input()) stringStatus = [] for i in range(len(initial)): stringStatus.append(initial[i]) if len(stringStatus) >= 2: if stringStatus[-1] == stringStatus[-2]: stringStatus.pop() stringStatus.pop() if len(stringStatus) == 0: print("yes") else: print("no")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
a = input() if len(a) % 2 == 1: print("No") else: l = [] for i in a: if l == []: l.append(i) elif i != l[-1]: l.append(i) else: l.pop() if l == []: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR IF VAR LIST EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR LIST EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
wire = input() stack = ["null"] if len(wire) % 2 != 0: print("No") exit() for i in wire: if stack[-1] == i: stack.pop() else: stack.append(i) if len(stack) > 1: print("No") else: print("Yes")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
sequence = input() stack = [] for each in sequence: if len(stack) == 0: stack.append(each) elif stack[len(stack) - 1] == each: stack.pop() else: stack.append(each) if len(stack) == 0: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
s = input().strip() stack = [] for k in range(len(s)): if not stack or s[k] != stack[-1]: stack.append(s[k]) elif s[k] == stack[-1]: stack.pop() if not stack: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
class Stack: def __init__(self): self._data = [] def __len__(self): return len(self._data) def is_empty(self): return len(self._data) == 0 def push(self, a): self._data.append(a) def top(self): if self.is_empty(): raise Empty("Stack is empty") return self._data[-1] def pop(self): if self.is_empty(): raise Empty("Stack is empty") return self._data.pop() l = str(input()) s = Stack() for i in l: if s.is_empty(): s.push(i) elif s.top() == i: s.pop() else: s.push(i) if s.is_empty(): print("Yes") else: print("No")
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR FUNC_CALL VAR STRING RETURN VAR NUMBER FUNC_DEF IF FUNC_CALL VAR FUNC_CALL VAR STRING RETURN FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
s = input() st = [s[0]] for sign in s[1:]: if len(st) != 0 and sign == st[-1]: st.pop() else: st.append(sign) if len(st) == 0: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
i = input() s = [] for k in i: l = len(s) if l == 0: s += k elif k == s[l - 1]: del s[l - 1] else: s += k l = len(s) if l == 0: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
class Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def push(self, item): self.items.append(item) def pop(self): self.items.pop() def peek(self): return self.items[len(self.items) - 1] def size(self): return len(self.items) def printStack(self): return self.items s = input() stack = Stack() i = 0 while i < len(s): if stack.isEmpty() == True: stack.push(s[i]) i += 1 continue while i < len(s) and stack.isEmpty() == False and stack.peek() == s[i]: stack.pop() i += 1 if i < len(s): stack.push(s[i]) i += 1 ans = "".join(stack.printStack()) if len(ans) == 0: print("Yes") else: print("No")
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF RETURN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
st = input() st = list(st) st1 = [] cc = 0 for i in range(len(st)): if len(st1) > 0: if st1[-1] == st[i]: st1.pop() else: st1.append(st[i]) else: st1.append(st[i]) if len(st1) == 0: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
input = input() stack = [] for wire in input: if len(stack) == 0 or wire != stack[-1]: stack.append(wire) else: stack.pop() print("YES" if len(stack) == 0 else "NO")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER STRING STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
W, st = list(input()), [] for i in W: if len(st) and st[-1] == i: st.pop() else: st.append(i) print("No") if len(st) else print("Yes")
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
from sys import stdin, stdout def fin(): return stdin.readline().strip() def fout(x): stdout.write(str(x) + "\n") stk = [] for i in fin(): if stk and stk[-1] == i: stk.pop() else: stk.append(i) if not stk: fout("Yes") else: print("No")
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
a = list(input()) for i in range(len(a)): if i % 2 == 0: if a[i] == "+": a[i] = "B" elif a[i] == "-": a[i] = "A" elif i % 2 != 0: if a[i] == "+": a[i] = "A" elif a[i] == "-": a[i] = "B" if a.count("A") == a.count("B"): print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR STRING IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
class ArrayStack: def __init__(self): self._data = [] def __len__(self): return len(self._data) def is_empty(self): return len(self._data) == 0 def push(self, e): self._data.append(e) def top(self): return self._data[-1] def pop(self): return self._data.pop() S = ArrayStack() A = input() A = list(A) l = len(A) S.push(A[0]) for i in range(1, l): if S.is_empty(): S.push(A[i]) else: a = S.pop() if A[i] == a: pass else: S.push(a) S.push(A[i]) if S.is_empty(): print("Yes") else: print("No")
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
withFile = 0 if withFile == 1: fin = open("input.txt", "r") fout = open("output.txt", "w") def getl(): if withFile == 0: return input() else: return fin.readline() def printl(s): if withFile == 0: print(s) else: fout.write(str(s)) def get_arr(): x = getl().split(" ") if x[-1] == "": x = x[:-1] return list(map(int, x)) s = getl() ans = [] for c in s: if len(ans) != 0 and ans[-1] == c: ans.pop() else: ans.append(c) if len(ans) == 0: printl("YES") else: printl("NO") if withFile == 1: fin.close() fout.close()
ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER STRING ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
s = input() l = len(s) Acount = Bcount = 0 for i in range(l): if i + 1 & 1: if s[i] == "-": Bcount += 1 else: Acount += 1 elif s[i] == "-": Acount += 1 else: Bcount += 1 print("Yes" if Acount == Bcount else "No")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
s = input() ans = [0] * len(s) n = 0 for x in s: if n and ans[n - 1] == x: n -= 1 else: ans[n] = x n += 1 print("No" if n else "Yes")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
s = input() st = list() for i in s: if st and st[-1] == i: st.pop() else: st.append(i) if not st: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
s, l = input(), [] for i in s: if l == []: l.append(i) elif i == l[-1]: del l[-1] else: l.append(i) if l == []: print("Yes") else: print("No")
ASSIGN VAR VAR FUNC_CALL VAR LIST FOR VAR VAR IF VAR LIST EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR LIST EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
import sys x = input() if len(x) == 1: print("No") sys.exit(0) else: st = [] a = 1 st.append(x[0]) for i in range(1, len(x)): e = x[i] if a == 0: st.append(e) a += 1 elif e == st[-1]: st.pop() if len(st) == 0: a = 0 else: a = 1 else: st.append(e) a = 1 if a % 2 == 0: print("Yes") else: print("No")
IMPORT ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
s = input() a, b = [], [] x, y = 0, 0 while x < len(s): while x < len(s) - 1 and s[x] == s[x + 1]: x = x + 2 if x < len(s): a.append(s[x]) x = x + 1 while y < len(a): if len(b) != 0 and b[-1] != a[y] or len(b) == 0: b.append(a[y]) y = y + 1 else: b.pop() y = y + 1 if len(b) == 0: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
lista = input() contp = contm = 0 ult = "x" for i in lista: if i == "-": contm += 1 if i == ult: contm -= 2 ult = "+" else: ult = "-" if i == "+": contp += 1 if i == ult: contp -= 2 ult = "-" else: ult = "+" if contp == 0 and contm == 0: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING IF VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
l = input() l = list(l) p = [] for i in l: if len(p) == 0: p.append(i) elif p[-1] == i: p.pop() else: p.append(i) if len(p) == 0: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
l = list(input()) if len(l) % 2 != 0: print("No") else: stack = [] for i in l: if stack: if stack[-1] == i: stack.pop() else: stack.append(i) else: stack.append(i) if stack: print("No") else: print("Yes")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR IF VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
from sys import stdin lines, line_index = stdin.readlines(), -1 def get_line(): global lines, line_index line_index += 1 return lines[line_index] def main(): string = get_line().strip() stack = [] for c in string: if not stack: stack.append(c) elif stack[-1] == c: stack.pop() else: stack.append(c) if not stack: print("Yes") else: print("No") main()
ASSIGN VAR VAR FUNC_CALL VAR NUMBER FUNC_DEF VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
s = input() if len(s) % 2: print("No") exit(0) if s[::2].count("+") - s[1::2].count("+") == 0: print("Yes") exit(0) print("No")
ASSIGN VAR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF BIN_OP FUNC_CALL VAR NUMBER STRING FUNC_CALL VAR NUMBER NUMBER STRING NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
s = input() st = [] for i in s: if st: if st[-1] == i: st.pop() else: st.append(i) else: st.append(i) if st: print("No") else: print("Yes")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
string = list(input()) stack = list() match = [("+", "+"), ("-", "-")] for i in string: if stack and (stack[-1], i) in match: stack.pop() else: stack.append(i) if stack == []: print("yes") else: print("no")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING FOR VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR LIST EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): [Image] Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. -----Input----- The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. -----Output----- Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. -----Examples----- Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No -----Note----- The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: [Image] In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: [Image] In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: [Image]
s = input().strip() x = [] for i in range(len(s)): if not x or x[-1] != s[i]: x.append(s[i]) else: x.pop() if len(x) == 0: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING