• 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);
    
        }
    
    }
    
    
  • 相关阅读:
    1260. [CQOI2007]涂色【区间DP】
    2733. [HNOI2012]永无乡【平衡树-splay】
    1087. [SCOI2005]互不侵犯King【状压DP】
    1026. [SCOI2009]windy数【数位DP】
    1066. [SCOI2007]蜥蜴【最大流】
    luogu P2776 [SDOI2007]小组队列
    cogs 717. [SDOI2007] 小组队列
    luogu P1160 队列安排
    2612. [FHZOI 2017]被窃的项链
    codevs 3336 电话网络 (2)
  • 原文地址:https://www.cnblogs.com/Tony100K/p/11758026.html
Copyright © 2020-2023  润新知