Time limit: 1.000 seconds
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, print Impossible 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是不行的,因为加一条边度数+2,n*3为奇数,肯定不满足,所以只有n为偶数可以,构造方式为,首尾连成圈,这样度数为2了,然后在对角线全相连,度数为3。
#include <iostream>
#include <cstdio>
using namespace std;
int n;
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==0) break;
if(n%2==1 || n==2) printf("Impossible\n");
else
{
int m=n/2;
printf("%d\n",n+m);
for(int i=1;i<n;i++) printf("%d %d\n",i,i+1);
printf("%d 1\n",n);
for(int i=1;i<=m;i++) printf("%d %d\n",i,i+m);
}
}
return 0;
}