Little town Nsk consists of n junctions connected by m bidirectional roads. Each road connects two distinct junctions and no two roads connect the same pair of junctions. It is possible to get from any junction to any other junction by these roads. The distance between two junctions is equal to the minimum possible number of roads on a path between them.
In order to improve the transportation system, the city council asks mayor to build one new road. The problem is that the mayor has just bought a wonderful new car and he really enjoys a ride from his home, located near junction s to work located near junction t. Thus, he wants to build a new road in such a way that the distance between these two junctions won't decrease.
You are assigned a task to compute the number of pairs of junctions that are not connected by the road, such that if the new road between these two junctions is built the distance between s and t won't decrease.
Input
The firt line of the input contains integers n, m, s and t (2 ≤ n ≤ 1000, 1 ≤ m ≤ 1000, 1 ≤ s, t ≤ n, s ≠ t) — the number of junctions and the number of roads in Nsk, as well as the indices of junctions where mayors home and work are located respectively. The i-th of the following m lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi), meaning that this road connects junctions ui and vi directly. It is guaranteed that there is a path between any two junctions and no two roads connect the same pair of junctions.
Output
Print one integer — the number of pairs of junctions not connected by a direct road, such that building a road between these two junctions won't decrease the distance between junctions s and t.
Examples
Input
5 4 1 5
1 2
2 3
3 4
4 5
Output
0
Input
5 4 3 5
1 2
2 3
3 4
4 5
Output
5
Input
5 6 1 5
1 2
1 3
1 4
4 5
3 5
2 5
Output
3
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to solve: given an undirected connected graph with n nodes, m existing edges. We want to count the number of unordered pairs (u,v) such that there is no existing edge between u and v, and adding edge (u,v) does NOT decrease the distance between s and t. In other words, the shortest path distance after adding (u,v) is at least the original distance d = dist_original(s, t). The new edge could potentially create a shortcut, so we need to count pairs that do not reduce the distance.
n up to 1000, m up to 1000 (sparse). We need to compute for all non-edges (including all possible pairs not in edge set). Total pairs = n*(n-1)/2, m ≤ 1000, so number of non-edges is up to ~500k for n=1000. That's manageable if we can evaluate each quickly.
We need to check condition: after adding edge (u,v), the distance between s and t does not become less than original d. Let d0 = dist(s,t) in original graph.
When we add edge (u,v) of length 1, the new shortest path distance between s and t is min( d0, dist(s,u) + 1 + dist(v,t), dist(s,v) + 1 + dist(u,t) ). Actually we consider both directions because undirected. So new distance d' = min( d0, ds[u] + 1 + dt[v], ds[v] + 1 + dt[u] ), where ds[x] = original shortest distance from s to x, dt[x] = original shortest distance from t to x. (We could also have paths that use the new edge in the middle, but this is captured by the two expressions, as the path could go s -> ... -> u, then edge (u,v), then v -> ... -> t, or the reverse starting from v then u. There could be paths that use the new edge twice? Not beneficial.)
Thus, the distance will not decrease iff both ds[u] + 1 + dt[v] >= d0 and ds[v] + 1 + dt[u] >= d0. Actually we need that the min of those candidates is >= d0, i.e., ds[u] + 1 + dt[v] >= d0 AND ds[v] + 1 + dt[u] >= d0 (since d0 itself is also considered; but if either candidate is < d0, then min becomes < d0 -> decrease). So condition: ds[u] + 1 + dt[v] >= d0 AND ds[v] + 1 + dt[u] >= d0.
But is there any scenario where adding an edge could shorten the distance via some longer path that includes the new edge and also other edges? Actually the expression ds[u] + 1 + dt[v] is the length of a path s->u (shortest) + new edge + v->t (shortest). This is a specific candidate. Any path that uses the new edge exactly once will be of that form: s to one endpoint via some path, the edge, then to t from the other endpoint. Since we use shortest distances from s to endpoint and endpoint to t (which are shortest among paths that don't use the new edge, but using the new edge twice is not beneficial, and we can consider that maybe using the new edge plus some other segments, but the shortest from s to endpoint ignoring new edge is not necessarily the shortest when the new edge is present because the new edge could create shortcuts to reach endpoints. However, the shortest path that uses the new edge might also use it in a more complex way: e.g., s -> ... -> u (maybe using new edge en route) but then using new edge again? Actually to use the new edge exactly once, the path consists of three segments: s to u (possibly using the new edge? but if you use new edge earlier, then you'd be using it more than once if you later go from u to v, because that edge is also the new edge? But the path could go s -> ... -> u via a path that does not include the new edge, because if it included the new edge, you'd be counting it twice. However, could there be a shorter path that uses the new edge, but not in the simple "s -> ... -> u" using old graph? The candidate we compute is: take shortest path from s to u in original graph (no new edge), then the new edge, then shortest path from v to t in original graph. But maybe there is a path that goes s -> ... -> w, then uses new edge (w, x) different? Wait we only consider (u,v) as the added edge. So if the path uses new edge, it must use (u,v) exactly once. However, the segments from s to u and from v to t could themselves be improved by the presence of the new edge, because they could also use the new edge. But if they use the new edge, then the overall path would use it more than once (since you could use it again later) or could use it in the opposite direction? But a shortest path that uses the new edge could be such that you go s -> ... -> v (using new edge? Actually you could go s -> ... -> v via new edge? To reach u you might go s -> ... -> v -> u via the new edge? That would use new edge as part of the s-to-u segment, and then use it again to go from u to v? That would be using it twice; not beneficial. Or you could go s -> ... -> u without using new edge, then new edge to v, then v->...->t without using new edge. That's the candidate we considered. Could there be a shorter path that does s -> ... -> v (original), then new edge v-u, then u->...->t (original) which is essentially the other ordering. But that's the other candidate. Could there be a path that uses the new edge but not as a direct connection between the two segments? For instance, s -> ... -> a -> u (original) but that a->u might be replaced by a shorter via new edge? Actually to incorporate new edge, the path must traverse it exactly once. So it splits the path at the point of traversal. The path before the edge ends at either u or v, after the edge starts at the other. So the part before the edge is a path from s to one endpoint (say x) that does not use the new edge; the part after is from the other endpoint (say y) to t that does not use the new edge. However, the part before could be shortened because maybe there is a shorter route from s to x that uses the new edge? But then that route would include the new edge before we even get to the traversal we counted, meaning that the overall path would have the new edge twice (once to get to x, then again from x to y). That would be a path that uses the new edge at least twice, which is longer than using it once if we can avoid it. Since the new edge is weight 1, you could possibly have a path that goes s -> ... -> v -> u (new edge) -> ... -> t, but if you also use the new edge earlier, it's extra. So the optimal path using the new edge exactly once is indeed as we described: the minimum over x,y in {u,v} of (dist_original(s, x) + 1 + dist_original(y, t)). However, note that in the presence of the new edge, the distances from s to u might be improved by using the new edge to go s -> ... -> v -> u, but that would be dist_original(s, v) + 1. That's actually a candidate: if we consider the route s->v (original) + new edge (v-u) + ??? but then we need to go from u to t: we could either use original dist(u,t) or maybe use new edge again? That's not allowed if we want exactly one usage? But maybe a shortest path that uses the new edge may use it as part of a route that goes s -> v (original) then new edge to u then original from u to t. That is exactly the candidate ds[v] + 1 + dt[u]. So that's already accounted. Now could there be a path that uses the new edge but not as a direct connection? For example, s -> ... -> u, then new edge (u,v), then v -> ... -> w, then maybe other edges, but that's covered because the segment after new edge ends at t. Actually any simple path that uses new edge exactly once can be written as s -> ... -> a -> (new edge) -> b -> ... -> t. So a and b are the endpoints. So candidate length = dist_new(s,a) + 1 + dist_new(b,t). However, dist_new(s,a) could be smaller than dist_original(s,a) because there could be a route using the new edge to get to a. But then that route would contain the new edge already, so the path would contain the new edge twice if we also use it from a to b. For the shortest path that uses the new edge exactly once, you would not use it again. So you would restrict to subpaths that do not include the new edge. So dist_new(s,a) without using the new edge equals dist_original(s,a). Because if there was a shorter path from s to a that uses the new edge, that path would include the new edge, so then the overall path would have two usages (or could be restructured to have one usage? Possibly you could restructure: if the path from s to a uses the new edge, you could combine that into a different traversal. For instance, suppose the optimal overall path uses new edge exactly once, but the s->a segment itself uses the new edge. Then the overall path would have at least two usages, unless the usage in s->a is exactly the same edge (a,b) but then you're traveling from s to a and then from a to b, that's essentially using the edge after arriving at a? Actually if s->a includes the edge (u,v), then the path goes from s to either u or v, then traverses the new edge to the other, then continues to a. But then if the subsequent part also uses the edge (u,v) to go from a to b, you would traverse it again. So the overall would have at least two traversals. To have exactly one traversal, the subpaths cannot include the new edge. Therefore, for the purpose of computing the shortest path that uses the new edge exactly once, we can restrict to original distances for the subpaths. The candidate we considered gives an upper bound. Could there be a path that uses the new edge but not at the boundary? Actually it's always at the boundary between the two subpaths. So yes, the new distance is min(d0, ds[u]+1+dt[v], ds[v]+1+dt[u]).
But there is nuance: In some graphs, the new edge might create a shorter path that uses the new edge more than once? That cannot be shorter than using it once, because using it twice adds at least 2, and you could skip the cycles. So ignore.
Thus condition for no decrease: ds[u] + 1 + dt[v] >= d0 and ds[v] + 1 + dt[u] >= d0.
We need to count unordered pairs (u,v) such that u<v, no existing edge, and both inequalities hold.
We can compute ds[] and dt[] via BFS (since unweighted) from s and t. d0 = ds[t] = dt[s].
Now naive approach: iterate over all unordered pairs (u,v) not connected, check condition. That's O(n^2) which for n=1000 is 500k, and each check O(1). That should be fine. But we need to know which pairs are already edges. We can store adjacency matrix (n=1000 => 1e6 booleans) or set of edges. Since m ≤ 1000, we can store a boolean matrix or use unordered_set of pairs. With n=1000, adjacency matrix bool[1001][1001] is okay. So we can compute.
But is there any catch? The problem asks for "pairs of junctions that are not connected by the road, such that if the new road between these two junctions is built the distance between s and t won't decrease." That is exactly our condition.
Check examples:
Example 1: n=5,m=4,s=1,t=5, edges are path 1-2-3-4-5. So ds: 1:0,2:1,3:2,4:3,5:4. dt from t=5: 5:0,4:1,3:2,2:3,1:4. d0=4. Condition for pair (u,v): ds[u]+1+dt[v] >=4 and ds[v]+1+dt[u]>=4. For each pair, compute. Let's list all non-edges: total pairs 10, edges=4, so non-edges=6. Which are (1,3),(1,4),(1,5) but (1,5) is distance 4? Actually (1,5) is not an edge? In the graph, edges are 1-2,2-3,3-4,4-5, so 1-5 is not directly connected. So non-edges: (1,3),(1,4),(1,5),(2,4),(2,5),(3,5). Check condition:
(1,3): ds1=0, dt3=2 (since from t=5, dist to 3 =2) -> 0+1+2=3 <4 => fails first? Actually need both? ds1+1+dt3=3<4, so decrease would happen. So not counted.
(1,4): ds1=0+1+dt4=0+1+1=2<4 fail.
(1,5): ds1+1+dt5=0+1+0=1<4 fail. (also other side same)
(2,4): ds2=1, dt4=1 => 1+1+1=3<4 fail.
(2,5): ds2=1, dt5=0 => 2? Actually 1+1+0=2<4 fail.
(3,5): ds3=2, dt5=0 => 2+1+0=3<4 fail. So none counted, output 0 matches.
Example 2: n=5,m=4,s=3,t=5, same path edges. Compute ds from s=3: distances: 3:0, adjacent 2:1,4:1, then 1:2,5:2? Actually path: 1-2-3-4-5. From 3: to 2 dist1, to 4 dist1, to 1 dist2, to 5 dist2. So ds[1]=2, ds[2]=1, ds[3]=0, ds[4]=1, ds[5]=2. dt from t=5: dt[5]=0, dt[4]=1, dt[3]=2, dt[2]=3, dt[1]=4. d0 = ds[t]=2. Condition: ds[u]+1+dt[v] >=2 and ds[v]+1+dt[u]>=2. This is much easier. Count non-edges: as before 6 pairs.
Thus we have 5 qualifying pairs, output 5 matches.
So condition works.
Thus solution: BFS from s and t to get distances. Then iterate over all unordered pairs (i,j) with i<j. If there is no edge between i and j, check if ds[i]+1+dt[j] >= d0 and ds[j]+1+dt[i] >= d0. If yes, increment answer.
Complexities: O(n^2) time, O(n^2) for adjacency matrix (or O(m+n) for adjacency list plus O(1) edge check with set). n=1000, so O(1e6) okay.
But is there any nuance about large n? n ≤ 1000, so O(n^2) is fine.
Double-check: In first example, condition gave all false. Second example, 5 true.
Third example: n=5,m=6,s=1,t=5. Edges: 1-2,1-3,1-4,4-5,3-5,2-5. So graph is almost complete? Actually total possible edges 10, m=6, so non-edges 4: (1,5) is edge? Wait edge list includes 4-5,3-5,2-5, so 1-5 is NOT listed. Also (2,3) not listed? Edges: 1-2,1-3,1-4,4-5,3-5,2-5. So non-edges: (2,3), (2,4), (3,4), (1,5)? Actually (1,5) is not listed. Check: 1-5 not present. So non-edges: (1,5), (2,3), (2,4), (3,4). That's 4 pairs. But output is 3, so one of these fails.
Compute ds from s=1: distances: 1:0; neighbors 2,3,4 all 1; then from 2,3,4 we can go to 5? edges: 2-5,3-5,4-5, so 5 is distance 2 from 1 (via any neighbor). So ds: 1:0,2:1,3:1,4:1,5:2. dt from t=5: distances: 5:0; neighbors 2,3,4:1; then from those, 1 is neighbor of 2,3,4? Actually 1 is directly connected to 2,3,4, so dist from 5 to 1 is 2 (5-2-1 or 5-3-1 or 5-4-1). So dt: 5:0,2:1,3:1,4:1,1:2. d0 = ds[t] = 2.
Now check non-edges:
(1,5): ds1=0, dt5=0 => 0+1+0=1 <2 => fails first. So not counted.
(2,3): ds2=1, dt3=1 => 1+1+1=3>=2; ds3=1, dt2=1 => 1+1+1=3>=2 => both >=2, counts.
(2,4): ds2=1, dt4=1 => 3>=2; ds4=1, dt2=1 =>3>=2 => counts.
(3,4): similar, counts. So total 3 counted, matches output. Good.
Thus condition correct.
Now we just implement.
But note: The condition also requires that the new road is not already present. So we need to skip existing edges. Also note that the pair (s,t) might not be directly connected originally, but adding that edge would reduce distance? Actually distance d0 may be >1, so adding direct edge would give distance 1 < d0, so it would decrease. So (s,t) would not be counted unless both inequalities hold, but they won't because ds[s]=0, dt[t]=0 => 0+1+0=1 < d0 if d0>1. So (s,t) not counted unless d0 <=1? But d0 >=1 since s!=t. If original distance =1, then d0=1, then ds[s]+1+dt[t] = 0+1+0 =1, which equals d0, so condition holds? Actually need >= d0, so equality is okay (distance does not decrease, stays same). But if d0=1, then originally s and t are adjacent. Adding another direct edge between them would be a parallel edge? But input says no two roads connect the same pair, so such edge doesn't exist initially. But would building a new road between s and t be allowed? It would be a new road connecting same pair, but it's not prohibited as long as not already there? The problem statement: "build one new road" between two junctions; the pair should be not connected by a road already. So if s and t are not directly connected, it's allowed. If they are already directly connected (d0=1), then building another road between them is not allowed because they are already connected by a road? But wait: It says "pairs of junctions that are not connected by the road, such that if the new road between these two junctions is built..." So we only consider pairs that are not already directly connected. So if s and t are already directly connected (i.e., edge exists), then (s,t) is not a candidate because it's already connected. So we don't need to consider that case. If they are not directly connected but d0=1? That's impossible: distance 1 implies directly connected. So for candidate (s,t) it's only when there is no edge, but distance must be >1. So condition fails.
Thus fine.
Now we need to compute BFS distances. Since m up to 1000, n up to 1000, graph is sparse but BFS is O(n+m). Good.
Implementation steps:
Read n, m, s, t.
Initialize adjacency list and a boolean matrix connected[ n+1 ][ n+1 ] false.
For each edge: add to adjacency list both directions, set connected[u][v] = connected[v][u] = true.
BFS from s: queue, distance array ds. Use -1 for unvisited, but graph is connected so all reachable.
BFS from t: dt.
d0 = ds[t] (or dt[s]).
Initialize answer = 0.
For i from 1 to n:
For j from i+1 to n:
if not connected[i][j]:
if ds[i] + 1 + dt[j] >= d0 and ds[j] + 1 + dt[i] >= d0:
answer++
Print answer.
Complexities: O(n^2) ~1e6, fine.
We should ensure we use int arrays of size n+1.
Potential pitfalls: distances are computed on original graph, which is unweighted. BFS gives integer distances.
Edge case: d0 could be large, but n up to 1000, distances up to n-1.
Now we should consider if the condition is sufficient? I think so.
But wait: There is also the possibility that adding the new edge might create a shorter path that doesn't use the new edge directly? That doesn't make sense. Actually adding an edge cannot create a shorter path that does not use the new edge because the original graph is unchanged. So any new path must include the new edge. So the candidate expressions cover all possibilities.
Thus solution complete.
But maybe there is a nuance: The new distance might be shorter due to the new edge enabling a path that uses the new edge multiple times? As argued, that would be longer.
Alternatively, could the new edge create a shorter path that goes s -> ... -> u, new edge, v -> ... -> w, then maybe another segment? No, because it ends at t. So it's still s -> ... -> u -> v -> ... -> t. So it's exactly what we consider.
Thus solution stands.
Now I'll provide final answer.
But we might want to consider optimization? Not needed.
Double-check constraints: m can be as low as 1? Graph still connected, so there is a tree? BFS fine.
Now final answer: Provide code.
I'll write in C++.
Pseudo:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, s, t;
cin >> n >> m >> s >> t;
vector<vector> adj(n+1);
vector<vector> con(n+1, vector(n+1, false));
for(int i=0;i<m;++i) {
int u,v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
con[u][v] = con[v][u] = true;
}
auto bfs = [&](int start) {
vector dist(n+1, -1);
queue q;
dist[start] = 0;
q.push(start);
while(!q.empty()) {
int u = q.front(); q.pop();
for(int v : adj[u]) {
if(dist[v] == -1) {
dist[v] = dist[u] + 1;
q.push(v);
}
}
}
return dist;
};
vector ds = bfs(s);
vector dt = bfs(t);
int d0 = ds[t];
int ans = 0;
for(int i=1;i<=n;++i) {
for(int j=i+1;j<=n;++j) {
if(!con[i][j]) {
if(ds[i] + 1 + dt[j] >= d0 && ds[j] + 1 + dt[i] >= d0)
++ans;
}
}
}
cout << ans << endl;
return 0;
}
Edge Cases: Graph may have distances where ds or dt are -1? But graph connected, so all distances defined. But we still set -1 initially, but after BFS all are set.
Now we can also consider using bitset for con, but vector okay.
Now verify on given examples.
Example 1: n=5,m=4,s=1,t=5. BFS ds: [0,1,2,3,4], dt: [4,3,2,1,0], d0=4. Loop over non-edges, counts 0, prints 0.
Example 2: correct.
Example 3: correct.
Thus final answer.
But we must ensure we count pairs that are not directly connected. The adjacency matrix ensures we skip edges.