A. King Escape
To check if the king can reach the target, we need to ensure that both the king and the target lie in the same quadrant relative to the black queen. We can determine this by comparing the signs of the x and y coordinates of the king and the target with respect to the queen. If they have the same signs for both coordinates, then they are in the same quadrant and the king can reach the target.
dimension = int(input())
ax, ay = map(int, input().split())
bx, by = map(int, input().split())
cx, cy = map(int, input().split())
# Check if king and target are in the same quadrant
if (bx-ax) * (cx-ax) > 0 and (by-ay) * (cy-ay) > 0:
print("YES")
else:
print("NO")
B. Party
The problem involves a graph of employees and their managers, with one employee called "root" not having a manager. We can label each employee with a number indicating their distance from the root. The solution to the problem is the highest number assigned to any employee.
Why is this? It's because if two employees have the same label, they cannot be in the same group because one cannot be the superior of the other. Therefore, we can form groups of employees by putting those with the same label together, and these groups will not overlap.
import sys
from collections import defaultdict
# Increase recursion limit to avoid stack overflow during DFS
sys.setrecursionlimit(2500)
def main():
# Read in number of employees
n = int(input())
# Initialize empty adjacency list and list of "root" employees
adjlist = defaultdict(list)
roots = []
# Loop through each employee and their manager
for i in range(1, n + 1):
emp = int(input())
# If employee has no manager, add them to list of "root" employees
if emp == -1:
roots.append(i)
# Otherwise, add this employee to their manager's list of direct reports
else:
adjlist[emp].append(i)
# Initialize variable to store maximum depth found during DFS
max_level = 0
# Loop through each root employee and perform DFS to find maximum depth
for root in roots:
level = dfs(root, adjlist)
max_level = max(level, max_level)
# Output the maximum depth found
print(max_level)
# Recursive DFS function to find depth of each employee in the tree
def dfs(cur, adjlist):
# Initialize current depth to 0
level = 0
# Loop through each direct report of this employee and perform DFS to find maximum depth
for nxt in adjlist[cur]:
level = max(dfs(nxt, adjlist), level)
# Add 1 to the maximum depth found for this employee
return level + 1
# Call the main function to run the program
main()
C. Transformation: from A to B
To solve this problem, we can work backwards from the target number A to the given number B. If the last digit of B is 1, then the last operation must have been appending a 1 to the right of the current number. So, we can delete the last digit of B to get the new number and continue.
If the last digit of B is even, then the last operation must have been multiplying the current number by 2. So, we can divide B by 2 to get the new number and continue.
If the last digit of B is an odd digit except for 1, then it is not possible to obtain A by performing the given operations on B.
We repeat this algorithm until we either reach A or we obtain a number less than A, in which case it is not possible to transform B to A.
def can_transform_to_target(start_num, target_num, transformation):
# Keep applying operations until target_num <= start_num
while target_num > start_num:
# If target_num is even, divide it by 2
if target_num % 2 == 0:
target_num //= 2
transformation.append(target_num)
# If target_num ends in 1, remove that digit
elif target_num % 10 == 1:
target_num //= 10
transformation.append(target_num)
# If target_num is odd and doesn't end in 1, it can't be transformed to A
else:
return "NO"
# If target_num ended up equal to A, it can be transformed to A
return "YES" if target_num == start_num else "NO"
start, target = map(int, input().split())
transformation = [target]
is_possible = can_transform_to_target(start, target, transformation)
print(is_possible)
if is_possible == "YES":
print(len(transformation))
print(*transformation[::-1])
D. Kefa and Park
We can start traversing the tree from the root node and keep track of the number of consecutive vertices with cats using a parameter k. If k exceeds the maximum number of consecutive vertices with cats allowed, m, then we can stop traversing that path since it's not a valid path for the cat to travel. Otherwise, we can continue traversing the tree.
The answer we're looking for is the total number of leaves (endpoints of the tree) that we're able to reach within the constraints set by m.
num_vertices, max_cats = map(int, input().split())
cat_count = list(map(int, input().split()))
adj_list = [[] for _ in range(num_vertices)]
for _ in range(num_vertices - 1):
vertex_a, vertex_b = map(int, input().split())
adj_list[vertex_a - 1].append(vertex_b - 1)
adj_list[vertex_b - 1].append(vertex_a - 1)
def dfs(start_vertex, parent_vertex, start_cats):
stack = [(start_vertex, parent_vertex, start_cats)]
leaf_count = 0
while stack:
current_vertex, parent_vertex, current_cats = stack.pop()
if current_cats > max_cats:
continue
is_leaf = True
for neighbor in adj_list[current_vertex]:
if neighbor != parent_vertex:
is_leaf = False
stack.append((neighbor, current_vertex, current_cats * cat_count[neighbor] + cat_count[neighbor]))
leaf_count += is_leaf
return leaf_count
num_leafs = dfs(0, -1, cat_count[0])
print(num_leafs)




