We need to build a simple connected undirected graph on N vertices (1..N) such that there exists an integer S (same for all vertices) where for each vertex v, the sum of indices of its neighbors equals S.
i.e., For each vertex v, sum_{u ∈ N(v)} u = S.
We can choose the graph and S accordingly.
We need to find any such graph for given N.
Constraints N up to 100, so small.
We need to prove existence.
Observations: This is reminiscent of regular graphs? Actually condition implies each vertex has neighbor sum constant. Not necessarily degree constant. But if we think of adjacency matrix A, condition is: For each i, sum_{j} A_{ij} * j = S.
We need to construct such a graph.
One approach: Complete graph? For complete graph on N vertices, for vertex i, sum of neighbors = total sum of all vertices minus i = (N(N+1)/2) - i. That's not constant unless N=2? Actually N>=3, it's not constant because depends on i. So not.
What about a star? For star with center c, leaves others. For center, sum of neighbors = sum_{v≠c} v = total - c. For leaf v (v≠c), neighbor sum = c. So we need total - c = c => total = 2c => c = total/2. So if total = N(N+1)/2 is even and there exists a vertex equal to half that, then star works. Not always.
Another idea: A regular graph? If degree d constant, then neighbor sum constant might be arranged by symmetry? Not necessarily.
Better: Consider constructing a graph where each vertex i is connected to vertex N+1-i (complement pairs). Then neighbor sum for each vertex i is (N+1-i). So S_i = N+1-i. This is constant only if N+1-i is same for all i => i constant => impossible unless N=1. So not.
Maybe we can construct a graph where each vertex is connected to all other vertices except maybe some? Not constant.
Another angle: The condition means the vector of vertex indices (1,2,...,N) is an eigenvector of the adjacency matrix? Because sum_{j} A_{ij} j = S for all i => A * x = S * 1, where x = (1,2,...,N)^T, and 1 is vector of ones. So A x = S 1.
We need a symmetric (0,1)-matrix with zero diagonal such that for each i, (A x)_i = S.
Thus (A x)_i independent of i.
We need to find such A.
We could think of constructing A such that each row sums to something? Not required.
Idea: If we let A = (1/N) * (1 * 1^T) - something? But A must be 0-1.
Maybe we can find a graph where every vertex has exactly two neighbors, forming a cycle. For a cycle 1-2-3-...-N-1, neighbor sum for i: (i-1)+(i+1) mod N, but careful: for i=1, neighbors N and 2 -> sum = N+2; for i=2, sum=1+3=4; for i=3, sum=2+4=6; only constant if differences cancel? Let's compute general: neighbor sum = 2i if i not at boundaries? Actually for cycle with vertices 1..N in order, with edges (i,i+1) mod N. For i from 2 to N-1, neighbors: i-1 and i+1 -> sum = 2i. For i=1, neighbors N and 2 -> sum = N+2. For i=N, neighbors N-1 and 1 -> sum = (N-1)+1 = N. So not constant unless N=2? Not.
What about a complete bipartite graph? E.g., partition vertices into two sets A and B, with all edges between A and B. For a vertex in A, neighbor sum = sum of all vertices in B. For vertex in B, neighbor sum = sum of all vertices in A. To have constant S, we need sum(A) = sum(B) = S? Actually for vertex in A, sum = sum(B); for vertex in B, sum = sum(A). So need sum(A)=sum(B)=S. So the sums of the two parts must be equal. Is it possible to partition {1,...,N} into two sets with equal sum? That's the partition problem. For N(N+1)/2 must be even. That is possible sometimes, but not always. For N=3, total=6 even, but can we partition into two sets of equal sum? {1,2,3} possible subsets sum to 3? {1,2} sum=3, {3} sum=3, yes. So complete bipartite K_{2,1} (since sets sizes 2 and 1) gives: A={1,2}, B={3}. Then vertex 1 neighbor sum=3, vertex2 neighbor sum=3, vertex3 neighbor sum=1+2=3. So S=3. Works! That's the example? Input 3 output edges: 1-3, 2-3. Exactly. So for N=3, complete bipartite with partition where sums equal works.
Generalizing: For any N, can we partition vertices into two sets A and B such that sum(A)=sum(B)? Total sum = N(N+1)/2. For equal partition, total must be even. If total is odd, partition impossible. So for N such that total is odd, complete bipartite can't work.
But maybe we can use other graphs.
Alternate approach: Use a regular graph? Or consider a graph where each vertex i is connected to all vertices j such that i+j = constant? If we connect each vertex i to vertex S - i (if exists), then neighbor sum for i would be S - i (if exactly one neighbor) or if multiple? That would give neighbor sum not constant.
But we could connect each vertex to a set of vertices such that the sum of those neighbors is constant. This is like each row sums to a constant (in terms of indices). This resembles a "magic graph" concept.
One construction: For all i, connect i to N+1-i only? That gives one neighbor sum N+1-i, not constant.
Another: Connect each i to all j such that j > i? Then neighbor sum varies.
Maybe we can use a "complete graph minus a perfect matching"? For even N, total sum of all vertices is T. For a vertex i, if it's connected to all except itself and maybe its partner? Let's compute.
But perhaps we can design a graph where each vertex has degree 2, and the two neighbors are chosen such that their sum is constant. For each vertex i, we need two numbers a,b (distinct from i) such that a+b = S. So we need to pair up vertices? For each i, we can assign two neighbors that sum to S. This looks like constructing a 2-regular graph (disjoint cycles) where each vertex's two neighbors sum to S. If we can arrange that each vertex i is connected to S - i (if S-i exists and S-i != i) and also to something else? But degree 2, each vertex i should have edges to two vertices that sum to S: if we include edge (i, S-i), then the other neighbor must be something else? Not necessarily; we could have degree 1? But graph must be connected, so could be a tree, etc.
What if we set S = N+1? Then each vertex i, its neighbor sum should be N+1. So we need to assign neighbors to i such that sum of their indices = N+1. A natural idea: For each i, connect it to vertex N+1-i (its complement) if i != N+1-i, i.e., if i ≠ (N+1)/2. That gives one neighbor, sum N+1-i. To get total sum N+1, we could also connect i to something else that sums with N+1-i to N+1, i.e., the something else would be i. But can't self-loop. Or connect to multiple neighbors that sum to N+1. For instance, if we make a complete graph on all vertices, neighbor sum = T - i, not constant. Not.
Another thought: Use a "windmill" graph: one central vertex connected to all others, and additional edges among leaves to adjust sums? For center, sum = T - center. For leaf i, sum = center + (sum of other leaves it's connected to). To make leaf sums constant, we need to design connections among leaves so that for each leaf i, sum of its leaf neighbors = constant - center. So we need to create a graph on the leaf set (N-1 vertices) such that for each leaf i, the sum of its neighbors (within leaves) is some constant K. Additionally, we have center c. Then leaf i's total neighbor sum = c + sum_{leaf neighbor j} j = S. So we need sum_{leaf neighbor j} j = S - c, independent of i. So we need a graph on vertices V{c} where each vertex has neighbor sum constant (S - c). That's the same problem but on N-1 vertices. Recursively? But we might be able to solve base cases.
Alternatively, we can construct for any N a graph like a "complete bipartite" but with possible additional edges to balance sums when total is odd.
But note that problem statement says "It can be proved that at least one such graph exists under the constraints." So there is always a construction. We need to provide one.
Let's search for known results: Graph with constant neighbor sum is called "vertex-magic" or "magic graph"? Actually there is concept of "edge-magic total labeling" but not this.
Maybe we can use the following: For N odd, we can make a complete graph minus a perfect matching? Let's test N=5. Total sum =15. Complete graph minus perfect matching: Suppose vertices 1..5. We need to remove some edges to make neighbor sum constant. If we take complete graph, neighbor sum for i = T - i =15-i, which varies. If we remove a set of edges for each i to adjust? Not easy.
Alternatively, consider the graph where each vertex i is connected to all vertices except itself and maybe one other? For even N, could we make it such that each vertex is missing exactly one neighbor, and that missing neighbor is such that the sum of the remaining is constant? For vertex i, if it's connected to all j ≠ i except maybe f(i), then neighbor sum = T - i - f(i). To be constant, we need i + f(i) constant. So we need a derangement f such that for each i, f(i) is distinct and f(i) ≠ i, and i + f(i) = constant C. That implies f(i) = C - i. For f to be a permutation with no fixed points, we need that mapping i -> C-i is a permutation of [1,N]. This is indeed an involution. Condition: for all i, C-i must be between 1 and N. So C must be between 2 and N+1? Actually if we require that mapping is a permutation, then C must be N+1 to map 1->N,2->N-1,... So C = N+1. Then f(i) = N+1-i. This is a permutation with no fixed points only if N is even; if N is odd, the middle element i=(N+1)/2 maps to itself, causing a fixed point (which is not allowed because we would be removing self? Actually we are removing edge to f(i) which cannot be i because then vertex i would be missing itself? But we cannot have self-loop anyway. For i such that f(i)=i, that would mean we remove the non-existent self-loop? Actually we intend that vertex i is not connected to f(i). For f(i)=i, that would mean we are removing the self-loop, which is irrelevant; but the idea is to have a graph that is complete except we remove a perfect matching? In this construction, we would remove edges (i, f(i)) for each i. But if f(i)=i, we would remove (i,i) which doesn't exist, so effectively we don't remove anything for that vertex, making its neighbor set all others. That would break constant sum because for that vertex, neighbor sum = T - i, while for others it's T - i - f(i). So to have constant, we need to avoid fixed points. So we need N even. Then define graph: connect all pairs except edges (i, N+1-i) for each i? Actually careful: For each i, we remove the edge between i and N+1-i. Since mapping is an involution, each edge is removed exactly once? For i and N+1-i, both i and N+1-i map to each other, so the same edge. So we remove those N/2 edges (a perfect matching). Then for any vertex i, its neighbors are all vertices except i and its partner p = N+1-i. So neighbor set = all vertices except i and p. Then neighbor sum = T - i - p = T - (i + (N+1-i)) = T - (N+1) = T - N - 1. So indeed constant! Because i + p = N+1 always. So S = T - N - 1.
Check: For N even, T = N(N+1)/2. Then S = N(N+1)/2 - N - 1 = (N(N+1) - 2N - 2)/2 = (N^2 + N - 2N - 2)/2 = (N^2 - N - 2)/2 = ( (N-2)(N+1) )/2? Let's compute: (N^2 - N -2)/2 = (N-2)(N+1)/2. For N=4, T=10, S=10-4-1=5. Graph: vertices 1,2,3,4. Remove edges (1,4) and (2,3). So edges: complete graph on 4 vertices (6 edges) minus those 2 = 4 edges: 1-2,1-3,2-4,3-4. Check: For vertex1: neighbors 2,3 sum=5. Vertex2: neighbors 1,4 sum=5. Vertex3: neighbors 1,4 sum=5. Vertex4: neighbors 2,3 sum=5. Works! So for even N, the graph "complete graph minus a perfect matching" works, provided we choose the perfect matching as pairs (i, N+1-i). That yields constant neighbor sum. This is simple, connected? Complete graph minus a matching is still connected (since N>=3, even N>=4, still connected). For N=2? Not allowed (N>=3). So even N works.
Now for odd N, total sum T is odd? Actually N odd => T = N(N+1)/2 is integer. If N ≡ 1 mod 4? Not relevant. But T may be odd or even? For N=3, T=6 even; N=5, T=15 odd; N=7, T=28 even; So parity varies. But our construction with perfect matching required even N. So for odd N, we need a different construction.
We already have example N=3 works via complete bipartite with equal partition. For N=3, odd but total even, so partition possible. For N=5, total 15 odd, cannot partition into equal sum. So that construction fails.
But maybe there is another general construction for odd N. Consider the complete graph minus a matching plus an extra vertex? For odd N, we can't have a perfect matching covering all vertices. But we can consider taking a complete graph on N vertices (odd) and remove a near-perfect matching (covering all but one vertex) and also add some adjustments? Let's try to derive condition.
General idea: For any N, we can try to assign a set of forbidden edges such that each vertex i misses exactly one neighbor (or maybe some constant number of missing neighbors) to adjust sum to constant.
Suppose we aim for a graph where each vertex i is missing exactly one neighbor f(i) (f(i) ≠ i), and is connected to all other vertices. Then neighbor sum = T - i - f(i). For constancy, we need i + f(i) = constant C for all i. So f(i) = C - i must be an integer between 1 and N, and f(i) ≠ i. Also f must be a permutation (since each vertex is missing exactly one neighbor, and that neighbor is f(i); note that if we define missing edges as (i, f(i)), then for consistency we need that if i misses j, then j misses i? In our graph definition, we would be removing edges (i, f(i)). For the graph to be undirected, if we remove edge (i,j), then both i and j are missing each other. So we need f to be an involution: f(f(i)) = i, and f(i) ≠ i. Because if we remove edge (i, f(i)), then for vertex f(i), its missing neighbor should be i, i.e., f(f(i)) = i. Also we must have f(i) ≠ i. So we need a fixed-point-free involution (a perfect matching) on the vertex set. That's only possible if N is even. So the "missing exactly one neighbor" construction only works for even N. So for odd N we need a different approach.
Alternative: Perhaps we can allow each vertex to miss a different number of neighbors, but adjust such that sum of neighbors is constant. Could we construct a graph that is regular? If each vertex has degree d, then neighbor sum constant might be easier if the graph is also vertex-transitive? But not necessarily.
Another idea: For odd N, we can take a complete graph on N vertices and then remove a 1-factor (matching) from a subset? But we have odd number, maybe we can add a "dummy" vertex? Not.
We can also consider a graph where each vertex i is connected to all vertices except two: maybe i and some other that depends on i. Suppose we remove two edges per vertex in a structured way.
Let's try to find construction for odd N. Consider N=5. We need a graph with 5 vertices. We want S constant. Let's attempt to find small graphs manually.
For N=5, can we find a graph? Let's try to use the complete bipartite idea but with unequal sums but add extra edges to balance? Suppose we partition into A and B with sums not equal, but add some edges within parts to adjust each vertex's neighbor sum.
Let's try to find any graph on 5 vertices satisfying condition. Let's denote degrees and neighbor sums.
We need a graph such that for each i, sum of neighbors = S.
We can attempt to solve for adjacency matrix variables (edges). There are many possibilities.
We can try to use symmetry: maybe a cycle with chords? For N=5, consider the graph being a cycle 1-2-3-4-5-1. Compute neighbor sums: 1:2+5=7, 2:1+3=4, 3:2+4=6, 4:3+5=8, 5:4+1=5. Not constant.
What about a star? For center c, leaf sums = c. Center sum = total - c. Set equal => total - c = c => 2c = total => c = total/2 = 15/2 =7.5 not integer. So star fails.
What about a "wheel" graph: center connected to all, and cycle on outer vertices. For N=5, wheel: center say 5, edges from 5 to 1,2,3,4, and cycle 1-2-3-4-1? Actually cycle on 4 vertices: 1-2,2-3,3-4,4-1. Then compute neighbor sums: For center 5: neighbors 1,2,3,4 sum=10. For vertex 1: neighbors = center5 + 2 + 4? Actually cycle edges: 1 adjacent to 2 and 4 (if cycle is 1-2-3-4-1). So neighbor sum for 1 =5+2+4=11. Vertex2: neighbors=5+1+3=9. Vertex3: 5+2+4=11. Vertex4:5+1+3=9. Not constant.
Maybe we can use a complete graph on 5 minus a 5-cycle? Not sure.
Let's attempt systematic construction for odd N. Since problem says exists for all N up to 100, there must be a generic method.
We could try to reduce to even case by adding a vertex? But we need exactly N vertices.
Another idea: For odd N, we can construct a graph that is "complete graph" but with one vertex having a special treatment. Suppose we let the graph be complete on all vertices except we remove edges of a near-perfect matching covering N-1 vertices (which is even), and then adjust the last vertex differently. For N odd, say N=2k+1. Consider vertices 1,...,2k+1. For the first 2k vertices, we can pair them as (1,2k), (2,2k-1), ... (k, k+1). That is pairs summing to 2k+1? Actually 2k+1 = N? Wait N=2k+1, so for even part (2k) we can pair as (1,2k), (2,2k-1), ... (k, k+1). These pairs sum to 2k+1? 1+2k=2k+1, 2+(2k-1)=2k+1, etc. That's N. So if we remove edges between those pairs for the first 2k vertices, but also consider vertex 2k+1 (the last). For vertices 1..2k, if we take complete graph on all N vertices but remove only edges (i, partner) for i=1..k? But that would give each of those vertices missing exactly one neighbor (their partner). But vertex 2k+1 is not paired; it would miss no one? Let's define graph: Start with complete graph on N vertices. For i from 1 to k, remove edge between i and its partner p_i = 2k+2 - i? Actually if N=2k+1, then define partner for i (1≤i≤2k) as N - i? Because N - i = 2k+1 - i. But note 2k+1 - i ranges from 2k down to 1. So mapping i -> N - i is an involution without fixed points on the set {1,...,2k}? Check: For i in 1..2k, N-i is also in 1..2k (since N-i from 2k to 1). And N-(N-i)=i. It has no fixed point because i = N-i => 2i = N = 2k+1 impossible. So it's a perfect matching on the set {1,...,2k}. So we can remove edges (i, N-i) for i=1..k? Actually edges are undirected, so we remove each such pair once.
Now, after removing these edges, what are the neighbor sums? For any vertex i in 1..2k, its neighbors = all vertices except i and its partner N-i. So neighbor sum = T - i - (N-i) = T - N. For vertex N = 2k+1, its neighbors = all vertices except itself (since we didn't remove any edge incident to N? Actually we removed edges only among pairs within 1..2k, so vertex N is still connected to everyone). So its neighbor sum = T - N. Wait T - N? T = total sum of all vertices = N(N+1)/2. Then T - N = N(N+1)/2 - N = N(N-1)/2. But for i in 1..2k, neighbor sum = T - i - (N-i) = T - N. Yes same! So for all vertices, neighbor sum = T - N. That's constant! Let's verify.
But careful: For i in 1..2k, we removed edge (i, N-i). So i is NOT connected to N-i. But i is still connected to N (the last vertex) because we didn't remove that edge. So neighbor set of i = all vertices except i and N-i. That includes N. So sum = sum of all vertices except i and N-i = T - i - (N-i) = T - N. For vertex N, neighbor set = all vertices except N, because no edges removed incident to N? But we removed edges (i, N-i) which do not involve N (since N-i <= 2k, not N). So vertex N is connected to all other vertices (1..2k). So sum = T - N. So indeed all vertices have neighbor sum = T - N. Great! So this construction works for any N (both even and odd)! Wait, we assumed we remove edges (i, N-i) for i=1..? But we need to ensure we don't double-remove. For N even, N-i would also be in 1..N. But when N is even, mapping i -> N-i is an involution on the whole set. If we remove edges (i, N-i) for all i, we would remove a perfect matching, giving the earlier even construction. However, we must check whether that also gives neighbor sum = T - N? For even N, earlier we had neighbor sum = T - N - 1? Let's recalc: In the even construction we removed edges (i, N+1-i) and got neighbor sum = T - N - 1. Wait discrepancy: In earlier even construction we used pairs summing to N+1, not N. Because we wanted i + partner = N+1 to get constant. Here we use partner = N - i, summing to N. Which one is correct? Let's test with N=4 (even). Using our new proposed removal: remove edges (i, 4-i) for i=1..? Pairs: 1-3? Since 4-1=3, 2-2? 4-2=2, but that would be self? Actually i=2 gives partner 2, fixed point! That's a problem: For even N, mapping i -> N-i has fixed point when i = N/2? Because N-i = i => 2i = N => i = N/2. So for even N, the mapping i -> N-i is not fixed-point-free. It has a fixed point at i = N/2. So my earlier statement that it's a perfect matching on {1,...,2k} when N=2k+1 (odd) holds because there is no integer i such that i = N-i when N odd. For even N, mapping i -> N-i has a fixed point, so we can't just remove all edges (i, N-i) because i = N/2 would be a self-loop, and also for that vertex we would remove edge to itself? Actually we can't remove that. So we need to treat even and odd separately.
But we just discovered a construction that works for odd N: remove edges (i, N-i) for i=1..(N-1)/2? Actually we need to ensure each pair is distinct. For odd N, N=2k+1, the set {1,...,2k} can be paired as (1,2k), (2,2k-1), ..., (k, k+1). These are exactly i and N-i. And vertex N remains unpaired. So we start with complete graph, remove edges for these k pairs. Then each vertex (including N) has neighbor sum = T - N. Let's test N=5: N=5, T=15, T - N = 10. Remove edges: (1,4) because 5-1=4; (2,3) because 5-2=3. So graph: complete graph on 5 vertices has 10 edges. Remove edges (1,4) and (2,3). So remaining edges: all except those two. That's 10-2=8 edges. Now compute neighbor sums:
Vertex1: neighbors: 2,3,5 (not 1,4) -> sum=2+3+5=10.
Vertex2: neighbors: 1,4,5 (not 2,3) -> sum=1+4+5=10.
Vertex3: neighbors: 1,4,5? Wait vertex3 is not connected to 2 (since edge (2,3) removed) and not to 3 itself. So neighbors: 1,4,5 -> sum=1+4+5=10.
Vertex4: neighbors: 2,3,5 -> sum=2+3+5=10.
Vertex5: neighbors: 1,2,3,4 (all others) -> sum=1+2+3+4=10.
Works! So N=5 works. Good.
Now for N=3: using this construction: N=3, remove edges (i, 3-i) for i=1? For i=1, partner=2; remove edge (1,2). Then graph: complete on 3 vertices (edges: (1,2),(1,3),(2,3)) minus (1,2) gives edges (1,3) and (2,3). That's exactly the example! And S = T - N = 6-3=3. Works.
So this construction works for odd N. For even N, we used a different pairing: pairs (i, N+1-i). That yields neighbor sum = T - (N+1). Let's test N=4: T=10, S= T - (N+1)=10-5=5. That matched earlier. For N=6: T=21, S=21-7=14? Let's test: remove edges (1,6), (2,5), (3,4). Then each vertex i is missing its partner p = N+1-i. For i=1: neighbors all except 1 and 6 => sum = T -1-6=21-7=14. i=2: sum =21-2-5=14. i=3:21-3-4=14. i=4:21-4-3=14. i=5:21-5-2=14. i=6:21-6-1=14. Works.
Now check if graph is connected: For N>=3, the complete graph minus a matching is still connected (since it's highly connected). For even N, we removed a perfect matching; the resulting graph is still connected for N>2? For N=4, we got edges: (1,2),(1,3),(2,4),(3,4) which is connected. For N=6, complete minus matching should be connected because each vertex still connects to many others. For odd N, we removed a matching on the first N-1 vertices, leaving the graph complete minus those edges. It's still connected because vertex N connects to everyone, so definitely connected.
Thus we have a unified construction:
-
If N is odd: Start with complete graph on N vertices. Remove edges (i, N-i) for i = 1, 2, ..., (N-1)/2. (Since those are disjoint pairs). Then the resulting graph satisfies condition with S = T - N, where T = N(N+1)/2.
-
If N is even: Start with complete graph. Remove edges (i, N+1-i) for i = 1, 2, ..., N/2. Then S = T - (N+1).
We should verify for N even that this indeed yields S constant. For i, neighbor sum = T - i - (N+1-i) = T - (N+1). Good.
Thus we have a simple construction for any N >= 3.
We can output the edges. Since we start from complete graph and remove some edges, we could output all edges except those removed. That means M = total edges - number of removed edges. Total edges in complete graph = N(N-1)/2. Removed edges: if N even, remove N/2 edges; if N odd, remove (N-1)/2 edges. So M = N(N-1)/2 - floor(N/2). For N odd: M = N(N-1)/2 - (N-1)/2 = (N-1)(N-1)/2? Let's compute: N(N-1)/2 - (N-1)/2 = (N-1)(N-1)/2 = ((N-1)^2)/2. For N odd, (N-1) even, so integer. For N even: M = N(N-1)/2 - N/2 = (N(N-1) - N)/2 = N(N-2)/2. That's integer because N even? For N even, N(N-2)/2 integer.
But we don't need to compute M explicitly; we can just generate all pairs (i,j) with i<j and (i,j) not in the set of removed pairs, and output them.
We must ensure the graph is simple and connected. It is.
Thus solution: For given N, compute T = N(N+1)/2. Determine S but we don't need to output S, just graph.
We'll output edges: all pairs (a,b) with 1 ≤ a < b ≤ N, except those that are matched for removal.
For even N: remove if a + b = N+1 (i.e., b = N+1-a). Because i < j, we need to check a+b = N+1. Since mapping is symmetric, we can just skip those.
For odd N: remove if a + b = N (i.e., b = N - a). Since N odd, a != N-a for any integer a? Actually when N odd, a = N - a implies 2a = N, not integer, so no self-pair, fine. So we skip when a+b == N.
Wait: For odd N, we previously removed edges (i, N-i). That is a+b = N. For i from 1 to (N-1)/2, we remove (i, N-i). Since i < N-i when i < N/2, which holds for i up to (N-1)/2 because N is odd, N/2 is non-integer, and for i <= (N-1)/2, i < N-i indeed. So condition a+b = N captures exactly those removed pairs. For even N, we remove a+b = N+1. Good.
So algorithm: iterate over all 1 ≤ i < j ≤ N, if (i+j) != target (where target = N+1 if N even else N) then output i j.
We need to output M first (number of edges). So count edges satisfying condition.
Complexity O(N^2), N ≤ 100, fine.
We should also confirm that this graph is connected. For N=3, we output (1,3) and (2,3), connected. For N=4, we output edges where i+j !=5, so edges: (1,2),(1,3),(2,4),(3,4) plus also (1,4)? 1+4=5 skip; (2,3)=5 skip; (2,1) duplicate; (3,2) skip; etc. So we have 4 edges: (1,2),(1,3),(2,4),(3,4). That's a 4-cycle? Actually vertices: 1-2-4-3-1? Check: 1-2, 2-4, 4-3, 3-1 => cycle of length 4. Connected.
For N=5, we output edges where i+j !=5, i.e., skip (1,4) and (2,3). So edges: all other pairs. That's complete graph minus those two edges. It's connected.
Thus solution works.
We need to prove that the condition holds: For any vertex v, neighbor set = all vertices except v and its partner p(v) defined as:
-
If N even: p(v) = N+1 - v. Then neighbor sum = T - v - p(v) = T - (N+1).
-
If N odd: For v != N, p(v) = N - v; for v = N, p(N) = ? Actually for N odd, we removed edges (i, N-i). Vertex N is not involved in any removal, so its neighbor set = all except N. So neighbor sum = T - N. For other vertices v, neighbor sum = T - v - (N - v) = T - N. So constant.
Thus condition satisfied.
Hence we have a valid answer.
But wait: Check for N even, what about vertex v such that v = N+1 - v? That would imply 2v = N+1, so v = (N+1)/2. Since N even, N+1 is odd, so v is half-integer? Actually (N+1)/2 is not an integer when N even? N even, N+1 is odd, so (N+1)/2 is not integer. So no vertex equals its partner. So each vertex has exactly one missing neighbor (its partner). So neighbor set = all except itself and its partner. So sum = T - v - partner = T - (N+1). Good.
Thus solution holds.
Now we can implement.
Edge count: M = total - removed = N(N-1)/2 - floor(N/2). For even: N/2; for odd: (N-1)/2. So we can compute that.
Now ensure output format: first line M, then M lines each with a_i b_i (any order, but typical ascending order). We'll output i j with i<j.
Testing with sample: N=3 odd, target sum to skip = N=3. Pairs: (1,2): sum=3 skip; (1,3): sum=4 output; (2,3): sum=5 output. So M=2, edges: 1 3 and 2 3. Matches sample.
Thus correct.
Now we should also consider if there is any risk that the graph is not simple? We are outputting each undirected edge once, so fine.
Therefore final answer: implement.
But is there any scenario where this graph might be disconnected for small N? N=3, we got star which is connected. N=4, we got a cycle of length 4, connected. N=5, complete minus two edges is connected because it's still dense. For larger N, it's clearly connected because each vertex has degree at least N-2 (since removed only one edge per vertex for even, and for odd only one vertex has degree N-1, others N-2). Since N>=3, N-2 >=1, so graph is connected? Actually need to ensure connectivity: For even, each vertex is missing exactly one neighbor, so degree = N-2. For N-2 >= 1 if N>=3, so degree >=1. But could the graph be disconnected? Possibly if N=2, but not allowed. For N=4, degree of each vertex is 2, and graph is a 4-cycle, connected. For N=6, degree = 4, so definitely connected. So fine.
Hence solution.
Let's write code.
Pseudo:
read N
edges = []
target = N+1 if N%2==0 else N
for i in range(1,N+1):
for j in range(i+1,N+1):
if i+j != target:
edges.append((i,j))
print(len(edges))
for (a,b) in edges: print(a,b)
That's it.
But wait: In odd case, target = N. However, we should verify that we are not skipping any pair incorrectly? For N odd, we skip exactly (i, N-i). But when i = N? Actually i ranges from 1 to N-1, j from i+1 to N, so we never have i = N. So for i = N? not considered. For i = N/2? Not integer. So fine.
One nuance: For odd N, when i = (N+1)/2? That is not integer because N odd, (N+1)/2 is integer? For N odd, say N=5, (5+1)/2=3, which is integer. i=3, j? i+j = 3+? = 5 => j=2, but j < i, so not considered because j > i? i=3, j would need to be >3 to have sum 5? 3+2=5 but j=2 < i, not in loop. So we only skip when i < j and i+j = N. That pairs (1,4) and (2,3) for N=5, as expected. Good.
For even N, target = N+1. Pairs like (i, N+1-i). For i < j, this will happen when i < N+1-i => i < (N+1)/2, which is valid for i=1,...,N/2 because (N+1)/2 is not integer? Actually N even, (N+1)/2 is half-integer, so i <= N/2 gives i < (N+1)/2? For i = N/2, i = N/2, then j = N+1 - i = N+1 - N/2 = N/2 + 1, which > i. So (N/2, N/2+1) is a pair. So we skip exactly N/2 edges: (1,N), (2,N-1), ..., (N/2, N/2+1). Good.
Thus code is correct.
Now we should also mention that the problem statement says "It can be proved that at least one such graph exists under the constraints of this problem." So we can present this construction.
Edge case: N=1? Not allowed (N>=3). N=2? Not allowed.
Thus answer.
Now implement.
But wait: For odd N, we are skipping edges where i+j = N. But what about i+j = N+1? Not needed.
Check N=3: skip (1,2) because 1+2=3, output (1,3),(2,3). Good.
Now verify S constant: For odd N, S = T - N = N(N+1)/2 - N = N(N-1)/2. For N=3, S=32/2=3. For N=5, S=54/2=10. Good.
For even N, S = T - (N+1) = N(N+1)/2 - N -1 = (N^2 - N - 2)/2 = (N-2)(N+1)/2. For N=4, S= (25)/2=5. For N=6, S= (47)/2=14.
Thus works.
Therefore final answer: print edges of complete graph except the matching pairs.
Now confirm connectivity for all N>=3: As argued, each vertex has degree at least N-2. For N=3, degree of vertices 1 and 2 =1, vertex3=2, connected. For N=4, each degree=2, but graph is a 4-cycle, connected. For N=5, vertices 1,2,3,4 have degree 3 (since N-2=3), vertex5 degree 4, connected. For larger, degrees high, definitely connected.
Thus solution correct.
But is there any nuance: The graph must be simple and connected. Yes.
Now we produce final code.
But we need to ensure that we output M first. So compute M as count.
Alternatively, compute formula M = N(N-1)//2 - (N//2). Because floor(N/2) = N//2 in integer division. For even N, N//2 = N/2; for odd N, N//2 = (N-1)/2? Actually in Python integer division, N//2 gives floor, so for odd N, N//2 = (N-1)/2. That's exactly the number of removed edges. So M = N*(N-1)//2 - N//2. Let's test: N=3: 32//2=3; N//2=1; M=2. N=4: 43//2=6; N//2=2; M=4. N=5: 5*4//2=10; N//2=2; M=8. Good. So we can output M easily.
Now code:
def solve():
import sys
N = int(sys.stdin.readline().strip())
target = N+1 if N%2==0 else N
edges = []
for i in range(1, N+1):
for j in range(i+1, N+1):
if i + j != target:
edges.append((i, j))
print(len(edges))
for a,b in edges:
print(a, b)
That's it.
But we need to be careful about reading input: there might be extra spaces.
Now check constraints: N up to 100, edges up to ~5000 (when N=100, M = 100*99/2 - 50 = 4950-50=4900). That's well within output limits.
Thus final answer.
We should maybe also discuss proof of correctness in solution but not necessary.
So final.
But wait: The problem says "There exists an integer S such that, for every vertex, the sum of the indices of the vertices adjacent to that vertex is S." This S must be same for all vertices. Our graph yields S = T - N for odd, T - (N+1) for even. That's integer. So fine.
Thus final answer.
Now we produce final answer.
```python
import sys
def solve() -> None:
N = int(sys.stdin.readline().strip())
# target sum of the pair we remove
target = N + 1 if N % 2 == 0 else N
edges = []
for i in range(1, N + 1):
for j in range(i + 1, N + 1):
if i + j != target:
edges.append((i, j))
print(len(edges))
for a, b in edges:
print(a, b)
if name == "main":
solve()