UVA11387 The 3-Regular Graph【数学推理】

The degree of a vertex in a graph is the number of edges adjacent to the vertex. A graph is 3-regular if all of its vertices have degree 3. Given an integer n, you are to build a simple undirected 3-regular graph with n vertices. If there are multiple solutions, any one will do.
Input
For each test case, the input will be a single integer n as described above. End of input will be denoted by a case where n = 0. This case should not be processed.
Output
If it is possible to build a simple undirected 3-regular graph with n vertices, print a line with an integer e which is the number of edges in your graph. Each of the following e lines describes an edge of the graph. An edge description contains two integers a and b, the two endpoints of the edge. Note that the vertices are indexed from 1 to n. If it is not possible to build a simple undirected 3-regular graph with n vertices, print ‘Impossible’ in a single line.
Constraints
• 1 ≤ n ≤ 100
Sample Input
4
3
0
Sample Output
6
1 2
1 3
1 4
2 3
2 4
3 4
Impossible

问题链接UVA11387 The 3-Regular Graph
问题简述:给定n个结点,判定能否给出一个无向图,该图每个结点连接3条边。如果可以的话输出连接的边。
问题分析
    对于图来说,每增加1条边则图的出入度增加2,所以图的总度数总是为偶数。若n为奇数,每个结点连接3条边的话,那么总度数为n*3也为奇数,矛盾。所有只有n为偶数时,才有可能给出想要的无向图。
    其他读程序代码不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA11387 The 3-Regular Graph */

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    while(~scanf("%d", &n) && n) {
        if(n % 2 || n <= 2) printf("Impossible\n");
        else {
            int n2 = n / 2;
            printf("%d\n", n2 * 3);
            for (int i = 1; i <= n; i++)
                printf("%d %d\n", i, i % n + 1);
            for (int i = 1; i <= n2; i++)
                printf("%d %d\n", i, i + n2);
        }
    }

    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值