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;
}