zoukankan      html  css  js  c++  java
  • PAT-1013 Battle Over Cities (25 分) DFS求连通块

    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 cit**y1-cit**y2 and cit**y1-cit**y3. Then if cit**y1 is occupied by the enemy, we must have 1 highway repaired, that is the highway cit**y2-cit**y3.

    Input Specification:

    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 Specification:

    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
    

    题目大意 给一个图,查询删掉某一个点之后还有几个连通块

    思路分析 第五个例子太迷了,写邻接表和前向星都有段错误,不知道前向星是不是开小了,这题点只有1000个,所以还是用最传统的矩阵存储比较好,第五个测试样例接近完全图了都.这种点比较少的图很容易出这种恶心的样例

    #include<bits/stdc++.h>
    #define de(x) cout<<#x<<" "<<(x)<<endl
    #define each(a,b,c) for(int a=b;a<=c;a++)
    using namespace std;
    const int maxn=1000+5;
    const int maxm=1e6+5;
    const int inf=0x3f3f3f3f;
    //vector<int>G[maxn];
    /*
    int head[maxn];
    struct edge
    {
        int v;
        int next;
    }edge[maxm];
    int cnt;
    void addEdge(int u,int v)
    {
        edge[cnt].v=v;
        edge[cnt].next=head[u];
        head[u]=cnt++;
    }*/
    int a[maxn][maxn];
    ///段错误?????
    /*
    3 2 3
    1 2
    1 3
    1 2 3
    */
    int ban=0;
    bool vis[maxn];
    void dfs(int x,int n)
    {
        vis[x]=true;
        for(int i=1;i<=n;i++)
        {
    
            if(!vis[i]&&a[x][i]==1)
            {
                dfs(i,n);
            }
        }
        return;
    }
    int main()
    {
        int n,m,q;
        cin>>n>>m>>q;
        //memset(head,0,sizeof(head));
        //cnt=1;
        memset(a,0,sizeof(a));
        while(m--)
        {
            int aa,b;
            cin>>aa>>b;
            a[aa][b]=1;
            a[b][aa]=1;
    
            //G[a].push_back(b);
            //G[b].push_back(a);///别写vector和map了
        }
        while(q--)
        {
            int query;
            cin>>query;
    
            memset(vis,0,sizeof(vis));
            vis[query]=true;
            int cnt=0;
            for(int i=1;i<=n;i++)
            {
                if(vis[i])continue;
                dfs(i,n);
                cnt++;
            }
            printf("%d
    ",cnt-1);
    
        }
    
    }
    
    
  • 相关阅读:
    Stack Overflow 2016最新架构探秘
    (转)个人职业规划中如何使自己的职业生涯升华
    (转)软件架构师应该知道的97件事
    (转)一共81个,开源大数据处理工具汇总
    (转) 架构师的能力模型
    (转)数据库表分割技术浅析(水平分割/垂直分割/库表散列)
    (转)乐观锁与悲观锁——解决并发问题
    (转)从“如何设计用户超过1亿的应用”说起—数据库调优实战
    (转)浅谈数据库的水平拆分
    (转).NET Memory Profiler 使用简介
  • 原文地址:https://www.cnblogs.com/Tony100K/p/11758026.html
Copyright © 2011-2022 走看看