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

Source: Internet
Author: User

Question:

It is necessary to build roads between N cities so that any two cities can reach and there are no more than two roads. In addition, some cities cannot build roads.

Ideas:

To connect all N cities, we thought it was the smallest spanning tree problem at the beginning, which is actually a simple question. It is required that there be no more than two roads between two cities, so all cities should be connected to one point. As for this point, it is very easy to find, you only need to find a path that is not restricted by other points.

//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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.