提交链接:CF 550D
题面:
An undirected graph is called k-regular, if the degrees of all its vertices are equalk. An edge of a connected graph is called abridge, if after removing it the graph is being split into two connected components.
Build a connected undirected k-regular graph containing at least one bridge, or else state that such graph doesn't exist.
The single line of the input contains integer k (1 ≤ k ≤ 100) — the required degree of the vertices of the regular graph.
Print "NO" (without quotes), if such graph doesn't exist.
Otherwise, print "YES" in the first line and the description of any suitable graph in the next lines.
The description of the made graph must start with numbers n and m — the number of vertices and edges respectively.
Each of the next m lines must contain two integers,a and b (1 ≤ a, b ≤ n,a ≠ b), that mean that there is an edge connecting the verticesa and b. A graph shouldn't contain multiple edges and edges that lead from a vertex to itself. A graph must be connected, the degrees of all vertices of the graph must be equalk. At least one edge of the graph must be a bridge. You can print the edges of the graph in any order. You can print the ends of each edge in any order.
The constructed graph must contain at most 106 vertices and106 edges (it is guaranteed that if at least one graph that meets the requirements exists, then there also exists the graph with at most106 vertices and at most106 edges).
1
YES 2 1 1 2
In the sample from the statement there is a suitable graph consisting of two vertices, connected by a single edge.
题意:
求构造一个无向图,图中每个节点的度数都为k,且该图存在至少一个割边,即断开后,图不连通。k小于等于100,要求构造的图中,点数不大于10^6,且边数也不大于10^6。
解题:
先画一条割边,割边上的一点A,必然会有另外K-1条边连出去,对于A这一侧,设还需添加a个节点,x条边,根据度数和边的关系可得。(k-1)*(k-1)+k*a=2*x,明显(k-1)*(k-1)为偶数,欲使等式有解,最小的a取值为2。明显当k为奇数无解。
于是开始构造,A侧新加2个节点,将这两个节点与k-1个节点相连,且两个节点间连一条边,这两点已符合度数要求,剩余的k-1个点通过循环后连的方式补足剩余度数即可,B侧同理。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <cstdio>
#include <vector>
#define pe(u,v) printf("%d %d\n",u,v)
using namespace std;
void show(int x,int y,int t)
{
int tmp;
for(int i=x;i<=y;i++)
{
for(int j=1;j<=t;j++)
{
tmp=i+j;
if(tmp>y)
tmp=tmp-y+x-1;
pe(i,tmp);
}
}
}
int main()
{
int k;
scanf("%d",&k);
if(k%2)
{
printf("YES\n");
if(k==1)
printf("2 1\n1 2\n");
else
{
printf("%d %d\n",2*k+4,k*k+2*k);
for(int i=1;i<=k-1;i++)
{
pe(1,i+1);
pe(k+1,i+1);
pe(k+2,i+1);
}
pe(k+1,k+2);
show(2,k,(k-3)/2);
pe(1,k+3);
for(int i=1;i<=k-1;i++)
{
pe(k+3,k+3+i);
pe(2*k+3,k+3+i);
pe(2*k+4,k+3+i);
}
pe(2*k+3,2*k+4);
show(k+4,2*k+2,(k-3)/2);
}
}
else
printf("NO\n");
return 0;
}