PAT Advanced 1013 Battle Over Cities (25) [图的遍历,统计连通分量的个数,DFS,BFS]

题目

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
100

题目分析

已知城市数N,现有公路数M,现有公路信息,测试样例数K,求若某一城市被敌军占领后,需要修建几条公路可以从新连接剩余城市

题目翻译:已知图中顶点数和边,求删除某一顶点后,有多少个连通分量cnt,需cnt-1条边将这些连通分量连接

解题思路

保存图中边信息

  • 邻接矩阵
  • 邻接表

求连通分量数量

  • 深度优先遍历DFS
  • 广度优先遍历BFS

Code

Code 01(邻接矩阵 DFS)

#include <iostream>
using namespace std;
const int maxn=1010;
int n,g[maxn][maxn],vis[maxn];
void dfs(int c) {
    vis[c]=true;
    for(int i=1; i<=n; i++) {
        if(g[c][i]!=0&&vis[i]==false) {
            dfs(i);
        }
    }
}
int dfs_travel(int c) {
    int ans=0;
    vis[c]=true;
    for(int i=1; i<=n; i++) {
        if(vis[i]==false) {
            dfs(i);
            ans++;
        }
    }
    return ans-1;
}
int main(int argc,char * argv[]) {
    int m,k,a,b,c;
    scanf("%d %d %d",&n,&m,&k);
    for(int i=0; i<m; i++) {
        scanf("%d %d",&a,&b);
        g[a][b]=1;
        g[b][a]=1;
    }
    for(int i=0; i<k; i++) {
        scanf("%d",&c);
        fill(vis,vis+maxn,0);
        int ans=dfs_travel(c);
        printf("%d\n",ans);
    }
    return 0;
}

Code 02(邻接矩阵 BFS)

#include <iostream>
#include <queue>
using namespace std;
const int maxn=1010;
int n,g[maxn][maxn],inq[maxn];
void bfs(int c) {
    queue<int> q;
    q.push(c);
    inq[c]=true;
    while(!q.empty()) {
        int p = q.front();
        q.pop();
        for(int i=1; i<=n; i++) {
            if(g[p][i]==1&&inq[i]==false) {
                q.push(i);
                inq[i]=true;
            }
        }
    }
}
int bfs_travel(int c) {
    int ans=0;
    inq[c]=true;
    for(int i=1; i<=n; i++) {
        if(inq[i]==false) {
            bfs(i);
            ans++;
        }
    }
    return ans-1;
}
int main(int argc,char * argv[]) {
    int m,k,a,b,c;
    scanf("%d %d %d",&n,&m,&k);
    for(int i=0; i<m; i++) {
        scanf("%d %d",&a,&b);
        g[a][b]=1;
        g[b][a]=1;
    }
    for(int i=0; i<k; i++) {
        scanf("%d",&c);
        fill(inq,inq+maxn,0);
        int ans=bfs_travel(c);
        printf("%d\n",ans);
    }
    return 0;
}    

Code 03(邻接表 DFS)

#include <iostream>
#include <vector>
using namespace std;
const int maxn=1010;
int n,vis[maxn];
vector<int> g[maxn];
void dfs(int c) {
    vis[c]=true;
    for(int i=0; i<g[c].size(); i++) {
        if(vis[g[c][i]]==false) {
            dfs(g[c][i]);
        }
    }
}
int dfs_travel(int c) {
    int ans=0;
    vis[c]=true;
    for(int i=1; i<=n; i++) {
        if(vis[i]==false) {
            dfs(i);
            ans++;
        }
    }
    return ans-1;
}
int main(int argc,char * argv[]) {
    int m,k,a,b,c;
    scanf("%d %d %d",&n,&m,&k);
    for(int i=0; i<m; i++) {
        scanf("%d %d",&a,&b);
        g[a].push_back(b);
        g[b].push_back(a);
    }
    for(int i=0; i<k; i++) {
        scanf("%d",&c);
        fill(vis,vis+maxn,0);
        int ans=dfs_travel(c);
        printf("%d\n",ans);
    }
    return 0;
}

Code 04(邻接表 BFS)

#include <iostream>
#include <queue>
using namespace std;
const int maxn=1010;
int n,inq[maxn];
vector<int> g[maxn];
void bfs(int c) {
    queue<int> q;
    q.push(c);
    inq[c]=true;
    while(!q.empty()) {
        int p = q.front();
        q.pop();
        for(int i=0; i<g[p].size(); i++) {
            if(inq[g[p][i]]==false) {
                q.push(g[p][i]);
                inq[g[p][i]]=true;
            }
        }
    }
}
int bfs_travel(int c) {
    int ans=0;
    inq[c]=true;
    for(int i=1; i<=n; i++) {
        if(inq[i]==false) {
            bfs(i);
            ans++;
        }
    }
    return ans-1;
}
int main(int argc,char * argv[]) {
    int m,k,a,b,c;
    scanf("%d %d %d",&n,&m,&k);
    for(int i=0; i<m; i++) {
        scanf("%d %d",&a,&b);
        g[a].push_back(b);
        g[b].push_back(a);
    }
    for(int i=0; i<k; i++) {
        scanf("%d",&c);
        fill(inq,inq+maxn,0);
        int ans=bfs_travel(c);
        printf("%d\n",ans);
    }
    return 0;
}


猜你喜欢

转载自www.cnblogs.com/houzm/p/12354781.html