• 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;
    }
  • 相关阅读:
    妙用Telnet快速收发电子邮件(转载)
    windows server 2003如何安装IIS,配置IIS,让iis支持aspx(收集)
    T7400等DELL工作站及服务器的Windows server 2003系统安装——解决“找不到安装在计算机上的硬盘驱动器 安装无法继续,要退出请按F3”问题
    PostgresSQL连接认证设置(收集)
    安装PostgreSQL :Problem running postinstall (收集)
    UltiDev Cassini Web Server介绍
    配置Lumisoft Mail Server给外网邮箱发消息
    mysql命令行常用命令(收集)
    SQL复制数据表及表结构
    解决lumisoft mail server使用中的错误“550 5.7.1 Unable to relay for xxx”
  • 原文地址:https://www.cnblogs.com/Traveller-Leon/p/5035341.html
Copyright © 2020-2023  润新知