Codeforces Round #192 (Div. 2) (330B) B.Road Construction

简介: 要将N个城市全部相连,刚开始以为是最小生成树的问题,其实就是一道简单的题目。 要求两个城市之间不超过两条道路,那么所有的城市应该是连在一个点上的,至于这个点就很好找了,只要找到一个没有和其他点有道路限制的即可。

要在N个城市之间修建道路,使得任意两个城市都可以到达,而且不超过两条路,还有,有些城市之间是不能修建道路的。


思路:


   要将N个城市全部相连,刚开始以为是最小生成树的问题,其实就是一道简单的题目。  要求两个城市之间不超过两条道路,那么所有的城市应该是连在一个点上的,至于这个点就很好找了,只要找到一个没有和其他点有道路限制的即可。

//cf 192 B
#include <stdio.h>
#include <string.h>
char map[1005][1005];
int main()
{
    int n, m;
    while (scanf("%d %d", &n, &m) != EOF)
    {
        int s, e;
        memset(map, 0, sizeof(map));
        for (int i = 1; i <= m; i++)
        {
            scanf("%d %d", &s, &e);
            map[s][e] = 1;
            map[e][s] = 1;
        }
        int x;
        for (int i = 1; i <= n; i++)
        {
            int flag = 1;
            for (int j = 1; j <= n; j++)
            {
                if (map[i][j])
                {
                    flag = 0;
                    break;
                }
            }
            if (flag)
            {
                x = i;
                break;
            }
        }
        printf("%d\n", n-1);
        for (int i = 1; i <= n; i++)
        {
            if (x == i)
                continue;
            else
                printf("%d %d\n", x, i);
        }
    }
    return 0;
}
目录
相关文章
Educational Codeforces Round 113 (Rated for Div. 2)A. Balanced Substring
Educational Codeforces Round 113 (Rated for Div. 2)A. Balanced Substring
68 0
|
机器学习/深度学习 Java
codeforces Educational Codeforces Round 49 (Rated for Div. 2) C题
刚开始拿到这题很懵逼,知道了别人的思路之后开始写,但是还是遇到很多坑,要求求P2/S最大。p=a b。就是求(a2+ b2 +2ab)/ab最大,也就是a/b +b/a最大。那么题意就很明显了。
93 0
|
机器学习/深度学习
|
人工智能
Educational Codeforces Round 98 (Rated for Div. 2)B-Toy Blocks
You are asked to watch your nephew who likes to play with toy blocks in a strange way. He has n boxes and the i-th box has ai blocks. His game consists of two steps: he chooses an arbitrary box i; he tries to move all blocks from the i-th box to other boxes.
240 0