mediumGraphsPure DSA~30 min
IAM Role Cycle Check
An identity service lets roles inherit permissions from other roles via `inherits` relationships. Before saving a new inheritance edge, the validator must reject any change that would create a cycle — otherwise privilege resolution loops forever.
Problem
Given an integer n (number of roles, 0..n−1) and a list of directed edges (a, b) meaning role a inherits from role b, return true if the resulting graph contains a cycle, false otherwise.
Input
Integer n (1 ≤ n ≤ 10^4) and m edges (0 ≤ m ≤ 5·10^4), each a pair (a, b) with 0 ≤ a, b < n.
Output
A single boolean.
Constraints
- 1 ≤ n ≤ 10,000
- 0 ≤ m ≤ 50,000
- Self-loops (a == a) count as cycles
- Multi-edges between the same pair count once
Examples
Example 1
Input
n = 3, edges = [(0,1), (1,2), (2,0)]
Output
true
Triangle cycle 0→1→2→0.
Example 2
Input
n = 3, edges = [(0,1), (1,2)]
Output
false
Pure chain, no cycle.