description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer — the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) L0 = [int(x) for x in input().split()] L1 = [int(x) for x in input().split()] if n == 1: print(max(L0[0], L1[0])) else: dp = [[0, 0] for i in range(n)] dp[0][0] = L0[0] dp[0][1] = L1[0] dp[1][0] = dp[0][1] + L0[1] dp[1][1] = dp[0][0] + L1[1] for i in range(1, n): dp[i][0] = max(dp[i - 1][1], dp[i - 2][1]) + L0[i] dp[i][1] = max(dp[i - 1][0], dp[i - 2][0]) + L1[i] print(max(dp[n - 1][0], dp[n - 1][1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer — the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) A = [int(a) for a in input().split()] B = [int(b) for b in input().split()] dp = [[(0) for _ in range(3)] for _ in range(n + 1)] for j in range(3): dp[0][j] = 0 dp[1][0] = A[0] dp[1][1] = B[0] dp[1][2] = 0 for i in range(2, n + 1): for j in range(3): if j == 0: dp[i][j] = A[i - 1] + max(dp[i - 1][1], dp[i - 1][2]) elif j == 1: dp[i][j] = B[i - 1] + max(dp[i - 1][0], dp[i - 1][2]) else: dp[i][j] = max(dp[i - 1][0], dp[i - 1][1], dp[i - 1][2]) m = -1 for i in range(n + 1): for j in range(3): m = max(m, dp[i][j]) print(m)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Berland has a long and glorious history. To increase awareness about it among younger citizens, King of Berland decided to compose an anthem. Though there are lots and lots of victories in history of Berland, there is the one that stand out the most. King wants to mention it in the anthem as many times as possible. He has already composed major part of the anthem and now just needs to fill in some letters. King asked you to help him with this work. The anthem is the string s of no more than 10^5 small Latin letters and question marks. The most glorious victory is the string t of no more than 10^5 small Latin letters. You should replace all the question marks with small Latin letters in such a way that the number of occurrences of string t in string s is maximal. Note that the occurrences of string t in s can overlap. Check the third example for clarification. -----Input----- The first line contains string of small Latin letters and question marks s (1 ≤ |s| ≤ 10^5). The second line contains string of small Latin letters t (1 ≤ |t| ≤ 10^5). Product of lengths of strings |s|·|t| won't exceed 10^7. -----Output----- Output the maximum number of occurrences of string t you can achieve by replacing all the question marks in string s with small Latin letters. -----Examples----- Input winlose???winl???w?? win Output 5 Input glo?yto?e??an? or Output 3 Input ??c????? abcab Output 2 -----Note----- In the first example the resulting string s is "winlosewinwinlwinwin" In the second example the resulting string s is "glorytoreorand". The last letter of the string can be arbitrary. In the third example occurrences of string t are overlapping. String s with maximal number of occurrences of t is "abcabcab".
t = input() s = input() n = len(s) h = [0] * (n + 1) h[0] = 0 j = 0 fa = [0] * (n + 1) for i in range(2, n + 1): while j and s[i - 1] != s[j]: j = fa[j] if s[i - 1] == s[j]: j += 1 fa[i] = j l = list() j = fa[n] while j > 0: l.append(j) j = fa[j] tmp = t t = s s = tmp n = len(s) dp = [0] * n m = [0] * n for i in range(len(t) - 1, len(s)): can = True for j in range(len(t)): if s[i - len(t) + 1 + j] == "?": continue if s[i - len(t) + 1 + j] != t[j]: can = False break if can: dp[i] = 1 for d in l: d = len(t) - d dp[i] = max(dp[i], 1 + dp[i - d]) if i - len(t) >= 0: dp[i] = max(dp[i], m[i - len(t)] + 1) m[i] = max(m[i - 1], dp[i]) print(m[-1])
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR STRING IF VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Berland has a long and glorious history. To increase awareness about it among younger citizens, King of Berland decided to compose an anthem. Though there are lots and lots of victories in history of Berland, there is the one that stand out the most. King wants to mention it in the anthem as many times as possible. He has already composed major part of the anthem and now just needs to fill in some letters. King asked you to help him with this work. The anthem is the string s of no more than 10^5 small Latin letters and question marks. The most glorious victory is the string t of no more than 10^5 small Latin letters. You should replace all the question marks with small Latin letters in such a way that the number of occurrences of string t in string s is maximal. Note that the occurrences of string t in s can overlap. Check the third example for clarification. -----Input----- The first line contains string of small Latin letters and question marks s (1 ≤ |s| ≤ 10^5). The second line contains string of small Latin letters t (1 ≤ |t| ≤ 10^5). Product of lengths of strings |s|·|t| won't exceed 10^7. -----Output----- Output the maximum number of occurrences of string t you can achieve by replacing all the question marks in string s with small Latin letters. -----Examples----- Input winlose???winl???w?? win Output 5 Input glo?yto?e??an? or Output 3 Input ??c????? abcab Output 2 -----Note----- In the first example the resulting string s is "winlosewinwinlwinwin" In the second example the resulting string s is "glorytoreorand". The last letter of the string can be arbitrary. In the third example occurrences of string t are overlapping. String s with maximal number of occurrences of t is "abcabcab".
def prefix(st): t = 0 p = [0] * (len(st) + 1) o = [0] * (len(st) + 1) for i in range(2, len(st)): while t > 0 and st[i] != st[t + 1]: t = p[t] if st[i] == st[t + 1]: t += 1 p[i] = t while t > 0: o[t] = 1 t = p[t] return o s = " " + input() t = " " + input() o = prefix(t) m = len(t) - 1 ans = [[0, 0] for _ in range(len(s) + 5)] ans[0][1] = float("-inf") for i in range(1, len(s)): j = m ans[i][1] = float("-inf") for j in range(m, 0, -1): if s[i - m + j] != "?" and s[i - m + j] != t[j]: break if o[j - 1]: ans[i][1] = max(ans[i][1], ans[i - m + j - 1][1] + 1) if j == 1: ans[i][1] = max(ans[i][1], ans[i - m][0] + 1) ans[i][0] = max(ans[i][1], ans[i - 1][0]) if ans[len(s) - 1][0] == 7: print(o.count(1)) else: print(ans[len(s) - 1][0])
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR STRING VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER
Alice and Bob are playing a game using a string S of length N. They both have their individual strings which are initially empty. Both players take alternate turns. Alice starts first. In Alice's turn, she will: Choose a prefix of S; Remove the chosen prefix from S; Append the prefix to the end of her string. In Bob's turn, he will: Choose a suffix of S; Remove the chosen suffix from S; Reverse the suffix and append it to the end of his string. Chef has decided to reward them if the length of the *Longest Common Subsequence* (LCS) of Alice's and Bob's strings is maximized. Help Chef calculate the length of maximum LCS that can be achieved by Alice and Bob. Note: A prefix is obtained by deleting some (possibly zero) elements from the end of the string. A suffix is obtained by deleting some (possibly zero) elements from the beginning of the string. Please use fast I/O for input and pypy for python submissions. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines, the first line contains a single integer N denoting the length of the original string. - The second line contains the original string S. ------ Output Format ------ For each test case, output the length of the maximum LCS achievable by Alice and Bob. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ - The sum of $N$ over all test cases won't exceed $5000$. ----- Sample Input 1 ------ 3 4 abab 6 abccda 4 aaaa ----- Sample Output 1 ------ 1 2 2 ----- explanation 1 ------ Test case $1$: In Alice's turn, she can pick the prefix $S[1, 2] =$ ab, and append it to her string. Thus, the remaining string is ab. In Bob's turn, he can pick the suffix $S[1, 2] =$ ab, reverse it, and append to his string. Thus, Alice's string is ab, and, Bob's string is ba. The length of the longest common subsequence in these two strings is $1$. Test case $2$: In Alice's turn, she can pick the prefix $S[1, 3] =$ abc, and append it to her string. Thus, the remaining string is cda. In Bob's turn, he can pick the suffix $S[1, 3] =$ cda, reverse it, and append to his string. Thus, Alice's string is abc, and, Bob's string is adc. The length of the longest common subsequence in these two strings is $2$. Test case $3$: In Alice's turn, she can pick the prefix $S[1, 2] =$ aa, and append it to her string. Thus, the remaining string is aa. In Bob's turn, he can pick the suffix $S[1, 2] =$ aa, reverse it, and append to his string. Thus, Alice's string is aa, and, Bob's string is aa. The length of the longest common subsequence in these two strings is $2$.
def solve(s, s1): mx = 0 l = [([0] * (n + 1)) for i in range(n + 1)] for i in range(1, n + 1): for j in range(1, n - i + 2): if s[i - 1] == s1[j - 1]: l[i][j] = 1 + l[i - 1][j - 1] else: l[i][j] = max(l[i - 1][j], l[i][j - 1]) if i + j == n: mx = max(mx, l[i][j]) print(mx) for _ in range(int(input())): n = int(input()) s = input() s1 = "".join(reversed(s)) solve(s, s1)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Alice and Bob are playing a game using a string S of length N. They both have their individual strings which are initially empty. Both players take alternate turns. Alice starts first. In Alice's turn, she will: Choose a prefix of S; Remove the chosen prefix from S; Append the prefix to the end of her string. In Bob's turn, he will: Choose a suffix of S; Remove the chosen suffix from S; Reverse the suffix and append it to the end of his string. Chef has decided to reward them if the length of the *Longest Common Subsequence* (LCS) of Alice's and Bob's strings is maximized. Help Chef calculate the length of maximum LCS that can be achieved by Alice and Bob. Note: A prefix is obtained by deleting some (possibly zero) elements from the end of the string. A suffix is obtained by deleting some (possibly zero) elements from the beginning of the string. Please use fast I/O for input and pypy for python submissions. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines, the first line contains a single integer N denoting the length of the original string. - The second line contains the original string S. ------ Output Format ------ For each test case, output the length of the maximum LCS achievable by Alice and Bob. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ - The sum of $N$ over all test cases won't exceed $5000$. ----- Sample Input 1 ------ 3 4 abab 6 abccda 4 aaaa ----- Sample Output 1 ------ 1 2 2 ----- explanation 1 ------ Test case $1$: In Alice's turn, she can pick the prefix $S[1, 2] =$ ab, and append it to her string. Thus, the remaining string is ab. In Bob's turn, he can pick the suffix $S[1, 2] =$ ab, reverse it, and append to his string. Thus, Alice's string is ab, and, Bob's string is ba. The length of the longest common subsequence in these two strings is $1$. Test case $2$: In Alice's turn, she can pick the prefix $S[1, 3] =$ abc, and append it to her string. Thus, the remaining string is cda. In Bob's turn, he can pick the suffix $S[1, 3] =$ cda, reverse it, and append to his string. Thus, Alice's string is abc, and, Bob's string is adc. The length of the longest common subsequence in these two strings is $2$. Test case $3$: In Alice's turn, she can pick the prefix $S[1, 2] =$ aa, and append it to her string. Thus, the remaining string is aa. In Bob's turn, he can pick the suffix $S[1, 2] =$ aa, reverse it, and append to his string. Thus, Alice's string is aa, and, Bob's string is aa. The length of the longest common subsequence in these two strings is $2$.
def lcs(s1, s2): m, n = len(s1), len(s2) prev, cur = [0] * (n + 1), [0] * (n + 1) for i in range(1, m + 1): for j in range(1, n + 1): if s1[i - 1] == s2[j - 1]: cur[j] = 1 + prev[j - 1] elif cur[j - 1] > prev[j]: cur[j] = cur[j - 1] else: cur[j] = prev[j] cur, prev = prev, cur return prev[n] for i in range(int(input())): c = int(input()) b = input() f = b[::-1] g = c print(lcs(b, b[::-1]) // 2)
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER
Alice and Bob are playing a game using a string S of length N. They both have their individual strings which are initially empty. Both players take alternate turns. Alice starts first. In Alice's turn, she will: Choose a prefix of S; Remove the chosen prefix from S; Append the prefix to the end of her string. In Bob's turn, he will: Choose a suffix of S; Remove the chosen suffix from S; Reverse the suffix and append it to the end of his string. Chef has decided to reward them if the length of the *Longest Common Subsequence* (LCS) of Alice's and Bob's strings is maximized. Help Chef calculate the length of maximum LCS that can be achieved by Alice and Bob. Note: A prefix is obtained by deleting some (possibly zero) elements from the end of the string. A suffix is obtained by deleting some (possibly zero) elements from the beginning of the string. Please use fast I/O for input and pypy for python submissions. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines, the first line contains a single integer N denoting the length of the original string. - The second line contains the original string S. ------ Output Format ------ For each test case, output the length of the maximum LCS achievable by Alice and Bob. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ - The sum of $N$ over all test cases won't exceed $5000$. ----- Sample Input 1 ------ 3 4 abab 6 abccda 4 aaaa ----- Sample Output 1 ------ 1 2 2 ----- explanation 1 ------ Test case $1$: In Alice's turn, she can pick the prefix $S[1, 2] =$ ab, and append it to her string. Thus, the remaining string is ab. In Bob's turn, he can pick the suffix $S[1, 2] =$ ab, reverse it, and append to his string. Thus, Alice's string is ab, and, Bob's string is ba. The length of the longest common subsequence in these two strings is $1$. Test case $2$: In Alice's turn, she can pick the prefix $S[1, 3] =$ abc, and append it to her string. Thus, the remaining string is cda. In Bob's turn, he can pick the suffix $S[1, 3] =$ cda, reverse it, and append to his string. Thus, Alice's string is abc, and, Bob's string is adc. The length of the longest common subsequence in these two strings is $2$. Test case $3$: In Alice's turn, she can pick the prefix $S[1, 2] =$ aa, and append it to her string. Thus, the remaining string is aa. In Bob's turn, he can pick the suffix $S[1, 2] =$ aa, reverse it, and append to his string. Thus, Alice's string is aa, and, Bob's string is aa. The length of the longest common subsequence in these two strings is $2$.
def lps(s): n = len(s) a = [0] * n for i in range(n - 1, -1, -1): back_up = 0 for j in range(i, n): if j == i: a[j] = 1 elif s[i] == s[j]: temp = a[j] a[j] = back_up + 2 back_up = temp else: back_up = a[j] a[j] = max(a[j - 1], a[j]) return a[n - 1] for aa in range(int(input())): n = int(input()) s = input() d = {} for i in s: if i in d: d[i] += 1 else: d[i] = 1 a = "" for i in s: if d[i] > 1: a += i s = a[:] n = len(s) if n == 0: print(0) continue if n <= 3: print(1) continue k = s[::-1] x = [] u = lps(s) print(u // 2)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Alice and Bob are playing a game using a string S of length N. They both have their individual strings which are initially empty. Both players take alternate turns. Alice starts first. In Alice's turn, she will: Choose a prefix of S; Remove the chosen prefix from S; Append the prefix to the end of her string. In Bob's turn, he will: Choose a suffix of S; Remove the chosen suffix from S; Reverse the suffix and append it to the end of his string. Chef has decided to reward them if the length of the *Longest Common Subsequence* (LCS) of Alice's and Bob's strings is maximized. Help Chef calculate the length of maximum LCS that can be achieved by Alice and Bob. Note: A prefix is obtained by deleting some (possibly zero) elements from the end of the string. A suffix is obtained by deleting some (possibly zero) elements from the beginning of the string. Please use fast I/O for input and pypy for python submissions. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines, the first line contains a single integer N denoting the length of the original string. - The second line contains the original string S. ------ Output Format ------ For each test case, output the length of the maximum LCS achievable by Alice and Bob. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ - The sum of $N$ over all test cases won't exceed $5000$. ----- Sample Input 1 ------ 3 4 abab 6 abccda 4 aaaa ----- Sample Output 1 ------ 1 2 2 ----- explanation 1 ------ Test case $1$: In Alice's turn, she can pick the prefix $S[1, 2] =$ ab, and append it to her string. Thus, the remaining string is ab. In Bob's turn, he can pick the suffix $S[1, 2] =$ ab, reverse it, and append to his string. Thus, Alice's string is ab, and, Bob's string is ba. The length of the longest common subsequence in these two strings is $1$. Test case $2$: In Alice's turn, she can pick the prefix $S[1, 3] =$ abc, and append it to her string. Thus, the remaining string is cda. In Bob's turn, he can pick the suffix $S[1, 3] =$ cda, reverse it, and append to his string. Thus, Alice's string is abc, and, Bob's string is adc. The length of the longest common subsequence in these two strings is $2$. Test case $3$: In Alice's turn, she can pick the prefix $S[1, 2] =$ aa, and append it to her string. Thus, the remaining string is aa. In Bob's turn, he can pick the suffix $S[1, 2] =$ aa, reverse it, and append to his string. Thus, Alice's string is aa, and, Bob's string is aa. The length of the longest common subsequence in these two strings is $2$.
for t in range(int(input())): def func(s: str): dp = [([0] * len(s)) for _ in range(len(s))] for k in range(len(s)): for i in range(len(s) - k): j = k + i if i == j: dp[i][j] = 1 elif s[i] == s[j]: dp[i][j] = dp[i + 1][j - 1] + 2 else: dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]) return dp[0][-1] N = int(input()) S = input() print(func(S) // 2)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER
Alice and Bob are playing a game using a string S of length N. They both have their individual strings which are initially empty. Both players take alternate turns. Alice starts first. In Alice's turn, she will: Choose a prefix of S; Remove the chosen prefix from S; Append the prefix to the end of her string. In Bob's turn, he will: Choose a suffix of S; Remove the chosen suffix from S; Reverse the suffix and append it to the end of his string. Chef has decided to reward them if the length of the *Longest Common Subsequence* (LCS) of Alice's and Bob's strings is maximized. Help Chef calculate the length of maximum LCS that can be achieved by Alice and Bob. Note: A prefix is obtained by deleting some (possibly zero) elements from the end of the string. A suffix is obtained by deleting some (possibly zero) elements from the beginning of the string. Please use fast I/O for input and pypy for python submissions. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines, the first line contains a single integer N denoting the length of the original string. - The second line contains the original string S. ------ Output Format ------ For each test case, output the length of the maximum LCS achievable by Alice and Bob. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ - The sum of $N$ over all test cases won't exceed $5000$. ----- Sample Input 1 ------ 3 4 abab 6 abccda 4 aaaa ----- Sample Output 1 ------ 1 2 2 ----- explanation 1 ------ Test case $1$: In Alice's turn, she can pick the prefix $S[1, 2] =$ ab, and append it to her string. Thus, the remaining string is ab. In Bob's turn, he can pick the suffix $S[1, 2] =$ ab, reverse it, and append to his string. Thus, Alice's string is ab, and, Bob's string is ba. The length of the longest common subsequence in these two strings is $1$. Test case $2$: In Alice's turn, she can pick the prefix $S[1, 3] =$ abc, and append it to her string. Thus, the remaining string is cda. In Bob's turn, he can pick the suffix $S[1, 3] =$ cda, reverse it, and append to his string. Thus, Alice's string is abc, and, Bob's string is adc. The length of the longest common subsequence in these two strings is $2$. Test case $3$: In Alice's turn, she can pick the prefix $S[1, 2] =$ aa, and append it to her string. Thus, the remaining string is aa. In Bob's turn, he can pick the suffix $S[1, 2] =$ aa, reverse it, and append to his string. Thus, Alice's string is aa, and, Bob's string is aa. The length of the longest common subsequence in these two strings is $2$.
t = int(input()) def solve(n, x): a, b = x, "".join(reversed(list(x))) dp = [([0] * (n + 1)) for i in range(n + 1)] mx = 0 for i in range(1, n + 1): for j in range(1, n - i + 2): if a[i - 1] == b[j - 1]: dp[i][j] = dp[i - 1][j - 1] + 1 else: dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) if i + j == n: mx = max(mx, dp[i][j]) return mx for _ in range(t): n = int(input()) x = input() print(solve(n, x))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Alice and Bob are playing a game using a string S of length N. They both have their individual strings which are initially empty. Both players take alternate turns. Alice starts first. In Alice's turn, she will: Choose a prefix of S; Remove the chosen prefix from S; Append the prefix to the end of her string. In Bob's turn, he will: Choose a suffix of S; Remove the chosen suffix from S; Reverse the suffix and append it to the end of his string. Chef has decided to reward them if the length of the *Longest Common Subsequence* (LCS) of Alice's and Bob's strings is maximized. Help Chef calculate the length of maximum LCS that can be achieved by Alice and Bob. Note: A prefix is obtained by deleting some (possibly zero) elements from the end of the string. A suffix is obtained by deleting some (possibly zero) elements from the beginning of the string. Please use fast I/O for input and pypy for python submissions. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines, the first line contains a single integer N denoting the length of the original string. - The second line contains the original string S. ------ Output Format ------ For each test case, output the length of the maximum LCS achievable by Alice and Bob. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ - The sum of $N$ over all test cases won't exceed $5000$. ----- Sample Input 1 ------ 3 4 abab 6 abccda 4 aaaa ----- Sample Output 1 ------ 1 2 2 ----- explanation 1 ------ Test case $1$: In Alice's turn, she can pick the prefix $S[1, 2] =$ ab, and append it to her string. Thus, the remaining string is ab. In Bob's turn, he can pick the suffix $S[1, 2] =$ ab, reverse it, and append to his string. Thus, Alice's string is ab, and, Bob's string is ba. The length of the longest common subsequence in these two strings is $1$. Test case $2$: In Alice's turn, she can pick the prefix $S[1, 3] =$ abc, and append it to her string. Thus, the remaining string is cda. In Bob's turn, he can pick the suffix $S[1, 3] =$ cda, reverse it, and append to his string. Thus, Alice's string is abc, and, Bob's string is adc. The length of the longest common subsequence in these two strings is $2$. Test case $3$: In Alice's turn, she can pick the prefix $S[1, 2] =$ aa, and append it to her string. Thus, the remaining string is aa. In Bob's turn, he can pick the suffix $S[1, 2] =$ aa, reverse it, and append to his string. Thus, Alice's string is aa, and, Bob's string is aa. The length of the longest common subsequence in these two strings is $2$.
def lcs(X, Y): m = n = len(Y) mx = 0 L = [([0] * (n + 1)) for i in range(m + 1)] for i in range(1, m + 1): for j in range(1, n - i + 2): if i == 0 or j == 0: L[i][j] = 0 elif X[i - 1] == Y[j - 1]: L[i][j] = L[i - 1][j - 1] + 1 else: L[i][j] = max(L[i - 1][j], L[i][j - 1]) if i + j == n: mx = max(mx, L[i][j]) return mx for _ in range(int(input())): n = int(input()) l = input() s = l[::-1] print(lcs(l, s))
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Alice and Bob are playing a game using a string S of length N. They both have their individual strings which are initially empty. Both players take alternate turns. Alice starts first. In Alice's turn, she will: Choose a prefix of S; Remove the chosen prefix from S; Append the prefix to the end of her string. In Bob's turn, he will: Choose a suffix of S; Remove the chosen suffix from S; Reverse the suffix and append it to the end of his string. Chef has decided to reward them if the length of the *Longest Common Subsequence* (LCS) of Alice's and Bob's strings is maximized. Help Chef calculate the length of maximum LCS that can be achieved by Alice and Bob. Note: A prefix is obtained by deleting some (possibly zero) elements from the end of the string. A suffix is obtained by deleting some (possibly zero) elements from the beginning of the string. Please use fast I/O for input and pypy for python submissions. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines, the first line contains a single integer N denoting the length of the original string. - The second line contains the original string S. ------ Output Format ------ For each test case, output the length of the maximum LCS achievable by Alice and Bob. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ - The sum of $N$ over all test cases won't exceed $5000$. ----- Sample Input 1 ------ 3 4 abab 6 abccda 4 aaaa ----- Sample Output 1 ------ 1 2 2 ----- explanation 1 ------ Test case $1$: In Alice's turn, she can pick the prefix $S[1, 2] =$ ab, and append it to her string. Thus, the remaining string is ab. In Bob's turn, he can pick the suffix $S[1, 2] =$ ab, reverse it, and append to his string. Thus, Alice's string is ab, and, Bob's string is ba. The length of the longest common subsequence in these two strings is $1$. Test case $2$: In Alice's turn, she can pick the prefix $S[1, 3] =$ abc, and append it to her string. Thus, the remaining string is cda. In Bob's turn, he can pick the suffix $S[1, 3] =$ cda, reverse it, and append to his string. Thus, Alice's string is abc, and, Bob's string is adc. The length of the longest common subsequence in these two strings is $2$. Test case $3$: In Alice's turn, she can pick the prefix $S[1, 2] =$ aa, and append it to her string. Thus, the remaining string is aa. In Bob's turn, he can pick the suffix $S[1, 2] =$ aa, reverse it, and append to his string. Thus, Alice's string is aa, and, Bob's string is aa. The length of the longest common subsequence in these two strings is $2$.
def solve(s, n): dp = [([0] * n) for _ in range(n)] for i in range(n - 1, -1, -1): dp[i][i] = 1 for j in range(i + 1, n): if s[i] == s[j]: dp[i][j] = dp[i + 1][j - 1] + 2 else: dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]) return dp[0][n - 1] // 2 t = int(input()) for _ in range(t): n = int(input()) s = input() result = solve(s, n) print(result)
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game using a string S of length N. They both have their individual strings which are initially empty. Both players take alternate turns. Alice starts first. In Alice's turn, she will: Choose a prefix of S; Remove the chosen prefix from S; Append the prefix to the end of her string. In Bob's turn, he will: Choose a suffix of S; Remove the chosen suffix from S; Reverse the suffix and append it to the end of his string. Chef has decided to reward them if the length of the *Longest Common Subsequence* (LCS) of Alice's and Bob's strings is maximized. Help Chef calculate the length of maximum LCS that can be achieved by Alice and Bob. Note: A prefix is obtained by deleting some (possibly zero) elements from the end of the string. A suffix is obtained by deleting some (possibly zero) elements from the beginning of the string. Please use fast I/O for input and pypy for python submissions. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines, the first line contains a single integer N denoting the length of the original string. - The second line contains the original string S. ------ Output Format ------ For each test case, output the length of the maximum LCS achievable by Alice and Bob. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 5000$ - The sum of $N$ over all test cases won't exceed $5000$. ----- Sample Input 1 ------ 3 4 abab 6 abccda 4 aaaa ----- Sample Output 1 ------ 1 2 2 ----- explanation 1 ------ Test case $1$: In Alice's turn, she can pick the prefix $S[1, 2] =$ ab, and append it to her string. Thus, the remaining string is ab. In Bob's turn, he can pick the suffix $S[1, 2] =$ ab, reverse it, and append to his string. Thus, Alice's string is ab, and, Bob's string is ba. The length of the longest common subsequence in these two strings is $1$. Test case $2$: In Alice's turn, she can pick the prefix $S[1, 3] =$ abc, and append it to her string. Thus, the remaining string is cda. In Bob's turn, he can pick the suffix $S[1, 3] =$ cda, reverse it, and append to his string. Thus, Alice's string is abc, and, Bob's string is adc. The length of the longest common subsequence in these two strings is $2$. Test case $3$: In Alice's turn, she can pick the prefix $S[1, 2] =$ aa, and append it to her string. Thus, the remaining string is aa. In Bob's turn, he can pick the suffix $S[1, 2] =$ aa, reverse it, and append to his string. Thus, Alice's string is aa, and, Bob's string is aa. The length of the longest common subsequence in these two strings is $2$.
def solve(n, s): s = "!" + s + "!" dp = [([0] * (n + 2)) for i in range(n + 2)] for i in range(1, n + 1): for j in reversed(range(i + 1, n + 1)): dp[i][j] = max(dp[i - 1][j], dp[i][j + 1]) if s[i] == s[j]: dp[i][j] = max(dp[i][j], dp[i - 1][j + 1] + 1) ans = max([dp[i][i + 1] for i in range(1, n)]) return ans t = int(input()) for _ in range(t): n = int(input()) s = input() print(solve(n, s))
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP STRING VAR STRING ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef invented a new game named Quick Battle Royale. $N$ people play the game. One or more rounds are played until all players except one are eliminated. In each round: Let there be $x$ non-eliminated players at the start of the round. Let's number them $1$ through $x$. First, each of these players chooses an integer between $1$ and $x$ inclusive. For each valid $i$, let's denote the integer chosen by the $i$-th player by $p_{i}$. In addition, each player is not allowed to choose $p_{i} = i$. Under these conditions, all players choose uniformly randomly and independently. Then, the elimination process starts. For each $i$ from $1$ to $x$, in this order: - if the player $i$ is currently still in the game, the player $p_{i}$ is eliminated from the game immediately - if the player $i$ was already eliminated from the game in this round, nothing happens Note that it is impossible for all players to be eliminated and in each round, at least one player is eliminated. For example, a game for $N = 4$ could look like this: Round 1: The players choose $p_{1} = 3$, $p_{2} = 4$, $p_{3} = 2$, $p_{4} = 2$. For $i = 1$, since player $1$ is still in the game, player $p_{i} = 3$ is eliminated. For $i = 2$, since player $2$ is also in the game, player $p_{i} = 4$ is eliminated. Since the players $3$ and $4$ are eliminated, nothing more happens. Round 2: Two players remain. The only choice they have is $p_{1} = 2$ and $p_{2} = 1$. For $i = 1$, since player $1$ is still in the game, player $p_{i} = 2$ gets eliminated. For $i = 2$, player $2$ is eliminated, so nothing happens. Now there is one player left, so the game ends. It lasted $2$ rounds. You have to find the expected value of the number of rounds the game will last. It can be shown that the expected number of rounds can be written as a fraction $\frac{p}{q}$, where $p$ and $q$ are positive integers and $q$ is coprime with $10^{9}+7$. You have to compute $p \cdot q^{-1}$ modulo $10^{9} + 7$, where $q^{-1}$ is the multiplicative inverse of $q$ modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single integer $N$ denoting the number of players at the start of the game. ------ Output ------ For each test case, print a single line containing one integer $p \cdot q^{-1}$ modulo $10^{9} + 7$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 100$ ------ Example Input ------ 5 1 2 3 4 5 ------ Example Output ------ 0 1 500000005 944444453 616319451 ------ Explanation ------ Example case 3: The answer is $\frac{3}{2}$ modulo $(10^{9} + 7)$. The following cases can happen in the first round (with equal probabilities): - $p = [2, 1, 1]$, game lasts $1$ round - $p = [2, 1, 2]$, game lasts $2$ rounds - $p = [2, 3, 1]$, game lasts $1$ round - $p = [2, 3, 2]$, game lasts $2$ rounds - $p = [3, 1, 1]$, game lasts $1$ round - $p = [3, 1, 2]$, game lasts $1$ round - $p = [3, 3, 1]$, game lasts $2$ rounds - $p = [3, 3, 2]$, game lasts $2$ rounds Example case 4: The answer is $\frac{35}{18}$ modulo $(10^{9} + 7)$.
import sys import time start_time = time.time() try: sys.stdin = open("input.txt", "r") except: pass input = sys.stdin.readline T = int(input()) for _ in range(T): n = int(input()) a = [ 0, 0, 1, 500000005, 944444453, 616319451, 9519447, 209734212, 296383343, 628293695, 892973932, 356721615, 785855324, 361737272, 967345863, 679451161, 415860585, 729060713, 431984843, 457364455, 542034172, 964679885, 566502625, 496065745, 108329251, 662984474, 307846612, 855014714, 66146985, 119254357, 257802637, 264701451, 930559986, 678191641, 905607334, 467527512, 203751573, 986993074, 735484901, 721694625, 386461279, 143328353, 143483369, 151698607, 815501106, 51568266, 922529840, 806686677, 484850440, 357612704, 546890204, 138199580, 130236504, 830343136, 340009752, 941400553, 80181583, 182166649, 721756903, 728722095, 75215877, 743127269, 416469302, 733330112, 172934432, 547360246, 554523576, 85034297, 391690454, 192161704, 687810348, 10407221, 972866134, 12559159, 748575548, 159010395, 864978181, 618228776, 141543901, 354755749, 512799621, 43063918, 412889615, 772528261, 925918779, 519433705, 111930618, 826761812, 792022244, 50186129, 485279643, 434459903, 510999110, 535106533, 516360947, 745278364, 451364722, 507798590, 233323591, 104875659, 99043332, ] print(a[n]) end_time = time.time() sys.stderr.write("Time: " + str(end_time - start_time))
IMPORT IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR BIN_OP VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well. Chef invented a new game named Quick Battle Royale. $N$ people play the game. One or more rounds are played until all players except one are eliminated. In each round: Let there be $x$ non-eliminated players at the start of the round. Let's number them $1$ through $x$. First, each of these players chooses an integer between $1$ and $x$ inclusive. For each valid $i$, let's denote the integer chosen by the $i$-th player by $p_{i}$. In addition, each player is not allowed to choose $p_{i} = i$. Under these conditions, all players choose uniformly randomly and independently. Then, the elimination process starts. For each $i$ from $1$ to $x$, in this order: - if the player $i$ is currently still in the game, the player $p_{i}$ is eliminated from the game immediately - if the player $i$ was already eliminated from the game in this round, nothing happens Note that it is impossible for all players to be eliminated and in each round, at least one player is eliminated. For example, a game for $N = 4$ could look like this: Round 1: The players choose $p_{1} = 3$, $p_{2} = 4$, $p_{3} = 2$, $p_{4} = 2$. For $i = 1$, since player $1$ is still in the game, player $p_{i} = 3$ is eliminated. For $i = 2$, since player $2$ is also in the game, player $p_{i} = 4$ is eliminated. Since the players $3$ and $4$ are eliminated, nothing more happens. Round 2: Two players remain. The only choice they have is $p_{1} = 2$ and $p_{2} = 1$. For $i = 1$, since player $1$ is still in the game, player $p_{i} = 2$ gets eliminated. For $i = 2$, player $2$ is eliminated, so nothing happens. Now there is one player left, so the game ends. It lasted $2$ rounds. You have to find the expected value of the number of rounds the game will last. It can be shown that the expected number of rounds can be written as a fraction $\frac{p}{q}$, where $p$ and $q$ are positive integers and $q$ is coprime with $10^{9}+7$. You have to compute $p \cdot q^{-1}$ modulo $10^{9} + 7$, where $q^{-1}$ is the multiplicative inverse of $q$ modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains a single integer $N$ denoting the number of players at the start of the game. ------ Output ------ For each test case, print a single line containing one integer $p \cdot q^{-1}$ modulo $10^{9} + 7$. ------ Constraints ------ $1 ≤ T ≤ 100$ $2 ≤ N ≤ 100$ ------ Example Input ------ 5 1 2 3 4 5 ------ Example Output ------ 0 1 500000005 944444453 616319451 ------ Explanation ------ Example case 3: The answer is $\frac{3}{2}$ modulo $(10^{9} + 7)$. The following cases can happen in the first round (with equal probabilities): - $p = [2, 1, 1]$, game lasts $1$ round - $p = [2, 1, 2]$, game lasts $2$ rounds - $p = [2, 3, 1]$, game lasts $1$ round - $p = [2, 3, 2]$, game lasts $2$ rounds - $p = [3, 1, 1]$, game lasts $1$ round - $p = [3, 1, 2]$, game lasts $1$ round - $p = [3, 3, 1]$, game lasts $2$ rounds - $p = [3, 3, 2]$, game lasts $2$ rounds Example case 4: The answer is $\frac{35}{18}$ modulo $(10^{9} + 7)$.
for T in range(int(input())): N = int(input()) a = [ 0, 0, 1, 500000005, 944444453, 616319451, 9519447, 209734212, 296383343, 628293695, 892973932, 356721615, 785855324, 361737272, 967345863, 679451161, 415860585, 729060713, 431984843, 457364455, 542034172, 964679885, 566502625, 496065745, 108329251, 662984474, 307846612, 855014714, 66146985, 119254357, 257802637, 264701451, 930559986, 678191641, 905607334, 467527512, 203751573, 986993074, 735484901, 721694625, 386461279, 143328353, 143483369, 151698607, 815501106, 51568266, 922529840, 806686677, 484850440, 357612704, 546890204, 138199580, 130236504, 830343136, 340009752, 941400553, 80181583, 182166649, 721756903, 728722095, 75215877, 743127269, 416469302, 733330112, 172934432, 547360246, 554523576, 85034297, 391690454, 192161704, 687810348, 10407221, 972866134, 12559159, 748575548, 159010395, 864978181, 618228776, 141543901, 354755749, 512799621, 43063918, 412889615, 772528261, 925918779, 519433705, 111930618, 826761812, 792022244, 50186129, 485279643, 434459903, 510999110, 535106533, 516360947, 745278364, 451364722, 507798590, 233323591, 104875659, 99043332, ] print(a[N])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR
You have 2 counters a and b, both initialised as 0. You also have a score initialised as 0. At the start of every minute from minute 1 to minute N, you must increase either a or b by 1. Additionally, there are M events. The i^{th} event happens at minute T_{i}+0.5 and has type C_{i} (C_{i} \in \{1, 2\}). At each event, the counters or the score is updated as: if (type == 1): if (a > b): score = score + 1; else: a = 0; b = 0; else: if (a < b): score = score + 1; else: a = 0; b = 0; Your task is to maximise the score after N minutes have passed. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M — the number of minutes and the number of events. - The second line of each test case contains M integers T_{1},T_{2},\ldots,T_{M} — where the i^{th} event happens at minute T_{i} + 0.5. - The third line of each test case contains M integers C_{1},C_{2},\ldots,C_{M} — the type of the i^{th} event. ------ Output Format ------ For each test case, output on a new line, the maximum score after N minutes have passed. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ M ≤ N ≤ 10^{5}$ $1 ≤ T_{1} < T_{2} < \ldots < T_{M} ≤ N$ $C_{i} \in \{1,2\}$ - The sum of $N$ over all test cases will not exceed $10^{5}$. ----- Sample Input 1 ------ 4 5 3 1 2 5 1 2 1 5 5 1 2 3 4 5 1 1 1 1 1 5 5 1 2 3 4 5 1 2 1 2 1 1 1 1 2 ----- Sample Output 1 ------ 2 5 3 1 ----- explanation 1 ------ Test case $1$: We will demonstrate a way to achieve a score of $2$. - In the first minute, we will increase $a$ by $1$ so that $a=1$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. - In the second minute, we will increase $b$ by $1$ so that $a=1$ and $b=1$. Since there is an event with type $2$ and $a$ is not less than $b$, our score remains $1$ and the counters are reset. - In the third minute, we will increase $a$ by $1$ so that $a=1$ and $b=0$. There are no events in this minute. - In the fourth minute, we will increase $a$ by $1$ so that $a=2$ and $b=0$. There are no events in this minute. - In the fifth minute, we will increase $a$ by $1$ so that $a=3$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. Thus, the total score achieved is $2$. It can be shown that there is no possible way for our score to be greater than $2$. Test case $2$: We will demonstrate a way to achieve a score of $5$. - In the first minute, we will increase $a$ by $1$ so that $a=1$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. - In the second minute, we will increase $a$ by $1$ so that $a=2$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. - In the third minute, we will increase $a$ by $1$ so that $a=3$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. - In the fourth minute, we will increase $a$ by $1$ so that $a=4$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. - In the fifth minute, we will increase $a$ by $1$ so that $a=5$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. Thus, the total score achieved is $5$. It can be shown that there is no possible way for our score to be greater than $5$.
for _ in range(int(input())): n, m = map(int, input().split()) t = list(map(int, input().split())) c = list(map(int, input().split())) d = [[-3, -3, -3, -3, -3] for i in range(n + 1)] d[0][2] = 0 idx = 0 for i in range(1, n + 1): d[i][0] = max(d[i][0], d[i - 1][1]) d[i][1] = max(d[i][1], d[i - 1][0], d[i - 1][2]) d[i][2] = max(d[i - 1][1], d[i][2], d[i - 1][3]) d[i][3] = max(d[i - 1][2], d[i - 1][4], d[i][3]) d[i][4] = max(d[i][4], d[i - 1][3]) if idx < m and t[idx] == i: if c[idx] == 1: d[i][4] += 1 d[i][3] += 1 d[i][2] = max(d[i][2], d[i][1], d[i][0]) d[i][1] = -3 d[i][0] = -3 else: d[i][0] += 1 d[i][1] += 1 d[i][2] = max(d[i][2], d[i][3], d[i][4]) d[i][3] = -3 d[i][4] = -3 idx += 1 print(max(d[n][0], d[n][1], d[n][2], d[n][3], d[n][4]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER
You have 2 counters a and b, both initialised as 0. You also have a score initialised as 0. At the start of every minute from minute 1 to minute N, you must increase either a or b by 1. Additionally, there are M events. The i^{th} event happens at minute T_{i}+0.5 and has type C_{i} (C_{i} \in \{1, 2\}). At each event, the counters or the score is updated as: if (type == 1): if (a > b): score = score + 1; else: a = 0; b = 0; else: if (a < b): score = score + 1; else: a = 0; b = 0; Your task is to maximise the score after N minutes have passed. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M — the number of minutes and the number of events. - The second line of each test case contains M integers T_{1},T_{2},\ldots,T_{M} — where the i^{th} event happens at minute T_{i} + 0.5. - The third line of each test case contains M integers C_{1},C_{2},\ldots,C_{M} — the type of the i^{th} event. ------ Output Format ------ For each test case, output on a new line, the maximum score after N minutes have passed. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ M ≤ N ≤ 10^{5}$ $1 ≤ T_{1} < T_{2} < \ldots < T_{M} ≤ N$ $C_{i} \in \{1,2\}$ - The sum of $N$ over all test cases will not exceed $10^{5}$. ----- Sample Input 1 ------ 4 5 3 1 2 5 1 2 1 5 5 1 2 3 4 5 1 1 1 1 1 5 5 1 2 3 4 5 1 2 1 2 1 1 1 1 2 ----- Sample Output 1 ------ 2 5 3 1 ----- explanation 1 ------ Test case $1$: We will demonstrate a way to achieve a score of $2$. - In the first minute, we will increase $a$ by $1$ so that $a=1$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. - In the second minute, we will increase $b$ by $1$ so that $a=1$ and $b=1$. Since there is an event with type $2$ and $a$ is not less than $b$, our score remains $1$ and the counters are reset. - In the third minute, we will increase $a$ by $1$ so that $a=1$ and $b=0$. There are no events in this minute. - In the fourth minute, we will increase $a$ by $1$ so that $a=2$ and $b=0$. There are no events in this minute. - In the fifth minute, we will increase $a$ by $1$ so that $a=3$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. Thus, the total score achieved is $2$. It can be shown that there is no possible way for our score to be greater than $2$. Test case $2$: We will demonstrate a way to achieve a score of $5$. - In the first minute, we will increase $a$ by $1$ so that $a=1$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. - In the second minute, we will increase $a$ by $1$ so that $a=2$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. - In the third minute, we will increase $a$ by $1$ so that $a=3$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. - In the fourth minute, we will increase $a$ by $1$ so that $a=4$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. - In the fifth minute, we will increase $a$ by $1$ so that $a=5$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. Thus, the total score achieved is $5$. It can be shown that there is no possible way for our score to be greater than $5$.
for _ in range(int(input())): n, m = [int(i) for i in input().split()] n = n + 1 t = [int(i) for i in input().split()] c = [int(i) for i in input().split()] C = {} for i in range(m): C[t[i] + 1] = c[i] dp = [[(-1000000000.0) for i in range(5)] for j in range(n + 1)] dp[0][2] = 0 for i in range(1, n + 1): if i in C: if C[i] == 1: dp[i][0] = -1000000000.0 dp[i][1] = max(dp[i - 1][2], dp[i - 1][1], dp[i - 1][0]) dp[i][2] = dp[i - 1][3] + 1 dp[i][3] = max( dp[i - 1][2], dp[i - 1][1], dp[i - 1][0], dp[i - 1][4] + 1 ) dp[i][4] = dp[i - 1][3] + 1 else: dp[i][4] = -1000000000.0 dp[i][3] = max(dp[i - 1][2], dp[i - 1][3], dp[i - 1][4]) dp[i][2] = dp[i - 1][1] + 1 dp[i][1] = max( dp[i - 1][2], dp[i - 1][3], dp[i - 1][4], dp[i - 1][0] + 1 ) dp[i][0] = dp[i - 1][1] + 1 else: for j in range(5): lst = [] if j - 1 >= 0: lst.append(dp[i - 1][j - 1]) if j + 1 <= 4: lst.append(dp[i - 1][j + 1]) dp[i][j] = max(lst) print(max(dp[n]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You have 2 counters a and b, both initialised as 0. You also have a score initialised as 0. At the start of every minute from minute 1 to minute N, you must increase either a or b by 1. Additionally, there are M events. The i^{th} event happens at minute T_{i}+0.5 and has type C_{i} (C_{i} \in \{1, 2\}). At each event, the counters or the score is updated as: if (type == 1): if (a > b): score = score + 1; else: a = 0; b = 0; else: if (a < b): score = score + 1; else: a = 0; b = 0; Your task is to maximise the score after N minutes have passed. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M — the number of minutes and the number of events. - The second line of each test case contains M integers T_{1},T_{2},\ldots,T_{M} — where the i^{th} event happens at minute T_{i} + 0.5. - The third line of each test case contains M integers C_{1},C_{2},\ldots,C_{M} — the type of the i^{th} event. ------ Output Format ------ For each test case, output on a new line, the maximum score after N minutes have passed. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ M ≤ N ≤ 10^{5}$ $1 ≤ T_{1} < T_{2} < \ldots < T_{M} ≤ N$ $C_{i} \in \{1,2\}$ - The sum of $N$ over all test cases will not exceed $10^{5}$. ----- Sample Input 1 ------ 4 5 3 1 2 5 1 2 1 5 5 1 2 3 4 5 1 1 1 1 1 5 5 1 2 3 4 5 1 2 1 2 1 1 1 1 2 ----- Sample Output 1 ------ 2 5 3 1 ----- explanation 1 ------ Test case $1$: We will demonstrate a way to achieve a score of $2$. - In the first minute, we will increase $a$ by $1$ so that $a=1$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. - In the second minute, we will increase $b$ by $1$ so that $a=1$ and $b=1$. Since there is an event with type $2$ and $a$ is not less than $b$, our score remains $1$ and the counters are reset. - In the third minute, we will increase $a$ by $1$ so that $a=1$ and $b=0$. There are no events in this minute. - In the fourth minute, we will increase $a$ by $1$ so that $a=2$ and $b=0$. There are no events in this minute. - In the fifth minute, we will increase $a$ by $1$ so that $a=3$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. Thus, the total score achieved is $2$. It can be shown that there is no possible way for our score to be greater than $2$. Test case $2$: We will demonstrate a way to achieve a score of $5$. - In the first minute, we will increase $a$ by $1$ so that $a=1$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. - In the second minute, we will increase $a$ by $1$ so that $a=2$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. - In the third minute, we will increase $a$ by $1$ so that $a=3$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. - In the fourth minute, we will increase $a$ by $1$ so that $a=4$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. - In the fifth minute, we will increase $a$ by $1$ so that $a=5$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. Thus, the total score achieved is $5$. It can be shown that there is no possible way for our score to be greater than $5$.
inf = 10**9 for _ in range(int(input())): n, m = map(int, input().split()) times = list(map(int, input().split())) types = list(map(int, input().split())) dp = [[-inf, -inf, -inf, -inf, -inf] for _ in range(n + 2)] dp[0][2] = 0 ptr = 0 for i in range(1, n + 1): for dif in range(5): if dif > 0: dp[i][dif] = max(dp[i][dif], dp[i - 1][dif - 1]) if dif < 4: dp[i][dif] = max(dp[i][dif], dp[i - 1][dif + 1]) if ptr < m and times[ptr] == i: if types[ptr] == 1: dp[i][4] += 1 dp[i][3] += 1 dp[i][2] = max(dp[i][:3]) dp[i][0] = -inf dp[i][1] = -inf else: dp[i][0] += 1 dp[i][1] += 1 dp[i][2] = max(dp[i][2:]) dp[i][3] = -inf dp[i][4] = -inf ptr += 1 print(max(dp[n]))
ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You have 2 counters a and b, both initialised as 0. You also have a score initialised as 0. At the start of every minute from minute 1 to minute N, you must increase either a or b by 1. Additionally, there are M events. The i^{th} event happens at minute T_{i}+0.5 and has type C_{i} (C_{i} \in \{1, 2\}). At each event, the counters or the score is updated as: if (type == 1): if (a > b): score = score + 1; else: a = 0; b = 0; else: if (a < b): score = score + 1; else: a = 0; b = 0; Your task is to maximise the score after N minutes have passed. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M — the number of minutes and the number of events. - The second line of each test case contains M integers T_{1},T_{2},\ldots,T_{M} — where the i^{th} event happens at minute T_{i} + 0.5. - The third line of each test case contains M integers C_{1},C_{2},\ldots,C_{M} — the type of the i^{th} event. ------ Output Format ------ For each test case, output on a new line, the maximum score after N minutes have passed. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ M ≤ N ≤ 10^{5}$ $1 ≤ T_{1} < T_{2} < \ldots < T_{M} ≤ N$ $C_{i} \in \{1,2\}$ - The sum of $N$ over all test cases will not exceed $10^{5}$. ----- Sample Input 1 ------ 4 5 3 1 2 5 1 2 1 5 5 1 2 3 4 5 1 1 1 1 1 5 5 1 2 3 4 5 1 2 1 2 1 1 1 1 2 ----- Sample Output 1 ------ 2 5 3 1 ----- explanation 1 ------ Test case $1$: We will demonstrate a way to achieve a score of $2$. - In the first minute, we will increase $a$ by $1$ so that $a=1$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. - In the second minute, we will increase $b$ by $1$ so that $a=1$ and $b=1$. Since there is an event with type $2$ and $a$ is not less than $b$, our score remains $1$ and the counters are reset. - In the third minute, we will increase $a$ by $1$ so that $a=1$ and $b=0$. There are no events in this minute. - In the fourth minute, we will increase $a$ by $1$ so that $a=2$ and $b=0$. There are no events in this minute. - In the fifth minute, we will increase $a$ by $1$ so that $a=3$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. Thus, the total score achieved is $2$. It can be shown that there is no possible way for our score to be greater than $2$. Test case $2$: We will demonstrate a way to achieve a score of $5$. - In the first minute, we will increase $a$ by $1$ so that $a=1$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. - In the second minute, we will increase $a$ by $1$ so that $a=2$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. - In the third minute, we will increase $a$ by $1$ so that $a=3$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. - In the fourth minute, we will increase $a$ by $1$ so that $a=4$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. - In the fifth minute, we will increase $a$ by $1$ so that $a=5$ and $b=0$. Since there is an event with type $1$ and $a> b$ our score increases by $1$. Thus, the total score achieved is $5$. It can be shown that there is no possible way for our score to be greater than $5$.
TT = int(input()) for tt in range(TT): n, m = map(int, input().split()) T = map(int, input().split()) C = map(int, input().split()) state = [0, -100, -100] prev_t, prev_c = 0, 0 for t, c in zip(T, C): diff = t - prev_t new_state = [-100, -100, -100] if c == prev_c: if diff % 2 == 0: v = max(state[0], state[1], state[2]) else: v = max(state[0], state[1]) if diff > 1: v = max(v, state[2]) else: v = max(state[0], state[1], state[2]) new_state[0] = max(new_state[0], v) if c == prev_c: if diff % 2 == 0: new_state[1] = max(state[1] + 1, new_state[1]) else: new_state[1] = max(state[0] + 1, state[2] + 1, new_state[1]) elif diff % 2 == 0: new_state[1] = max(state[1] + 1, new_state[1]) else: new_state[1] = max(state[0] + 1, new_state[1]) if diff > 1: new_state[1] = max(state[2] + 1, new_state[1]) if c == prev_c: if diff % 2 == 0: new_state[2] = max(state[0] + 1, state[2] + 1, new_state[2]) else: new_state[2] = max(state[1] + 1, new_state[2]) elif diff % 2 == 0: new_state[2] = max(state[0] + 1, new_state[2]) if diff > 2: new_state[2] = max(state[2] + 1, new_state[2]) elif diff > 1: new_state[2] = max(state[1] + 1, new_state[2]) prev_t = t prev_c = c state = new_state print(max(state))
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 ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER IF VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, Query): p = len(str) q = len(Query) f = ord(str[0]) - 97 a = [[(0) for i in range(26)] for j in range(max(p, q))] a[0][ord(str[0]) - 97] = 1 for i in range(1, max(p, q)): a[i] = a[i - 1].copy() if i < p: a[i][ord(str[i]) - 97] = a[i - 1][ord(str[i]) - 97] + 1 b = [] for i, j in Query: c = 0 for k in range(26): if a[j - 1][k] != a[i - 1][k] or i == 1 and k == f: c += 1 elif i != 1 and a[i - 1][k] != a[i - 2][k]: c += 1 b.append(c) return b
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, _str, Query): a = [] for _list in Query: substring = _str[_list[0] - 1 : _list[1]] a.append(len(set(substring))) return a
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, Query): res = [] for i in Query: countdistinct = helper(str, i[0] - 1, i[1]) res.append(countdistinct) return res def helper(str, i, j): dis = set() for a in str[i:j]: dis.add(a) return len(dis)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, Query): l = [] for i in Query: x = str[i[0] - 1 : i[1]] l.append(x) x = [] for i in l: s = "" for j in i: if j not in s: s += j x.append(len(s)) return x
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR STRING FOR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, Query): res = [] for i in Query: cnt = 0 s1 = str[i[0] - 1 : i[1]] p = set(s1) res.append(len(p)) return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, s, Query): l = [(0) for i in range(26)] d = dict() d[-1] = [(0) for i in range(26)] l1 = [] for i in range(len(s)): l[ord(s[i]) - ord("a")] += 1 d[i] = [i for i in l] for i in Query: a, b = i[0] - 2, i[1] - 1 x, y = d[a], d[b] count = 0 for j in range(len(x)): if y[j] - x[j] == 0: continue count = count + 1 l1.append(count) return l1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, s, q): s = list(s) c = [] for i in q: a, b = i[0], i[1] c.append(len(set(s[a - 1 : b]))) return c
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, Query): don = [] for query in Query: l = query[0] r = query[1] arr = str[l - 1 : r] don.append(len(set(arr))) return don
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, Query): lst = [] for i in Query: myDict = {} lst.append(self.fun(str, i[0] - 1, i[1] - 1, myDict)) return lst def fun(self, str, l, r, myDict): if l == r: if str[l] not in myDict: return 1 else: return 0 if str[r] not in myDict: myDict[str[r]] = 1 return 1 + self.fun(str, l, r - 1, myDict) return self.fun(str, l, r - 1, myDict)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR DICT EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR RETURN VAR FUNC_DEF IF VAR VAR IF VAR VAR VAR RETURN NUMBER RETURN NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, Query): lst = [] for i in Query: L = i[0] R = i[1] lst1 = [] for i in range(L, R + 1): if str[i - 1] not in lst1: lst1.append(str[i - 1]) lst.append(len(lst1)) return lst
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, st, Query): res = [] for q in Query: start = q[0] - 1 end = q[1] - 1 s = st[start : end + 1] new_set = set(s) res.append(len(new_set)) return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, s, Query): n = len(s) dp = [[(0) for i in range(n)] for j in range(n)] for i in range(n): f = dict() for j in range(i, n): if s[j] not in f: f[s[j]] = 1 dp[i][j] = len(f) ans = [] for q in Query: l, r = q[0], q[1] ans.append(dp[l - 1][r - 1]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, S, Query): N = len(S) dp = [[(0) for i in range(26)] for i in range(N + 1)] for i in range(N): for j in range(26): dp[i + 1][j] = dp[i][j] dp[i + 1][ord(S[i]) - ord("a")] += 1 ans = [] for l, r in Query: c = 0 for i in range(26): if dp[r][i] - dp[l - 1][i] > 0: c += 1 ans.append(c) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR LIST FOR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, Query): ans = [] for i in Query: arr = [] count = 0 for j in range(i[0] - 1, i[1]): if str[j] in arr: pass else: arr.append(str[j]) count += 1 ans.append(count) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, Query): unique_chars = [([0] * (len(str) + 1)) for _ in range(26)] for english_ch_idx in range(26): ch = chr(ord("a") + english_ch_idx) prefix_sum = 0 for idx in range(1, len(str) + 1): if str[idx - 1] == ch: prefix_sum += 1 unique_chars[english_ch_idx][idx] = prefix_sum result = [] for query in Query: uniques = 0 for ch_idx in range(26): if ( unique_chars[ch_idx][query[1]] - unique_chars[ch_idx][query[0] - 1] != 0 ): uniques += 1 result.append(uniques) return result
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, s, q): s = "a" + s maps = [{} for n in range(len(s))] temp = {} ans = [] for n in range(len(s)): if s[n] not in temp: temp[s[n]] = 0 temp[s[n]] += 1 maps[n] = temp.copy() for n in q: [a, b] = n x = maps[a - 1].copy() y = maps[b].copy() for n in x: y[n] -= x[n] res = 0 for n in y: if y[n]: res += 1 ans.append(res) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR DICT VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FOR VAR VAR ASSIGN LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, a): out = [] for i in a: out.append(len(set(str[i[0] - 1 : i[1]]))) return out
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, Query): res = [] for i in Query: count = [0] * 26 c = 0 for j in range(i[0] - 1, i[1]): if count[ord(str[j]) - ord("a")] == 0: c += 1 count[ord(str[j]) - ord("a")] += 1 res.append(c) return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, s, query): ans = [] for i, j in query: hashmap = {} count = 0 for x in s[i - 1 : j]: if hashmap.get(x) == None: hashmap[x] = 1 count += 1 ans.append(count) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, Query): r = [] for i in Query: r.append(self.findunique(str[i[0] - 1 : i[1]])) return r def findunique(self, s): d = dict() for i in s: d[i] = 0 return len(d)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, queries): length = [] for query in queries: left, right = query length.append(len(set(str[left - 1 : right]))) return length
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, Query): l = [] w = [] for i in str: l.append(i) for i in Query: k = [] g = [] for j in range(i[0] - 1, i[1]): if l[j] not in k: k.append(l[j]) w.append(len(k)) return w
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, Query): S = str ans = [] for i in range(0, len(Query)): sets = set(S[Query[i][0] - 1 : Query[i][1]]) ans.append(len(sets)) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, Query): res = [] l = len(str) dp = [[(0) for c in range(l + 1)] for r in range(26)] for r in range(26): for c in range(1, l + 1): if ord(str[c - 1]) - ord("a") == r: dp[r][c] = dp[r][c - 1] + 1 else: dp[r][c] = dp[r][c - 1] for q in Query: l = q[0] r = q[1] count = 0 for row in range(26): if dp[row][r] - dp[row][l - 1] > 0: count += 1 res.append(count) return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, Query): ans = [] for element in Query: temp = str[element[0] - 1 : element[1]] dicti = {} count = 0 for i in temp: if not i in dicti: count += 1 dicti[i] = 1 ans.append(count) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, Query): res = [] for queryi in Query: left, right = queryi[0], queryi[1] tmp = set() for i in range(left - 1, right): tmp.add(str[i]) res.append(len(tmp)) return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, Query): dp = [([0] * 26) for i in range(len(str) + 1)] n = len(str) ans = list() for i in range(1, n + 1): for j in range(26): dp[i][j] = dp[i - 1][j] dp[i][ord(str[i - 1]) - 97] = dp[i - 1][ord(str[i - 1]) - 97] + 1 ans = list() for i in Query: start, end = i[0], i[1] cnt = 0 for i in range(26): if dp[end][i] - dp[start - 1][i] > 0: cnt += 1 ans.append(cnt) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, Query): d = [] ans = [] for i in range(len(str)): p = [] d.append(p) d1 = [0] * 26 for i in range(len(str)): p = ord(str[i]) - 97 d1[p] += 1 h = d1.copy() d[i] = h for x in Query: l = x[0] - 1 r = x[1] - 1 c = 0 for i in range(26): if d[r][i] - d[l][i] > 0 or i == ord(str[l]) - 97: c += 1 ans.append(c) return ans if __name__ == "__main__": T = int(input()) for i in range(T): str = input() q = int(input()) Query = [] for i in range(q): a = list(map(int, input().split())) Query.append(a) obj = Solution() ans = obj.SolveQueris(str, Query) for _ in ans: print(_, end=" ") print()
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, string, Query): dp = [["" for i in range(len(string))] for j in range(len(string))] for i in range(len(string)): dp[i][i] = string[i] for i in range(len(string) - 1): for j in range(i + 1, len(string)): dp[i][j] = dp[i][j - 1] + ( string[j] if string[j] not in dp[i][j - 1] else "" ) ans = [] for item in Query: ans.append(len(dp[item[0] - 1][item[1] - 1])) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR STRING ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, s, Q): l1 = [] for i in Q: j, k = i x = set(list(s[j - 1 : k])) l1.append(len(x)) return l1
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, s, q): p = [] l = [0] * 26 for i in range(len(s)): l[ord(s[i]) - 97] += 1 p.append(l.copy()) ans = [] for i, j in q: c = 0 for z in range(26): if i == 1: if p[j - 1][z] > 0: c += 1 elif p[j - 1][z] - p[i - 2][z] > 0: c += 1 ans.append(c) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, s, Q): n = len(s) dp = [[(0) for i in range(26)] for k in range(n + 1)] for i in range(1, n + 1): for j in range(26): dp[i][j] = dp[i - 1][j] dp[i][ord(s[i - 1]) - ord("a")] += 1 k = [] for p in range(0, len(Q)): a = Q[p][0] b = Q[p][1] l = [(0) for i in range(26)] for i in range(26): l[i] = dp[b][i] - dp[a - 1][i] ans = 0 for i in l: if i > 0: ans += 1 k.append(ans) return k
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, string, Query): lst = [] for q in Query: l = q[0] - 1 r = q[1] if l < 0: l = 0 if r > len(string): r = len(string) s = set() for char in string[l:r]: s.add(char) n = len(s) lst.append(n) return lst
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, Query): arr = [[(0) for i in range(26)] for j in range(len(str) + 1)] for i in range(len(str)): ind = ord(str[i]) - 97 arr[i + 1][ind] = 1 for j in range(26): arr[i + 1][j] += arr[i][j] ans = [] for q in Query: a, b = q[0], q[1] total = 0 for j in range(26): total += 1 if arr[b][j] - arr[a - 1][j] > 0 else 0 ans.append(total) return ans if __name__ == "__main__": T = int(input()) for i in range(T): str = input() q = int(input()) Query = [] for i in range(q): a = list(map(int, input().split())) Query.append(a) obj = Solution() ans = obj.SolveQueris(str, Query) for _ in ans: print(_, end=" ") print()
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, s, queries): letters = [(ord(c) - 97) for c in s] n = len(letters) counts = [] counts.append([0] * 26) for i in range(n): c = letters[i] current = counts[i][:] current[c] += 1 counts.append(current) result = [] for q in queries: start = q[0] end = q[1] count = 0 for i in range(26): if counts[end][i] == counts[start - 1][i]: count += 1 result.append(26 - count) return result
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, Query): final = [] for i in Query: chars = set() start = i[0] - 1 end = i[1] tot = len(set(str[start:end])) final.append(tot) return final
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def unique(self, str, n, b): d = {} p = [(0) for i in range(n)] ind = b for i in range(b, n): try: d[str[i]] += 1 except: d[str[i]] = 1 p[ind] = len(d.keys()) ind += 1 return p def SolveQueris(self, str, Query): n = len(str) dp = [[(0) for i in range(n)] for j in range(n)] for i in range(n): dp[i] = self.unique(str, n, i) ans = [] for i in Query: ans.append(dp[i[0] - 1][i[1] - 1]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, Query): out = [] a = [[(0) for i in range(26)] for j in range(len(str) + 1)] for i in range(len(str)): for k in range(len(a[i + 1])): if k == ord(str[i]) - 97: a[i + 1][k] = a[i][k] + 1 else: a[i + 1][k] = a[i][k] for j in Query: l = j[0] c = 0 r = j[1] if l == r: out.append(1) else: for i, j in zip(a[r], a[l - 1]): if i - j > 0: c += 1 out.append(c) return out
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, strs, query): ans = [] s = "" for i in range(len(query)): x = query[i][0] y = query[i][1] s = strs[x - 1 : y] ans.append(len(set(s))) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, Query): tmp_list = [] for i in range(len(Query)): tmp_list.append(len(set(str[Query[i][0] - 1 : Query[i][1]]))) return tmp_list
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, Query): result = [] for L, R in Query: d = {} for i in range(L - 1, R): if str[i] not in d: d[str[i]] = 1 else: d[str[i]] += 1 result.append(len(d)) return result
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str1, l): l1 = [] s1 = "" for i in range(len(l)): for j in range(l[i][0] - 1, l[i][1]): if str1[j] not in s1: s1 += str1[j] l1.append(len(s1)) s1 = "" return l1
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, Query): ans = [0] * len(Query) d = set() for i in range(len(Query)): a = Query[i][0] b = Query[i][1] for j in range(a, b + 1): d.add(str[j - 1]) ans[i] = len(d) d = set() return ans
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN VAR
Given a string str you have to answer several queries on that string. In each query you will be provided two values L and R and you have to find the number of distinct characters in the sub string from index L to index R (inclusive) of the original string. Example 1: Input: str = "abcbaed", Query = {{1,4},{2,4},{1,7}} Output: {3,2,5} Explanation: For the first query distinct characters from [1, 4] are a, b and c. For the second query distinct characters from [2, 4] are b and c. For the third query distinct characters from [1, 7] are a, b, c, d and e. Your Task: You don't need to read or print anyhting. Your task is to complete the function SolveQueries() which takes str and Query as input parameter and returns a list containing answer for each query. Expected Time Complexity: O(max(26*length of str, 26 * No of queries)) Expected Space Complexity: O(26 * length of str) Constraints: 1 <= |str| <= 10^{5} 1 <= No of Queries <= 10^{4} 1 <= L_{i} <= R_{i} <= |str|
class Solution: def SolveQueris(self, str, Query): result = [] d = {} for i in Query: for j in str[i[0] - 1 : i[1]]: if j not in d: d[j] = 1 else: d[j] += 1 result.append(len(d)) d.clear() return result
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR FOR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
import sys ii = lambda: sys.stdin.readline().strip() idata = lambda: [int(x) for x in ii().split()] def dfs(graph, ver): used = set() level = [ver] depth = [0] * (len(graph) + 1) levels_number = 0 while level: new_level = [] for vertex in level: used.add(vertex) depth[vertex] = levels_number for new_vertex in graph[vertex]: if new_vertex not in used: new_level += [new_vertex] levels_number += 1 level = new_level[:] return depth def solve(): n, a, b, da, db = idata() graph = {} for i in range(n - 1): v, u = idata() if not v in graph: graph[v] = [u] else: graph[v] += [u] if not u in graph: graph[u] = [v] else: graph[u] += [v] depth = dfs(graph, a) k = max(depth) for i in range(len(depth)): if depth[i] == k: a = i break diametr = max(dfs(graph, a)) if diametr <= 2 * da or depth[b] <= da or 2 * da >= db: print("Alice") else: print("Bob") return for t in range(int(ii())): solve()
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR LIST VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR LIST VAR IF VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, a, b, da, db = map(int, input().split()) g = [[] for i in range(n)] for i in range(n - 1): u, v = map(int, input().split()) g[u - 1].append(v - 1) g[v - 1].append(u - 1) a -= 1 b -= 1 d0 = [0] * n q = [[-1, 0]] while q: par, ver = q.pop() for to in g[ver]: if par != to: d0[to] = d0[ver] + 1 q.append([ver, to]) p = d0.index(max(d0)) q = [[-1, p]] d = [0] * n while q: par, ver = q.pop() for to in g[ver]: if par != to: d[to] = d[ver] + 1 q.append([ver, to]) R = max(d) q = [[-1, a]] d1 = [0] * n while q: par, ver = q.pop() for to in g[ver]: if par != to: d1[to] = d1[ver] + 1 q.append([ver, to]) ab = d1[b] if ab <= da: print("Alice") elif 2 * da >= R: print("Alice") elif db > 2 * da: print("Bob") else: print("Alice")
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST LIST NUMBER NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR STRING IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
import sys sys.setrecursionlimit(10**5) def search(tree, cur, find, was): was.add(cur) for i in tree[cur]: if i == find: return 1, True elif not i in was: a = search(tree, i, find, was) if a[1]: return a[0] + 1, a[1] return -1, False def max_depth(tree, cur, was_2): was_2.add(cur) dist = 0 dot = cur for i in tree[cur]: if not i in was_2: new_dist, new_dot = max_depth(tree, i, was_2) if max(new_dist, dist) == new_dist: dist = max(new_dist, dist) dot = new_dot return dist + 1, dot def main(): was = set() was_2 = set() n, a, b, da, db = map(int, input().split()) tree = dict() for i in range(1, n): u, v = map(int, input().split()) if not u in tree: tree[u] = set() if not v in tree: tree[v] = set() tree[u].add(v) tree[v].add(u) dist = search(tree, a, b, was) far, dot = max_depth(tree, a, was_2) was_2 = set() ln = max_depth(tree, dot, was_2) if dist[0] <= da or db <= 2 * da or ln[0] - 1 <= 2 * da: print("Alice") return else: print("Bob") return t = int(input()) for i in range(t): main()
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR RETURN NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN NUMBER NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR 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 FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
from sys import stdin class Problem: def __init__(self, n, a, b, da, db, e): self.n = n self.a = a - 1 self.b = b - 1 self.da = da self.db = db self.g = [[] for i in range(n)] for u, v in e: self.g[u - 1].append(v - 1) self.g[v - 1].append(u - 1) def dfs(self, v, dist=0): self.d[v] = dist for u in self.g[v]: if self.d[u] == None: self.dfs(u, dist + 1) def solve(self): if self.da * 2 >= self.db: return "Alice" self.d = [None for i in range(self.n)] self.dfs(self.a) if self.d[self.b] <= self.da: return "Alice" f = self.d.index(max(self.d)) self.d = [None for i in range(self.n)] self.dfs(f) d = max(self.d) if self.da * 2 >= d: return "Alice" return "Bob" def test_cases(): cases = int(stdin.readline()) for case in range(cases): n, a, b, da, db = list(map(int, stdin.readline().split())) e = [list(map(int, stdin.readline().split())) for i in range(n - 1)] yield Problem(n, a, b, da, db, e) for test in test_cases(): ans = test.solve() print(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF NUMBER ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR NONE EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF BIN_OP VAR NUMBER VAR RETURN STRING ASSIGN VAR NONE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR RETURN STRING RETURN STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [([c] * b) for i in range(a)] def list3d(a, b, c, d): return [[([d] * c) for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print("Yes") def No(): print("No") def YES(): print("YES") def NO(): print("NO") INF = 10**19 MOD = 10**9 + 7 def dfs(nodes, src): N = len(nodes) stack = [(src, -1)] dist = [INF] * N dist[src] = 0 while stack: u, prev = stack.pop() for v in nodes[u]: if v != prev: dist[v] = dist[u] + 1 stack.append((v, u)) return dist for _ in range(INT()): N, a, b, da, db = MAP() a -= 1 b -= 1 nodes = [[] for i in range(N)] for i in range(N - 1): x, y = MAP() x -= 1 y -= 1 nodes[x].append(y) nodes[y].append(x) dist = dfs(nodes, a)[b] if da * 2 + 1 > db or dist <= da: print("Alice") continue res1 = dfs(nodes, 0) t = res1.index(max(res1)) res2 = dfs(nodes, t) mx = max(res2) if da * 2 + 1 > mx: print("Alice") else: print("Bob")
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
import sys input = sys.stdin.readline def f(): n, a, b, da, db = map(int, input().split()) T = [[] for _ in range(n + 1)] for _ in range(n - 1): u, v = map(int, input().split()) T[u].append(v) T[v].append(u) dist = [-1] * (n + 1) dist[a] = 0 m = 0 v = -1 stack = [a] while stack: i = stack.pop() for nv in T[i]: if dist[nv] != -1: continue dist[nv] = dist[i] + 1 if m < dist[nv]: m = dist[nv] v = nv stack.append(nv) if dist[b] <= da: print("Alice") return m = 0 stack = [v] dist = [-1] * (n + 1) dist[v] = 0 while stack: i = stack.pop() for nv in T[i]: if dist[nv] != -1: continue dist[nv] = dist[i] + 1 if m < dist[nv]: m = dist[nv] stack.append(nv) if 2 * da >= m: print("Alice") return if db <= 2 * da: print("Alice") else: print("Bob") t = int(input()) for x in range(t): f()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
for t in range(int(input())): n, a, b, da, db = map(int, input().split()) graph = [set() for i in range(n + 1)] for i in range(n - 1): u, v = map(int, input().split()) graph[u].add(v) graph[v].add(u) visited = {1} last_visits = set() visit_now = {1} while len(visited) < n: while visit_now: i = visit_now.pop() last_visits |= graph[i] - visited visited |= graph[i] visit_now = last_visits last_visits = set() last = visit_now.pop() visited = {last} last_visits = set() visit_now = {last} N = 1 while len(visited) < n: N += 1 while visit_now: i = visit_now.pop() last_visits |= graph[i] - visited visited |= graph[i] visit_now = last_visits last_visits = set() visited = {a} last_visits = set() visit_now = {a} dist = 0 while not b in visit_now: dist += 1 while visit_now: i = visit_now.pop() last_visits |= graph[i] - visited visited |= graph[i] visit_now = last_visits last_visits = set() if db < 2 * da + 1: print("Alice") elif 2 * da + 1 >= N: print("Alice") elif dist <= da: print("Alice") else: print("Bob")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP BIN_OP NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
def dfs(x, p): l = 0 for i in graph[x]: if i != p: depth[i] = depth[x] + 1 cur = 1 + dfs(i, x) diam[0] = max(diam[0], cur + l) l = max(l, cur) return l for _ in range(int(input())): n, a, b, da, db = [int(i) for i in input().split()] global graph graph = [[] for i in range(n)] for i in range(n - 1): uu, vv = [(int(i) - 1) for i in input().split()] graph[uu].append(vv) graph[vv].append(uu) depth = [0] * n global diam diam = [0] temp = dfs(a - 1, -1) if 2 * da >= min(diam[0], db) or depth[b - 1] <= da: print("Alice") else: print("Bob")
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
import sys as _sys ALICE = "Alice" BOB = "Bob" MAX_GRAPH_SIZE = 10**5 def main(): t = int(input()) for i in range(t): n, a, b, da, db = _read_ints() a -= 1 b -= 1 graph = [set() for v in range(n)] for i in range(n - 1): u, v = _read_ints() u -= 1 v -= 1 graph[u].add(v) graph[v].add(u) winner = who_wins( graph=graph, alice_start_vertex=a, alice_speed=da, bob_start_vertex=b, bob_speed=db, ) print(winner) def _read_line(): return _sys.stdin.readline() def _read_ints(): return map(int, _read_line().split(" ")) def who_wins(*, graph, alice_start_vertex, bob_start_vertex, alice_speed, bob_speed): assert len(graph) <= MAX_GRAPH_SIZE distance = find_distance(alice_start_vertex, bob_start_vertex, graph) v_1 = find_farthest_vertices(0, graph)[0].pop() max_vertices_distance = find_farthest_vertices(v_1, graph)[1] if alice_speed >= distance: winner = ALICE elif 2 * alice_speed + 1 >= max_vertices_distance + 1: winner = ALICE elif bob_speed >= 2 * alice_speed + 1: winner = BOB else: winner = ALICE return winner def find_distance(vertex_1, vertex_2, graph): passed = {vertex_1} distance = 0 candidates = graph[vertex_1] while vertex_2 not in passed: new_candidates = set() for candidate in candidates: passed.add(candidate) new_candidates |= graph[candidate] - passed candidates = new_candidates distance += 1 return distance def find_farthest_vertices(vertex, graph): passed = {vertex} distance = 0 candidates = graph[vertex] while True: new_candidates = set() for candidate in candidates: passed.add(candidate) new_candidates |= graph[candidate] - passed if not new_candidates: return candidates, distance + 1 candidates = new_candidates distance += 1 main()
IMPORT ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR IF VAR RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
import sys ii = lambda: sys.stdin.readline().strip() idata = lambda: [int(x) for x in ii().split()] def bfs(graph, start_ver): visited = set() queue = [[start_ver, 0]] visited.add(start_ver) ans_way_length, ans_ver = 0, 1 while queue: ver = queue[0] queue = queue[1:] for new_ver in graph[ver[0]]: if new_ver not in visited: visited.add(new_ver) queue += [[new_ver, ver[1] + 1]] if ans_way_length < ver[1] + 1: ans_ver, ans_way_length = new_ver, ver[1] + 1 return ans_ver, ans_way_length def dfs(graph, ver): visited = set() depth = [0] * (len(graph) + 1) level = [ver] num_of_level = 0 while level: new_level = [] for v in level: depth[v] = num_of_level visited.add(v) for v1 in graph[v]: if v1 not in visited: new_level += [v1] level = new_level[:] num_of_level += 1 return depth def solve(): n, a, b, da, db = idata() graph = {} for i in range(n - 1): v, u = idata() if not v in graph: graph[v] = [u] else: graph[v] += [u] if not u in graph: graph[u] = [v] else: graph[u] += [v] q = dfs(graph, a)[b] if q <= da: print("Alice") return x, y = bfs(graph, a) x, y = bfs(graph, x) if y <= 2 * da: print("Alice") return if db <= 2 * da: print("Alice") return print("Bob") return for t in range(int(ii())): solve()
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST LIST VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR LIST LIST VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR LIST VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR LIST VAR IF VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
import sys LI = lambda: list(map(int, sys.stdin.readline().strip("\n").split())) MI = lambda: map(int, sys.stdin.readline().strip("\n").split()) SI = lambda: sys.stdin.readline().strip("\n") II = lambda: int(sys.stdin.readline().strip("\n")) for _ in range(II()): n, a, b, da, db = MI() g = {i: [] for i in range(1, n + 1)} for __ in range(n - 1): u, v = MI() g[u].append(v) g[v].append(u) vis = [False] * (n + 1) dist = [0] * (n + 1) q = [a] m1, v1 = 0, 0 while q: u = q.pop() vis[u] = True for v in g[u]: if not vis[v]: dist[v] = dist[u] + 1 if dist[v] > m1: m1 = dist[v] v1 = v q.append(v) distab = dist[b] vis = [False] * (n + 1) dist = [0] * (n + 1) q = [v1] diam = 0 while q: u = q.pop() vis[u] = True for v in g[u]: if not vis[v]: dist[v] = dist[u] + 1 if dist[v] > diam: diam = dist[v] v1 = v q.append(v) if distab <= da: print("Alice") elif 2 * da >= diam: print("Alice") elif db > 2 * da: print("Bob") else: print("Alice")
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR STRING IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
from sys import stdin input = stdin.readline q = int(input()) for _ in range(q): n, sa, sb, a, b = map(int, input().split()) sa -= 1 sb -= 1 nbr = [[] for i in range(n)] for i in range(n - 1): x, y = map(int, input().split()) x -= 1 y -= 1 nbr[x].append(y) nbr[y].append(x) if 2 * a >= b: print("Alice") else: q = [sa] ind = 0 dist = [-1] * n dist[sa] = 0 while q: if ind >= len(q): break v = q[ind] ind += 1 for w in nbr[v]: if dist[w] == -1: q.append(w) dist[w] = dist[v] + 1 if dist[sb] <= a: print("Alice") else: q = [0] ind = 0 dist = [-1] * n dist[0] = 0 while q: if ind >= len(q): break v = q[ind] ind += 1 for w in nbr[v]: if dist[w] == -1: q.append(w) dist[w] = dist[v] + 1 maksik = 0 best = 0 for i in range(n): if dist[i] > maksik: best = i maksik = dist[i] q = [best] ind = 0 dist = [-1] * n dist[best] = 0 while q: if ind >= len(q): break v = q[ind] ind += 1 for w in nbr[v]: if dist[w] == -1: q.append(w) dist[w] = dist[v] + 1 if max(dist) > 2 * a: print("Bob") else: print("Alice")
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER WHILE VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER WHILE VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER WHILE VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
import sys input = sys.stdin.readline t = int(input()) for tests in range(t): n, a, b, da, db = map(int, input().split()) EDGE = [[] for i in range(n + 1)] for i in range(n - 1): x, y = map(int, input().split()) EDGE[x].append(y) EDGE[y].append(x) if db <= da * 2: print("Alice") continue Q = [a] USE = [-1] * (n + 1) USE[a] = 0 while Q: x = Q.pop() for to in EDGE[x]: if USE[to] == -1: USE[to] = USE[x] + 1 Q.append(to) if USE[b] <= da: print("Alice") continue MAX = -1 MAXIND = -1 for i in range(1, n + 1): if USE[i] > MAX: MAX = USE[i] MAXIND = i Q = [MAXIND] USE2 = [-1] * (n + 1) USE2[MAXIND] = 0 while Q: x = Q.pop() for to in EDGE[x]: if USE2[to] == -1: USE2[to] = USE2[x] + 1 Q.append(to) DIA = max(USE2) if DIA <= da * 2: print("Alice") continue else: print("Bob")
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
import sys ii = lambda: sys.stdin.readline().strip() idata = lambda: [int(x) for x in ii().split()] def bfs(graph, start_ver): visited = set() queue = [[start_ver, 0]] visited.add(start_ver) ans_way_length, ans_ver = 0, start_ver while queue: ver = queue.pop(0) for new_ver in graph[ver[0]]: if new_ver not in visited: visited.add(new_ver) queue += [[new_ver, ver[1] + 1]] if ans_way_length < ver[1] + 1: ans_ver, ans_way_length = new_ver, ver[1] + 1 return ans_ver, ans_way_length def solve(): n, a, b, da, db = idata() graph = {} for i in range(n - 1): v, u = idata() if not v in graph: graph[v] = [u] else: graph[v] += [u] if not u in graph: graph[u] = [v] else: graph[u] += [v] used = set() s1 = [a] d = [0] * (n + 1) l = 0 while s1: s2 = [] for i in s1: used.add(i) d[i] = l for j in graph[i]: if j not in used: s2 += [j] l += 1 s1 = s2[:] a = 0 q = d[b] k = max(d) for i in range(len(d)): if d[i] == k: a = i break used = set() s1 = [a] d = [0] * (n + 1) l = 0 while s1: s2 = [] for i in s1: used.add(i) d[i] = l for j in graph[i]: if j not in used: s2 += [j] l += 1 s1 = s2[:] if max(d) <= 2 * da or q <= da or 2 * da >= db: print("Alice") else: print("Bob") return for t in range(int(ii())): solve()
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST LIST VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR LIST LIST VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR LIST VAR IF VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR LIST VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR LIST VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
import sys input = sys.stdin.readline for _ in range(int(input())): n, a, b, da, db = map(int, input().split()) dic = {} dis = {} for i in range(1, n + 1): dic[i] = [] dis[i] = 0 for i in range(n - 1): x, y = map(int, input().split()) dic[x].append(y) dic[y].append(x) se = {a} ne = {a} level = 1 while ne: tem = set({}) for i in ne: xx = dic[i] for j in xx: if not j in se: se.add(j) tem.add(j) dis[j] = level ne = tem.copy() level += 1 ma = max(list(dic.keys()), key=lambda x: dis[x]) dab = dis[b] dis[ma] = 0 se = {ma} ne = {ma} level = 1 while ne: tem = set({}) for i in ne: xx = dic[i] for j in xx: if not j in se: se.add(j) tem.add(j) dis[j] = level ne = tem.copy() level += 1 diam = max(list(dis.values())) if dab <= da: print("Alice") elif diam <= 2 * da: print("Alice") elif db > 2 * da: print("Bob") elif db <= 2 * da: print("Alice")
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR DICT FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR DICT FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
import sys readline = sys.stdin.readline def parorder(Edge, p): N = len(Edge) par = [0] * N par[p] = -1 stack = [p] order = [] visited = set([p]) ast = stack.append apo = order.append while stack: vn = stack.pop() apo(vn) for vf in Edge[vn]: if vf in visited: continue visited.add(vf) par[vf] = vn ast(vf) return par, order def getcld(p): res = [[] for _ in range(len(p))] for i, v in enumerate(p[1:], 1): res[v].append(i) return res T = int(readline()) Ans = ["Bob"] * T for qu in range(T): N, fa, fb, da, db = map(int, readline().split()) fa -= 1 fb -= 1 Edge = [[] for _ in range(N)] for _ in range(N - 1): a, b = map(int, readline().split()) a -= 1 b -= 1 Edge[a].append(b) Edge[b].append(a) if 2 * da >= db: Ans[qu] = "Alice" continue stack = [fa] dist = [0] * N used = set([fa]) while stack: vn = stack.pop() for vf in Edge[vn]: if vf not in used: used.add(vf) dist[vf] = dist[vn] + 1 stack.append(vf) if dist[fb] <= da: Ans[qu] = "Alice" continue left = dist.index(max(dist)) stack = [left] dist = [0] * N used = set([left]) while stack: vn = stack.pop() for vf in Edge[vn]: if vf not in used: used.add(vf) dist[vf] = dist[vn] + 1 stack.append(vf) D = max(dist) if 2 * da >= D: Ans[qu] = "Alice" print("\n".join(Ans))
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR LIST VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP NUMBER VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP NUMBER VAR VAR ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
diameter = 0 def dfs(u, tree, depth): global diameter length = 0 level[u] = depth isCheck[u] = True for vertex in tree[u]: if isCheck[vertex] != True: isCheck[vertex] = True cur = dfs(vertex, tree, depth + 1) + 1 diameter = max(diameter, cur + length) length = max(length, cur) return length t = int(input()) for _ in range(t): n, a, b, da, db = map(int, input().split()) tree = [[] for _ in range(n + 1)] for _ in range(n - 1): u, v = map(int, input().split()) tree[u].append(v) tree[v].append(u) isCheck = [(0) for _ in range(n + 1)] level = [(0) for _ in range(n + 1)] diameter = 0 dfs(a, tree, 1) dist = abs(level[a] - level[b]) if dist <= da: print("Alice") elif 2 * da >= min(diameter, db): print("Alice") elif db > 2 * da: print("Bob")
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
import sys input = iter(sys.stdin.read().splitlines()).__next__ def furthest(graph, source): S = [(source, 0)] furthest_node = source furthest_dist = 0 discovered = [False] * len(graph) while S: u, dist = S.pop() if dist > furthest_dist: furthest_node = u furthest_dist = dist if discovered[u]: continue discovered[u] = True for v in graph[u]: S.append((v, dist + 1)) return furthest_node, furthest_dist def distance(graph, source, dest): S = [(source, 0)] discovered = set() while S: u, dist = S.pop() if u == dest: return dist if u in discovered: continue discovered.add(u) for v in graph[u]: S.append((v, dist + 1)) t = int(input()) ans = [] for _ in range(t): n, a, b, da, db = map(int, input().split()) if da + da >= db: for edge in range(n - 1): input() ans.append("Alice") continue graph = [[] for node in range(n)] for edge in range(n - 1): u, v = [(int(i) - 1) for i in input().split()] graph[u].append(v) graph[v].append(u) if distance(graph, a - 1, b - 1) <= da: ans.append("Alice") continue diameter = furthest(graph, furthest(graph, 0)[0])[1] if da + da >= diameter: ans.append("Alice") else: ans.append("Bob") print("\n".join(ans))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR RETURN VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
import sys input = sys.stdin.readline for f in range(int(input())): n, a, b, da, db = map(int, input().split()) neig = [0] * n for i in range(n): neig[i] = [0] for i in range(n - 1): vertexa, vertexb = map(int, input().split()) vertexa -= 1 vertexb -= 1 neig[vertexa][0] += 1 neig[vertexb][0] += 1 neig[vertexa].append(vertexb) neig[vertexb].append(vertexa) height = [0] * n dn = [-1] * n diam = 0 tod = [] for i in range(n): if neig[i][0] == 1: tod.append(i) while len(tod) > 0: x = tod.pop() neig[x][0] = 0 for i in range(1, len(neig[x])): ng = neig[x][i] if neig[ng][0] > 0: neig[ng][0] -= 1 diam = max(diam, height[ng] + height[x] + 1) height[ng] = max(height[ng], height[x] + 1) if neig[ng][0] == 1: tod.append(ng) dn[x] = ng dist = 0 a -= 1 b -= 1 while a != b: dist += 1 if height[a] < height[b]: a = dn[a] else: b = dn[b] if dist <= da or 2 * da >= db or 2 * da >= diam: print("Alice") else: print("Bob")
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR 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 LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
def melysegi(x): latatlan.remove(int(x)) for i in ellista[x]: if i in latatlan: melyseg[i] = melyseg[x] + 1 melysegi(i) def melysegi2(x): latatlan2.remove(int(x)) for i in ellista[x]: if i in latatlan2: melyseg2[i] = melyseg2[x] + 1 melysegi2(i) def fa_atmero(): vegpont = melyseg.index(max(melyseg)) melysegi2(vegpont) atmero = max(melyseg2) return atmero for _ in range(int(input())): n, a, b, da, db = list(map(int, input().split(" "))) ellista = [[] for i in range(n)] for i in range(n - 1): u, v = [(int(i) - 1) for i in input().split()] ellista[u].append(v) ellista[v].append(u) ellista = tuple(ellista) latatlan = set([i for i in range(0, n)]) latatlan2 = set([i for i in range(0, n)]) melyseg = [0] * n melyseg2 = [0] * n melysegi(a - 1) atmero = fa_atmero() if 2 * da < atmero and 2 * da < db and melyseg[b - 1] > da: print("Bob") else: print("Alice")
FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
import sys input = sys.stdin.readline for _ in range(int(input())): n, sa, sb, da, db = map(int, input().split()) e = {} g = 0 if 2 * da >= db: exec((n - 1) * "input();") print("Alice") continue for i in range(n): e[i] = [] for i in range(n - 1): u, v = map(int, input().split()) e[u - 1] += [v - 1] e[v - 1] += [u - 1] s = [] d = [([None] * 2 + [0] * 2) for i in range(n)] q = [(0, -1)] while q: v, p = q.pop() if v == sa - 1: d[v][0] = 0 elif v == sb - 1: d[v][1] = 0 if v < 1 or len(e[v]) > 1: s.append((v, p)) for i in e[v]: if i != p: q.append((i, v)) while s: v, p = s.pop() m = [0, 0] b = 0 for i in e[v]: if i != p: w = d[i][2] if w > m[0]: m = [w, m[0]] elif w > m[1]: m = [m[0], w] b = max(b, d[i][3]) w = d[i][0] if w != None: d[v][0] = w + 1 w = d[i][1] if w != None: d[v][1] = w + 1 d[v][2] = m[0] + 1 d[v][3] = max(min(2, len(e[v]) - (v > 0)) + sum(m), b) if g < 1 and d[v][0] != None and d[v][1] != None: g = d[v][0] + d[v][1] print(["Alice", "Bob"][g > da and d[0][3] > 2 * da])
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER IF BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP LIST NONE NUMBER BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR LIST VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NONE ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NONE ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER NONE VAR VAR NUMBER NONE ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST STRING STRING VAR VAR VAR NUMBER NUMBER BIN_OP NUMBER VAR
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
import sys sys.setrecursionlimit(10**5) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def MI1(): return map(int1, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] for _ in range(II()): n, a, b, da, db = MI() a, b = a - 1, b - 1 to = [[] for _ in range(n)] for _ in range(n - 1): u, v = MI1() to[u].append(v) to[v].append(u) if db < da * 2 + 1: print("Alice") continue dist = [-1] * n dist[a] = 0 stack = [a] while stack: u = stack.pop() for v in to[u]: if dist[v] != -1: continue dist[v] = dist[u] + 1 stack.append(v) if dist[b] <= da: print("Alice") continue mx = max(dist) u = dist.index(mx) dist = [-1] * n dist[u] = 0 stack = [u] while stack: u = stack.pop() for v in to[u]: if dist[v] != -1: continue dist[v] = dist[u] + 1 stack.append(v) mx = max(dist) if mx >= da * 2 + 1: print("Bob") else: print("Alice")
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR 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 VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
import sys input = sys.stdin.readline t = int(input()) for i in range(t): n, a, b, da, db = map(int, input().split()) C = [list(map(int, input().split())) for j in range(n - 1)] for j in range(n - 1): for k in range(2): C[j][k] -= 1 a -= 1 b -= 1 M = [[] for j in range(n)] for j in range(n - 1): M[C[j][0]].append(C[j][1]) M[C[j][1]].append(C[j][0]) V = [-1] * n V[a] = 0 Q = [[a, 0]] s = 0 while len(Q) > s: V[Q[s][0]] = Q[s][1] for x in M[Q[s][0]]: if V[x] == -1: Q.append([x, Q[s][1] + 1]) s += 1 mi = V.index(max(V)) if V[b] <= da: print("Alice") continue V = [-1] * n V[mi] = 0 Q = [[mi, 0]] s = 0 while len(Q) > s: V[Q[s][0]] = Q[s][1] for x in M[Q[s][0]]: if V[x] == -1: Q.append([x, Q[s][1] + 1]) s += 1 mi = V.index(max(V)) if db >= da * 2 + 1 and max(V) >= da * 2 + 1: print("Bob") else: print("Alice")
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST LIST VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST LIST VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
from sys import stdin, stdout get_string = lambda: stdin.readline().strip("\n") get_intmap = lambda: map(int, get_string().split(" ")) def testcase(): n, a, b, da, db = get_intmap() adj = [[] for i in range(n + 1)] for i in range(n - 1): u, v = get_intmap() adj[u].append(v) adj[v].append(u) used = [0] * (n + 1) frontier, nxt, used[a], distance = [a], [], 1, 0 while len(frontier): distance += 1 for u in frontier: for v in adj[u]: if used[v]: continue if v == b: distance_ab = distance used[v] = 1 nxt.append(v) if nxt == []: leaf_node = frontier.pop() frontier, nxt = nxt, [] if distance_ab <= da: print("Alice") return used = [0] * (n + 1) frontier, nxt, used[leaf_node], distance = [leaf_node], [], 1, 0 while len(frontier): distance += 1 for u in frontier: for v in adj[u]: if used[v]: continue used[v] = 1 nxt.append(v) if nxt == []: tree_diameter = distance - 1 frontier, nxt = nxt, [] if tree_diameter <= 2 * da: print("Alice") return if db <= 2 * da: print("Alice") else: print("Bob") for t in range(int(input())): testcase()
ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR LIST VAR LIST NUMBER NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR FOR VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR LIST IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR LIST VAR LIST NUMBER NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR LIST ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR LIST IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
def Diameter(x): v[x] = True stack = [x] while stack: a = stack.pop() for i in d[a]: if not v[i]: v[i] = True stack.append(i) dis[i] = dis[a] + 1 def BFS(a): v[a] = True q = [a] while q: p = q.pop(0) for i in d[p]: if not v[i]: q.append(i) v[i] = True distance[i] = distance[p] + 1 for _ in range(int(input())): n, a, b, da, db = map(int, input().split()) d = {i: [] for i in range(1, n + 1)} for i in range(n - 1): x, y = map(int, input().split()) d[x].append(y) d[y].append(x) dis = [(0) for i in range(n + 1)] v = [(False) for i in range(n + 1)] Diameter(1) ans = float("-inf") for i in range(len(dis)): if dis[i] > ans: ans = dis[i] node = i dis = [(0) for i in range(n + 1)] v = [(False) for i in range(n + 1)] Diameter(node) dia = float("-inf") for i in range(len(dis)): if dis[i] > dia: dia = dis[i] distance = [(0) for i in range(n + 1)] v = [(False) for i in range(n + 1)] BFS(a) if distance[b] > da and 2 * da < dia and db > 2 * da: print("Bob") else: print("Alice")
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
from sys import stdin, stdout def tree_tag(n, a, b, da, db, dic): dis = distance(a, -1, b, dic) if dis <= da: return "Alice" ln1 = dfs(1, -1, dic) ln2 = dfs(ln1[0], -1, dic) mxpath = ln2[1] if da * 2 < db and mxpath > da * 2: return "Bob" return "Alice" def distance(node, pnode, b, dic): if node == b: return 0 for nxt in dic[node]: if nxt != pnode: wk = distance(nxt, node, b, dic) if wk != -1: return 1 + wk return -1 def dfs(node, pnode, dic): sub = [-1, -1] for nxt in dic[node]: if nxt != pnode: wk = dfs(nxt, node, dic) if wk[1] > sub[1]: sub = wk if sub[0] != -1: return [sub[0], sub[1] + 1] else: return [node, 0] t = int(stdin.readline()) for _ in range(t): n, a, b, da, db = map(int, stdin.readline().split()) dic = {} for _ in range(n - 1): u, v = map(int, stdin.readline().split()) if u not in dic: dic[u] = [] if v not in dic: dic[v] = [] dic[u].append(v) dic[v].append(u) ans = tree_tag(n, a, b, da, db, dic) stdout.write(ans + "\n")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN STRING RETURN STRING FUNC_DEF IF VAR VAR RETURN NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER RETURN BIN_OP NUMBER VAR RETURN NUMBER FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER NUMBER RETURN LIST VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR LIST IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
from sys import stdin for _ in range(int(input())): n, a, b, da, db = map(int, input().split()) a -= 1 b -= 1 go = [[] for _ in range(n)] for _ in range(n - 1): x, y = map(int, stdin.readline().split()) x -= 1 y -= 1 go[x].append(y) go[y].append(x) MAX = 0 def f(pre, node): global MAX re = [0, 0] for x in go[node]: if pre != x: re.append(f(node, x)) re.sort() MAX = max(MAX, re[-1] + re[-2]) return re[-1] + 1 f(-1, 0) q = [(a, 0, -1)] while q != []: node, level, pre = q.pop() if node == b: dis = level MAX = max(MAX, level) for x in go[node]: if x != pre: q.append([x, level + 1, node]) if dis <= da or da * 2 >= db or da * 2 >= MAX: print("Alice") else: print("Bob")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER WHILE VAR LIST ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
import sys def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return map(int, minp().split()) def solve(): n, a, b, da, db = mints() e = [[] for i in range(n + 1)] for i in range(n - 1): u, v = mints() e[u].append(v) e[v].append(u) if db < da * 2 + 1: return False q = [0] * (n + 2) ql = 0 qr = 1 d = [None] * (n + 1) d[a] = 0 q[0] = a while ql < qr: x = q[ql] ql += 1 for v in e[x]: if d[v] is None: d[v] = d[x] + 1 q[qr] = v qr += 1 if d[b] <= da: return False far = q[qr - 1] if d[far] >= da * 2 + 1: return True ql = 0 qr = 1 d = [None] * (n + 1) d[far] = 0 q[0] = far while ql < qr: x = q[ql] ql += 1 for v in e[x]: if d[v] is None: d[v] = d[x] + 1 q[qr] = v qr += 1 far2 = q[qr - 1] return d[far2] >= da * 2 + 1 for i in range(mint()): print(["Alice", "Bob"][solve()])
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 VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NONE ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NONE ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST STRING STRING FUNC_CALL VAR
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
import sys input = sys.stdin.readline t = int(input()) for rrr in range(t): n, a, b, da, db = list(map(int, input().split())) a -= 1 b -= 1 if da >= db: for _ in range(n - 1): x, y = list(map(int, input().split())) print("Alice") else: N = [[] for _ in range(n)] V = [(0) for _ in range(n)] D = [(0) for _ in range(n)] for _ in range(n - 1): x, y = list(map(int, input().split())) x -= 1 y -= 1 N[x].append(y) N[y].append(x) Q = [a] V[a] = 1 depth = 0 done = 0 while Q: depth += 1 new_Q = [] for node in Q: for child in N[node]: if V[child] == 0: new_Q.append(child) V[child] = 1 if child == b: if depth <= da: print("Alice") done = 1 Q = new_Q if done == 0: V = [(0) for _ in range(n)] Q = [0] OR = [0] V[0] = 1 depth = 0 while Q: new_Q = [] for node in Q: for child in N[node]: if V[child] == 0: new_Q.append(child) V[child] = 1 OR.append(child) Q = new_Q depth += 1 Q = [OR[-1]] V = [(0) for _ in range(n)] V[OR[-1]] = 1 depth = 0 while Q: new_Q = [] for node in Q: for child in N[node]: if V[child] == 0: new_Q.append(child) V[child] = 1 OR.append(child) Q = new_Q depth += 1 depth -= 1 if depth > 2 * da and db > 2 * da: print("Bob") else: print("Alice")
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
gans = [] for _ in range(int(input())): n, a, b, da, db = map(int, input().split()) a -= 1 b -= 1 u = [] for i in range(n): u.append([]) for i in range(n - 1): v1, v2 = map(lambda x: int(x) - 1, input().split()) u[v1].append(v2) u[v2].append(v1) ind = a q = [ind] q0 = 0 INF = 100000000 d = [INF] * n d[ind] = 0 while len(q) > q0: v = q[q0] q0 += 1 for i in u[v]: if d[i] > d[v] + 1: d[i] = d[v] + 1 q.append(i) if d[b] <= da: gans.append("Alice") continue ind = -1 mx = max(d) for i in range(n): if d[i] == mx: ind = i break q = [ind] q0 = 0 d = [INF] * n d[ind] = 0 while len(q) > q0: v = q[q0] q0 += 1 for i in u[v]: if d[i] > d[v] + 1: d[i] = d[v] + 1 q.append(i) D = max(d) if db > 2 * da and 2 * da < D: gans.append("Bob") else: gans.append("Alice") print("\n".join(gans))
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
from sys import stdin def input(): return stdin.readline().strip() tests = int(input()) for t in range(tests): n, a, b, da, db = list(map(int, input().split())) edge_ls = [[] for _ in range(n + 1)] for _ in range(n - 1): e1, e2 = list(map(int, input().split())) edge_ls[e1].append(e2) edge_ls[e2].append(e1) if da * 2 < db: stack = [[a, 0]] visited = [(False) for _ in range(n + 1)] visited[a] = True found = None furthest_edge = None furthest_dist = -1 while stack: curr, curr_step = stack.pop(-1) if curr_step > furthest_dist: furthest_dist = curr_step furthest_edge = curr for item in edge_ls[curr]: if not visited[item]: stack.append([item, curr_step + 1]) visited[item] = True if item == b: found = curr_step + 1 stack = [[furthest_edge, 1]] visited = [(False) for _ in range(n + 1)] visited[furthest_edge] = True furthest_dist = -1 while stack: curr, curr_step = stack.pop(-1) if curr_step > furthest_dist: furthest_dist = curr_step for item in edge_ls[curr]: if not visited[item]: stack.append([item, curr_step + 1]) visited[item] = True if found > da and furthest_dist >= da * 2 + 2: print("Bob") else: print("Alice") else: print("Alice")
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR LIST LIST VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST LIST VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
def solve(n, a, b, da, db, G): if 2 * da >= db: print("Alice") return dist = [-1] * n stk = [a] dist[a] = 0 while stk: x = stk.pop() for y in G[x]: if dist[y] == -1: dist[y] = 1 + dist[x] stk.append(y) if dist[b] <= da: print("Alice") return x1 = dist.index(max(dist)) dist = [-1] * n dist[x1] = 0 stk = [x1] while stk: x = stk.pop() for y in G[x]: if dist[y] == -1: dist[y] = 1 + dist[x] stk.append(y) diameter = max(dist) if diameter <= 2 * da: print("Alice") else: print("Bob") return for nt in range(int(input())): n, a, b, da, db = map(int, input().split()) a -= 1 b -= 1 G = [[] for _ in range(n)] for _ in range(n - 1): x, y = map(int, input().split()) G[x - 1].append(y - 1) G[y - 1].append(x - 1) solve(n, a, b, da, db, G)
FUNC_DEF IF BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
import sys class node: def __init__(self, i): self.i = i self.lvl1 = 0 self.lvl2 = 0 self.neighbour = [] def dfs1(n, p, visited): visited[n] = 1 for i in p[n].neighbour: if visited[i.i] == 0: i.lvl1 = p[n].lvl1 + 1 dfs1(i.i, p, visited) t = int(sys.stdin.readline()) while t: n, a, b, da, db = map(int, sys.stdin.readline().split()) p = [] m = n - 1 for i in range(1, n + 1): a1 = node(i - 1) p.append(a1) for i in range(m): u, v = map(int, sys.stdin.readline().split()) u -= 1 v -= 1 p[u].neighbour.append(p[v]) p[v].neighbour.append(p[u]) if db <= 2 * da: sys.stdout.write("Alice" + "\n") t -= 1 continue visited = [0] * n dfs1(a - 1, p, visited) dab = p[b - 1].lvl1 if dab <= da: sys.stdout.write("Alice" + "\n") t -= 1 continue for i in range(n): p[i].lvl1 = 0 visited = [0] * n dfs1(0, p, visited) k = 0 max = -1 for i in range(len(p)): if p[i].lvl1 > max: k = i max = p[i].lvl1 p[i].lvl1 = 0 visited = [0] * n dfs1(k, p, visited) k = 0 max = -1 for i in range(len(p)): if p[i].lvl1 > max: k = i max = p[i].lvl1 diameter = max if 2 * da >= diameter: sys.stdout.write("Alice" + "\n") else: sys.stdout.write("Bob" + "\n") t -= 1
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP STRING STRING VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING STRING EXPR FUNC_CALL VAR BIN_OP STRING STRING VAR NUMBER
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
import sys input = sys.stdin.buffer.readline T = int(input()) def f(x, y): dg = 10**6 if x == 0: return y elif x < dg: if x > y: return y * dg + x else: return x * dg + y else: xs, xf = divmod(x, dg) if xs < y: xs = y else: return x if xf < xs: return xf * dg + xs else: return xs * dg + xf for testcase in range(T): n, a, b, da, db = map(int, input().split()) a -= 1 b -= 1 edge = [[] for i in range(n)] for i in range(n - 1): u, v = map(int, input().split()) edge[u - 1].append(v - 1) edge[v - 1].append(u - 1) pa = [-1] * n pa[a] = 0 tank = [a] order = [] par = [-1] * n while tank: now = tank.pop() order.append(now) for e in edge[now]: if pa[e] == -1: pa[e] = pa[now] + 1 tank.append(e) par[e] = now pb = [-1] * n pb[b] = 0 tank = [b] while tank: now = tank.pop() for e in edge[now]: if pb[e] == -1: pb[e] = pb[now] + 1 tank.append(e) if 2 * da + 1 > db or pa[b] <= da: print("Alice") else: depth = [0] * n dg = 10**6 for e in order[::-1]: if par[e] == -1: continue depth[par[e]] = f(depth[par[e]], depth[e] % dg + 1) flag = False for e in order: se, fi = divmod(depth[e], dg) if fi >= 2 * da + 1: flag = True break for nxt in edge[e]: if par[e] == nxt: continue nse, nfi = divmod(depth[nxt], dg) if nfi == fi - 1: depth[nxt] = f(depth[nxt], se + 1) else: depth[nxt] = f(depth[nxt], fi + 1) if flag: print("Bob") else: print("Alice")
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR NUMBER RETURN VAR IF VAR VAR IF VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR IF VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
t = int(input()) depth = [(0) for _ in range(200050)] for _ in range(t): n, a, b, da, db = list(map(int, input().split(" "))) edges = [[] for _ in range(n + 2)] for i in range(n - 1): u, v = list(map(int, input().split(" "))) edges[u].append(v) edges[v].append(u) diameter = 0 depth[a] = 0 def dfs(u, f): global diameter l = 0 for v in edges[u]: if v == f: continue depth[v] = depth[u] + 1 cur = 1 + dfs(v, u) diameter = max(diameter, cur + l) l = max(l, cur) return l dfs(a, -1) if depth[b] <= da or da * 2 >= diameter or da * 2 >= db: print("Alice") else: print("Bob")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a fun game of tree tag. The game is played on a tree of $n$ vertices numbered from $1$ to $n$. Recall that a tree on $n$ vertices is an undirected, connected graph with $n-1$ edges. Initially, Alice is located at vertex $a$, and Bob at vertex $b$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $da$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $db$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them. If after at most $10^{100}$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins. Determine the winner if both players play optimally. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains five integers $n,a,b,da,db$ ($2\le n\le 10^5$, $1\le a,b\le n$, $a\ne b$, $1\le da,db\le n-1$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $n-1$ lines describe the edges of the tree. The $i$-th of these lines contains two integers $u$, $v$ ($1\le u, v\le n, u\ne v$), denoting an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output a single line containing the winner of the game: "Alice" or "Bob". -----Example----- Input 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 Output Alice Bob Alice Alice -----Note----- In the first test case, Alice can win by moving to vertex $1$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. [Image] In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $1$ or $6$ is farthest from Alice. [Image]
from sys import stdin class Input: def readline(self): return stdin.readline().strip() def read_int(self): return int(self.readline()) def read_list(self): return self.readline().split() def test_cases(self): cases = self.read_int() for case in range(cases): yield self class Problem: def __init__(self, input): self.n, self.a, self.b, self.da, self.db = list(map(int, input.read_list())) self.a -= 1 self.b -= 1 self.g = [[] for i in range(self.n)] for i in range(self.n - 1): u, v = list(map(int, input.read_list())) self.g[u - 1].append(v - 1) self.g[v - 1].append(u - 1) def dfs(self, v, dist=0): self.d[v] = dist for u in self.g[v]: if self.d[u] == None: self.dfs(u, dist + 1) def solution(self): if self.da * 2 >= self.db: return "Alice" self.d = [None for i in range(self.n)] self.dfs(self.a) if self.d[self.b] <= self.da: return "Alice" f = self.d.index(max(self.d)) self.d = [None for i in range(self.n)] self.dfs(f) d = max(self.d) if self.da * 2 >= d: return "Alice" return "Bob" def answer(self): print(self.solution()) for case_input in Input().test_cases(): Problem(case_input).answer()
CLASS_DEF FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF NUMBER ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR NONE EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF BIN_OP VAR NUMBER VAR RETURN STRING ASSIGN VAR NONE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR RETURN STRING RETURN STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL FUNC_CALL VAR VAR
Ivan is a student at Berland State University (BSU). There are n days in Berland week, and each of these days Ivan might have some classes at the university. There are m working hours during each Berland day, and each lesson at the university lasts exactly one hour. If at some day Ivan's first lesson is during i-th hour, and last lesson is during j-th hour, then he spends j - i + 1 hours in the university during this day. If there are no lessons during some day, then Ivan stays at home and therefore spends 0 hours in the university. Ivan doesn't like to spend a lot of time in the university, so he has decided to skip some lessons. He cannot skip more than k lessons during the week. After deciding which lessons he should skip and which he should attend, every day Ivan will enter the university right before the start of the first lesson he does not skip, and leave it after the end of the last lesson he decides to attend. If Ivan skips all lessons during some day, he doesn't go to the university that day at all. Given n, m, k and Ivan's timetable, can you determine the minimum number of hours he has to spend in the university during one week, if he cannot skip more than k lessons? -----Input----- The first line contains three integers n, m and k (1 ≤ n, m ≤ 500, 0 ≤ k ≤ 500) — the number of days in the Berland week, the number of working hours during each day, and the number of lessons Ivan can skip, respectively. Then n lines follow, i-th line containing a binary string of m characters. If j-th character in i-th line is 1, then Ivan has a lesson on i-th day during j-th hour (if it is 0, there is no such lesson). -----Output----- Print the minimum number of hours Ivan has to spend in the university during the week if he skips not more than k lessons. -----Examples----- Input 2 5 1 01001 10110 Output 5 Input 2 5 0 01001 10110 Output 8 -----Note----- In the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day. In the second example Ivan can't skip any lessons, so he spends 4 hours every day.
n, m, k = input().split(" ") n = int(n) m = int(m) k = int(k) ind = [] pre = [] for _ in range(n): s = input() ind.append([]) for i, c in enumerate(s): if c == "1": ind[-1].append(i) for i in range(n): pre.append([]) for j in range(k + 1): pre[i].append([]) if len(ind[i]) > j: pre[i][j] = ind[i][-1] - ind[i][0] + 1 else: pre[i][j] = 0 continue for x in range(j + 1): y = len(ind[i]) - 1 - j + x if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]: pre[i][j] = ind[i][y] - ind[i][x] + 1 dp = [[]] for i in range(k + 1): dp[0].append(pre[0][i]) for i in range(1, n): dp.append([]) for j in range(0, k + 1): dp[i].append(pre[i][j] + dp[i - 1][0]) for z in range(j + 1): dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z]) print(dp[n - 1][k])
ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR LIST IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR
Ivan is a student at Berland State University (BSU). There are n days in Berland week, and each of these days Ivan might have some classes at the university. There are m working hours during each Berland day, and each lesson at the university lasts exactly one hour. If at some day Ivan's first lesson is during i-th hour, and last lesson is during j-th hour, then he spends j - i + 1 hours in the university during this day. If there are no lessons during some day, then Ivan stays at home and therefore spends 0 hours in the university. Ivan doesn't like to spend a lot of time in the university, so he has decided to skip some lessons. He cannot skip more than k lessons during the week. After deciding which lessons he should skip and which he should attend, every day Ivan will enter the university right before the start of the first lesson he does not skip, and leave it after the end of the last lesson he decides to attend. If Ivan skips all lessons during some day, he doesn't go to the university that day at all. Given n, m, k and Ivan's timetable, can you determine the minimum number of hours he has to spend in the university during one week, if he cannot skip more than k lessons? -----Input----- The first line contains three integers n, m and k (1 ≤ n, m ≤ 500, 0 ≤ k ≤ 500) — the number of days in the Berland week, the number of working hours during each day, and the number of lessons Ivan can skip, respectively. Then n lines follow, i-th line containing a binary string of m characters. If j-th character in i-th line is 1, then Ivan has a lesson on i-th day during j-th hour (if it is 0, there is no such lesson). -----Output----- Print the minimum number of hours Ivan has to spend in the university during the week if he skips not more than k lessons. -----Examples----- Input 2 5 1 01001 10110 Output 5 Input 2 5 0 01001 10110 Output 8 -----Note----- In the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day. In the second example Ivan can't skip any lessons, so he spends 4 hours every day.
R = lambda: map(int, input().split()) n, m, k = R() cls = [list(i for i, x in enumerate(map(int, input())) if x) for _ in range(n)] dp = [([n * m] * (k + 1)) for i in range(n + 1)] dp.append([0] * (k + 1)) for i in range(n): row = cls[i] c2l = [m + 1] * (m + 1) c2l[0] = row[-1] - row[0] + 1 if row else 0 c2l[len(row)] = 0 for r in range(len(row)): for l in range(r + 1): c2l[len(row) - (r - l + 1)] = min( c2l[len(row) - (r - l + 1)], row[r] - row[l] + 1 ) for j in range(k + 1): for c, l in enumerate(c2l): if j + c <= k and l < m + 1: dp[i][j] = min(dp[i][j], dp[i - 1][j + c] + l) print(min(dp[n - 1]))
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER