I I U C O N L I N E C O N T E S T 2 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 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 & 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, 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;
}