We are asked: Given an undirected graph with edges having integer intervals [l_k, r_k]. Seryozha picks an integer x before starting movement. He can traverse a path (sequence of edges) from node 1 to node n only if for every edge on the path, x ∈ [l_k, r_k]. The loyalty of a path is defined as the number of integers x such that the entire path is traversable (i.e., x belongs to all intervals of edges on the path). Actually: "the number of integers x, such that if he initially chooses one of them, he passes the whole path." So it's the size of the set of x such that x is within all intervals along the path. That is essentially the intersection of the intervals on the path: If the path's intervals have intersection [L, R] (where L = max(l_i), R = min(r_i)), then loyalty = max(0, R - L + 1). So the loyalty is the length of the common intersection of intervals along the path. We need to find the maximum possible loyalty over all paths from 1 to n.
If no path exists or the maximum loyalty is 0, output "Nice work, Dima!".
n ≤ 1000, m ≤ 3000. Intervals bounds up to 1e6.
We need to compute: For each path, intersection length = (min(r_i) - max(l_i) + 1) if positive else 0. We want max over all paths.
We could consider that the loyalty depends on both min of r's and max of l's. That's like a bottleneck: we want to maximize (R - L + 1) where L = max(l_i), R = min(r_i) over edges in the path. Equivalent to finding a path that maximizes (min(r_i) - max(l_i)). This is reminiscent of two-parameter optimization.
One approach: iterate over possible L and R? Since intervals up to 1e6, but m only 3000, maybe we can consider events? Another way: For each edge, we can think of x as candidate. But loyalty is count of x's that work for a given path, so we need to find x that are in intersection. The maximum loyalty path will be the path that has the largest number of x values for which the path is traversable. Equivalent: find an interval of x values (contiguous integer interval) such that there exists a path from 1 to n where all edges are valid for those x, and maximize length of that interval. That is: we want to find a maximum length integer interval [a,b] such that the graph (with edges that contain the whole interval? Wait careful: For a given x, we can traverse edges if x ∈ [l,r]. For an interval of x, if we want a path that works for all x in the interval, then for each edge on the path, we need [a,b] ⊆ [l_i, r_i] because the edge must be traversable for each x in the interval. But the loyalty definition: number of integers x such that if he initially chooses one of them, he passes the whole path. That means we consider each x separately: For a fixed x, we ask is there a path from 1 to n where each edge contains x? Then the loyalty of a path is the count of x for which the path is valid (i.e., for that x, the path exists with all edges containing x). But note: The problem says: "He defined the loyalty of some path from the 1-st node to the n-th one as the number of integers x, such that if he initially chooses one of them, he passes the whole path." That means for a specific path P, the loyalty is # of x such that ∀ edge e in P, x∈[l_e, r_e]. That's exactly the length of intersection of intervals on P. So the path's loyalty is independent of other paths; it's a property of the path. We want max over paths of that intersection length.
Now, can there be two different paths that both are valid for some x? Possibly, but that doesn't matter. So we need to compute max over paths of (min r_i - max l_i + 1) if positive.
Classic problem: Given a graph with intervals on edges, maximize min(r_i) - max(l_i). This can be solved by sorting intervals by l, or using two-pointer approach, or by binary search on length? Let's think.
We can think of it as: For each possible value of L (the maximum l_i on the path), we want to maximize min(r_i) among paths where all edges have l_i ≤ L? Not exactly.
Let's formalize: For a path, define L = max(l_i), R = min(r_i). The path is feasible for any x in [L,R] if L ≤ R. The length is R - L + 1.
We want max_{paths} (R - L + 1). Equivalent to max_{paths} (min(r_i) - max(l_i) + 1).
Observe that for a fixed L, we want to find a path from 1 to n such that max(l_i) = L (or ≤ L, but the actual max is at most L, but we need L as the maximum; however to maximize R-L, we may want the maximum l_i to be as low as possible while min r_i as high as possible). So we can treat L as a threshold: consider only edges with l_i ≤ L (since any edge with l_i > L cannot be on a path with max(l_i)=L because if we include such edge, max would be at least that l_i > L). But if we restrict to edges with l_i ≤ L, then any path in that subgraph will have max(l_i) ≤ L, not necessarily equal to L. The loyalty for that path would be (min(r_i) - L' + 1) where L' ≤ L is actual max. But if we calculate using L as the lower bound on the x (the intersection's left), we would need to consider actual L' which might be less than L. That would yield a larger length? Wait: For a path, the left endpoint of the intersection is max(l_i). So if we consider a candidate L (as the left bound we intend), we could consider paths where all l_i ≤ L, but the actual max may be smaller, say L0. Then the intersection is [L0, R]. So the length is R - L0 + 1. Since L0 ≤ L, this length could be greater than R - L + 1. But we are free to choose any path; we don't have to fix L. So it's not simple to fix L.
Alternative: We can treat the problem as find a path that maximizes (min r - max l). This can be solved by binary search on the length D? If we guess a length LEN (integer), we can ask if there exists a path with min r - max l + 1 ≥ LEN, i.e., min r - max l ≥ LEN-1. That is like: Is there a path such that min(r_i) - max(l_i) >= K? That is a typical feasibility check. Could we check existence of a path with min r - max l ≥ K? We can try to treat it as: find a path where for all edges, r_i - l_i? Not directly.
Another transformation: For a path, let a_i = l_i, b_i = r_i. The condition min(b) - max(a) >= K means there exists some X such that for all edges, a_i ≤ X ≤ b_i? Wait if min(b) - max(a) >= K, then there exists some x such that max(a) ≤ x ≤ min(b). Actually the existence of x is equivalent to max(a) ≤ min(b). The length is min(b) - max(a) + 1. So the condition length >= LEN is equivalent to min(b) - max(a) >= LEN-1, i.e., max(a) + (LEN-1) ≤ min(b). This is like the path's "gap" between max(l) and min(r) is at least some value.
We can think of it as: choose two numbers L and R such that R - L + 1 = loyalty, and there is a path where all edges satisfy L ≤ l_i? Actually to have intersection at least [L,R], we need every edge on the path to have l_i ≤ L and r_i ≥ R? Wait if the intersection of intervals contains [L,R], then for each edge, [L,R] ⊆ [l_i, r_i]. That means l_i ≤ L and r_i ≥ R. But the intersection of intervals might be larger; we only care about the count, not the specific interval. For a given path, the intersection is exactly [max(l_i), min(r_i)]. So it is determined by the extreme values. So to achieve high loyalty, we need both a low max(l_i) and a high min(r_i). So we want to pick a path that simultaneously minimizes the maximum l_i and maximizes the minimum r_i. This is like a bi-criteria optimization.
We can approach by enumerating which edge gives the min(r) (the bottleneck on the right) and which gives the max(l) (the bottleneck on the left), but that might be O(m^2).
Given constraints n ≤ 1000, m ≤ 3000, maybe we can do O(m^2) or O(m * something). 3000^2 = 9e6, which is okay. But we also need to check connectivity under constraints.
Consider sorting edges by l (left endpoint). Then we can iterate over edges as the "maximum l" candidate: i.e., we consider paths where the maximum l is exactly l_i (or at most but we can treat as fixing a lower bound). Alternatively, we can consider for each edge as the one giving the min r (or max l). Actually, we could use a two-pointer approach akin to "maximum interval length in union of intersections" or use binary search on the result and check via BFS/DFS with constraints.
Another known technique: For each possible value of L (or r), we can consider the graph with edges where l_i ≤ L and r_i ≥ something. But we need to capture both.
We can think of this as: we want to maximize (R - L + 1) where there exists a path such that for all edges, l_i ≤ L and r_i ≥ R? No: For a given path, L = max(l_i), R = min(r_i). So if we fix L and R, we require that all edges have l_i ≤ L and r_i ≥ R, and also at least one edge has l_i = L (or at least not exceed L) but that's fine. So existence of a path from 1 to n in the subgraph consisting of edges with l_i ≤ L and r_i ≥ R ensures that there is some path whose max(l) ≤ L and min(r) ≥ R, so the intersection contains at least [L,R], and the actual intersection length is at least (R-L+1). But note: If the path's actual max(l) is less than L, then the intersection is even larger than [L,R] (since left endpoint is smaller). That only increases the loyalty, but the loyalty computed as (min(r) - max(l) + 1) would be ≥ (R - L + 1). So if we can find any L,R such that there exists a path using edges all satisfying l_i ≤ L and r_i ≥ R, then there is a path with loyalty at least (R-L+1). So to find the maximum possible loyalty, we can consider all pairs (L,R) where there is a path in the subgraph G_{L,R} = {edges: l_i ≤ L and r_i ≥ R}. Then the answer is max_{L,R} (R - L + 1) over those pairs where a path exists. However, note that the actual loyalty of the path might be larger than that because the actual L' = max(l_i) could be < L and/or R' = min(r_i) > R. But if we consider the actual L',R' for that path, then (R' - L' + 1) is at least (R - L + 1). So the maximum loyalty over all paths equals the maximum over all (L,R) such that there is a path in G_{L,R} of the quantity (R - L + 1). But is it possible that the maximum over (L,R) could be larger than the actual max path loyalty? No, because if a path exists in G_{L,R}, then there is a specific path whose max(l) ≤ L and min(r) ≥ R, so its loyalty ≥ (R-L+1). Conversely, for any path with actual L*,R*, we can take L = L*, R = R* and obviously the path exists in G_{L*,R*} (since each edge satisfies l_i ≤ L* and r_i ≥ R*? Wait: For the path, L* = max(l_i), so each edge has l_i ≤ L*. Yes, by definition max. Similarly, each edge has r_i ≥ R* because R* = min(r_i). So the path is in G_{L*,R*}. So the maximum loyalty among paths equals max_{(L,R): G_{L,R} contains a 1-n path} (R-L+1). Thus we can reframe: find an interval [L,R] (with integer endpoints) such that there exists a path from 1 to n using only edges whose intervals contain the whole [L,R] (i.e., l ≤ L and r ≥ R) and maximize the length R-L+1.
Now this is reminiscent of finding maximum width interval that is contained in all edges of some path? Actually it's like we want to find two numbers L,R such that there exists a path where each edge's interval covers [L,R]; equivalently, the set of edges that cover [L,R] must connect 1 and n. So the problem reduces to: Consider each edge e with interval [l_e, r_e]. For a given x (single point), the edge is usable if x ∈ [l_e, r_e]. The graph of usable edges for a given x connects 1 and n? But now we require that the same integer x works for the entire path; but if we require an interval [L,R] to be covered, it's equivalent to requiring that for any x in [L,R], the edge is usable, i.e., the edge's interval must contain the whole interval [L,R]. This is stronger than just covering a point. However, we can notice that if an edge covers [L,R], then it covers every point in between, especially the left endpoint L and right endpoint R. So it must satisfy l ≤ L and r ≥ R. So the condition is: l_e ≤ L and r_e ≥ R. That is exactly two constraints. For a fixed L, the condition l_e ≤ L; for a fixed R, r_e ≥ R.
Now we need to maximize R-L+1. Since L and R are integers from 1 to 1e6, we could try to iterate over possible L or R, but might be too many.
But m is only 3000, so the number of distinct l_i and r_i values is at most 6000. Actually l and r are up to 1e6, but there are at most 3000 edges, so there are at most 3000 distinct l_i and 3000 distinct r_i. That's a manageable set of candidate breakpoints. The feasible interval [L,R] that can be candidate for optimal solution can be assumed to have L equal to some l_i or maybe l_i - 1? Because if we have a solution where L is not equal to any l_i, we could increase L up to the next l_i? Let's think.
For a given optimal path, the left endpoint L* = max(l_i) on that path is itself some l_j of an edge in the path. So L* is one of the l_i values. Similarly, R* = min(r_i) is one of the r_i values. So the optimal interval's endpoints are among the set of all l_i and r_i? Actually R* is min of r_i, so it is some r_k from an edge in the path. So the optimal (L,R) pair that yields the maximum loyalty can be taken with L = max(l_i) and R = min(r_i) from that optimal path. Therefore, L is some l_i and R is some r_j, and we only need to consider L from the set of all l_i values, and R from the set of all r_i values. There are at most 3000 each, so up to 9e6 pairs. That's okay for O(m^2) perhaps, but we need to test connectivity for each pair? That would be too heavy if we do a BFS per pair (9e6 * (n+m) is huge). However, we can be smarter: We can iterate over L (sorted ascending) and maintain something to compute the maximal R such that connectivity exists? Or use two-pointer technique.
Since we need to maximize R-L, we can think of iterating over edges sorted by l (or L) and, for each L, find the maximum R such that there exists a path using only edges with l_i ≤ L and r_i ≥ R. This is like for a fixed L, we want to know the maximum possible R for which the subgraph of edges with l_i ≤ L and r_i ≥ R connects 1 and n. The subgraph condition: edges must satisfy l_i ≤ L and r_i ≥ R. For a fixed L, as R increases, the set of edges shrinks (since we require larger r_i). So connectivity is monotonic: If there is a path for a given R, then for any lower R' < R, the graph includes more edges, so connectivity remains. Actually careful: For fixed L, as R increases, the condition r_i ≥ R becomes stricter, so the set of allowed edges can only shrink or stay same. Thus connectivity is monotonic decreasing with R: if there is a 1-n path at R, then for any R' ≤ R, there is also a path? Wait if we lower R, we relax condition, so the set of allowed edges becomes larger. So if we have a path at R, then at any smaller R, the same edges still satisfy r_i ≥ R' (since r_i ≥ R ≥ R'), so they are still allowed. Actually if r_i ≥ R, then for R' < R, also r_i ≥ R'. So the same path remains allowed. So for fixed L, if there is a path at R, then there is also a path for all R' < R (since the path's edges satisfy r_i ≥ R ≥ R'). So indeed the set of R for which connectivity holds (for fixed L) is an interval from 1 up to some maximum R_max(L) (maybe inclusive) where R_max(L) is the maximum R such that there is a path. The connectivity exists for all R ≤ R_max(L). So R_max(L) is the "best" R for that L. Then the candidate loyalty is R_max(L) - L + 1 (provided R_max(L) ≥ L). We want max over L of that.
Thus we can compute, for each L (from the set of l_i values perhaps), the maximum R such that there is a path using edges with l_i ≤ L and r_i ≥ R. Then answer = max_{L} (R_max(L) - L + 1) if R_max(L) >= L, else not considered.
Now how to compute R_max(L) efficiently? Since m is 3000, we could perhaps preprocess edges, sort them by l, and then for each L, consider a graph that includes edges with l_i ≤ L. Then the R_max(L) is essentially the maximum value such that the graph of edges with l_i ≤ L and r_i ≥ R connects 1 and n. This is similar to finding the maximum "bottleneck" R from node 1 to n in the graph where each edge has a weight r_i, and we only consider edges with l_i ≤ L. But we want the maximum R such that there exists a path where the minimum r_i along the path is at least R. For a given set of edges (those with l_i ≤ L), the maximum R such that there is a 1-n path with min(r_i) ≥ R is exactly the value of the maximum capacity path in terms of the minimum edge r_i: i.e., the maximum over paths of the minimum r_i along the path. This is the classic "maximin" path (also known as the path that maximizes the minimum edge weight). In graph terms, it's the maximum bottleneck path. For an undirected graph with edge weights r_i, the maximum possible minimum r_i on a path from s to t is the maximum value R such that s and t are connected in the subgraph of edges with r_i ≥ R. This is exactly the same: For fixed L, we consider edges with l_i ≤ L. Among those, we want the highest R such that there is a path using only edges with r_i ≥ R. That R is the maximum bottleneck value from 1 to n considering only those edges.
Thus R_max(L) = maximum over all paths (in the graph restricted to edges with l_i ≤ L) of min(r_i) along the path. This can be computed by finding the maximum spanning tree (in terms of r_i) or using a variant of Dijkstra (modified to maximize min). Actually the classic solution: Compute the maximum capacity path: we can sort edges by r_i descending and add them incrementally until 1 and n become connected; the r_i of the edge that connects them gives the maximum bottleneck. But that is for the whole graph. Here we have an additional constraint l_i ≤ L, which depends on L. So we need to answer for many L values.
We can pre-sort edges by l ascending. When we increase L, we add edges with l_i ≤ L. For each L, we need to compute the maximum bottleneck R (i.e., the maximum min(r) path) in the dynamic graph as we add edges. This looks like a two-dimensional offline problem: edges have two parameters (l, r). We want to query, for a given L, the maximum bottleneck R in the graph formed by edges with l ≤ L. And we also need to consider that R must be ≥ L to get positive loyalty.
So we could sweep L increasing, maintaining a data structure that supports adding edges (with weight r) and queries: what is the maximum bottleneck between node 1 and n? The maximum bottleneck is essentially the minimum r value on the best path, but we can also think of it as: connectivity threshold. If we maintain a union-find (DSU) over edges sorted by r descending, but edges are added over time as L increases. However, we need to handle both constraints simultaneously. Let's think.
Approach 1: Since n and m are small (n=1000,m=3000), we might be able to compute R_max(L) for each distinct L by running a max-bottleneck algorithm (like modified Dijkstra) on the graph consisting of edges with l_i ≤ L, which would be O(m) per L. There are at most m distinct L values? Actually l_i values are at most 3000 distinct (maybe less). So O(m * (m + n log n)) ~ 30003000 = 9e6, plus log factors, which is acceptable. Actually 9e6 operations is fine. But the max-bottleneck algorithm: we can use Dijkstra-like with priority queue maximizing min r, which is O(m log n) per run. That's 3000 * (3000 log 1000) ~ 3000300010? Actually 3000 * (3000 log 1000) ~ 30003000*10 ~ 90 million, which might be okay in C++ but we need to be careful. 9e6 edges processed overall? Let's compute: For each L (distinct l), we would examine all edges with l ≤ L (varying set) and run a BFS/DFS? Actually Dijkstra-like for max-min: we can treat each edge weight r. To compute maximum bottleneck from 1 to n, we can use a variant: start from node 1 with bottleneck = INF (or large). Use priority queue on bottleneck value, pop node with highest bottleneck, relax neighbors: new_bottleneck = min(current_bottleneck, r). If new_bottleneck > best[neighbor], update. This runs in O(m log n). For each L, the number of edges considered is all edges with l ≤ L, which at max is m. So total time for all L would be O(K * (m log n)), where K = number of distinct L values (≤ m). That's O(m^2 log n) ≈ 9e6 * log n ~ 9e6 * 10 = 90e6, maybe borderline but could pass in optimized C++? Possibly, but Python may struggle. However, we can do better.
Alternative approach: Since m is only 3000, we could even do O(m^2) by enumerating both L and R and testing connectivity with a simple BFS for each pair? 9e6 BFS with each BFS O(m) would be 27e9, too much.
Better: For each L, we can compute R_max(L) more efficiently by pre-sorting edges by r descending and using DSU as we sweep R decreasing, but with a fixed L we could use offline technique: process L in increasing order, maintain a DSU over edges that have l ≤ L. However, we need to answer: "what is the maximum R such that 1 and n are connected using edges with r ≥ R?" This is essentially the maximum r threshold. In a static graph (set of edges), the maximum bottleneck R is the largest r such that the subgraph of edges with r ≥ R connects 1 and n. That can be found by sorting edges by r descending and union-find until 1 and n become connected. The r value at which they first become connected is the max bottleneck. So for a fixed L, if we have the set of edges with l ≤ L, we can compute max bottleneck by sorting those edges by r descending and doing union-find. But doing that for each L from scratch would be O(m^2 α(n)). Still maybe okay? Let's compute: for each L (max 3000), we sort edges with l ≤ L by r descending. Sorting each subset each time would be heavy. But we can pre-sort edges by l, and as we increase L, we add edges. However, the max bottleneck query would need to process edges sorted by r. Could we maintain a DSU that supports deletions? Not easily. But we could use offline approach by enumerating both parameters: Since both l and r are from limited set, we could try to compute for each possible R (from distinct r values) the minimum L needed to connect 1 and n with edges satisfying l ≤ L and r ≥ R. Then the loyalty for a given (L,R) where connectivity holds is R-L+1, and we want max.
Define f(R) = minimum L such that there is a 1-n path using edges with l_i ≤ L and r_i ≥ R. If such L exists, then for any L' ≥ f(R), connectivity holds. For a fixed R, the condition for connectivity is monotonic in L: as L increases, we add more edges (those with l ≤ L), so if connectivity holds for L, it also holds for any larger L. So f(R) is the minimal L at which connectivity appears. Then for any L ≥ f(R), the pair (L,R) is feasible. The loyalty would be R - L + 1. To maximize this, we could consider for each R, we want to choose L as small as possible (to maximize R-L+1), but L must be ≥ f(R). So the best loyalty for that R is (R - f(R) + 1) provided f(R) ≤ R. And then answer = max over R of that. So if we can compute f(R) for each R (distinct r_i values), we can get answer.
Similarly, we could also consider symmetric: For each L, R_max(L) is maximum R with connectivity, and we want max over L of (R_max(L) - L + 1). This is symmetric to the above if we compute f(R). So we need to compute, for each R, the minimum L needed.
Now how to compute f(R) efficiently? For a fixed R, we consider the set of edges with r_i ≥ R. Among those, we need to find the smallest L such that there is a 1-n path using only edges with l_i ≤ L (and automatically r_i ≥ R). In other words, we need to find the path that minimizes the maximum l_i subject to each edge having r_i ≥ R. That is a minimax problem: find a path from 1 to n where each edge has r_i ≥ R, and minimize the maximum l_i along the path. That's similar to the earlier but with swapped roles. So for each R, we want the minimum L such that there is a path with min(r_i) ≥ R (i.e., all edges have r_i ≥ R) and max(l_i) ≤ L. So L_min(R) = minimum over paths (with all edges r_i ≥ R) of the max(l_i). This is like "given a graph with edge weights l_i, find the minimum possible maximum l along a path from 1 to n, but only considering edges with r_i ≥ R". This is a minimax path problem, can be solved by Dijkstra-like with minimizing max l: start from 1, maintain min-max l, explore edges only if r_i ≥ R. That's O(m log n) per R. Again, too many.
But we can also reverse sweep: Sort edges by r descending, add edges gradually. As we increase R (or rather decrease R, since we want to consider larger R first?), we can maintain something about L. Let's think: We want to compute for each R the minimal L. If we process R in decreasing order (from high to low), we add edges with r_i ≥ R. As we add edges, the graph becomes more connected. For each newly added edge, we might be able to improve (reduce) the minimal max-l path between nodes. However, the minimal max-l path in a graph can be maintained using a DSU with a kind of "minimum spanning tree" in terms of l? Actually, we want for each pair (1, n) the minimum possible maximum l along a path. This is exactly the minimax path value, which is given by the minimum spanning tree (MST) in terms of l: In any graph, the minimax path between two nodes (minimizing the maximum edge weight) is the value of the maximum edge on the path between them in the Minimum Spanning Tree (if we consider l as weight). Actually classic: For a graph with edge weights, the minimum possible maximum edge weight along a path between two vertices equals the weight of the maximum edge on the unique path between them in a Minimum Spanning Tree (or equivalently, the minimum bottleneck spanning tree). More precisely, the minimax path (also called the minimax distance) can be found by constructing a Minimum Spanning Tree (MST) and then the maximum edge weight on the path in the MST is the answer. This holds for any undirected graph: the MST minimizes the maximum edge weight on paths? Actually, it's known that for any two vertices, the path in the MST is a minimax path: it minimizes the maximum edge weight along the path. That is because MST is a minimum bottleneck spanning tree. Yes, a Minimum Bottleneck Spanning Tree (MBST) is a spanning tree that minimizes the maximum edge weight. Any MST is an MBST. So if we have a spanning tree that minimizes the maximum edge weight, then for any two vertices, the maximum edge weight on the unique path between them in that tree is the minimum possible maximum edge weight over all paths between them. So indeed, the minimax value between s and t can be computed by building an MST (or MBST) of the graph. However, this is true for a static graph with a single weight per edge. Here we have two weights, but we are fixing R, i.e., restricting to edges with r_i ≥ R. So for each R, we have a subgraph G_R = {edges: r_i ≥ R}. In that subgraph, we want the minimax value of l: i.e., the smallest L such that there is a path with all l_i ≤ L. This L is exactly the maximum l along the minimax path, which equals the maximum l on the path between 1 and n in an MBST of G_R (with respect to l). However, we could compute this more efficiently by processing edges in a certain order.
Idea: Since we want to compute f(R) = minimum L such that 1 and n are connected using edges with r_i ≥ R and l_i ≤ L. Another way: consider edges sorted by l (ascending). For each L, we know the max bottleneck R. That's symmetric. But maybe we can do a two-dimensional sweep: sort edges by l ascending and r descending, and use DSU with union by something.
Alternatively, we could use the fact that m is small enough to consider all possible pairs (i, j) where i is the edge with the maximum l (i.e., the bottleneck on left) and j is the edge with the minimum r (bottleneck on right) along the path. For a given pair (i, j), we ask: Is there a path from 1 to n such that for all edges e on the path, l_e ≤ l_i and r_e ≥ r_j, and also at least one edge has l_e = l_i and at least one edge has r_e = r_j? Actually for the path's max l to be l_i and min r to be r_j, we need that no edge has l > l_i (so all l ≤ l_i) and no edge has r < r_j (so all r ≥ r_j). Additionally, we need that there exists at least one edge with l = l_i (to be the max) and at least one edge with r = r_j (to be the min), but that's automatically if we don't require equality; the loyalty computed from the path would be (min(r) - max(l) + 1) which would be at least (r_j - l_i + 1) if min(r) ≥ r_j and max(l) ≤ l_i. The actual loyalty might be larger if the min(r) is > r_j or max(l) < l_i. But if we want to achieve a specific (l_i, r_j) pair as candidate, we can just consider paths that satisfy l ≤ l_i and r ≥ r_j. The best loyalty we can get from such a path is at least (r_j - l_i + 1) if r_j ≥ l_i. However, if there exists a path with l ≤ l_i and r ≥ r_j, then there exists some path whose actual max(l) is some L' ≤ l_i and min(r) is some R' ≥ r_j, giving loyalty R'-L'+1 ≥ r_j - l_i + 1. So we don't need to enforce equality. Therefore, we can simply consider all pairs (L,R) where L is some l_i and R is some r_j, and check if there is a path using only edges with l ≤ L and r ≥ R. The maximum loyalty is max_{L,R: exists path} (R-L+1). That is our earlier formulation.
Given the small m, we could enumerate all distinct L values (from l_i) and for each L, compute R_max(L) by running a max-min (bottleneck) algorithm. That seems straightforward and likely to be fast enough. Since n <= 1000, m <= 3000, and distinct L values <= 3000. For each L, we can compute the maximum bottleneck R from 1 to n in the graph restricted to edges with l ≤ L. This can be done by:
- Filter edges: only those with l ≤ L.
- If number of edges is E_L, run algorithm to find maximum bottleneck. The standard algorithm: sort edges by r descending, then union-find add edges until 1 and n become connected. The r value of the edge that connects them is the maximum bottleneck. That's O(E_L log E_L) per L due to sorting. But if we do this for each L separately, total could be O(∑{L} E_L log E_L). Since edges are added as L increases, the total sum of E_L over all L could be O(m^2) in worst case (if each L includes more edges). Actually if we have m edges with distinct l_i sorted: l1 < l2 < ... < lm. For each L = l_k, we include edges with l ≤ l_k, i.e., first k edges (if sorted). So E{l_k} = k. Sum_{k=1}^{m} k = m(m+1)/2 = O(m^2). Plus sorting each subset? But if we process L in increasing order, we could maintain a list of edges sorted by r, and as we add a new edge (with l = current L), we insert it into a sorted structure. Then we can compute max bottleneck by performing something like: we need to find the largest R such that union-find with edges having r ≥ R connects 1 and n. If we have all edges (with l ≤ L) in a data structure sorted by r descending, we could simulate adding edges from highest r down until connectivity, but that would require re-doing the union-find from scratch each time? But we can use a technique: we are processing L increasing, and we add edges one by one. For each new L, we want to compute the current maximum bottleneck. If we maintain a DSU and process edges in decreasing r order, but edges are being added over time, not all at once. However, we can use offline approach: For each possible R (distinct r values), we can precompute the minimum L needed. That's the other sweep.
Maybe the simplest: Since m=3000, O(m^2) = 9e6, we could for each pair of edges (i,j) or each pair (L,R) but that would be too many BFS. But O(m^2) operations of union-find or simple operations could be fine if each operation is cheap. For each L (distinct l values), we could run a BFS/DFS that only traverses edges with l ≤ L, and as we traverse, we compute the maximum min r along paths? Actually we can compute the max bottleneck in O(m log n) using Dijkstra-like, but maybe we can compute it more simply: Since r is up to 1e6, we could binary search on R for each L, but that would multiply by log 1e6 ~ 20, and each check BFS O(m) => O(m^2 log 1e6) = 60 million, which is okay. Let's evaluate: For each distinct L (≤3000), we binary search on R (possible values among r_i). We need to check connectivity in graph with edges satisfying l ≤ L and r ≥ mid. That's a BFS/DFS O(m). The number of binary search steps ~ log(3000) ~ 12 if we only consider distinct r values, but we need to consider integer range? Actually R is integer, but we only need to consider r values that are present? Because max bottleneck R_max(L) will be some r_i from the edges. It could be any integer between r values? Let's think: The maximum bottleneck is defined as the maximum value R such that there is a path with min(r_i) ≥ R. If we have edges with r_i values, the set of R for which connectivity holds is: all R ≤ R_max(L), where R_max(L) is the maximum bottleneck. This R_max(L) is exactly the maximum over paths of min(r_i). That value is itself a min of some edges' r_i on a path, so it equals some r_i. So indeed, R_max(L) is one of the r_i values. So we can restrict R candidate set to the distinct r_i values (at most 3000). So for each L, we can binary search over sorted list of distinct r_i values, performing a connectivity check each time. Complexity: For each L (up to 3000), we do O(log M) BFS where M ≤ 3000 (distinct r values). So at most about 12 BFS per L, each BFS O(m) = 3000 => total operations ~ 3000 * 12 * 3000 = 108 million edge checks