Contest1789 - 2019年第二阶段我要变强个人训练赛第十二场 问题 B: Balanced Neighbors 构造

题目描述

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.

题解:举个例子

奇数时,例如5,1 2 3 4 5,1和4一组,2和3一组,一组内的不连接,其他点都连

偶数时,例如6,1 2 3 4 5 6,1和6,2和5,3和4一组,一组内的不连接,其他点都连

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int vis[110][110];
int main() {
    int n;
    scanf("%d", &n);
    if(n%2) {
        printf("%d\n", ((n - 1) * (n - 2) + n - 1) / 2);
        int m = n - 1;
        for(int i = 1; i < n; i++) {
            for(int j = 1; j <= n; j++) {
                if(i + j != n && i != j && !vis[min(i, j)][max(i, j)]) {
                    printf("%d %d\n", i, j);
                    vis[min(i, j)][max(i, j)] = 1;
                }
                     
            }
        }
        for(int i = 1; i <= n - 1; i++) {
            if(!vis[min(i, n)][max(i, n)])
                printf("%d %d\n", i, n);
        }
             
    } else {
        printf("%d\n", n * (n - 2) / 2);
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                if(i + j != n + 1&& i != j && !vis[min(i, j)][max(i, j)]) {
                    printf("%d %d\n", i, j);
                    vis[min(i, j)][max(i, j)] = 1;
                }
                     
            }
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值