pat甲级1013. Battle Over Cities (25)

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

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

题目描述

题意分析

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

算法设计

当删除其中一个顶点及其相关的边之后,计算出剩下的图的连通分量,那么增加的边就应该是求出的连通分量-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;
}

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

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

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试