description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$. Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$. Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple. You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$. Note that, according to the definition, subarrays of length $1$ and $2$ are good. -----Input----- The first line contains one integer $t$ ($1 \le t \le 5000$) β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the length of array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of good subarrays of array $a$. -----Examples----- Input 3 4 2 4 1 3 5 6 9 1 9 6 2 13 37 Output 10 12 3 -----Note----- In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and: $d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$; $d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$; $d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$; In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$: $d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$; $d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$; $d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$; So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$.
import sys input = sys.stdin.readline def main(): t = int(input()) INF = 1001001001 for _ in range(t): n = int(input()) A = list(map(int, input().split())) ans = n + n - 1 for i in range(n - 2): if check(A[i], i, A[i + 1], i + 1, A[i + 2], i + 2): ans += 1 for i in range(n - 3): a = check(A[i], i, A[i + 1], i + 1, A[i + 2], i + 2) b = check(A[i], i, A[i + 1], i + 1, A[i + 3], i + 3) c = check(A[i], i, A[i + 2], i + 2, A[i + 3], i + 3) d = check(A[i + 1], i + 1, A[i + 2], i + 2, A[i + 3], i + 3) if a and b and c and d: ans += 1 print(ans) def check(ai, i, aj, j, ak, k): if abs(ak - ai) + abs(k - i) == abs(aj - ai) + abs(j - i) + abs(ak - aj) + abs( k - j ): return False else: return True main()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR 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 BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$. Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$. Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple. You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$. Note that, according to the definition, subarrays of length $1$ and $2$ are good. -----Input----- The first line contains one integer $t$ ($1 \le t \le 5000$) β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the length of array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of good subarrays of array $a$. -----Examples----- Input 3 4 2 4 1 3 5 6 9 1 9 6 2 13 37 Output 10 12 3 -----Note----- In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and: $d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$; $d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$; $d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$; In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$: $d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$; $d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$; $d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$; So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$.
for _ in range(int(input())): n = int(input()) zoz = [int(x) for x in input().split()] subs1 = subs2 = 0 if len(zoz) >= 3: for i in range(2, n): if ( zoz[i - 2] <= zoz[i - 1] and zoz[i - 1] <= zoz[i] or zoz[i - 2] >= zoz[i - 1] and zoz[i - 1] >= zoz[i] ): subs1 += 1 if len(zoz) >= 4: for i in range(3, n): if ( ( zoz[i - 2] <= zoz[i - 1] and zoz[i - 1] <= zoz[i] or zoz[i - 2] >= zoz[i - 1] and zoz[i - 1] >= zoz[i] ) or ( zoz[i - 3] <= zoz[i - 1] and zoz[i - 1] <= zoz[i] or zoz[i - 3] >= zoz[i - 1] and zoz[i - 1] >= zoz[i] ) or ( zoz[i - 3] <= zoz[i - 2] and zoz[i - 2] <= zoz[i] or zoz[i - 3] >= zoz[i - 2] and zoz[i - 2] >= zoz[i] ) or ( zoz[i - 3] <= zoz[i - 2] and zoz[i - 2] <= zoz[i - 1] or zoz[i - 3] >= zoz[i - 2] and zoz[i - 2] >= zoz[i - 1] ) ): subs2 += 1 if len(zoz) == 2: print(3) elif len(zoz) == 3: print(6 - subs1) elif len(zoz) == 1: print(1) elif len(zoz) == 0: print(0) else: print(n + n - 1 + n - 2 - subs1 + n - 3 - subs2)
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 ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$. Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$. Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple. You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$. Note that, according to the definition, subarrays of length $1$ and $2$ are good. -----Input----- The first line contains one integer $t$ ($1 \le t \le 5000$) β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the length of array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of good subarrays of array $a$. -----Examples----- Input 3 4 2 4 1 3 5 6 9 1 9 6 2 13 37 Output 10 12 3 -----Note----- In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and: $d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$; $d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$; $d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$; In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$: $d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$; $d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$; $d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$; So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$.
def calc(a, i): if a[i] == a[i + 1]: return i + 2 if a[i + 1] == a[i + 2]: return i + 2 if a[i] == a[i + 2]: return i + 3 if a[i] < a[i + 1] < a[i + 2]: return i + 2 if a[i] > a[i + 1] > a[i + 2]: return i + 2 if a[i] > a[i + 2] > a[i + 1]: return i + 3 if a[i] < a[i + 2] < a[i + 1]: return i + 3 if i + 3 >= len(a): return i + 3 if a[i + 1] < a[i] < a[i + 2]: if a[i + 1] < a[i + 3] < a[i + 2]: return i + 4 return i + 3 if a[i + 1] > a[i] > a[i + 2]: if a[i + 2] < a[i + 3] < a[i + 1]: return i + 4 return i + 3 def solve(a): d = [len(a)] * len(a) for i in range(len(a) - 2): d[i] = calc(a, i) answer = 0 for i in range(len(a)): answer += d[i] - i return answer for case in range(int(input())): input() a = list(map(int, input().split())) print(solve(a))
FUNC_DEF IF VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR 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
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
import sys input = sys.stdin.readline n, m = map(int, input().split()) x = [0] * (n + 1) c = 0 for i in range(m): u, v = map(int, input().split()) mn = min(u, v) if x[mn] == 0: c += 1 x[mn] += 1 q = int(input()) for i in range(q): a, *b = map(int, input().split()) if a == 3: print(n - c) elif a == 1: d = min(b) if x[d] == 0: c += 1 x[d] += 1 else: d = min(b) x[d] -= 1 if x[d] == 0: c -= 1
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER 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 VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
arr = input().split() n = int(arr[0]) m = int(arr[1]) list_frd = [] for i in range(n): list_frd.append(0) nobles = n for i in range(m): arr = input().split(" ") s = int(arr[0]) b = int(arr[1]) if s > b: s, b = b, s s = s - 1 if list_frd[s] == 0: nobles = nobles - 1 list_frd[s] = list_frd[s] + 1 q = int(input()) for i in range(q): arr = input().split() if len(arr) == 1: print(nobles) elif int(arr[0]) == 1: s = int(arr[1]) b = int(arr[2]) if s > b: s, b = b, s s = s - 1 if list_frd[s] == 0: nobles = nobles - 1 list_frd[s] = list_frd[s] + 1 else: s = int(arr[1]) b = int(arr[2]) if s > b: s, b = b, s s = s - 1 list_frd[s] = list_frd[s] - 1 if list_frd[s] == 0: nobles = nobles + 1
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
import sys input = sys.stdin.readline n, q = list(map(int, input().split())) arr = [0] * n c = 0 for _ in range(q): a, b = map(int, input().split()) if a < b: ind = a - 1 else: ind = b - 1 arr[ind] += 1 if arr[ind] == 1: c += 1 for i in range(int(input())): y = list(map(int, input().split())) if len(y) == 1: print(n - c) continue v, a, b = y if a < b: ind = a - 1 else: ind = b - 1 if v == 1: arr[ind] += 1 if arr[ind] == 1: c += 1 elif v == 2: arr[ind] -= 1 if arr[ind] == 0: c -= 1
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
import sys input = sys.stdin.readline def inp(): return int(input()) def inlt(): return list(map(int, input().split())) def insr(): s = input() return list(s[: len(s) - 1]) def invr(): return map(int, input().split()) n = inlt() a = [] m = n[1] n = n[0] ans = n for i in range(0, n + 1): a.append(0) for i in range(0, m): x = inlt() y = x[1] x = x[0] a[min(x, y)] -= 1 for i in range(1, n + 1): if a[i] < 0: ans -= 1 q = inp() for i in range(0, q): q1 = inlt() if q1[0] == 1: j = q1[1] k = q1[2] if a[min(j, k)] == 0: ans -= 1 a[min(j, k)] -= 1 elif q1[0] == 2: j = q1[1] k = q1[2] a[min(j, k)] += 1 if a[min(j, k)] == 0: ans += 1 else: print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
import sys n, m = map(int, sys.stdin.readline().split()) graph = [0] * n ans = 0 for f in range(m): u, v = map(int, sys.stdin.readline().split()) graph[min(u, v) - 1] += 1 if graph[min(u, v) - 1] == 1: ans += 1 q = int(sys.stdin.readline()) for qr in range(q): arr = list(map(int, sys.stdin.readline().split())) if arr[0] == 1: graph[min(arr[1], arr[2]) - 1] += 1 if graph[min(arr[1], arr[2]) - 1] == 1: ans += 1 elif arr[0] == 2: graph[min(arr[1], arr[2]) - 1] -= 1 if graph[min(arr[1], arr[2]) - 1] == 0: ans -= 1 else: print(n - ans)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
import sys input = sys.stdin.readline nn, m = map(int, input().split()) n = nn b = [(0) for i in range(n + 1)] p = 0 for i in range(m): c, d = map(int, input().split()) f = min(c, d) if b[f] == 0: p += 1 b[f] += 1 q = int(input()) for i in range(q): a, *bb = map(int, input().split()) if a == 1: d = min(bb) if b[d] == 0: p += 1 b[d] += 1 elif a == 2: d = min(bb) b[d] -= 1 if b[d] == 0: p -= 1 else: print(n - p)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER 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 VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
n, m = map(int, input().split()) seen = [0] * (n + 1) ans = n for i in range(m): u, v = map(int, input().split()) u, v = sorted([u, v]) seen[u] += 1 if seen[u] == 1: ans -= 1 q = int(input()) for i in range(q): row = input() if len(row) == 1: print(ans) else: k, u, v = map(int, row.split()) u, v = sorted([u, v]) if k == 1: seen[u] += 1 if seen[u] == 1: ans -= 1 else: seen[u] -= 1 if seen[u] == 0: ans += 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR LIST VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR LIST VAR VAR IF VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
n, m = map(int, input().split()) a = [(0) for i in range(n)] for i in range(m): ar, b = map(int, input().split()) x = min(ar, b) a[x] += 1 s = 0 for i in a: if i == 0: s += 1 at = a g = 1 st = int(s) for q in range(int(input())): t = input() if t[0] == "3": print(st) else: qn, ar, b = map(int, t.split()) if qn == 1: x = min(ar, b) at[x] += 1 if at[x] == 1: st -= 1 else: x = min(ar, b) at[x] -= 1 if at[x] == 0: st += 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
n, m = list(map(int, input().split())) w = [(0) for i in range(n)] for i in range(m): u, v = map(int, input().split()) if u > v: w[v - 1] += 1 else: w[u - 1] += 1 ans = w.count(0) for _ in range(int(input())): s = list(map(int, input().split())) if s[0] == 3: print(ans) elif s[0] == 1: if s[1] > s[2]: w[s[2] - 1] += 1 if w[s[2] - 1] == 1: ans -= 1 else: w[s[1] - 1] += 1 if w[s[1] - 1] == 1: ans -= 1 elif s[1] > s[2]: w[s[2] - 1] -= 1 if w[s[2] - 1] == 0: ans += 1 else: w[s[1] - 1] -= 1 if w[s[1] - 1] == 0: ans += 1
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
n, m = map(int, input().split()) stronger = [0] * (n + 1) total = 0 for i in range(m): u, v = map(int, input().split()) u, v = min(u, v), max(u, v) stronger[u] += 1 if stronger[u] == 1: total += 1 query = int(input()) for q in range(query): X = input().split() i, u, v = int(X[0]), None, None if len(X) > 1: u = int(X[1]) v = int(X[2]) if i == 3: print(n - total) elif i == 1: u, v = min(u, v), max(u, v) stronger[u] += 1 if stronger[u] == 1: total += 1 else: u, v = min(u, v), max(u, v) stronger[u] -= 1 if stronger[u] == 0: total -= 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER NONE NONE IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
import sys input = sys.stdin.readline n, m = map(int, input().split()) count = [0] * (n + 1) preans = n for i in range(m): arr = list(map(int, input().split())) mini = min(arr) if count[mini] == 0: count[mini] += 1 preans -= 1 else: count[mini] += 1 ans = preans temp = count for i in range(int(input())): q = list(map(int, input().split())) if len(q) == 1: print(ans) elif q[0] == 1: mini = min(q[1:]) if temp[mini] == 0: ans -= 1 temp[mini] += 1 else: temp[mini] += 1 else: mini = min(q[1:]) if temp[mini] == 1: ans += 1 temp[mini] -= 1 else: temp[mini] -= 1
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
from sys import stdin N, M = map(int, stdin.readline().split()) counts = [0] * (N + 1) ans = 0 for _ in range(M): u, v = map(int, stdin.readline().split()) if u > v: u, v = v, u counts[u] += 1 if counts[u] == 1: ans += 1 Q = int(stdin.readline()) for _ in range(Q): args = list(map(int, stdin.readline().split())) if args[0] == 1: u, v = args[1], args[2] if u > v: u, v = v, u counts[u] += 1 if counts[u] == 1: ans += 1 elif args[0] == 2: u, v = args[1], args[2] if u > v: u, v = v, u counts[u] -= 1 if counts[u] == 0: ans -= 1 else: print(N - ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
import sys input = sys.stdin.readline cnt = [0] * 200050 ans = 0 n, m = map(int, input().split()) for i in range(1, m + 1): u, v = map(int, input().split()) if u > v: u, v = v, u cnt[u] += 1 if cnt[u] == 1: ans += 1 q = int(input().strip()) while q: query = list(map(int, input().split())) if query[0] == 1: u, v = query[1], query[2] if u > v: u, v = v, u cnt[u] += 1 if cnt[u] == 1: ans += 1 elif query[0] == 2: u, v = query[1], query[2] if u > v: u, v = v, u cnt[u] -= 1 if cnt[u] == 0: ans -= 1 else: print(n - ans) q -= 1
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
import sys def input(): s = sys.stdin.readline() return s n, m = map(int, input().strip().split()) nodes = [set() for i in range(1 + n)] empty_sets = n pairs = [] for i in range(m): f, t = sorted(list(map(int, input().strip().split()))) if len(nodes[f]) == 0: empty_sets -= 1 nodes[f].add(t) q = int(input()) qs = [] for i in range(q): nums = list(map(int, input().strip().split())) qs.append(nums) for nums in qs: if nums[0] == 3: print(empty_sets) else: f, t = sorted(nums[1:]) if nums[0] == 1: if len(nodes[f]) == 0: empty_sets -= 1 nodes[f].add(t) else: nodes[f].remove(t) if len(nodes[f]) == 0: empty_sets += 1
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
n, m = map(int, input().split()) ar = [(0) for _ in range(n + 1)] ans = n for _ in range(m): u, v = map(int, input().split()) u, v = min(u, v), max(u, v) if ar[u] == 0: ans -= 1 ar[u] += 1 q = int(input()) for _ in range(q): inp = input() if inp[0] == "1": _, u, v = inp.split() u, v = min(int(u), int(v)), max(int(u), int(v)) if ar[u] == 0: ans -= 1 ar[u] += 1 elif inp[0] == "2": _, u, v = inp.split() u, v = min(int(u), int(v)), max(int(u), int(v)) ar[u] -= 1 if ar[u] == 0: ans += 1 else: print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
def ii(): return int(input()) def si(): return input() def mi(): return map(int, input().split()) def msi(): return map(str, input().split()) def li(): return list(mi()) for _ in range(1): n, m = mi() ans = 0 res = [0] * (n + 1) for i in range(m): u, v = mi() u = min(u, v) res[u] += 1 for i in range(1, n + 1): if res[i] > 0: ans += 1 q = ii() for i in range(q): s = input().split() if s[0] == "3": print(n - ans) elif s[0] == "1": u = int(s[1]) v = int(s[2]) u = min(u, v) if res[u] > 0: res[u] += 1 else: ans += 1 res[u] += 1 elif s[0] == "2": u = int(s[1]) v = int(s[2]) u = min(u, v) if res[u] == 1: ans -= 1 res[u] -= 1 else: res[u] -= 1
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
m, n = map(int, input().split()) l = [0] * (m + 1) ans = 0 for i in range(n): u, v = map(int, input().split()) if u > v: u, v = v, u l[u] += 1 if l[u] == 1: ans += 1 q = int(input()) for i in range(q): li = list(map(int, input().split())) if len(li) == 1: print(m - ans) continue if li[0] == 1: if li[1] > li[2]: li[1], li[2] = li[2], li[1] l[li[1]] += 1 if l[li[1]] == 1: ans += 1 elif li[0] == 2: if li[1] > li[2]: li[1], li[2] = li[2], li[1] l[li[1]] -= 1 if l[li[1]] == 0: ans -= 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
import sys input = sys.stdin.readline n, m = map(int, input().split()) E = [0] * n ans = n for i in range(m): u, v = map(int, input().split()) u -= 1 v -= 1 if u > v: u, v = v, u E[u] += 1 if E[u] == 1: ans -= 1 q = int(input()) for i in range(q): tmp = input() if len(tmp) > 3: d, u, v = map(int, tmp.split()) u -= 1 v -= 1 if u > v: u, v = v, u if d == 1: E[u] += 1 if E[u] == 1: ans -= 1 else: E[u] -= 1 if E[u] == 0: ans += 1 else: print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
n, m = map(int, input().split()) g = [] c1 = [] c2 = [] for i in range(n): c1.append(0) c2.append(0) q = [1] * n for i in range(m): a, b = map(lambda z: int(z) - 1, input().split()) a, b = min(a, b), max(a, b) c1[a] += 1 c2[b] += 1 q[a] = 0 cur = sum(q) ans = [] for _ in range(int(input())): inp = input() if inp[0] == "3": ans.append(cur) continue t, u, v = map(lambda z: int(z) - 1, inp.split()) u, v = min(u, v), max(u, v) if t == 0: c1[u] += 1 c2[v] += 1 if q[u] == 1: q[u] = 0 cur -= 1 else: c1[u] -= 1 c2[v] -= 1 if c1[u] == 0 and q[u] == 0: q[u] = 1 cur += 1 print("\n".join(map(str, ans)))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
import sys input = sys.stdin.readline n, m = list(map(int, input().split())) a = [0] * (n + 1) ans = n for i in range(m): u, v = list(map(int, input().split())) u = min(u, v) if a[u] == 0: ans -= 1 a[u] += 1 q = int(input()) for i in range(q): b = list(map(int, input().split())) if b[0] == 1: b[1] = min(b[1], b[2]) if a[b[1]] == 0: ans -= 1 a[b[1]] += 1 elif b[0] == 2: b[1] = min(b[1], b[2]) a[b[1]] -= 1 if a[b[1]] == 0: ans += 1 else: print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
import sys input = sys.stdin.readline n, m = map(int, input().split()) a = [0] * (n + 1) for _ in range(m): x, y = map(int, input().split()) y = min(x, y) a[y] += 1 summ = 0 for i in range(1, n + 1): if a[i] == 0: summ += 1 q = int(input()) for _ in range(q): x = list(map(int, input().split())) if x[0] == 3: print(summ) elif x[0] == 1: v = min(x[1], x[2]) if a[v] == 0: summ -= 1 a[v] += 1 elif x[0] == 2: v = min(x[1], x[2]) a[v] -= 1 if a[v] == 0: summ += 1
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
from sys import stdin input = stdin.readline m, n = map(int, input().split()) dp = [0] * (m + 1) dead = 0 for i in range(n): u, v = map(int, input().split()) if dp[min(u, v)] == 0: dead += 1 dp[min(u, v)] += 1 for q in range(int(input())): command = list(input().split()) if command[0] == "1": if dp[min(int(command[1]), int(command[2]))] == 0: dead += 1 dp[min(int(command[1]), int(command[2]))] += 1 elif command[0] == "2": dp[min(int(command[1]), int(command[2]))] -= 1 if dp[min(int(command[1]), int(command[2]))] == 0: dead -= 1 elif command[0] == "3": print(m - dead)
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
import sys def add(d, obj, counter): if d[obj] == 0: counter += 1 d[obj] += 1 return counter def sub(d, obj, counter): d[obj] -= 1 if d[obj] == 0: counter -= 1 return counter input = sys.stdin.readline n, m = map(int, input().split()) a = {x: (0) for x in range(1, n + 1)} count = 0 for i in range(m): b = [int(x) for x in input().split()] count = add(a, min(b), count) q = int(input()) for i in range(q): c = [int(x) for x in input().split()] if c[0] == 1: count = add(a, min(c[1:]), count) elif c[0] == 2: count = sub(a, min(c[1:]), count) elif c[0] == 3: print(n - count)
IMPORT FUNC_DEF IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
import sys input = sys.stdin.readline n, m = map(int, input().split()) l = [0] * (n + 1) for i in range(m): u, v = sorted(map(int, input().split())) l[u] += 1 c = 0 for i in range(1, n + 1): if l[i] == 0: c += 1 z = [] q = int(input()) for i in range(q): s = input().split() if s[0] == "1": u, v = sorted(map(int, s[1:])) l[u] += 1 if l[u] == 1: c -= 1 if s[0] == "2": u, v = sorted(map(int, s[1:])) l[u] -= 1 if l[u] == 0: c += 1 if s[0] == "3": z.append(str(c)) print("\n".join(z))
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
n, m = map(int, input().split()) g = [0] * n for _ in range(m): u, v = map(int, input().split()) u -= 1 v -= 1 if v > u: g[u] += 1 else: g[v] += 1 q = int(input()) answer = sum([(1) for i in range(n) if g[i] == 0]) allAnswers = [] for _ in range(q): query = list(map(int, input().split())) if query[0] == 1: query[1] -= 1 query[2] -= 1 if query[1] > query[2]: if g[query[2]] == 0: answer -= 1 g[query[2]] += 1 else: if g[query[1]] == 0: answer -= 1 g[query[1]] += 1 elif query[0] == 2: query[1] -= 1 query[2] -= 1 if query[1] > query[2]: if g[query[2]] == 1: answer += 1 g[query[2]] -= 1 else: if g[query[1]] == 1: answer += 1 g[query[1]] -= 1 else: allAnswers.append(answer) for x in allAnswers: print(x)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
n, m = map(int, input().split()) arr = [(0) for i in range(n)] for i in range(m): u, v = map(int, input().split()) if u > v: arr[v - 1] += 1 else: arr[u - 1] += 1 q = int(input()) ans = 0 for i in arr: if i == 0: ans += 1 for i in range(q): temp2 = input() if temp2 != "3": a, u, v = map(int, temp2.split()) if a == 1: if u > v: if arr[v - 1] == 0: ans -= 1 arr[v - 1] += 1 else: if arr[u - 1] == 0: ans -= 1 arr[u - 1] += 1 elif a == 2: if u > v: arr[v - 1] -= 1 if arr[v - 1] == 0: ans += 1 else: arr[u - 1] -= 1 if arr[u - 1] == 0: ans += 1 else: print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR IF VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
n, m = map(int, input().split()) di = [0] * (n + 2) dead = 0 for _ in range(m): a, b = map(int, input().split()) if a > b: a, b = b, a if di[a] == 0: dead += 1 di[a] += 1 q = int(input()) for _ in range(q): st = input() if st == "3": print(n - dead) else: x, a, b = map(int, st.split()) if x == 1: if a > b: a, b = b, a if di[a] == 0: dead += 1 di[a] += 1 else: if a > b: a, b = b, a if di[a] == 1: dead -= 1 di[a] -= 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
import sys input = sys.stdin.readline n, m = map(int, input().split()) friends = [0] * n dead = 0 for _ in range(m): i, j = map(int, input().split()) if friends[min(i, j)] == 0: dead += 1 friends[min(i, j)] += 1 q = int(input()) for _ in range(q): a = input().split() if a[0] == "1": i, j = int(a[1]), int(a[2]) if friends[min(i, j)] == 0: dead += 1 friends[min(i, j)] += 1 if a[0] == "2": i, j = int(a[1]), int(a[2]) if friends[min(i, j)] == 1: dead -= 1 friends[min(i, j)] -= 1 if a[0] == "3": print(n - dead)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
import sys input_file = sys.stdin output_file = sys.stdout n_str, m_str = input_file.readline().strip().split() n = int(n_str) m = int(m_str) f = [0] * (n + 1) for i in range(m): u, v = input_file.readline().strip().split() a, b = min(int(u), int(v)), max(int(u), int(v)) f[a] += 1 killed = 0 for i in range(1, int(n) + 1): if f[i] > 0: killed += 1 q = input_file.readline().strip() for i in range(int(q)): query = input_file.readline().strip().split() if query[0] == "3": output_file.write(str(n - killed) + "\n") continue u, v = int(query[1]), int(query[2]) a, b = min(u, v), max(u, v) if query[0] == "1": if f[a] == 0: killed += 1 f[a] += 1 else: f[a] -= 1 if f[a] == 0: killed -= 1
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER STRING IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
from sys import stdin, stdout n, m = map(int, stdin.readline().split()) li = [0] * (n + 1) for i in range(m): u, v = map(int, stdin.readline().split()) li[min(u, v)] += 1 a = 0 for j in range(1, n + 1): if li[j] == 0: a = a + 1 q = int(stdin.readline()) for j in range(q): ai = list(map(int, stdin.readline().split())) if ai[0] == 3: print(a) elif ai[0] == 2: li[min(ai[1], ai[2])] -= 1 if li[min(ai[1], ai[2])] == 0: a = a + 1 else: li[min(ai[1], ai[2])] += 1 if li[min(ai[1], ai[2])] == 1: a = a - 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
import sys input = sys.stdin.readline t = 1 for _ in range(t): n, m = map(int, input().split()) can = 0 dead = 0 now = [[0, 0] for i in range(n + 1)] for i in range(m): a, b = map(int, input().split()) if a > b: now[a][0] += 1 now[b][1] += 1 else: now[b][0] += 1 now[a][1] += 1 ans = 0 for i in range(1, n + 1): if now[i][1] == 0: ans += 1 q = int(input()) for i in range(q): inp = list(map(int, input().split())) if len(inp) == 1: print(ans) continue if inp[0] == 1: a, b = inp[1], inp[2] if a > b: if now[b][1] == 0: ans -= 1 now[a][0] += 1 now[b][1] += 1 else: if now[a][1] == 0: ans -= 1 now[b][0] += 1 now[a][1] += 1 else: a, b = inp[1], inp[2] if a > b: if now[b][1] == 1: ans += 1 now[a][0] -= 1 now[b][1] -= 1 else: if now[a][1] == 1: ans += 1 now[b][0] -= 1 now[a][1] -= 1
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
n, m = [int(i) for i in input().split()] nobles = [(0) for i in range(n)] for i in range(m): u, v = [(int(i) - 1) for i in input().split()] if u > v: u, v = v, u if nobles[u] == 0: n -= 1 nobles[u] += 1 m = int(input()) for i in range(m): u = input() if u == "3": print(n) elif u[0] == "1": v = int(u.split()[2]) - 1 u = int(u.split()[1]) - 1 if u > v: u, v = v, u if nobles[u] == 0: n -= 1 nobles[u] += 1 else: v = int(u.split()[2]) - 1 u = int(u.split()[1]) - 1 if u > v: u, v = v, u nobles[u] -= 1 if nobles[u] == 0: n += 1
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR NUMBER STRING ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
for _ in range(1): n, m = map(int, input().split()) mp = dict() for i in range(m): x, y = map(int, input().split()) x, y = max(x, y), min(x, y) if y in mp: mp[y] += 1 else: mp[y] = 1 q = int(input()) for h in range(q): qu = input() if qu[0] == "3": print(n - len(mp)) else: t, x, y = map(int, qu.split()) if t == 1: x, y = max(x, y), min(x, y) if y in mp: mp[y] += 1 else: mp[y] = 1 else: x, y = max(x, y), min(x, y) mp[y] -= 1 if mp[y] == 0: del mp[y]
FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
n, m = list(map(int, input().split())) arr = [1] * n frnd = [0] * n for _ in range(m): x, y = list(map(int, input().split())) if x > y: arr[y] = 0 frnd[y] += 1 else: arr[x] = 0 frnd[x] += 1 ans = frnd.count(0) tem = 0 temp = 0 q = int(input()) for _ in range(q): tem = input().split() if len(tem) == 1: print(ans) elif int(tem[0]) == 2: temp = min(int(tem[1]), int(tem[2])) frnd[temp] -= 1 if frnd[temp] == 0: ans += 1 else: temp = min(int(tem[1]), int(tem[2])) frnd[temp] += 1 if frnd[temp] == 1: ans -= 1
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
import sys def int_arr(): return list(map(int, sys.stdin.readline().strip().split())) def str_arr(): return list(map(str, sys.stdin.readline().strip().split())) def input(): return sys.stdin.readline() def out(x): return sys.stdout.write(str(x)) n, q = list(map(int, input().split())) arr = [0] * n c = 0 for _ in range(q): a, b = map(int, input().split()) if a < b: ind = a - 1 else: ind = b - 1 arr[ind] += 1 if arr[ind] == 1: c += 1 for i in range(int(input())): y = list(map(int, input().split())) if len(y) == 1: print(n - c) continue v, a, b = y if a < b: ind = a - 1 else: ind = b - 1 if v == 1: arr[ind] += 1 if arr[ind] == 1: c += 1 elif v == 2: arr[ind] -= 1 if arr[ind] == 0: c -= 1
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
n, m = map(int, input().split()) small = [0] * (n + 1) big = [0] * (n + 1) for i in range(m): u, v = map(int, input().split()) small[max(u, v)] += 1 big[min(u, v)] += 1 ans = 0 for i in range(1, n + 1): if big[i] == 0: ans += 1 q = int(input()) for i in range(q): ch = input().split(" ") if ch[0] == "1": u = int(ch[1]) v = int(ch[2]) if big[min(u, v)] == 0: ans -= 1 small[max(u, v)] += 1 big[min(u, v)] += 1 elif ch[0] == "2": u = int(ch[1]) v = int(ch[2]) small[max(u, v)] -= 1 big[min(u, v)] -= 1 if big[min(u, v)] == 0: ans += 1 else: print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
import sys input = sys.stdin.readline n, m = list(map(int, input().split())) data = [(0) for i in range(n + 1)] ans = n while m > 0: a, b = list(map(int, input().split())) if a > b: a, b = b, a if data[a] == 0: ans -= 1 data[a] += 1 m -= 1 q = int(input()) while q > 0: q -= 1 item = list(map(int, input().split())) if len(item) == 1: print(ans) elif item[0] == 1: a, b = item[1:] if a > b: a, b = b, a if data[a] == 0: ans -= 1 data[a] += 1 elif item[0] == 2: a, b = item[1:] if a > b: a, b = b, a data[a] -= 1 if data[a] == 0: ans += 1
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
n, m = map(lambda x: int(x), input().split()) N = n E = [0] * (n + 1) res = [] for _ in range(m): a, b = sorted(map(lambda x: int(x), input().split())) if E[a] == 0: N -= 1 E[a] += 1 Q = int(input()) for _ in range(Q): q = input().split() if q[0] == "3": res.append(N) elif q[0] == "1": a, b = sorted(map(lambda x: int(x), q[1:])) if E[a] == 0: N -= 1 E[a] += 1 else: a, b = sorted(map(lambda x: int(x), q[1:])) E[a] -= 1 if E[a] == 0: N += 1 [print(r) for r in res]
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR IF VAR NUMBER STRING ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
n, m = map(int, input().split()) a = [0] * n for _ in range(m): b, c = map(int, input().split()) a[min(b, c)] += 1 z = 0 for i in a: if i == 0: z += 1 s1 = z v = list(a) q = int(input()) for _ in range(q): t = input() if t[0] == "3": print(s1) else: query, b, c = map(int, t.split()) if query == 1: v[min(b, c)] += 1 if v[min(b, c)] == 1: s1 -= 1 else: v[min(b, c)] -= 1 if v[min(b, c)] == 0: s1 += 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
n, m = map(int, input().split()) frens = [(0) for __ in range(n + 1)] for _ in range(m): u, v = map(int, input().split()) if u > v: frens[v] += 1 else: frens[u] += 1 alive = 0 for i in range(1, n + 1): if frens[i] == 0: alive += 1 for ___ in range(int(input())): inp = input().strip() if len(inp) == 1: print(max(alive, 0)) else: a, b, c = map(int, inp.split()) if a == 1: if b > c: if frens[c] == 0: alive -= 1 frens[c] += 1 else: if frens[b] == 0: alive -= 1 frens[b] += 1 elif b > c: frens[c] -= 1 if frens[c] == 0: alive += 1 else: frens[b] -= 1 if frens[b] == 0: alive += 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR IF VAR NUMBER IF VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
A = list(map(int, input().split())) n, m = A[0], A[1] dic = [(0) for _ in range(n + 1)] ans = 0 for _ in range(m): A = list(map(int, input().split())) minn = min(A) if dic[minn] == 0: ans += 1 dic[minn] += 1 x = int(input()) for _ in range(x): A = input() if len(A) == 1: print(n - ans) elif A[0] == "1": A = list(map(int, A.split())) minn = min(A[1:]) if dic[minn] == 0: ans += 1 dic[minn] += 1 elif A[0] == "2": A = list(map(int, A.split())) minn = min(A[1:]) dic[minn] -= 1 if dic[minn] == 0: ans -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
a, m = map(int, input().split()) c = [0] * (a + 1) for _ in range(m): u, v = map(int, input().split()) z = min(u, v) if c[z] == 0: a -= 1 c[z] += 1 for _ in range(int(input())): tes = input().split() if len(tes) == 1: print(a) else: x, u, v = map(int, tes) if x == 1: z = min(u, v) if c[z] == 0: a -= 1 c[z] += 1 else: z = min(u, v) if c[z] == 1: a += 1 c[u] -= 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
n, m = map(int, input().split()) x = [0] * n for i in range(m): u, v = map(int, input().split()) u -= 1 v -= 1 if u < v: x[u] += 1 else: x[v] += 1 c = 0 for i in range(n): if x[i] == 0: c += 1 q = int(input()) while q: q -= 1 s = input() if len(s) == 1: print(c) continue else: y, u, v = map(int, s.split()) u -= 1 v -= 1 if y == 1: if u < v: if x[u] == 0: c -= 1 x[u] += 1 else: if x[v] == 0: c -= 1 x[v] += 1 elif u < v: x[u] -= 1 if x[u] == 0: c += 1 else: x[v] -= 1 if x[v] == 0: c += 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
q = list(map(int, input().split())) a = [(0) for _ in range(q[0])] res = q[0] for _ in range(q[1]): x = min(list(map(int, input().split()))) res -= 1 if a[x] == 0 else 0 a[x] += 1 for _ in range(int(input())): x = input().split() if x[0] == "1": x = min(int(x[1]), int(x[2])) res -= 1 if a[x] == 0 else 0 a[x] += 1 elif x[0] == "2": x = min(int(x[1]), int(x[2])) a[x] -= 1 res += 1 if a[x] == 0 else 0 else: print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
n, m = map(int, input().split()) g = [0] * (n + 1) dead = 0 for i in range(m): x, y = map(int, input().split()) if x > y: weak = y else: weak = x g[weak] += 1 if g[weak] == 1: dead += 1 for q in range(int(input())): miku = input().split() if len(miku) == 1: print(n - dead) continue typ, x, y = map(int, miku) if x > y: weak = y else: weak = x if typ == 1: g[weak] += 1 if g[weak] == 1: dead += 1 if typ == 2: g[weak] -= 1 if g[weak] == 0: dead -= 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
import sys N = 200010 f = [-1] * N def find(x): if f[x] == x: return x f[x] = find(f[x]) return f[x] def ufsInit(): for i in range(1, n + 1): f[i] = i a = [0] * N input = sys.stdin.readline n, m = map(int, input().split()) for i in range(0, m): x, y = map(int, input().split()) if x > y: x, y = y, x a[x] += 1 cnt = 0 for i in range(1, n + 1): cnt += a[i] == 0 q = int(input()) for i in range(0, q): l = list(map(int, input().split())) if l[0] == 3: print(cnt) continue x, y = l[1], l[2] if x > y: x, y = y, x if l[0] == 1: a[x] += 1 if a[x] == 1: cnt -= 1 elif l[0] == 2: a[x] -= 1 if a[x] == 0: cnt += 1
IMPORT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
n, m = [int(i) for i in input().split(" ")] vin = {} vout = {} ans = 0 for i in range(1, n + 1): vin[i] = 0 vout[i] = 0 for i in range(m): u, v = [int(j) for j in input().split(" ")] if u > v: vout[u] += 1 vin[v] += 1 else: vout[v] += 1 vin[u] += 1 for i in vin: if vin[i] == 0: ans += 1 q = int(input()) for i in range(q): s = input() if len(s) > 1: a, u, v = [int(j) for j in s.split(" ")] else: a = 3 if a == 1: if u > v: vout[u] += 1 vin[v] += 1 if vin[v] == 1: ans -= 1 else: vout[v] += 1 vin[u] += 1 if vin[u] == 1: ans -= 1 elif a == 2: if u > v: vout[u] -= 1 vin[v] -= 1 if vin[v] == 0: ans += 1 else: vout[v] -= 1 vin[u] -= 1 if vin[u] == 0: ans += 1 else: print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
ANS = 0 DEG = [] def add(u, v): global ANS, DEG if u > v: u, v = v, u ANS -= DEG[u] == 0 DEG[u] += 1 def del_(u, v): global ANS, DEG if u > v: u, v = v, u DEG[u] -= 1 ANS += DEG[u] == 0 def main(): global ANS, DEG n, m = map(int, input().split()) ANS = n DEG = [0] * n for _ in range(m): u, v = map(int, input().split()) add(u, v) q = int(input()) for _ in range(q): s = input().split() c = int(s[0]) if c == 1: u, v = map(int, s[1:]) add(u, v) elif c == 2: u, v = map(int, s[1:]) del_(u, v) elif c == 3: print(ANS) else: raise NotImplementedError main()
ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
n, m = map(int, input().split()) a = [0] * n ot = 0 for i in range(m): u, v = map(int, input().split()) b = min(u, v) a[b - 1] += 1 if a[b - 1] == 1: ot += 1 q = int(input()) for i in range(q): s = input() if s[0] == "3": print(n - ot) if s[0] != "3": x, y, z = map(int, s.split()) if x == 1: a[min(y, z) - 1] += 1 if a[min(y, z) - 1] == 1: ot += 1 else: a[min(y, z) - 1] -= 1 if a[min(y, z) - 1] == 0: ot -= 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
def solve(): n, m = map(int, input().split()) ans = dict() for i in range(m): u, v = map(int, input().split()) if u > v: if v in ans: ans[v] += 1 else: ans[v] = 1 elif u in ans: ans[u] += 1 else: ans[u] = 1 q = int(input()) res = len(ans) for _ in range(q): temp = list(map(int, input().split())) t = temp[0] if t == 2: u, v = temp[1], temp[2] if u > v: ans[v] -= 1 if ans[v] == 0: del ans[v] res -= 1 else: ans[u] -= 1 if ans[u] == 0: del ans[u] res -= 1 elif t == 1: u, v = temp[1], temp[2] if u > v: if v in ans: ans[v] += 1 else: ans[v] = 1 res += 1 elif u in ans: ans[u] += 1 else: ans[u] = 1 res += 1 else: print(n - res) t = 1 for i in range(t): solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
import sys input = sys.stdin.readline n, m = map(int, input().split()) arr = [0] * n ans = n for x in range(m): u, v = map(int, input().split()) k = min(u, v) if arr[k - 1] == 0: ans -= 1 arr[k - 1] += 1 for tt in range(int(input())): l = list(map(int, input().split())) a = l[0] if a == 1: u, v = l[1], l[2] k = min(u, v) if arr[k - 1] == 0: ans -= 1 arr[k - 1] += 1 elif a == 2: u, v = l[1], l[2] k = min(u, v) arr[k - 1] -= 1 if arr[k - 1] == 0: ans += 1 else: print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R. R. Martin There are $n$ nobles, numbered from $1$ to $n$. Noble $i$ has a power of $i$. There are also $m$ "friendships". A friendship between nobles $a$ and $b$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $u$ and $v$. Remove a friendship between nobles $u$ and $v$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! -----Input----- The first line contains the integers $n$ and $m$ ($1 \le n \le 2\cdot 10^5$, $0 \le m \le 2\cdot 10^5$) β€” the number of nobles and number of original friendships respectively. The next $m$ lines each contain the integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), describing a friendship. No friendship is listed twice. The next line contains the integer $q$ ($1 \le q \le 2\cdot {10}^{5}$) β€” the number of queries. The next $q$ lines contain the queries themselves, each query has one of the following three formats. $1$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” add a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are not friends at this moment. $2$ $u$ $v$ ($1 \le u,v \le n$, $u \ne v$) β€” remove a friendship between $u$ and $v$. It is guaranteed that $u$ and $v$ are friends at this moment. $3$ β€” print the answer to the process described in the statement. -----Output----- For each type $3$ query print one integer to a new line. It is guaranteed that there will be at least one type $3$ query. -----Examples----- Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Output 2 1 Input 4 3 2 3 3 4 4 1 1 3 Output 1 -----Note----- Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $1$ is weaker than all of his friends ($2$ and $3$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $3$ is weaker than his only friend, noble $4$, and is therefore killed. At this point, the process ends, and the answer is $2$. In the second type 3 query, the only surviving noble is $4$. The second example consists of only one type $3$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $1$, since only one noble survives.
n, m = map(int, input().split()) higher_friends = [(0) for i in range(n)] alive = n for i in range(m): a, b = map(int, input().split()) if a > b: if higher_friends[b - 1] == 0: alive -= 1 higher_friends[b - 1] += 1 else: if higher_friends[a - 1] == 0: alive -= 1 higher_friends[a - 1] += 1 query = int(input()) for i in range(query): s = input() if s[0] == "1": a, b = map(int, s[2:].split()) if a > b: if higher_friends[b - 1] == 0: alive -= 1 higher_friends[b - 1] += 1 else: if higher_friends[a - 1] == 0: alive -= 1 higher_friends[a - 1] += 1 elif s[0] == "2": a, b = map(int, s[2:].split()) if a > b: higher_friends[b - 1] -= 1 if higher_friends[b - 1] == 0: alive += 1 else: higher_friends[a - 1] -= 1 if higher_friends[a - 1] == 0: alive += 1 else: print(alive)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
On the way to school, Karen became fixated on the puzzle game on her phone! [Image] The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! -----Input----- The first line of input contains two integers, n and m (1 ≀ n, m ≀ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 ≀ g_{i}, j ≀ 500). -----Output----- If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 ≀ x ≀ n) describing a move of the form "choose the x-th row". col x, (1 ≀ x ≀ m) describing a move of the form "choose the x-th column". If there are multiple optimal solutions, output any one of them. -----Examples----- Input 3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1 Output 4 row 1 row 1 col 4 row 3 Input 3 3 0 0 0 0 1 0 0 0 0 Output -1 Input 3 3 1 1 1 1 1 1 1 1 1 Output 3 row 1 row 2 row 3 -----Note----- In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
def all_zero(A): for row in A: for e in row: if e != 0: return False return True n, m = input().split(" ") n = int(n) m = int(m) A = [] for _ in range(n): row = input().split(" ") row = list(map(int, row)) A += [row] moves = [] min_elem = min(min(row) for row in A) if min_elem > 0: if m >= n: for x in range(n): moves += [f"row {x + 1}"] * min_elem else: for y in range(m): moves += [f"col {y + 1}"] * min_elem for x in range(n): for y in range(m): A[x][y] -= min_elem pivot = None for x in range(n): for y in range(m): if A[x][y] == 0: pivot = x, y break assert pivot is not None pivot_x, pivot_y = pivot for x in range(n): if x == pivot_x: continue f = A[x][pivot_y] moves += [f"row {x + 1}"] * f for y in range(m): A[x][y] -= f for y in range(m): if y == pivot_y: continue f = A[pivot_x][y] moves += [f"col {y + 1}"] * f for x in range(n): A[x][y] -= f if not all_zero(A): print(-1) else: print(len(moves)) print("\n".join(moves))
FUNC_DEF FOR VAR VAR FOR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR LIST VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP LIST STRING BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP LIST STRING BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NONE ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP LIST STRING BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP LIST STRING BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
On the way to school, Karen became fixated on the puzzle game on her phone! [Image] The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! -----Input----- The first line of input contains two integers, n and m (1 ≀ n, m ≀ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 ≀ g_{i}, j ≀ 500). -----Output----- If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 ≀ x ≀ n) describing a move of the form "choose the x-th row". col x, (1 ≀ x ≀ m) describing a move of the form "choose the x-th column". If there are multiple optimal solutions, output any one of them. -----Examples----- Input 3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1 Output 4 row 1 row 1 col 4 row 3 Input 3 3 0 0 0 0 1 0 0 0 0 Output -1 Input 3 3 1 1 1 1 1 1 1 1 1 Output 3 row 1 row 2 row 3 -----Note----- In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
n, m = map(int, input().split()) a = [] tot = 0 row = [] col = [] n1 = "row" n2 = "col" for _ in range(n): a += [[int(f) for f in input().split()]] if n > m: a = list(map(list, zip(*a))) n, m = m, n n1, n2 = n2, n1 for x in range(n): for y in range(min(a[x])): row += [x + 1] tot += sum(a[x]) for x in range(m): t = a[0][x] - min(a[0]) if t <= 0: t = 0 for y in range(t): col += [x + 1] t = len(row) + len(col) if tot - m * len(row) - n * len(col) == 0: print(t) for x in row: print(n1, x) for x in col: print(n2, x) else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR LIST BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR LIST BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
On the way to school, Karen became fixated on the puzzle game on her phone! [Image] The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! -----Input----- The first line of input contains two integers, n and m (1 ≀ n, m ≀ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 ≀ g_{i}, j ≀ 500). -----Output----- If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 ≀ x ≀ n) describing a move of the form "choose the x-th row". col x, (1 ≀ x ≀ m) describing a move of the form "choose the x-th column". If there are multiple optimal solutions, output any one of them. -----Examples----- Input 3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1 Output 4 row 1 row 1 col 4 row 3 Input 3 3 0 0 0 0 1 0 0 0 0 Output -1 Input 3 3 1 1 1 1 1 1 1 1 1 Output 3 row 1 row 2 row 3 -----Note----- In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
import sys n, m = map(int, sys.stdin.readline().split()) l = [] row = [0] * n col = [0] * m flag = True if m > n: for i in range(n): l.append(list(map(int, sys.stdin.readline().split()))) tmp = min(l[i]) row[i] = tmp if tmp != 0: l[i] = list(map(lambda x: x - tmp, l[i])) l2 = list(map(list, zip(*l))) for i in range(m): tmp = min(l2[i]) col[i] = tmp if tmp != 0: l2[i] = list(map(lambda x: x - tmp, l2[i])) if sum(l2[i]) != 0: print(-1) flag = False break if flag: print(sum(row) + sum(col)) for i in range(n): for k in range(row[i]): print("row", i + 1) for i in range(m): for k in range(col[i]): print("col", i + 1) else: for i in range(n): l.append(list(map(int, sys.stdin.readline().split()))) l2 = list(map(list, zip(*l))) for i in range(m): tmp = min(l2[i]) col[i] = tmp if tmp != 0: l2[i] = list(map(lambda x: x - tmp, l2[i])) l = list(map(list, zip(*l2))) for i in range(n): tmp = min(l[i]) row[i] = tmp if tmp != 0: l[i] = list(map(lambda x: x - tmp, l[i])) if sum(l[i]) != 0: print(-1) flag = False break if flag: print(sum(row) + sum(col)) for i in range(n): for k in range(row[i]): print("row", i + 1) for i in range(m): for k in range(col[i]): print("col", i + 1)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING BIN_OP VAR NUMBER
On the way to school, Karen became fixated on the puzzle game on her phone! [Image] The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! -----Input----- The first line of input contains two integers, n and m (1 ≀ n, m ≀ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 ≀ g_{i}, j ≀ 500). -----Output----- If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 ≀ x ≀ n) describing a move of the form "choose the x-th row". col x, (1 ≀ x ≀ m) describing a move of the form "choose the x-th column". If there are multiple optimal solutions, output any one of them. -----Examples----- Input 3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1 Output 4 row 1 row 1 col 4 row 3 Input 3 3 0 0 0 0 1 0 0 0 0 Output -1 Input 3 3 1 1 1 1 1 1 1 1 1 Output 3 row 1 row 2 row 3 -----Note----- In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
import sys def min_i_v(row): min_v = None min_i = None for i, v in enumerate(row): if min_v is None or v < min_v: min_v = v min_i = i return min_i, min_v def dec_row(matrix, row_i, val): for i in range(len(matrix[row_i])): matrix[row_i][i] -= val def dec_col(matrix, col_i, val): for i in range(len(matrix)): matrix[i][col_i] -= val amount_of_rows, amount_of_cols = [int(a) for a in sys.stdin.readline().split()] matrix = [] for i in range(amount_of_rows): matrix.append([int(a) for a in sys.stdin.readline().split()]) log = [] interrupted = False if amount_of_cols >= amount_of_rows: for row_i, row in enumerate(matrix): min_i, min_v = min_i_v(row) dec_row(matrix, row_i, min_v) for i in range(min_v): log.append("row " + str(row_i + 1) + "\n") col = [0] * amount_of_rows for c_i in range(amount_of_cols): for r_i in range(amount_of_rows): col[r_i] = matrix[r_i][c_i] min_i, min_v = min_i_v(col) if min_v < col[row_i]: interrupted = True break else: min_v = col[row_i] dec_col(matrix, c_i, min_v) for i in range(min_v): log.append("col " + str(c_i + 1) + "\n") if interrupted: break else: col = [0] * amount_of_rows for col_i in range(amount_of_cols): for r_i in range(amount_of_rows): col[r_i] = matrix[r_i][col_i] min_i, min_v = min_i_v(col) dec_col(matrix, col_i, min_v) for i in range(min_v): log.append("col " + str(col_i + 1) + "\n") for r_i, row in enumerate(matrix): min_i, min_v = min_i_v(row) if min_v < row[col_i]: interrupted = True break else: min_v = row[col_i] dec_row(matrix, r_i, min_v) for i in range(min_v): log.append("row " + str(r_i + 1) + "\n") if interrupted: break if interrupted: sys.stdout.write("-1\n") else: sys.stdout.write(str(len(log)) + "\n") for i in log: sys.stdout.write(i)
IMPORT FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER IF VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER STRING IF VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER STRING FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER STRING IF VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR
On the way to school, Karen became fixated on the puzzle game on her phone! [Image] The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! -----Input----- The first line of input contains two integers, n and m (1 ≀ n, m ≀ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 ≀ g_{i}, j ≀ 500). -----Output----- If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 ≀ x ≀ n) describing a move of the form "choose the x-th row". col x, (1 ≀ x ≀ m) describing a move of the form "choose the x-th column". If there are multiple optimal solutions, output any one of them. -----Examples----- Input 3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1 Output 4 row 1 row 1 col 4 row 3 Input 3 3 0 0 0 0 1 0 0 0 0 Output -1 Input 3 3 1 1 1 1 1 1 1 1 1 Output 3 row 1 row 2 row 3 -----Note----- In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
def func(l, n, m, row, col): for i in range(n): mi = min(l[i]) if mi: l[i] = [(x - mi) for x in l[i]] for j in range(mi): row.append(i) for j in range(m): c = [x[j] for x in l] mi = min(c) if max(c) - mi > 0: print("-1") exit() for i in range(mi): col.append(j) R = lambda: map(int, input().split()) n, m = R() l, row, col, l2 = [], [], [], [] for _ in range(n): l.append(list(R())) if n > m: for j in range(m): l2.append([x[j] for x in l]) func(l2, m, n, col, row) else: func(l, n, m, row, col) print(len(row) + len(col)) for i in row: print("row ", i + 1) for i in col: print("col", i + 1)
FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR LIST LIST LIST LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR STRING BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR STRING BIN_OP VAR NUMBER
On the way to school, Karen became fixated on the puzzle game on her phone! [Image] The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! -----Input----- The first line of input contains two integers, n and m (1 ≀ n, m ≀ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 ≀ g_{i}, j ≀ 500). -----Output----- If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 ≀ x ≀ n) describing a move of the form "choose the x-th row". col x, (1 ≀ x ≀ m) describing a move of the form "choose the x-th column". If there are multiple optimal solutions, output any one of them. -----Examples----- Input 3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1 Output 4 row 1 row 1 col 4 row 3 Input 3 3 0 0 0 0 1 0 0 0 0 Output -1 Input 3 3 1 1 1 1 1 1 1 1 1 Output 3 row 1 row 2 row 3 -----Note----- In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
inpt = input().split(" ") n = int(inpt[0]) m = int(inpt[1]) arr = [] for i in range(n): inpt = input().split(" ") inpt = list(map(int, inpt)) arr.append(inpt) sol = [] def solver1(a, sol): allmin = min([min(suba) for suba in a]) if allmin < 0: return False allmax = max([max(suba) for suba in a]) if allmax == 0: return True else: for i in range(len(a)): p = min(a[i]) if p > 0: for j in range(len(a[i])): a[i][j] -= p for q in range(p): sol.append(i + 1) return solver1(a, sol) for j in range(len(a[0])): b = [] for i in range(len(a)): b.append(a[i][j]) p = min(b) if p > 0: for k in range(len(b)): a[k][j] -= p for q in range(p): sol.append(len(a) + j + 1) return solver1(a, sol) def solver2(a, sol): allmin = min([min(suba) for suba in a]) if allmin < 0: return False allmax = max([max(suba) for suba in a]) if allmax == 0: return True else: for j in range(len(a[0])): b = [] for i in range(len(a)): b.append(a[i][j]) p = min(b) if p > 0: for k in range(len(b)): a[k][j] -= p for q in range(p): sol.append(len(a) + j + 1) return solver2(a, sol) for i in range(len(a)): p = min(a[i]) if p > 0: for j in range(len(a[i])): a[i][j] -= p for q in range(p): sol.append(i + 1) return solver2(a, sol) if n < m: if solver1(arr, sol): print(len(sol)) for i in sol: if i <= n: print("row", i) else: print("col", i - n) else: print("-1") elif solver2(arr, sol): print(len(sol)) for i in sol: if i <= n: print("row", i) else: print("col", i - n) else: print("-1")
ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR STRING BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR STRING BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING
On the way to school, Karen became fixated on the puzzle game on her phone! [Image] The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! -----Input----- The first line of input contains two integers, n and m (1 ≀ n, m ≀ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 ≀ g_{i}, j ≀ 500). -----Output----- If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 ≀ x ≀ n) describing a move of the form "choose the x-th row". col x, (1 ≀ x ≀ m) describing a move of the form "choose the x-th column". If there are multiple optimal solutions, output any one of them. -----Examples----- Input 3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1 Output 4 row 1 row 1 col 4 row 3 Input 3 3 0 0 0 0 1 0 0 0 0 Output -1 Input 3 3 1 1 1 1 1 1 1 1 1 Output 3 row 1 row 2 row 3 -----Note----- In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
n, m = map(int, input().split()) g = [list(map(int, input().split())) for i in range(n)] x, xi, xj = 500, 0, 0 for i, gi in enumerate(g): for j, gij in enumerate(gi): if gij < x: x, xi, xj = gij, i, j r, c = [(g[i][xj] - x) for i in range(n)], [(g[xi][j] - x) for j in range(m)] for i, gi in enumerate(g): for j, gij in enumerate(gi): if gij != r[i] + c[j] + x: print(-1) return print(min(n, m) * x + sum(r) + sum(c)) for i in range(n): for k in range(r[i] + (x if n <= m else 0)): print("row", i + 1) for j in range(m): for k in range(c[j] + (x if m < n else 0)): print("col", j + 1)
ASSIGN 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 VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING BIN_OP VAR NUMBER
On the way to school, Karen became fixated on the puzzle game on her phone! [Image] The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! -----Input----- The first line of input contains two integers, n and m (1 ≀ n, m ≀ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 ≀ g_{i}, j ≀ 500). -----Output----- If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 ≀ x ≀ n) describing a move of the form "choose the x-th row". col x, (1 ≀ x ≀ m) describing a move of the form "choose the x-th column". If there are multiple optimal solutions, output any one of them. -----Examples----- Input 3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1 Output 4 row 1 row 1 col 4 row 3 Input 3 3 0 0 0 0 1 0 0 0 0 Output -1 Input 3 3 1 1 1 1 1 1 1 1 1 Output 3 row 1 row 2 row 3 -----Note----- In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
def isNullified(): for i in range(n): if max(matrix[i]) != 0: return False return True def processRows(): for i in range(n): minValue = min(matrix[i]) if minValue != 0: content = "row " + str(i + 1) result.extend(minValue * [content]) for j in range(m): matrix[i][j] -= minValue def processCols(): for i in range(m): column = [row[i] for row in matrix] minValue = min(column) if minValue != 0: content = "col " + str(i + 1) result.extend(minValue * [content]) for j in range(n): matrix[j][i] -= minValue inpt = input().split() n = int(inpt[0]) m = int(inpt[1]) matrix = [] result = [] for i in range(n): row = list(map(int, input().split())) matrix.append(row) if n < m: processRows() processCols() else: processCols() processRows() if isNullified(): print(len(result)) print("\n".join(result)) else: print(-1)
FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER
On the way to school, Karen became fixated on the puzzle game on her phone! [Image] The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! -----Input----- The first line of input contains two integers, n and m (1 ≀ n, m ≀ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 ≀ g_{i}, j ≀ 500). -----Output----- If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 ≀ x ≀ n) describing a move of the form "choose the x-th row". col x, (1 ≀ x ≀ m) describing a move of the form "choose the x-th column". If there are multiple optimal solutions, output any one of them. -----Examples----- Input 3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1 Output 4 row 1 row 1 col 4 row 3 Input 3 3 0 0 0 0 1 0 0 0 0 Output -1 Input 3 3 1 1 1 1 1 1 1 1 1 Output 3 row 1 row 2 row 3 -----Note----- In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
import sys input = sys.stdin.readline n, m = map(int, input().split()) realg = [list(map(int, input().split())) for _ in range(n)] g = [([0] * m) for _ in range(n)] ans = [] f1 = min([realg[0][i] for i in range(m)]) for i in range(m): for _ in range(realg[0][i] - f1): ans.append("col %d" % (i + 1)) for j in range(n): g[j][i] += realg[0][i] - f1 f2 = min([realg[i][0] for i in range(n)]) for i in range(n): for _ in range(realg[i][0] - f2): ans.append("row %d" % (i + 1)) for j in range(m): g[i][j] += realg[i][0] - f2 bad = 0 floor = realg[0][0] - g[0][0] for i in range(n): for j in range(m): if realg[i][j] - g[i][j] != floor: bad = 1 if bad: print("-1") else: if n < m: for i in range(n): for j in range(floor): ans.append("row %d" % (i + 1)) else: for i in range(m): for j in range(floor): ans.append("col %d" % (i + 1)) print(len(ans)) print("\n".join(ans))
IMPORT ASSIGN VAR VAR ASSIGN 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 VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING IF VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
On the way to school, Karen became fixated on the puzzle game on her phone! [Image] The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! -----Input----- The first line of input contains two integers, n and m (1 ≀ n, m ≀ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 ≀ g_{i}, j ≀ 500). -----Output----- If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 ≀ x ≀ n) describing a move of the form "choose the x-th row". col x, (1 ≀ x ≀ m) describing a move of the form "choose the x-th column". If there are multiple optimal solutions, output any one of them. -----Examples----- Input 3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1 Output 4 row 1 row 1 col 4 row 3 Input 3 3 0 0 0 0 1 0 0 0 0 Output -1 Input 3 3 1 1 1 1 1 1 1 1 1 Output 3 row 1 row 2 row 3 -----Note----- In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
def main(): n, m = list(map(int, input().split())) l, r = [list(map(int, input().split())) for _ in range(n)], [] if n > m: l = [list(col) for col in zip(*l)] cols, rows = "row %d", "col %d" else: cols, rows = "col %d", "row %d" for y, row in enumerate(l, 1): a = min(row) for i, t in enumerate(row): row[i] = t - a r += [rows % y] * a l = [list(col) for col in zip(*l)] for x, col in enumerate(l, 1): a = min(col) for i, t in enumerate(col): col[i] = t - a r += [cols % x] * a r.append(str(len(r))) print(-1 if any(map(any, l)) else "\n".join(r[::-1])) def __starting_point(): main() __starting_point()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR LIST IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR STRING STRING ASSIGN VAR VAR STRING STRING FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP LIST BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP LIST BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL STRING VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
On the way to school, Karen became fixated on the puzzle game on her phone! [Image] The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! -----Input----- The first line of input contains two integers, n and m (1 ≀ n, m ≀ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 ≀ g_{i}, j ≀ 500). -----Output----- If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 ≀ x ≀ n) describing a move of the form "choose the x-th row". col x, (1 ≀ x ≀ m) describing a move of the form "choose the x-th column". If there are multiple optimal solutions, output any one of them. -----Examples----- Input 3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1 Output 4 row 1 row 1 col 4 row 3 Input 3 3 0 0 0 0 1 0 0 0 0 Output -1 Input 3 3 1 1 1 1 1 1 1 1 1 Output 3 row 1 row 2 row 3 -----Note----- In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
from sys import stdin, stdout arr = ["row", "col"] def solve(g, n, m, flag): newG = [] ans = [] for i in range(n): newG.append([0] * m) for i in range(n): k = min(g[i]) newG[i] = [k] * m ans.extend([arr[flag] + " {}".format(i + 1)] * k) for i in range(m): k = 0 while min([newG[u][i] for u in range(n)]) < min([g[u][i] for u in range(n)]): for u in range(n): newG[u][i] += 1 k += 1 if [newG[u][i] for u in range(n)] != [g[u][i] for u in range(n)]: return -1, [] else: ans.extend([arr[1 - flag] + " {}".format(i + 1)] * k) else: return len(ans), ans n, m = [int(item) for item in stdin.readline().strip().split()] g = [] for i in range(n): g.append([int(item) for item in stdin.readline().strip().split()]) g2 = [] for i in range(m): g2.append([g[k][i] for k in range(n)]) a, ans1 = solve(g, n, m, 0) b, ans2 = solve(g2, m, n, 1) if a == -1 or b == -1: print(-1) elif a <= b: print(a) for item in ans1: stdout.write(str(item) + "\n") else: print(b) for item in ans2: stdout.write(str(item) + "\n")
ASSIGN VAR LIST STRING STRING FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP LIST VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST BIN_OP VAR VAR FUNC_CALL STRING BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER LIST EXPR FUNC_CALL VAR BIN_OP LIST BIN_OP VAR BIN_OP NUMBER VAR FUNC_CALL STRING BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
On the way to school, Karen became fixated on the puzzle game on her phone! [Image] The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! -----Input----- The first line of input contains two integers, n and m (1 ≀ n, m ≀ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 ≀ g_{i}, j ≀ 500). -----Output----- If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 ≀ x ≀ n) describing a move of the form "choose the x-th row". col x, (1 ≀ x ≀ m) describing a move of the form "choose the x-th column". If there are multiple optimal solutions, output any one of them. -----Examples----- Input 3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1 Output 4 row 1 row 1 col 4 row 3 Input 3 3 0 0 0 0 1 0 0 0 0 Output -1 Input 3 3 1 1 1 1 1 1 1 1 1 Output 3 row 1 row 2 row 3 -----Note----- In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
n, m = input().strip().split() n, m = int(n), int(m) g = [[int(j) for j in input().strip().split()] for i in range(n)] answer = [] def b(): for i in range(n): minRow = min(g[i]) if minRow != 0: for k in range(minRow): answer.append("row " + str(i + 1)) for j in range(m): g[i][j] -= minRow def a(): for j in range(m): minCol = g[0][j] for i in range(n): if g[i][j] < minCol: minCol = g[i][j] if minCol != 0: for k in range(minCol): answer.append("col " + str(j + 1)) for i in range(n): g[i][j] -= minCol if n < m: b() a() else: a() b() maxNumber = max(max(g)) if maxNumber == 0: print(len(answer)) for el in answer: print(el) else: print("-1")
ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
On the way to school, Karen became fixated on the puzzle game on her phone! [Image] The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! -----Input----- The first line of input contains two integers, n and m (1 ≀ n, m ≀ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 ≀ g_{i}, j ≀ 500). -----Output----- If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 ≀ x ≀ n) describing a move of the form "choose the x-th row". col x, (1 ≀ x ≀ m) describing a move of the form "choose the x-th column". If there are multiple optimal solutions, output any one of them. -----Examples----- Input 3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1 Output 4 row 1 row 1 col 4 row 3 Input 3 3 0 0 0 0 1 0 0 0 0 Output -1 Input 3 3 1 1 1 1 1 1 1 1 1 Output 3 row 1 row 2 row 3 -----Note----- In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
n, m = map(int, input().split()) grid = [list(map(int, input().split())) for i in range(n)] ans = [] if m > n: for i in range(n): mini = min(grid[i]) for j in range(m): grid[i][j] -= mini for _ in range(mini): ans.append("row " + str(i + 1)) for i in range(m): mini = float("inf") for j in range(n): mini = min(grid[j][i], mini) for j in range(n): grid[j][i] -= mini for _ in range(mini): ans.append("col " + str(i + 1)) else: for i in range(m): mini = float("inf") for j in range(n): mini = min(grid[j][i], mini) for j in range(n): grid[j][i] -= mini for _ in range(mini): ans.append("col " + str(i + 1)) for i in range(n): mini = min(grid[i]) for j in range(m): grid[i][j] -= mini for _ in range(mini): ans.append("row " + str(i + 1)) if all([(sum(i) == 0) for i in grid]): print(len(ans)) for i in ans: print(i) else: print(-1)
ASSIGN 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 VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
On the way to school, Karen became fixated on the puzzle game on her phone! [Image] The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! -----Input----- The first line of input contains two integers, n and m (1 ≀ n, m ≀ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 ≀ g_{i}, j ≀ 500). -----Output----- If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 ≀ x ≀ n) describing a move of the form "choose the x-th row". col x, (1 ≀ x ≀ m) describing a move of the form "choose the x-th column". If there are multiple optimal solutions, output any one of them. -----Examples----- Input 3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1 Output 4 row 1 row 1 col 4 row 3 Input 3 3 0 0 0 0 1 0 0 0 0 Output -1 Input 3 3 1 1 1 1 1 1 1 1 1 Output 3 row 1 row 2 row 3 -----Note----- In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
from itertools import product n, m = map(int, input().split()) matr = [] for i in range(n): matr.append([int(num) for num in input().split()]) def walker(start_row, start_col, dif_row, dif_col): row = start_row col = start_col while 0 <= row < n and 0 <= col < m: yield row, col row += dif_row col += dif_col def walker_row(row): return walker(row, 0, 0, 1) def walker_col(col): return walker(0, col, 1, 0) def get_min(line): res = float("inf") for row, col in line: res = min(res, matr[row][col]) return res def subtract(line, diff): for row, col in line: matr[row][col] -= diff def resolve_rows(): for row in range(n): min_num = get_min(walker_row(row)) if min_num > 0: subtract(walker_row(row), min_num) step = "row {}".format(row + 1) for i in range(min_num): ans.append(step) def resolve_cols(): for col in range(m): min_num = get_min(walker_col(col)) if min_num > 0: subtract(walker_col(col), min_num) step = "col {}".format(col + 1) for i in range(min_num): ans.append(step) ans = [] if n > m: resolve_cols() resolve_rows() else: resolve_rows() resolve_cols() cant = False for row, col in product(range(n), range(m)): cant |= matr[row][col] > 0 if cant: print(-1) else: print(len(ans)) print("\n".join(ans))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER VAR VAR NUMBER VAR VAR EXPR VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR NUMBER VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF FOR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
On the way to school, Karen became fixated on the puzzle game on her phone! [Image] The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! -----Input----- The first line of input contains two integers, n and m (1 ≀ n, m ≀ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 ≀ g_{i}, j ≀ 500). -----Output----- If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 ≀ x ≀ n) describing a move of the form "choose the x-th row". col x, (1 ≀ x ≀ m) describing a move of the form "choose the x-th column". If there are multiple optimal solutions, output any one of them. -----Examples----- Input 3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1 Output 4 row 1 row 1 col 4 row 3 Input 3 3 0 0 0 0 1 0 0 0 0 Output -1 Input 3 3 1 1 1 1 1 1 1 1 1 Output 3 row 1 row 2 row 3 -----Note----- In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
n, m = 0, 0 arr, res_r, res_c = [], [], [] def solve_row(): global arr, res_r, n, m res = 0 for i in range(n): mini = int(2000000000.0) for j in range(m): mini = min(mini, arr[i][j]) if mini == 0: continue res += mini res_r += [(i + 1, mini)] for j in range(m): arr[i][j] -= mini return res def solve_col(): global arr, res_c, n, m res = 0 for i in range(m): mini = int(2000000000.0) for j in range(n): mini = min(mini, arr[j][i]) if mini == 0: continue res += mini res_c += [(i + 1, mini)] for j in range(n): arr[j][i] -= mini return res def main(): global arr, res_r, res_c, n, m n, m = map(int, input().split()) arr = [0] * n for i in range(n): arr[i] = list(map(int, input().split())) res = 0 if n < m: res += solve_row() res += solve_col() else: res += solve_col() res += solve_row() for i in range(n): for j in range(m): if arr[i][j] > 0: print(-1) return print(res) for i in range(len(res_r)): for j in range(res_r[i][1]): print("row", res_r[i][0]) for i in range(len(res_c)): for j in range(res_c[i][1]): print("col", res_c[i][0]) main()
ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR LIST LIST LIST FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR LIST BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR LIST BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR VAR NUMBER EXPR FUNC_CALL VAR
On the way to school, Karen became fixated on the puzzle game on her phone! [Image] The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! -----Input----- The first line of input contains two integers, n and m (1 ≀ n, m ≀ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 ≀ g_{i}, j ≀ 500). -----Output----- If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 ≀ x ≀ n) describing a move of the form "choose the x-th row". col x, (1 ≀ x ≀ m) describing a move of the form "choose the x-th column". If there are multiple optimal solutions, output any one of them. -----Examples----- Input 3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1 Output 4 row 1 row 1 col 4 row 3 Input 3 3 0 0 0 0 1 0 0 0 0 Output -1 Input 3 3 1 1 1 1 1 1 1 1 1 Output 3 row 1 row 2 row 3 -----Note----- In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
def min_row(): ans = 0 for i in range(n): ans += min(a[i]) return ans def min_col(): ans = 0 for i in range(m): mm = 10000 for j in range(n): mm = min(mm, a[j][i]) ans += mm return ans def remove_cols(): ans = 0 min_col = [0] * m for i in range(m): min_col[i] = 1000 for j in range(n): min_col[i] = min(min_col[i], a[j][i]) ans += min_col[i] for j in range(n): a[j][i] -= min_col[i] return ans, min_col def remove_rows(): ans = 0 min_row = [] for i in range(n): min_row.append(min(a[i])) ans += min_row[-1] for j in range(m): a[i][j] -= min_row[i] return ans, min_row n, m = list(map(int, input().split())) a = [] s = 0 for i in range(n): row = list(map(int, input().split())) a.append(row) s += sum(row) rows = min_row() cols = min_col() if rows == 0 and cols == 0: if s == 0: print(0) exit() else: print(-1) exit() if ( cols == 0 or rows != 0 and (s - rows * m) % n == 0 and ( (s - cols * n) % m != 0 or (s - rows * m) // n + rows < (s - cols * n) // m + cols ) ): ans, min_row = remove_rows() ans1, min_col = remove_cols() else: ans1, min_col = remove_cols() ans, min_row = remove_rows() ans += ans1 for row in a: for ai in row: if ai != 0: print(-1) exit() print(ans) for i in range(n): for j in range(min_row[i]): print("row", i + 1) for i in range(m): for j in range(min_col[i]): print("col", i + 1)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING BIN_OP VAR NUMBER
On the way to school, Karen became fixated on the puzzle game on her phone! [Image] The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! -----Input----- The first line of input contains two integers, n and m (1 ≀ n, m ≀ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 ≀ g_{i}, j ≀ 500). -----Output----- If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 ≀ x ≀ n) describing a move of the form "choose the x-th row". col x, (1 ≀ x ≀ m) describing a move of the form "choose the x-th column". If there are multiple optimal solutions, output any one of them. -----Examples----- Input 3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1 Output 4 row 1 row 1 col 4 row 3 Input 3 3 0 0 0 0 1 0 0 0 0 Output -1 Input 3 3 1 1 1 1 1 1 1 1 1 Output 3 row 1 row 2 row 3 -----Note----- In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
n, m = map(int, input().split()) g = [list(map(int, input().split())) for _ in range(n)] a = [0] * n b = [0] * m a[0] = min(g[0]) for j in range(m): g[0][j] -= a[0] b[j] = g[0][j] good = True for i in range(n): for j in range(m): g[i][j] -= b[j] if min(g[i]) < 0 or min(g[i]) != max(g[i]): good = False a[i] += min(g[i]) if not good: print(-1) else: if n > m: x = min(a) for i in range(n): a[i] -= x for j in range(m): b[j] += x num = sum(a) + sum(b) print(num) for i in range(n): for _ in range(a[i]): print("row {}".format(i + 1)) for j in range(m): for _ in range(b[j]): print("col {}".format(j + 1))
ASSIGN 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 VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR NUMBER
On the way to school, Karen became fixated on the puzzle game on her phone! [Image] The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! -----Input----- The first line of input contains two integers, n and m (1 ≀ n, m ≀ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 ≀ g_{i}, j ≀ 500). -----Output----- If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 ≀ x ≀ n) describing a move of the form "choose the x-th row". col x, (1 ≀ x ≀ m) describing a move of the form "choose the x-th column". If there are multiple optimal solutions, output any one of them. -----Examples----- Input 3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1 Output 4 row 1 row 1 col 4 row 3 Input 3 3 0 0 0 0 1 0 0 0 0 Output -1 Input 3 3 1 1 1 1 1 1 1 1 1 Output 3 row 1 row 2 row 3 -----Note----- In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
n, m = map(int, input().split()) array = [] row_sums = [(0) for x in range(n)] for i in range(n): array.append(list(map(int, input().split()))) row_sums[i] = sum(array[i]) col_sums = [sum(x) for x in zip(*array)] min_row_index = row_sums.index(min(row_sums)) min_col_index = col_sums.index(min(col_sums)) row_boost = [(0) for x in range(n)] col_boost = [(0) for x in range(m)] if n <= m: col_boost[min_col_index] = 0 for i in range(m): if i != min_col_index: col_boost[i] = (col_sums[i] - col_sums[min_col_index]) // n sum_col_boosts = sum(col_boost) for i in range(n): row_boost[i] = (row_sums[i] - sum_col_boosts) // m if n >= m: row_boost[min_row_index] = 0 for i in range(n): if i != min_row_index: row_boost[i] = (row_sums[i] - row_sums[min_row_index]) // m sum_row_boosts = sum(row_boost) for i in range(m): col_boost[i] = (col_sums[i] - sum_row_boosts) // n for row in range(n): if [(row_boost[row] + c) for c in col_boost] != array[row]: print(-1) return print(sum(row_boost + col_boost)) for i in range(n): for count in range(row_boost[i]): print("row " + str(i + 1)) for j in range(m): for count in range(col_boost[j]): print("col " + str(j + 1))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER
On the way to school, Karen became fixated on the puzzle game on her phone! [Image] The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! -----Input----- The first line of input contains two integers, n and m (1 ≀ n, m ≀ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 ≀ g_{i}, j ≀ 500). -----Output----- If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 ≀ x ≀ n) describing a move of the form "choose the x-th row". col x, (1 ≀ x ≀ m) describing a move of the form "choose the x-th column". If there are multiple optimal solutions, output any one of them. -----Examples----- Input 3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1 Output 4 row 1 row 1 col 4 row 3 Input 3 3 0 0 0 0 1 0 0 0 0 Output -1 Input 3 3 1 1 1 1 1 1 1 1 1 Output 3 row 1 row 2 row 3 -----Note----- In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
n, m = map(int, input().split()) grid = [] for _ in range(n): grid.append(list(map(int, input().split()))) count = 0 moves = [] grid2 = [] for row in grid: grid2.append(row[:]) def do_row(grid, row): global count r = row row = grid[row] m = min(row) count += m for _ in range(m): moves.append("row " + str(r + 1)) for idx in range(len(row)): row[idx] -= m def do_rows(grid): for r in range(len(grid)): do_row(grid, r) def do_col(grid, col): global count mcol = min(row[col] for row in grid) for _ in range(mcol): moves.append("col " + str(col + 1)) count += mcol for row in grid: row[col] -= mcol def do_cols(grid): for c in range(len(grid[0])): do_col(grid, c) big = 9999999999999999999999999999999999 def get_count(grid): if sum(sum(row) for row in grid) != 0: return big return count do_rows(grid) do_cols(grid) crc = get_count(grid) gmoves = moves moves = [] count = 0 do_cols(grid2) do_rows(grid2) ccr = get_count(grid2) g2moves = moves ans = min(ccr, crc) ansmoves = gmoves if ans == crc else g2moves if ans == big: ans = -1 ansmoves = [] print(ans) for m in ansmoves: print(m)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR RETURN VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
On the way to school, Karen became fixated on the puzzle game on her phone! [Image] The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! -----Input----- The first line of input contains two integers, n and m (1 ≀ n, m ≀ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 ≀ g_{i}, j ≀ 500). -----Output----- If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 ≀ x ≀ n) describing a move of the form "choose the x-th row". col x, (1 ≀ x ≀ m) describing a move of the form "choose the x-th column". If there are multiple optimal solutions, output any one of them. -----Examples----- Input 3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1 Output 4 row 1 row 1 col 4 row 3 Input 3 3 0 0 0 0 1 0 0 0 0 Output -1 Input 3 3 1 1 1 1 1 1 1 1 1 Output 3 row 1 row 2 row 3 -----Note----- In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
x, y = map(int, input().split()) grid = [] for i in range(x): grid.append(list(map(int, input().split()))) mi = grid[0][0] prints = [] for i in range(x): mi = min(mi, min(grid[i])) if x > y: for k in range(mi): for i in range(1, y + 1): prints.append("col " + str(i)) else: for k in range(mi): for i in range(1, x + 1): prints.append("row " + str(i)) for i in range(x): for j in range(y): grid[i][j] -= mi for i in range(x): while min(grid[i]) > 0: prints.append("row " + str(i + 1)) for j in range(y): grid[i][j] -= 1 for i in range(y): indy = [] for ranind in range(x): indy.append(grid[ranind][i]) mindy = min(indy) for ran2 in range(mindy): prints.append("col " + str(i + 1)) for j in range(x): grid[j][i] -= 1 ma = grid[0][0] for i in range(x): ma = max(ma, max(grid[i])) if ma != 0: print("-1") quit() print(len(prints)) print("\n".join(prints))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
On the way to school, Karen became fixated on the puzzle game on her phone! [Image] The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! -----Input----- The first line of input contains two integers, n and m (1 ≀ n, m ≀ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 ≀ g_{i}, j ≀ 500). -----Output----- If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 ≀ x ≀ n) describing a move of the form "choose the x-th row". col x, (1 ≀ x ≀ m) describing a move of the form "choose the x-th column". If there are multiple optimal solutions, output any one of them. -----Examples----- Input 3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1 Output 4 row 1 row 1 col 4 row 3 Input 3 3 0 0 0 0 1 0 0 0 0 Output -1 Input 3 3 1 1 1 1 1 1 1 1 1 Output 3 row 1 row 2 row 3 -----Note----- In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
n, m = (int(i) for i in input().split()) tbl = [] for i in range(n): crstr = [int(j) for j in input().split()] tbl.append(crstr) true = 0 def prt(): for xx in range(n): for yy in range(m): print(tbl[xx][yy], end="\t") print() print() print() def lines(): rows = [] for i in range(n): while 1: may = 1 for j in range(m): if tbl[i][j] == 0: for k in range(j): tbl[i][k] += 1 may = 0 break tbl[i][j] -= 1 if not may: break rows.append(i + 1) return rows def cols(): stolbs = [] for i in range(m): while 1: may = 1 for j in range(n): if tbl[j][i] == 0: for k in range(j): tbl[k][i] += 1 may = 0 break tbl[j][i] -= 1 if not may: break stolbs.append(i + 1) return stolbs if m > n: l = lines() c = cols() else: c = cols() l = lines() br = 0 for i in range(n): for j in range(m): if tbl[i][j] != 0: br = 1 break if br: break if br: print(-1) else: print(len(l) + len(c)) for i in l: print("row", i) for i in c: print("col", i)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR STRING VAR FOR VAR VAR EXPR FUNC_CALL VAR STRING VAR
On the way to school, Karen became fixated on the puzzle game on her phone! [Image] The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! -----Input----- The first line of input contains two integers, n and m (1 ≀ n, m ≀ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 ≀ g_{i}, j ≀ 500). -----Output----- If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 ≀ x ≀ n) describing a move of the form "choose the x-th row". col x, (1 ≀ x ≀ m) describing a move of the form "choose the x-th column". If there are multiple optimal solutions, output any one of them. -----Examples----- Input 3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1 Output 4 row 1 row 1 col 4 row 3 Input 3 3 0 0 0 0 1 0 0 0 0 Output -1 Input 3 3 1 1 1 1 1 1 1 1 1 Output 3 row 1 row 2 row 3 -----Note----- In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
def Check(Mat, Row, Col, TotalSum): N = len(Mat) M = len(Mat[0]) SummTillNow = Row[0] * M + Col[0] * N for i in range(0, N): for j in range(0, M): if not (i == 0 and j == 0): if Mat[i][j] == 0: if i in Row and Row[i] != 0 or j in Col and Col[j] != 0: return False Row[i] = 0 Col[j] = 0 elif i in Row and j in Col: Val = Row[i] + Col[j] if Val != Mat[i][j]: return False elif i in Row: Val = Mat[i][j] - Row[i] if Val < 0: return False Col[j] = Val SummTillNow += N * Val elif j in Col: Val = Mat[i][j] - Col[j] if Val < 0: return False Row[i] = Val SummTillNow += M * Val if SummTillNow == TotalSum: return True return False N, M = map(int, input().strip().split()) Mat = [] for i in range(N): Mat.append(list(map(int, input().strip().split()))) Num = Mat[0][0] TotalSum = 0 Min_Count = float("inf") for l in Mat: TotalSum += sum(l) for i in range(0, Num + 1): Row = {} Col = {} Row[0] = i Col[0] = Num - i res = Check(Mat, Row, Col, TotalSum) if res: Count = 0 for k, v in Row.items(): Count += v for k, v in Col.items(): Count += v if Min_Count > Count: Min_Count = min(Min_Count, Count) AnsRow = Row AnsCol = Col if Min_Count == float("inf"): print(-1) else: print(Min_Count) for k, v in AnsRow.items(): for cnt in range(v): print("row", k + 1) for k, v in AnsCol.items(): for cnt in range(v): print("col", k + 1)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING BIN_OP VAR NUMBER
On the way to school, Karen became fixated on the puzzle game on her phone! [Image] The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! -----Input----- The first line of input contains two integers, n and m (1 ≀ n, m ≀ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 ≀ g_{i}, j ≀ 500). -----Output----- If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 ≀ x ≀ n) describing a move of the form "choose the x-th row". col x, (1 ≀ x ≀ m) describing a move of the form "choose the x-th column". If there are multiple optimal solutions, output any one of them. -----Examples----- Input 3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1 Output 4 row 1 row 1 col 4 row 3 Input 3 3 0 0 0 0 1 0 0 0 0 Output -1 Input 3 3 1 1 1 1 1 1 1 1 1 Output 3 row 1 row 2 row 3 -----Note----- In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
def printMatrix(matrix): s = [[str(e) for e in row] for row in matrix] lens = [max(map(len, col)) for col in zip(*s)] fmt = "\t".join("{{:{}}}".format(x) for x in lens) table = [fmt.format(*row) for row in s] print("_________________") print("\n".join(table)) print("`````````````````") n, m = map(int, input().split()) mat = [] ans = [] for i in range(n): temp = [int(x) for x in input().split()] mat.append(temp) ok = True for i in range(n): for j in range(m): if j == 0: if m > n: if mat[i][j]: MIN = mat[i][j] for row in range(m): MIN = min(MIN, mat[i][row]) for __ in range(MIN): ans.append(["row", i + 1]) for row in range(m): mat[i][row] -= MIN if mat[i][j]: MIN = mat[i][j] for col in range(n): MIN = min(MIN, mat[col][j]) for __ in range(MIN): ans.append(["col", j + 1]) for col in range(n): mat[col][j] -= MIN else: if mat[i][j]: MIN = mat[i][j] for col in range(n): MIN = min(MIN, mat[col][j]) for __ in range(MIN): ans.append(["col", j + 1]) for col in range(n): mat[col][j] -= MIN if mat[i][j]: MIN = mat[i][j] for row in range(m): MIN = min(MIN, mat[i][row]) for __ in range(MIN): ans.append(["row", i + 1]) for row in range(m): mat[i][row] -= MIN if mat[i][j]: ok = False break else: if mat[i][j]: MIN = mat[i][j] for col in range(n): MIN = min(MIN, mat[col][j]) for __ in range(MIN): ans.append(["col", j + 1]) for col in range(n): mat[col][j] -= MIN if mat[i][j]: ok = False if ok: print(len(ans)) for i in ans: print(i[0], i[1]) else: print(-1)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL STRING VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER
On the way to school, Karen became fixated on the puzzle game on her phone! [Image] The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! -----Input----- The first line of input contains two integers, n and m (1 ≀ n, m ≀ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 ≀ g_{i}, j ≀ 500). -----Output----- If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 ≀ x ≀ n) describing a move of the form "choose the x-th row". col x, (1 ≀ x ≀ m) describing a move of the form "choose the x-th column". If there are multiple optimal solutions, output any one of them. -----Examples----- Input 3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1 Output 4 row 1 row 1 col 4 row 3 Input 3 3 0 0 0 0 1 0 0 0 0 Output -1 Input 3 3 1 1 1 1 1 1 1 1 1 Output 3 row 1 row 2 row 3 -----Note----- In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
n, m = map(int, input().split()) L = [list(map(int, input().split())) for i in range(n)] s = "row" s1 = "col" if n > m: L1 = [[(0) for i in range(n)] for j in range(m)] for i in range(n): for j in range(m): L1[j][i] = L[i][j] L = L1 n, m = m, n s, s1 = s1, s w = [(0) for i in range(n)] w1 = [(0) for i in range(m)] for i in range(n): w[i] = min(L[i]) for i in range(m): for j in range(n): ma = 0 ma = max(L[j][i] - w[j], ma) w1[i] = ma for i in range(n): for j in range(m): if L[i][j] - w[i] - w1[j] != 0: print(-1) exit() otv = [] for i in range(n): otv += [s + " " + str(i + 1)] * w[i] for i in range(m): otv += [s1 + " " + str(i + 1)] * w1[i] print(len(otv)) print("\n".join(otv))
ASSIGN 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 VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING IF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR BIN_OP LIST BIN_OP BIN_OP VAR STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP LIST BIN_OP BIN_OP VAR STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
On the way to school, Karen became fixated on the puzzle game on her phone! [Image] The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column. To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to g_{i}, j. Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task! -----Input----- The first line of input contains two integers, n and m (1 ≀ n, m ≀ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_{i}, j (0 ≀ g_{i}, j ≀ 500). -----Output----- If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done: row x, (1 ≀ x ≀ n) describing a move of the form "choose the x-th row". col x, (1 ≀ x ≀ m) describing a move of the form "choose the x-th column". If there are multiple optimal solutions, output any one of them. -----Examples----- Input 3 5 2 2 2 3 2 0 0 0 1 0 1 1 1 2 1 Output 4 row 1 row 1 col 4 row 3 Input 3 3 0 0 0 0 1 0 0 0 0 Output -1 Input 3 3 1 1 1 1 1 1 1 1 1 Output 3 row 1 row 2 row 3 -----Note----- In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level: [Image] In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center. In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level: [Image] Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
class CodeforcesTask815ASolution: def __init__(self): self.result = "" self.n_m = [] self.board = [] def read_input(self): self.n_m = [int(x) for x in input().split(" ")] for x in range(self.n_m[0]): self.board.append([int(y) for y in input().split(" ")]) def process_task(self): columns = [] for x in range(self.n_m[1]): column = [] for y in range(self.n_m[0]): column.append(self.board[y][x]) columns.append(column) left_board_moves = [min(row) for row in self.board] bottom_board_moves = [min(column) for column in columns] reduce1 = min(bottom_board_moves) reduce2 = min(left_board_moves) if reduce1 * self.n_m[0] > reduce2 * self.n_m[1]: left_board_moves = [(x - reduce1) for x in left_board_moves] else: bottom_board_moves = [(x - reduce2) for x in bottom_board_moves] moves = sum(left_board_moves) + sum(bottom_board_moves) board_score = ( sum(left_board_moves) * self.n_m[1] + sum(bottom_board_moves) * self.n_m[0] ) real_board_score = sum([sum(x) for x in self.board]) if board_score != real_board_score: self.result = "-1" else: print("{0}".format(moves)) for x in range(self.n_m[0]): for y in range(left_board_moves[x]): print("row {0}".format(x + 1)) for x in range(self.n_m[1]): for y in range(bottom_board_moves[x]): print("col {0}".format(x + 1)) def get_result(self): return self.result Solution = CodeforcesTask815ASolution() Solution.read_input() Solution.process_task() print(Solution.get_result())
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR NUMBER FUNC_DEF RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
def to_int(lis): for i in range(len(lis)): lis[i] = int(lis[i]) return lis def sum_of_digits(lis): ans = 0 for d in lis: ans += d return ans n = int(input()) a = input().split() a = to_int(a) if 0 not in a: print(-1) else: a = sorted(a, reverse=True) s = sum_of_digits(a) if s % 3 == 1: d = 0 for i in range(len(a) - 1, -1, -1): if a[i] == 1 or a[i] == 4 or a[i] == 7: a.pop(i) d = 1 break if not d: for i in range(len(a) - 1, -1, -1): if a[i] == 2 or a[i] == 5 or a[i] == 8: a.pop(i) break for i in range(len(a) - 1, -1, -1): if a[i] == 2 or a[i] == 5 or a[i] == 8: a.pop(i) break elif s % 3 == 2: d = 0 for i in range(len(a) - 1, -1, -1): if a[i] == 2 or a[i] == 5 or a[i] == 8: a.pop(i) d = 1 break if not d: for i in range(len(a) - 1, -1, -1): if a[i] == 1 or a[i] == 4 or a[i] == 7: a.pop(i) break for i in range(len(a) - 1, -1, -1): if a[i] == 1 or a[i] == 4 or a[i] == 7: a.pop(i) break if a.count(0) == len(a): print(0) else: for i in range(len(a)): print(a[i], end="")
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
from sys import exit n = int(input()) mas = list(map(int, input().split())) di = {i: (0) for i in range(10)} s = 0 for i, x in enumerate(mas): s += x di[x] += 1 if di[0] > 0: a = s % 3 if a == 0: from sys import exit if n == di[0]: print(0) exit() for i in range(9, -1, -1): for j in range(di[i]): print(i, end="") exit() e = 0 for i in range(1, 10): if di[i] > 0: if i % 3 == a: di[i] -= 1 e = 1 break if e == 1: res = "" w = 0 for i in range(9, 0, -1): if di[i] > 0: w = 1 for j in range(di[i]): print(i, end="") if w == 0: print(0) else: for i in range(di[0]): print(0, end="") else: w, f = 0, 0 for i in range(1, 10): if i % 3 > 0: if di[i] == 1: w += 1 if w < 3: di[i] = 0 if w == 3: break if di[i] == 2: if w == 1: di[i] = 1 if w == 1 or w == 2: w = 3 break if w == 0: w = 2 di[i] = 0 if di[i] > 2: if w == 0: di[i] -= 2 if w == 1: di[i] -= 1 w = 3 break elif di[i] > 0: f = 1 if w < 2: if n == di[0]: print(0) else: print(-1) elif w == 2: if f == 0: print(0) else: for i in range(9, -1, -1): for j in range(di[i]): print(i, end="") else: for i in range(9, -1, -1): for j in range(di[i]): print(i, end="") else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
n = int(input()) l = list(map(int, input().split())) l.sort(reverse=True) if 0 not in l: print(-1) else: l = list(map(str, l)) k = int("".join(l)) if k % 3 == 0: print(k) else: c = k % 3 if c == 1: l1 = [] l2 = [] for i in range(n): if int(l[i]) % 3 == 1: l1.append(i) if int(l[i]) % 3 == 2: l2.append(i) if l1 != []: l.pop(l1[-1]) else: l.pop(l2[-1]) l.pop(l2[-2]) if c == 2: l1 = [] l2 = [] for i in range(n): if int(l[i]) % 3 == 1: l1.append(i) if int(l[i]) % 3 == 2: l2.append(i) if l2 != []: l.pop(l2[-1]) else: l.pop(l1[-1]) l.pop(l1[-2]) k = int("".join(l)) if int(k) % 3 != 0: print(-1) else: print(k)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR LIST EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR LIST EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
n = int(input()) a = list(map(int, input().split())) c = [0] * 10 for i in range(n): c[a[i]] += 1 if c[0] == 0: print(-1) exit() c[0] -= 1 m = 0 for i in range(1, 10): m += c[i] * i m %= 3 if m % 3 == 1: if c[1] > 0: c[1] -= 1 m -= 1 elif c[4] > 0: c[4] -= 1 m -= 1 elif c[7] > 0: c[7] -= 1 m -= 1 else: res = 2 if c[2] >= 2: res -= 2 c[2] -= 2 m -= 1 elif c[2] == 1: res -= 1 c[2] -= 1 m -= 2 if res == 2 and c[5] >= 2: res -= 2 c[5] -= 2 m -= 1 elif res >= 1 and c[5] >= 1: res -= 1 c[5] -= 1 m -= 2 if res == 2 and c[8] >= 2: res -= 2 c[8] -= 2 m -= 1 elif res >= 1 and c[8] >= 1: res -= 1 c[8] -= 1 m -= 2 elif m % 3 == 2: if c[2] > 0: c[2] -= 1 m -= 2 elif c[5] > 0: c[5] -= 1 m -= 2 elif c[8] > 0: c[8] -= 1 m -= 2 else: res = 2 if c[1] >= 2: res -= 2 c[1] -= 2 m -= 2 elif c[1] == 1: res -= 1 c[1] -= 1 m -= 1 if res == 2 and c[4] >= 2: res -= 2 c[4] -= 2 m -= 2 elif res >= 1 and c[4] >= 1: res -= 1 c[4] -= 1 m -= 1 if res == 2 and c[7] >= 2: res -= 2 c[7] -= 2 m -= 2 elif res >= 1 and c[7] >= 1: res -= 1 c[7] -= 1 m -= 1 if m % 3 != 0: print(-1) exit() ans = ["0"] for i in range(10): ans.extend([str(i)] * c[i]) while len(ans) > 1 and ans[-1] == "0": ans.pop() ans = ans[::-1] print("".join(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST STRING FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST FUNC_CALL VAR VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
def ans(): global answered global final if answered: return sum = 0 for i in final: sum += i if sum == 0: print(0) answered = True return for i in final: print(i, end="") print() answered = True answered = False N = int(input()) A = list(int(i) for i in input().split()) A.sort() A.reverse() final = A if A[N - 1] != 0: print(-1) answered = True else: sum = 0 for i in A: sum += i if sum % 3 == 0: ans() elif sum % 3 == 2: for i in reversed(range(len(A))): if A[i] % 3 == 2: final.pop(i) ans() break count = 0 for i in reversed(range(len(A))): if A[i] % 3 == 1: final.pop(i) count += 1 if count == 2: ans() break elif sum % 3 == 1: for i in reversed(range(len(A))): if A[i] % 3 == 1: A.pop(i) ans() break count = 0 for i in reversed(range(len(A))): if A[i] % 3 == 2: A.pop(i) count += 1 if count == 2: ans() break if not answered: print(-1)
FUNC_DEF IF VAR RETURN ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER RETURN FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR NUMBER
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
def s(): input() a = list(map(int, input().split())) a.sort(reverse=True) if a[-1] == 0: r = sum(a) % 3 if r == 0: if a[0] > 0: print(*a, sep="") else: print(0) else: for i, v in reversed(list(enumerate(a))): if v % 3 == r: a.pop(i) if a[0] > 0: print(*a, sep="") else: print(0) break else: c = 0 for i, v in reversed(list(enumerate(a))): if v % 3 == 3 - r: c += 1 a.pop(i) if c == 2: if a[0] > 0: print(*a, sep="") else: print(0) break else: print(-1) else: print(-1) s()
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
for i in range(1): input() a = sorted(map(int, input().split())) if a[0]: a = [-1] else: s = sum(a) if s % 3: for i in a: if i % 3 == s % 3: a.remove(i) break else: for i in a: if i % 3: a.remove(i) break for i in a: if i % 3: a.remove(i) break while len(a) > 1 and a[-1] == 0: a.pop() print("".join(map(str, a[::-1])))
FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
n = int(input()) a = list(map(int, input().split())) a.sort() if a[0] != 0: print(-1) exit() s = sum(a) % 3 if s != 0: b = next((i for i, v in enumerate(a) if v % 3 == s), None) if b is None: del a[next((i for i, v in enumerate(a) if v % 3 == 3 - s), None)] del a[next((i for i, v in enumerate(a) if v % 3 == 3 - s), None)] else: del a[b] a.reverse() if a[0] == 0: print(0) else: print("".join(map(str, a)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NONE IF VAR NONE VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR NONE VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR NONE VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
def diminish(count, k): for d in range(k, 10, 3): if count[d] > 0: count[d] -= 1 return True return False def solve(count, sum): if sum % 3 == 1: if not diminish(count, 1): if not diminish(count, 2) or not diminish(count, 2): print(0) return elif sum % 3 == 2: if not diminish(count, 2): if not diminish(count, 1) or not diminish(count, 1): print(0) return max = 0 for d in range(1, 10): if count[d] > 0: max = d if max == 0: print(0) return digits = [] for d in range(9, -1, -1): for i in range(count[d]): digits.append(str(d)) print("".join(digits)) n = int(input()) digits = [int(s) for s in input().split()] count = {d: (0) for d in range(0, 10)} for d in digits: count[d] += 1 if count[0] == 0: print(-1) else: solve(count, sum(digits))
FUNC_DEF FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
from sys import exit n = int(input()) arr = list(map(int, input().split())) if arr == [0]: print(0) exit(0) if 0 not in arr: print(-1) exit(0) arr = sorted(arr, reverse=True) arr.remove(0) while sum(arr) % 3 != 0: mod = sum(arr) % 3 for i in sorted(set(arr)): if i % 3 == mod: arr.remove(i) break if sum(arr) % 3 != 0: for i in sorted(arr): if i % 3 != 0: arr.remove(i) break if len(arr) == arr.count(0): print(0) exit(0) print(*arr, sep="", end="") print("0")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR LIST NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING EXPR FUNC_CALL VAR STRING
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
alist = [(0) for i in range(0, 10)] n = int(input()) test = input() numbers = test.split() for i in range(n): alist[int(numbers[i])] += 1 def checker(n): s = "" if not alist[0]: return -1 rem = ( 1 * alist[1] + 2 * alist[2] + 3 * alist[3] + 4 * alist[4] + 5 * alist[5] + 6 * alist[6] + 7 * alist[7] + 8 * alist[8] + 9 * alist[9] ) % 3 while rem: if rem == 2: if alist[2]: alist[2] -= 1 n -= 1 elif alist[5]: alist[5] -= 1 n -= 1 elif alist[8]: alist[8] -= 1 n -= 1 elif alist[1] > 1: alist[1] -= 2 n -= 2 elif alist[4] and alist[1]: alist[4] -= 1 alist[1] -= 1 n -= 2 elif alist[4] > 1: alist[4] -= 2 n -= 2 elif alist[7] and alist[1]: alist[7] -= 1 alist[1] -= 1 n -= 2 elif alist[7] > 1: alist[7] -= 2 n -= 2 elif alist[2] and alist[1] > 1: alist[2] -= 1 alist[1] -= 2 n -= 3 else: return 0 elif rem == 1: if alist[1]: alist[1] -= 1 n -= 1 elif alist[4]: alist[4] -= 1 n -= 1 elif alist[7]: alist[7] -= 1 n -= 1 elif alist[2] > 1: alist[2] -= 2 n -= 2 elif alist[5] and alist[2]: alist[2] -= 1 alist[5] -= 1 n -= 2 elif alist[5] > 1: alist[5] -= 2 n -= 2 elif alist[8] and alist[5]: alist[8] -= 1 alist[5] -= 1 n -= 2 elif alist[8] > 1: alist[8] -= 2 n -= 2 else: return 0 rem = ( 1 * alist[1] + 2 * alist[2] + 3 * alist[3] + 4 * alist[4] + 5 * alist[5] + 6 * alist[6] + 7 * alist[7] + 8 * alist[8] + 9 * alist[9] ) % 3 j = 9 while n: if alist[j]: s += str(j) alist[j] -= 1 n -= 1 else: j -= 1 if s[0] == "0": return 0 else: return s print(checker(n))
ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR STRING IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER NUMBER WHILE VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER STRING RETURN NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
n, t = int(input()), input()[::2] p, r = {i: (0) for i in "0123456789"}, "-1" for i in t: p[i] += 1 if p["0"]: t = ["147", "258"] x = [sum(p[i] for i in k) for k in t] d = x[0] % 3 - x[1] % 3 if d: d, t = (1, t[d > 0]) if abs(d) == 2 and x[d > 0] else (abs(d), t[d < 0]) for i in t: if p[i] > 0: if p[i] < d: p[i], d = 0, 1 else: p[i] -= d break r = "".join(i * p[i] for i in "9876543210") if r[0] == "0": r = "0" print(r)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR STRING STRING FOR VAR VAR VAR VAR NUMBER IF VAR STRING ASSIGN VAR LIST STRING STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL STRING BIN_OP VAR VAR VAR VAR STRING IF VAR NUMBER STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
from sys import stdin, stdout n = int(input()) l = list(map(int, stdin.readline().split())) l.sort() flag = 0 if l[0] != 0: flag = 1 else: sum1 = 0 for i in l: sum1 = sum1 + i if sum1 % 3 == 0: flag = 2 elif sum1 % 3 == 1: for i in range(n): if l[i] % 3 == 1: l.pop(i) flag = 2 break if flag != 2: c = [] cnt = 0 for i in range(n): if l[i] % 3 == 2: c.append(i) cnt = cnt + 1 if cnt == 2: flag = 3 break else: for i in range(n): if l[i] % 3 == 2: l.pop(i) flag = 2 break if flag != 2: c = [] cnt = 0 for i in range(n): if l[i] % 3 == 1: c.append(i) cnt = cnt + 1 if cnt == 2: flag = 3 break if flag == 1 or flag == 0: print(-1) elif flag == 2: l = l[::-1] sum1 = 0 for i in l: sum1 = sum1 + i if sum1 == 0: print(0) else: for i in l: print(i, end="") print() elif flag == 3: s = "" m = [] for i in range(n): if i not in c: m.append(l[i]) m = m[::-1] sum1 = 0 for i in m: sum1 = sum1 + i if sum1 == 0: print(0) else: for i in m: print(i, end="") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
from sys import stdin def solve(tc): n = int(stdin.readline().strip()) seq = list(map(int, stdin.readline().split())) seq = sorted(seq, reverse=True) if seq[-1] != 0: print(-1) return mod = 0 rem = [[], []] for i in range(n): res = seq[i] % 3 if res: rem[res - 1].append(i) mod += res mod %= 3 if mod == 1: if len(rem[0]) > 0: seq = seq[: rem[0][-1]] + seq[rem[0][-1] + 1 :] else: seq = seq[: rem[1][-1]] + seq[rem[1][-1] + 1 :] seq = seq[: rem[1][-2]] + seq[rem[1][-2] + 1 :] elif mod == 2: if len(rem[1]) > 0: seq = seq[: rem[1][-1]] + seq[rem[1][-1] + 1 :] else: seq = seq[: rem[0][-1]] + seq[rem[0][-1] + 1 :] seq = seq[: rem[0][-2]] + seq[rem[0][-2] + 1 :] if seq[0] == 0: print(0) return print("".join(map(lambda x: str(x), seq))) tcs = 1 for tc in range(tcs): solve(tc)
FUNC_DEF ASSIGN VAR FUNC_CALL 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 NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER ASSIGN VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
from sys import stdin, stdout n = int(stdin.readline()) a = [int(x) for x in stdin.readline().split()] a.sort() if a[0] != 0: stdout.write("-1") else: ans = "" suma = sum(a) rem = suma % 3 if rem == 1: mod2 = [] mod1 = -1 c = 0 for i in range(n): if a[i] % 3 == 2 and c != 2: mod2.append(a[i]) c += 1 elif a[i] % 3 == 1: mod1 = a[i] break if c != 2 and mod1 == -1: ans = "-1" elif mod1 != -1: a.remove(mod1) n -= 1 else: a.remove(mod2[0]) a.remove(mod2[1]) n -= 2 elif rem == 2: mod1 = [] mod2 = -1 c = 0 for i in range(n): if a[i] % 3 == 1 and c != 2: mod1.append(a[i]) c += 1 elif a[i] % 3 == 2: mod2 = a[i] break if c != 2 and mod2 == -1: ans = "-1" elif mod2 != -1: a.remove(mod2) n -= 1 else: a.remove(mod1[0]) a.remove(mod1[1]) n -= 2 if ans != "-1": for i in range(n - 1, -1, -1): ans += str(a[i]) if ans[0] == "0": ans = "0" stdout.write(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
n = int(input()) arr = list(map(int, input().split())) a = [[], [], []] c0 = 0 s = 0 for i in arr: s += i if i == 0: c0 += 1 a[i % 3].append(i) if not c0: print(-1) exit() a[0].sort(reverse=True) a[1].sort(reverse=True) a[2].sort(reverse=True) inc = [] if s % 3: if len(a[s % 3]): a[s % 3].pop() elif len(a[3 - s % 3]) >= 2: a[3 - s % 3].pop() a[3 - s % 3].pop() elif len(a[0]): print(int("".join(map(str, a[0])))) else: print(-1) exit() inc += a[0] inc += a[1] inc += a[2] inc.sort(reverse=True) print(int("".join(map(str, inc))))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST LIST LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST IF BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
def hometask(arr): n = "0" if arr.count(0) == 0: return -1 arr.remove(0) ans = [] arr = sorted(arr, reverse=True) while arr: c = sum(arr) % 3 if c == 0: break flag = True for i in range(len(arr) - 1, -1, -1): if arr[i] % 3 == c: arr.pop(i) flag = False break if flag == True: for i in range(len(arr) - 1, -1, -1): if arr[i] % 3 != 0: arr.pop(i) flag = False break for i in range(len(arr) - 1, -1, -1): if arr[i] % 3 != 0: arr.pop(i) flag = False break if arr == [] or arr[0] == 0: return "0" ans = "".join(map(str, sorted(arr, reverse=True))) return ans + n a = input() lst = list(map(int, input().strip().split())) print(hometask(lst))
FUNC_DEF ASSIGN VAR STRING IF FUNC_CALL VAR NUMBER NUMBER RETURN NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR LIST VAR NUMBER NUMBER RETURN STRING ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
import sys def input(): return sys.stdin.readline().strip() def iinput(): return int(input()) def rinput(): return map(int, sys.stdin.readline().strip().split()) def get_list(): return list(map(int, sys.stdin.readline().strip().split())) mod = int(1000000000.0) + 7 n = iinput() dig = get_list() if 0 not in dig: print(-1) else: dig.sort(reverse=True) s = sum(dig) if s % 3 == 0: if dig.count(0) == n: print(0) else: dig = [str(item) for item in dig] print("".join(dig)) elif s % 3 == 1: flag = False for i in range(n - 1, -1, -1): if dig[i] % 3 == 1: dig.remove(dig[i]) flag = True break if flag == True: dig = [str(item) for item in dig] if dig.count("0") == len(dig): print(0) else: print("".join(dig)) else: flag = 0 for i in range(n - 1, -1, -1): if flag == 2: break if dig[i] % 3 == 2: dig.remove(dig[i]) flag += 1 dig = [str(item) for item in dig] if dig.count("0") == len(dig): print(0) else: print("".join(dig)) else: flag = False for i in range(n - 1, -1, -1): if dig[i] % 3 == 2: dig.remove(dig[i]) flag = True break if flag == True: dig = [str(item) for item in dig] if dig.count("0") == len(dig): print(0) else: print("".join(dig)) else: flag = 0 for i in range(n - 1, -1, -1): if flag == 2: break if dig[i] % 3 == 1: dig.remove(dig[i]) flag += 1 dig = [str(item) for item in dig] if dig.count("0") == len(dig): print(0) else: print("".join(dig))
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
n = int(input()) d = [0] * 10 s = 0 ans = [] other = False for x in map(int, input().strip().split()): d[x] += 1 s += x if d[0] < 1: pass elif s % 3 == 0: for x in range(9, -1, -1): for i in range(d[x]): if x > 0: other = True ans.append(x) else: remain = s % 3 found = True if remain == 1: if d[1] > 0: d[1] -= 1 elif d[4] > 0: d[4] -= 1 elif d[7] > 0: d[7] -= 1 elif d[2] > 1: d[2] -= 2 elif d[2] > 0 and d[5] > 0: d[2] -= 1 d[5] -= 1 elif d[5] > 1: d[5] -= 2 elif d[5] > 0 and d[8] > 0: d[5] -= 1 d[8] -= 1 elif d[8] > 1: d[8] -= 2 else: found = False elif d[2] > 0: d[2] -= 1 elif d[5] > 0: d[5] -= 1 elif d[8] > 0: d[8] -= 1 elif d[1] > 1: d[1] -= 2 elif d[1] > 0 and d[4] > 0: d[1] -= 1 d[4] -= 1 elif d[4] > 1: d[4] -= 2 elif d[7] > 0 and d[4] > 0: d[7] -= 1 d[4] -= 1 elif d[7] > 1: d[7] -= 2 else: found = False if found == True: for x in range(9, -1, -1): for i in range(d[x]): if x > 0: other = True ans.append(x) if len(ans) > 0: if other == False: print(0) else: print("".join([str(x) for x in ans])) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
input() arr = sorted(map(int, input().split())) if arr[0]: print(-1) exit() s = sum(arr) if s % 3: for i in arr: if i % 3 == s % 3: arr.remove(i) break else: for i in arr: if i % 3: arr.remove(i) break for i in arr: if i % 3: arr.remove(i) break while len(arr) > 1 and arr[-1] == 0: arr.pop() print(*arr[::-1], sep="")
EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER STRING