codeforce B. Road Construction
来源:互联网 发布:学校奖学金算法 编辑:程序博客网 时间:2024/05/01 00:39
B. Road Construction
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 givenm 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 andm.
Then m lines follow, each consisting of two integersai andbi (1 ≤ ai, bi ≤ n,ai ≠ bi), which means that it is not possible to construct a road connecting citiesai andbi. Consider the cities are numbered from 1 ton.
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. Thens lines should follow, each consisting of two integersai andbi (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:
找到一条未出现的城市,然后以它为中心逐个建边就行了,一共n-1条边
#include<stdio.h>#include<math.h>#include<string.h>#define N 1010int main(){ int i,n,m,a[N]; while(scanf("%d%d",&n,&m)!=EOF) { m*=2; memset(a,0,sizeof(a)); while(m--) { scanf("%d",&i); a[i]=1; } for(i=1;i<=n;i++) if(!a[i]) break; printf("%d\n",n-1); for(m=1;m<=n;m++) if(m!=i) printf("%d %d\n",i,m); } return 0;}
- codeforce B. Road Construction
- B. Road Construction
- 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
- 【四圣龙神录的编程教室】第5章、研究下声明的方法吧
- 在android源码framework里添加的资源文件!
- 静态链表
- R语言-Knitr包的详细使用说明
- hdu oj 2012
- codeforce B. Road Construction
- Apache Drill: 大数据集互动分析的分布式框架
- 使用PL/SQL Developer连接oracle数据库报“无法解析指定的连接标识符”错误
- 关于spring+hibernate的FlushMode的记录
- 设计模式学习之装饰者模式
- Hadoop本地压缩库<转>
- Android Camera系列开发 (一): 通过Intent拍照
- 从新浪微博搜索站点中超大批量爬取新浪微博内容方法和核心源码(java)
- struts2的工作机制