B. Road Construction
来源:互联网 发布:2017最火的网络词 编辑:程序博客网 时间:2024/05/16 07:07
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 11 3
31 24 22 3
This is one possible solution of the example:
These are examples of wrong solutions:
解题说明:请看完题目后仔细观察数据,会发现 m < n/2。 这意味着必存在一个点能跟所有点相连,可以通过反证法来证明这个规律。 既然如此,那么只用找出这么一个能连接所有点的点就解决了此问题了。 然后输出的边肯定只有n-1条。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <string>#include<set>#include <algorithm>using namespace std;int main(){int n,m,x,y,i,ro,in[10001]={0};scanf("%d %d",&n,&m);for(i=0;i<m;i++){scanf("%d %d",&x,&y);in[x]++;in[y]++;}for(i=1;i<=n;i++){if(in[i]<1){ro=i;break;}}printf("%d\n",n-1);for(i=1;i<=n;i++){if(i!=ro){printf("%d %d\n",ro,i);}}return 0;}
- B. Road Construction
- B. Road Construction
- codeforce B. Road Construction
- Codeforces 330B Road Construction
- codeforces 330B Road Construction
- cf 192 div2 B. Road Construction
- Codeforces Round 192 B. Road Construction
- Problem-330B-Ceodeforces Road Construction(思维)
- Road Construction
- Codeforces Round #192 (Div. 2) (330B) B.Road Construction
- CF 330B Road Construction 构造路径 水题
- Codeforces Round #192 (Div. 2)-B. Road Construction
- poj3352 - Road Construction
- poj 3352 Road Construction
- POJ 3352 Road Construction
- POJ3352--Road Construction
- POJ 3352: Road Construction
- Aizu 2249 Road Construction
- UNIX IO---再谈文件描述符
- 即时通讯 我穿上球鞋
- 如何将工程打包导出
- SQL Server2008将服务器内存几乎占满,网上查找到的一些经验总结
- 用js实现facebook登录获取用户的信息(包括邮箱)
- B. Road Construction
- uva 11082(网络流)
- UVA 465 - Overflow(大数)
- Centos6.0 64位jdk tomcat MySQL 5.6.11 CMake安装部署
- 合并两个vector
- java Jackson 库操作 json 的基本演示
- 文件系统挂载(解挂)
- gdbserver 移植
- 随身wifi精灵比之360随身wif。。。