zoukankan      html  css  js  c++  java
  • 九度OJ 1325:Battle Over Cities(城市间的战争) (并查集)

    时间限制:1 秒

    内存限制:32 兆

    特殊判题:

    提交:376

    解决:132

    题目描述:

    It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the cities connected. Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly.

    For example, if we have 3 cities and 2 highways connecting city1-city2 and city1-city3. Then if city1 is occupied by the enemy, we must have 1 highway repaired, that is the highway city2-city3.

    输入:

    Each input file contains one test case. Each case starts with a line containing 3 numbers N (<1000), M and K, which are the total number of cities, the number of remaining highways, and the number of cities to be checked, respectively. Then M lines follow, each describes a highway by 2 integers, which are the numbers of the cities the highway connects. The cities are numbered from 1 to N. Finally there is a line containing K numbers, which represent the cities we concern.

    输出:

    For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.

    样例输入:
    3 2 3
    1 2
    1 3
    1 2 3
    样例输出:
    1
    0
    0

    思路:

    题意是求,若第i个城市被占领了,需要另外修几条路。

    我的思路是对每种情况求并查集,求集合个数num,num-1就是需要修的路。


    代码:

    #include <stdio.h>
     
    #define N 1000
    #define M 1000000
     
    typedef struct node {
        int x;
        int y;
    } ROAD;
     
    int n;
    int pre[N+1];
    int count[N+1];
    int num;
     
    void init()
    {
        for (int i=1; i<=n; i++)
        {
            pre[i] = i;
            count[i] = 1;
        }
        num = n;
    }
     
    int find(int i)
    {
        while (i != pre[i])
            i = pre[i];
        return i;
    }
     
    int combine(int i, int j)
    {
        int a = find(i);
        int b = find(j);
        if (a != b)
        {
            if (count[a] > count[b])
            {
                pre[b] = a;
                count[a] += count[b];
                count[b] = 0;
            }
            else
            {
                pre[a] = b;
                count[b] += count[a];
                count[a] = 0;
            }
            num --;
            return 1;
        }
        else
            return 0;
    }
     
    int main(void)
    {
        int m, k, i, j;
        ROAD r[M];
        int concern[N+1];
     
        while (scanf("%d%d%d", &n, &m, &k) != EOF)
        {
            for(i=0; i<m; i++)
                scanf("%d%d", &r[i].x, &r[i].y);
            for(j=0; j<k; j++)
            {
                init();
                scanf("%d", &concern[j]);
                for(i=0; i<m; i++)
                {
                    if (r[i].x != concern[j] && r[i].y != concern[j])
                        combine(r[i].x, r[i].y);
                    if (num == 2)
                        break;
                }
                printf("%d
    ", num-2);
            }
        }
     
        return 0;
    }
    /**************************************************************
        Problem: 1325
        User: liangrx06
        Language: C
        Result: Accepted
        Time:190 ms
        Memory:8664 kb
    ****************************************************************/


    编程算法爱好者。
  • 相关阅读:
    HTML head 头标签
    p标签text-align:justify以及CSS文字两端对齐
    css雪碧图制作
    ps去掉图片上的文字
    Qt Quick之TableView的使用
    Qt Quick之Canvas
    Qt Quick中的信号与槽
    QT之在QML中使用C++类和对象
    rhel7+apache+c cgi+动态域名实现web访问
    (转载)JDOM/XPATH编程指南
  • 原文地址:https://www.cnblogs.com/liangrx06/p/5083802.html
Copyright © 2011-2022 走看看