Problem Description
Given a tournament represented as a Directed Acyclic Graph (DAG) with n teams (nodes) and edges that indicate a “stronger-than” relation, find the unique champion team. A team is a champion if no other team is stronger than it (i.e. no incoming edge). Return the team number if there is a unique champion, otherwise return -1.
Key Insights
- A team is a champion if and only if it has no incoming edges.
- In the graph, an edge from team a to team b means a is stronger than b.
- Calculate the in-degree for every node; only nodes with in-degree 0 satisfy the condition.
- If more than one team has in-degree 0, there is no unique champion.
Space and Time Complexity
Time Complexity: O(n + m), where n is the number of teams and m is the number of edges. Space Complexity: O(n) for storing the in-degree of each team.
Solution
- We initialize an array (or map) to count the in-degree of every team.
- Iterate over all given edges; for each edge [u, v] increment the in-degree of team v because team u is stronger than team v.
- After processing all edges, scan through the in-degree list.
- If exactly one team has an in-degree of zero, that team is the champion; otherwise, if there are multiple teams with an in-degree of zero, then a unique champion does not exist and return -1.
- This simple approach leverages fundamental graph in-degree computation and works efficiently given the problem constraints.