zoukankan      html  css  js  c++  java
  • [Codeforces Round #192 (Div. 2)] 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
    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.
     
    题意:有n个城市,然后给定m对城市不可建路,一个城市到另一个城市最多经过两条路,问如何建路。
    题解:注意数据规模,不可建的道路数目m满足。说明必存在城市x可以与任何城市相连。又因为从一城市到任一城市最多经过两条路。所以为星型,中间为城市x。题目要求If there are several solutions, you may print any of them。本题为special judge。
     
     
     
    代码:
     1 #include<stdio.h>
     2 #include<string.h>
     3 int i,j,n,m,x,y,p,
     4     a[1100];
     5 
     6 int
     7 pre()
     8 {
     9     memset(a,0,sizeof(a));
    10      return 0;
    11 }
    12 
    13 int
    14 init()
    15 {
    16     scanf("%d%d",&n,&m);
    17     for(i=1;i<=m;i++)
    18     {
    19         scanf("%d%d",&x,&y);
    20         a[x]++;
    21         a[y]++;
    22     }
    23     
    24     return 0;
    25 }
    26 
    27 int 
    28 main()
    29 {
    30     pre();
    31     init();
    32     for(i=1;i<=n;i++)
    33     {    
    34         if(a[i]==0)
    35         {
    36             p=i;
    37             break;
    38         }
    39     }
    40         printf("%d
    ",n-1);
    41         for(i=1;i<=n;i++)
    42         if(i!=p)
    43         printf("%d %d
    ",p,i);
    44         
    45         return 0;
    46 }
    47     
    48     
  • 相关阅读:
    GitHub上受欢迎的Android UI Library
    推荐的优秀博客--多关注,多看看
    Android SDK在线更新镜像服务器大全
    SqlServer查看各个表所占空间大小的sql
    演化理解 Android 异步加载图片
    tsp、rtmp测试地址
    eclipse离线安装Properties Editor插件查看配置文件中Unicode内容
    亲测成功关闭谷歌浏览器自动更新方法分享 取消chrome自动更新
    SQL Server 中用While循环替代游标(Cursor的解决方案
    【转】service和serviceImpl的选择
  • 原文地址:https://www.cnblogs.com/sxiszero/p/3641777.html
Copyright © 2011-2022 走看看