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.
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.
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 ai and bi.
If there are several solutions, you may print any of them.
4 1 1 3
3 1 2 4 2 2 3
This is one possible solution of the example:
![](https://i-blog.csdnimg.cn/blog_migrate/76c8f4b828839bd30c138dd5e9d31fc2.png)
These are examples of wrong solutions:
![](https://i-blog.csdnimg.cn/blog_migrate/a0c374dfb5fcce7ef419943caa588653.png)
![](https://i-blog.csdnimg.cn/blog_migrate/77bbc45186ea7ff06818c3df5d784f25.png)
![](https://i-blog.csdnimg.cn/blog_migrate/d2070b4e805c3d461eefbafbe7596565.png)
题意:给定n个城市让修路 使得每个城市都可以通过走最多两条路到达另一个城市(重点!!) 然后给定m条路说这些路不能修
思路:比赛的时候就是让题目吓着了...还是做题少 心里素质不行 很简单很贱的一道水题
既然要求每个城市都走最多两条路到达另一个城市 那么一定是一个城市是中心 然后它周围连接着所有的城市 这样每次输入一条路就把端点消掉表示它不是中心 然后遍历一遍找中心就可以了
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#define max_ 1010
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
int n,m;
bool vis[max_];
int num[max_];
int main(int argc, char const *argv[])
{
scanf("%d%d",&n,&m);
while(m--)
{
int x,y;
scanf("%d%d",&x,&y);
vis[x]=vis[y]=true;
}
int i,j;
printf("%d\n",n-1 );
for(i=1;i<=n;i++)
{
if(vis[i]==false)
{
for(j=1;j<=n;j++)
{
if(j!=i)
{
printf("%d %d\n",i,j);
}
}
break;
}
}
return 0;
}