pat甲级1013. Battle Over Cities (25)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/richenyunqi/article/details/79541363

欢迎访问我的pat甲级题解目录哦https://blog.csdn.net/richenyunqi/article/details/79958195

1013. Battle Over Cities (25)

时间限制
400 ms
内存限制
65536 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 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
1
0
0

题意分析:

给定一个无向图,当删除其中一个结点时,会把跟这个结点有关的边全部删除,问要保持其余的顶点依旧连通,至少需要增加几条边

算法设计:

当删除其中一个顶点及其相关的边之后,计算出剩下的图的连通分量,那么增加的边就应该是求出的连通分量-1。计算连通分量的方法有两种:并查集和深(广)度优先遍历。

采用并查集的C++代码:

#include<bits/stdc++.h>
using namespace std;
struct UFS{//定义并查集类
    int*father=nullptr;
    int size=0;
    UFS(int n){//构造函数,定义一个维度为n的数组,并将元素初始化为其下标
        father=new int[n];
        for(int i=0;i<n;++i)
            father[i]=i;
        size=n;
    }
    ~UFS(){//析构函数
        delete []father;
    }
    int findFather(int x){//查找父亲结点并进行路径压缩
        if(x==father[x])
            return x;
        int temp=findFather(father[x]);
        father[x]=temp;
        return temp;
    }
    void unionSet(int a,int b){//合并两个集合
        int ua=findFather(a),ub=findFather(b);
        if(ua!=ub)
            father[ua]=ub;
    }
    int countRoot(){//计算并查集中集合的数量
        int num=0;
        for(int i=0;i<size;++i)
            if(father[i]==i)
                ++num;
        return num;
    }
};
vector<vector<int>>graph(1005);//存储图
int N,M,K;
int main(){
    scanf("%d%d%d",&N,&M,&K);
    for(int i=0;i<M;++i){
        int a,b;
        scanf("%d%d",&a,&b);
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    while(K--){
        int v;
        scanf("%d",&v);
        UFS ufs(N+1);//定义一个长度为N+1的并查集
        for(int i=1;i<N+1;++i)
            if(i!=v)//编号为v的结点已经被删除,不统计其边
                for(int j:graph[i])
                    if(j!=v)//如果边的末端不是编号为v的结点
                        ufs.unionSet(i,j);//合并两个集合
        printf("%d\n",ufs.countRoot()-3);//注意编号为0和v的集合均独立,所以需要减3
    }
    return 0;
}

采用深度优先遍历的c++代码:

#include<bits/stdc++.h>
using namespace std;
vector<vector<int>>graph(1005);//存储图
bool visit[1005]={false};//标记相应编号的结点是否已被访问
void DFS(int v,int deleteV){//深度优先遍历,v表示当前正在访问的结点,deleteV表示删除的结点
    visit[v]=true;
    for(int i=0;i<graph[v].size();++i){
        int temp=graph[v][i];
        if(!visit[temp]&&temp!=deleteV)
            DFS(temp,deleteV);
    }
}
int N,M,K;
int main(){
    scanf("%d%d%d",&N,&M,&K);
    for(int i=0;i<M;++i){
        int a,b;
        scanf("%d%d",&a,&b);
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    while(K--){
        int v;//删除的结点
        scanf("%d",&v);
        fill(visit+1,visit+N+1,false);//初始化visit为false
        int k=0;//连通分量的数量
        for(int i=1;i<N+1;++i)
            if(!visit[i]&&i!=v){
                DFS(i,v);
                ++k;
            }
        printf("%d\n",k-1);
    }
    return 0;
}
阅读更多

扫码向博主提问

日沉云起

用最短的代码,写最快的算法
  • 擅长领域:
  • 算法
去开通我的Chat快问
换一批

Battle over Cities

02-08

问题描述 :nnIt is vitally important to have all the cities connected by highways in a war, but some of them are destroyed now because of the war. Furthermore,if a city is conquered, all the highways from/toward that city will be closed by the enemy, and we must repair some destroyed highways to keep other cities connected, with the minimum cost if possible.Given the map of cities which have all the destroyed and remaining highways marked, you are supposed to tell the cost to connect other cities if each city is conquered by the enemy.n输入:nnThe input contains multiple test cases. The first line is the total number of cases T (T ≤ 10). Each case starts with a line containing 2 numbers N (0 < N ≤ 20000), and M (0 ≤ M ≤ 100000), which are the total number of cities, and the number of highways, respectively. Then M lines follow, each describes a highway by 4 integers: City1 City2 Cost Status where City1 and City2 are the numbers of the cities the highway connects (the cities are numbered from 1 to N), Cost (0 < Cost ≤ 20000) is the effort taken to repair that highway if necessary, and Status is either 0, meaning that highway is destroyed, or 1, meaning that highway is in use.Note: It is guaranteed that the whole country was connected before the war and there is no duplicated high ways between any two cities.n输出:nnThe input contains multiple test cases. The first line is the total number of cases T (T ≤ 10). Each case starts with a line containing 2 numbers N (0 < N ≤ 20000), and M (0 ≤ M ≤ 100000), which are the total number of cities, and the number of highways, respectively. Then M lines follow, each describes a highway by 4 integers: City1 City2 Cost Status where City1 and City2 are the numbers of the cities the highway connects (the cities are numbered from 1 to N), Cost (0 < Cost ≤ 20000) is the effort taken to repair that highway if necessary, and Status is either 0, meaning that highway is destroyed, or 1, meaning that highway is in use.Note: It is guaranteed that the whole country was connected before the war and there is no duplicated high ways between any two cities.n样例输入:nn34 51 2 1 11 3 1 12 3 1 02 4 1 13 4 2 04 51 2 1 11 3 1 12 3 1 02 4 1 13 4 1 03 21 2 1 11 3 1 1n样例输出:nn12001100inf00

没有更多推荐了,返回首页