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
    ****************************************************************/


    编程算法爱好者。
  • 相关阅读:
    [转]数值分析——多项式插值之Lagrange插值
    [转]上拉电阻&下拉电阻&高阻态
    [转]Verilog学习笔记基本语法篇(十三)...............Gate门
    Spring MVC中的Controller是Serlvet吗?
    preparestatement和statement的区别&&简单的SQL注入
    jquery局部变量和全局变量的错误
    js 中{},[]中括号,大括号使用详解
    Java数据库学习之SQL语句动态拼接
    jquery中关键字写错导致的错误——dataType写成dateType(data写成date)
    一次隐蔽的while死循环
  • 原文地址:https://www.cnblogs.com/liangrx06/p/5083802.html
Copyright © 2011-2022 走看看