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()



