Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

CF 330B Road Construction 构造路径 水题

$
0
0
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
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.


题解:
请看完题目后仔细观察数据。 你会发现 m < n/2。 这意味着什么意思呢。 意味着必存在一个点能跟所有点相连。  你可以通过反证法来证明这个规律。  既然如此,那么只用找出这么一个能连接所有点的点就解决了此问题了。 成了个大水题。 然后输出的边肯定只有n-1条。结束。

代码:

/*
 * @author ipqhjjybj
 * @date  20130720
 *
 */
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
#define ll long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define clr(x,k) memset(x,k,sizeof(x))
int map[1005];
int main(){
    int n,m;
    scanf("%d %d",&n,&m);
    if(n == 1)
    {
        printf("0\n");
        return 0;
    }
    clr(map,0);
    int z;
    for(int i=0,a,b;i<m;i++){
        scanf("%d %d",&a,&b);
        map[a]++,map[b]++;
    }
    for(int i=1;i<=n;i++){
        if(map[i]==0){
            z = i;
            break;
        }
    }
    printf("%d\n",n-1);
    for(int i = 1;i<=n;i++)
        if(i!=z)
            printf("%d %d\n",i,z);
    return 0;
}



作者:ipqhjjybj 发表于2013-7-21 0:09:48 原文链接
阅读:149 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>