• 1013——Battle Over Cities


    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.

    Input

    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.

    Output

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

    Sample Input

    3 2 3
    1 2
    1 3
    1 2 3
    

    Sample Output

    1
    0
    0

    利用并查集检测图中有几棵树。
    代码参考自:http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece7631046893b4c4380146d96864968d4e414c422460b012ebce424221303ce963c215afe170bf7a6613464587ef686c98348dbba922d2e9c6269304a894210d018b8ca3632c157875a98ff4aa1fcae6584aea38e9b0313dd53742bdbb6d00d41&p=9b63cd0286cc41ac52f7c7710f4dcc&newp=882a9546dc9602b706fbc7710f4b8f231610db2151d7d0162a978b1685&user=baidu&fm=sc&query=1013%2E+Battle+Over+Cities&qid=874b28040013689d&p1=1
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cctype>
    #include <cstdlib>
    #include<cmath>
    #include <string>
    #include <map>
    #include <set>
    #include <queue>
    #include <vector>
    #include <stack>
    #include <cctype>
    using namespace std;
    typedef unsigned long long ull;
    #define INF 0xfffffff
    
    int tree[1001];
    int findRoot(int x)
    {
        if(tree[x]==-1) 
        {
            return x;
        }
        else
        {
            int tmp=findRoot(tree[x]);
            tree[x]=tmp;
            return tmp;
        }
    }
    struct road{
        int x,y;
    }r[500000];
    int main()
    {
        int x,y,n,m,i,j,k,ans;
        
        cin>>n>>m>>k;
        for(i=0;i<m;++i)
        {
            cin>>r[i].x>>r[i].y;
        }
        while(k--)
        {
            for(i=1;i<=n;++i)
            {
                tree[i]=-1;
            }
            int c;
            cin>>c;
            for(i=0;i<m;++i)
            {
                if(r[i].x!=c&&r[i].y!=c)
                {
                    int tx=findRoot(r[i].x);
                    int ty=findRoot(r[i].y);
                    if(tx!=ty)
                    {
                        tree[tx]=ty;
                    }
                }
            }
            ans=0;
            for(i=1;i<=n;++i)
            {
                if(tree[i]==-1)
                {
                    ans++;
                }
            }
            cout<<ans-2<<endl;
        }
    
          return 0;
    }
  • 相关阅读:
    【HDOJ1811】【并查集预处理+拓扑排序】
    【HDOJ1598】【枚举+最小生成树】
    【HDOJ图论题集】【转】
    第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)
    POJ2985 The k-th Largest Group treap
    POJ 1056 IMMEDIATE DECODABILITY Trie 字符串前缀查找
    ZOJ2334 Monkey King 左偏树
    POJ2352 Stars 树状数组
    POJ2492 A Bug's Life 判断二分图
    POJ 3268 最短路应用
  • 原文地址:https://www.cnblogs.com/Traveller-Leon/p/5035341.html
Copyright © 2020-2023  润新知