CodeForces

来源:互联网 发布:cf刷枪软件永久 编辑:程序博客网 时间:2024/06/07 17:24

 CodeForces - 330B 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 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 ≤ n, ai ≠ 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.
Example
    Input
    4 1
    1 3
    Output
    3
    1 2
    4 2
    2 3

题意:在n个城市间修路,要求任意一个城市到其他任意一个城市之间的路不超过两条,并且有些路是不能修的

思路:开始只往最小生成树和弗洛伊德上想了,训练完听别人讲是简单的思维题,因为任意两个城市之间的路不能超过两条,所以其中有一个点连接着剩下的n-1个点,这个点就从可以与n-1个点都能修路的中任意选一个,修的最小路数就为n-1

#include<stdio.h>int a[1001];int main(void){int n,m;scanf("%d%d",&n,&m);for(int i=1;i<=m;i++){int u,v;scanf("%d%d",&u,&v);a[u]=1;a[v]=1;}int mid;for(int i=1;i<=n;i++){if(!a[i]){mid=i;break;}}printf("%d\n",n-1);for(int i=1;i<=n;i++){if(i==mid) continue;else{printf("%d %d\n",mid,i);}}return 0;}


原创粉丝点击