<p>You are given an integer <code>n</code> and a 2D integer array <code>queries</code>.</p>

<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code>. Initially, there is a <strong>unidirectional</strong> road from city <code>i</code> to city <code>i + 1</code> for all <code>0 &lt;= i &lt; n - 1</code>.</p>

<p><code>queries[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> represents the addition of a new <strong>unidirectional</strong> road from city <code>u<sub>i</sub></code> to city <code>v<sub>i</sub></code>. After each query, you need to find the <strong>length</strong> of the <strong>shortest path</strong> from city <code>0</code> to city <code>n - 1</code>.</p>

<p>Return an array <code>answer</code> where for each <code>i</code> in the range <code>[0, queries.length - 1]</code>, <code>answer[i]</code> is the <em>length of the shortest path</em> from city <code>0</code> to city <code>n - 1</code> after processing the <strong>first </strong><code>i + 1</code> queries.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, queries = [[2,4],[0,2],[0,4]]</span></p>

<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>

<p><strong>Explanation: </strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/28/image8.jpg" style="width: 350px; height: 60px;" /></p>

<p>After the addition of the road from 2 to 4, the length of the shortest path from 0 to 4 is 3.</p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/28/image9.jpg" style="width: 350px; height: 60px;" /></p>

<p>After the addition of the road from 0 to 2, the length of the shortest path from 0 to 4 is 2.</p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/28/image10.jpg" style="width: 350px; height: 96px;" /></p>

<p>After the addition of the road from 0 to 4, the length of the shortest path from 0 to 4 is 1.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 4, queries = [[0,3],[0,2]]</span></p>

<p><strong>Output:</strong> <span class="example-io">[1,1]</span></p>

<p><strong>Explanation:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/28/image11.jpg" style="width: 300px; height: 70px;" /></p>

<p>After the addition of the road from 0 to 3, the length of the shortest path from 0 to 3 is 1.</p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/28/image12.jpg" style="width: 300px; height: 70px;" /></p>

<p>After the addition of the road from 0 to 2, the length of the shortest path remains 1.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>3 &lt;= n &lt;= 500</code></li>
	<li><code>1 &lt;= queries.length &lt;= 500</code></li>
	<li><code>queries[i].length == 2</code></li>
	<li><code>0 &lt;= queries[i][0] &lt; queries[i][1] &lt; n</code></li>
	<li><code>1 &lt; queries[i][1] - queries[i][0]</code></li>
	<li>There are no repeated roads among the queries.</li>
</ul>

class Solution:
    def copied(self, n, queries):
        graph = [[] for _ in range(n)]
        for i in range(n-1): 
            graph[i].append(i+1)
        
        def bfs(graph): 
            """Return """
            queue = deque([0])
            seen = {0}
            ans = 0 
            while queue: 
                for _ in range(len(queue)): 
                    u = queue.popleft()
                    if u == n-1: return ans 
                    for v in graph[u]: 
                        if v not in seen: 
                            seen.add(v)
                            queue.append(v)
                ans += 1
                
                
        ans = []   
        for u, v in queries: 
            graph[u].append(v)
            ans.append(bfs(graph))
        return ans 

    def my_bfs(self, n, queries):
        def get_min_distance(neighbors):
            queue = [0]
            seen = {0}
            dist = 0
            while len(queue):
                
                next_queue = []
                for v in queue:
                    if v == n-1:
                        return dist
                    for q in neighbors[v]:
                        if q not in seen:
                            next_queue.append(q)
                            seen.add(q)
                dist += 1
                queue = next_queue
            return -1
        neig = []
        for i in range(n):
            temp = []
            if i+1 < n:
                temp.append(i+1)
            neig.append(temp)
        
        result = []
        for n1, n2 in queries:
            neig[n1].append(n2)
            dist = get_min_distance(neig)
            result.append(dist)
        return result

    def shortestDistanceAfterQueries(self, n: int, queries: List[List[int]]) -> List[int]:
        #return self.copied(n, queries)
        return self.my_bfs(n, queries)
        dist = []
        target_list = []
        for i in range(n):
            dist.append(i)
            next_target = set()
            if i+1 < n:
                next_target.add(i+1)
            target_list.append(next_target)
        
        queries.sort()
        res = []
        for s, d in queries:
            target_list[s].add(d)
            queue = [s]
            while len(queue):
                cur = queue.pop(0)
                for neig in target_list[cur]:
                    
                    dist[neig] = min(dist[neig], dist[cur]+1)
                    queue.append(neig)
            
            res.append(dist[n-1])
        return res

624