第一行给定n,m代表n个城市
下面m行输入:u,v代表u和v之间不允许建边
输出首先是建立的边数
然后下面输出这些边
思路:输出个星X图(忘记叫什么名字了。。离散里的)
就是找一个能和所有点建边的点(不允许边中度为0),然后这个点和所有点建边即可
B. Road Construction
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
A country has n cities. Initially, there is no road in the country. One day, the king decides to construct some roads connecting pairs of cities. Roads can be traversed either way. He wants those roads to be constructed in such a way that it is possible to go from each city to any other city by traversing at most two roads. You are also given m pairs of cities — roads cannot be constructed between these pairs of cities.
Your task is to construct the minimum number of roads that still satisfy the above conditions. The constraints will guarantee that this is always possible.
Input
The first line consists of two integers n and m .
Then m lines follow, each consisting of two integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which means that it is not possible to construct a road connecting cities ai and bi. Consider the cities are numbered from 1 to n.
It is guaranteed that every pair of cities will appear at most once in the input.
Output
You should print an integer s: the minimum number of roads that should be constructed, in the first line. Then s lines should follow, each consisting of two integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which means that a road should be constructed between cities aiand bi.
If there are several solutions, you may print any of them.
Sample test(s)
input
4 1
1 3
output
3
1 2
4 2
2 3
#include<stdio.h>
#include<string.h>
#define MAXN 1005
int vis[MAXN];
int main()
{
int M,n,a,b,i;
while(scanf("%d%d",&M,&n)!=EOF)
{
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
{
scanf("%d%d",&a,&b);
vis[a]++,vis[b]++;//计算所有不可行点的度
}
for(i=1;i<=M;i++)
if(vis[i]==0)
break;//找到一个可以任意建边的点
printf("%d\n",M-1);
for(int j=1;j<=M;j++)
{
if(j!=i)
printf("%d %d\n",i,j);
}
}
return 0;
}