PAT-1013 Battle Over Cities (25 分) DFS求连通块

Posted tony100k

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 city1-city2 and city1-city3. Then if city1 is occupied by the enemy, we must have 1 highway repaired, that is the highway city2-*cit**y*3.

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);

    }

}

以上是关于PAT-1013 Battle Over Cities (25 分) DFS求连通块的主要内容,如果未能解决你的问题,请参考以下文章

PAT1013: Battle Over Cities

pat 1013 Battle Over Cities(25 分) (并查集)

PAT A1013 Battle Over Cities (25 分)

PAT 1013Battle Over Cities

PAT-1013 Battle Over Cities (25 分) DFS求连通块

PAT1013. Battle Over Cities(邻接矩阵邻接表分别dfs)