问题 B: Balanced Neighbors
时间限制: 1 Sec 内存限制: 128 MB
提交: 83 解决: 43
[提交] [状态] [命题人:admin]
题目描述
You are given an integer N. Build an undirected graph with N vertices with indices 1 to N that satisfies the following two conditions:
·The graph is simple and connected.
·There exists an integer S such that, for every vertex, the sum of the indices of the vertices adjacent to that vertex is S.
It can be proved that at least one such graph exists under the constraints of this problem.
Constraints
·All values in input are integers.
·3≤N≤100
输入
Input is given from Standard Input in the following format:
N
输出
In the first line, print the number of edges, M, in the graph you made. In the i-th of the following M lines, print two integers ai and bi, representing the endpoints of the i-th edge.
The output will be judged correct if the graph satisfies the conditions.
样例输入
3
样例输出
2 1 3 2 3
提示
For every vertex, the sum of the indices of the vertices adjacent to that vertex is 3.
首先,肯定是在完全图中删去几条边。画个4边形和5边形就可以看出来,是在完全图中删去n/2条边。
1、当n为奇数时,删去的是两点u、v,满足u+v==n时,其之间的边。
2、当n为偶数时,删去的是两点u、v,满足u+v==n+1时,其之间的边。
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e2+10;
const int M = 2e4+10;
int main(void)
{
int n;
scanf("%d",&n);
printf("%d\n",n*(n-1)/2-n/2);
int cnt=n/2,m;
m=n;
if(m%2==0) m++;
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
if(cnt&&i+j==m)
{
cnt--;
continue;
}
printf("%d %d\n",i,j);
}
}
return 0;
}