Problem-330B-Ceodeforces Road Construction(思维)

柔情只为你懂 2022-06-07 00:45 203阅读 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 20a0c595b736bd56b51179e1faa90c1bebbbaf3e.png.

Then m lines follow, each consisting of two integers a**i and b**i (1 ≤ a**i, b**i ≤ n, a**i ≠ b**i), which means that it is not possible to construct a road connecting cities a**i and b**i. 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 a**i and b**i (1 ≤ a**i, b**i ≤ n, a**i ≠ b**i), which means that a road should be constructed between cities a**i and b**i.

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

Examples

input

  1. 4 1
  2. 1 3

output

  1. 3
  2. 1 2
  3. 4 2
  4. 2 3

Note

This is one possible solution of the example:

e0655a1cf7b39a8d2447728cde1059fb3cccbaeb.png

These are examples of wrong solutions:

9feb7083b4d6c60ba32c71a1738c63b8bfac1b15.png 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. 92ceeb24653609dd6c23343e6bbd2b97e0c3a551.png 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. 80b1d5102b747752e1a19964bb3495be37e9a41b.png 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 3, 2 to 3, and 4 to 3.

思路:该题看似很复杂,但其中有个条件限制: it is possible to go from each city to any other city by traversing at most two roads。难度一下就降低了。只要找到形状如下类型的一种情况就行:

![Image 1][]

  1. #include<stdio.h>
  2. #include<cstring>
  3. using namespace std;
  4. const int maxn=1010;
  5. const int INF=0x3f3f3f3f;
  6. int n, m, mp[maxn][maxn];
  7. int main()
  8. {
  9. memset(mp, 0, sizeof(mp));
  10. scanf("%d%d", &n, &m);
  11. int u, v;
  12. for(int i=1; i<=m; i++)
  13. {
  14. scanf("%d%d", &u, &v);
  15. mp[u][v]=1;
  16. mp[v][u]=1;
  17. }
  18. int tmp, flag=0;
  19. for(int i=1; i<=n; i++)
  20. {
  21. flag=0;
  22. for(int j=1; j<=n; j++)
  23. {
  24. if(mp[i][j])
  25. {
  26. flag=1;
  27. }
  28. }
  29. if(flag==0)
  30. {
  31. tmp=i;
  32. break;
  33. }
  34. }
  35. printf("%d\n", n-1);
  36. for(int i=1; i<=n; i++)
  37. {
  38. if(i!=tmp)
  39. {
  40. printf("%d %d\n", tmp, i);
  41. }
  42. }
  43. return 0;
  44. }

[Image 1]:

发表评论

表情:
评论列表 (有 0 条评论,203人围观)

还没有评论,来说两句吧...

相关阅读