Codeforces 330B(图论---寻找能连接其他所有点的点)

解决一个国王希望在国家中建设最少数量的道路,使得任意两城市间可通过至多两条道路相连的问题,同时避免某些特定道路组合。
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 ≤ nai ≠ 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 ai and bi.

If there are several solutions, you may print any of them.

Examples
input
4 1
1 3
output
3
1 2
4 2
2 3
Note

This is one possible solution of the example:

These are examples of wrong solutions:

The above solution is wrong because it doesn't use the minimum number of edges (4 vs 3). In addition, it also tries to construct a road between cities 1 and 3, while the input specifies that it is not allowed to construct a road between the pair.The above solution is wrong because you need to traverse at least 3 roads to go from city 1 to city 3, whereas in your country it must be possible to go from any city to another by traversing at most 2 roads.
Finally, the above solution is wrong because it must be possible to go from any city to another, whereas it is not possible in this country to go from city 1 to 32 to 3, and 4 to 3.

题目大意:
输入两个数n,m;再输入m对整数,城市的编码是从1到n。找一个点,使得它能够与其他所有点都相连。
解题思路:
通过观察数据,可以得出一个重要结论:n/2>m, 意味着必存在一个点可以与其他点相连。那么最重要的就是寻找那个点。


代码实现:
  1. #include <iostream>
  2. #include <algorithm> 
  3. #include <cstdio>  
  4. #include <cstdlib>  
  5. #include <cmath>  
  6. #include <cstring>  
  7. #include <string>  
  8. using namespace std;  
  9. int a[10010]; 
  10. int main()  
  11. {  
  12.     int n,m,x,y,i,cnt;  
  13.     scanf("%d %d",&n,&m);  
  14.     for(i=0;i<m;i++)  
  15.     {  
  16.         scanf("%d %d",&x,&y);  
  17.         a[x]++;                  //把出现的数对作为数组a的下标,记录存在
  18.         a[y]++;  
  19.     }  
  20.     for(i=1;i<=n;i++)  
  21.     {  
  22.         if(a[i]<1)               //寻找那个公共点cnt
  23.         {  
  24.             cnt=i;  
  25.             break;  
  26.         }  
  27.     }  
  28.     printf("%d\n",n-1);  
  29.     for(i=1;i<=n;i++)  
  30.     {  
  31.         if(i!=cnt)  
  32.         {  
  33.             printf("%d %d\n",i,cnt);  //依次输出与公共点相连的点
  34.         }  
  35.     }  
  36.     return 0;  







评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值