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\n",cnt-1);

    }

}

原文地址:https://www.cnblogs.com/Tony100K/p/11758026.html

时间: 2024-10-08 09:31:24

PAT-1013 Battle Over Cities (25 分) DFS求连通块的相关文章

1013 Battle Over Cities (25分) DFS | 并查集

1013 Battle Over Cities (25分) 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 othe

PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)

1013 Battle Over Cities (25 分) 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 oth

PAT Advanced 1013 Battle Over Cities (25分)

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

PAT Advanced Level 1013 Battle Over Cities (25)(25 分)

1013 Battle Over Cities (25)(25 分) 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

1013. Battle Over Cities (25)——PAT (Advanced Level) Practise

题目信息: 1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 th

UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)

UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常量数组或者写8条DFS调用. 下述算法是:种子填充(floodfill) 两种连通区域 四连通区域:从区域内一点出发,可通过上.下.左.右四个方向的移动组合,在不越出区域的前提下,能到达区域内的任意像素 八连通区域:从区域内每一像素出发,可通过八个方向,即上.下.左.右.左上.右上.左下.右下移动的

UVA 572 Oil Deposits油田(DFS求连通块)

UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M

DFS入门之二---DFS求连通块

用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所在位置相邻(上下左右对角共八个方位),则在一个连通块.典型例题:HDU 1241 Oil Deposits 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目描述:输入m行n列的字符矩阵, 统计字符“@”组成八连块的个数. 题意分析:读入数据

油田(Oil Deposits)-用DFS求连通块

[本博文非博主原创,均摘自:刘汝佳<算法竞赛入门经典>(第2版) 6.4 图] [程序代码根据书中思路,非独立实现] 例题6-12 油田(Oil Deposits,UVa572) 输入一个m行n列的字符矩阵,统计字符"@"组成多少个八连块.如果两个字符"@"所在的格子相邻(横.纵或者对角线方向),就说它们属于一个八连块.例如,下图中有两个八连块. 一.分析 1.1 整体思路 图也有DFS和BFS遍历.由于DFS更容易编写,一般用DFS查找连通块:从每个&