cf 192 div2 B. Road Construction

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.

Sample test(s)
input
4 1
1 3
output
3
1 2
4 2
2 3

这道题的意思就是先给出几条道路,然后给出这些道路间不能相连的是那些,然后问最少建几条路,可以保证相连,然后输出链接的方式。
关键就在于一定要看出来,有一条路是作为中转站链接所有的店得,看出来这一点就好做了。(从b哥那里学来的)
下面是代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

bool vis[2000];

int main(){
    int n,m;
    int a,b;
    int ans;
    cin>>n>>m;
    memset(vis,true,sizeof(vis));
    while(m--){
         cin>>a>>b;
         vis[a]=false;
         vis[b]=false;
    }
    for(int i=1;i<=n;i++){
         if(vis[i]){
            ans=i;
            break;
         }
    }
    printf("%d\n",n-1);
    for(int  i=1;i<=n;i++){
         if(ans!=i)
         printf("%d %d\n",ans,i);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值