UVA 11387 - The 3-Regular Graph(构造问题+推理证明)

I C   O N L I N E   C O T E S T   0 0 8

Problem C: The 3-Regular Graph

Input: standard input

Output: standard output

 

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 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 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 b, the two endpoints of the edge. Note that the vertices are indexed from to n. If it is not possible to build a simple undirected 3-regular graph with vertices, printImpossible in a single line.

 

Constraints

-           1 ≤ n ≤ 100

 

Sample Input

Output for Sample Input

4

3

0

6

1 2

1 3

1 4

2 3

2 4

3 4

Impossible


题意:给定n个点,要求用n个点构造出一个图,每个顶点度数为3,问构造方法。

思路:构造问题,n为奇数是不行的,因为加一条边度数+2,n*3为奇数,肯定不满足,所以只有n为偶数可以,构造方式为,首尾连成圈,这样度数为2了,然后在对角线全相连,度数为3,注意特判2的情况。

代码:

#include <stdio.h>
#include <string.h>

int n;

void solve() {
	if (n < 3 || n % 2) printf("Impossible\n");
	else { 
		int i; printf("%d\n", n + n / 2);
		for (i = 1; i < n; i++)
			printf("%d %d\n", i, i + 1);
		printf("%d 1\n", n);
		for (i = 1; i <= n / 2; i++)
			printf("%d %d\n", i, i + n / 2);
	}
}

int main() {
	while (~scanf("%d", &n) && n) {
		solve();
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值