1013 Battle Over Cities
创始人
2024-02-06 03:32:59
0

 

目录

Input Specification:

Output Specification:

Sample Input:

Sample Output:

一、题目大意 

二、思路

三、代码 

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

 

一、题目大意 

有N个城市和M条道路,每条道路只包含两端的城市

现给出K种城市被攻陷的情况,每种情况下只有一个城市被攻陷

要求给出每种情况下,让没有被攻陷的城市能够全连通最少需要再修建几条公路

二、思路

1.最好的情况,不需要新修建道路,如下图1(黑色线条代表已存在的道路,红色线条代表被攻陷后无法通行的道路,红色数字表示被攻陷的城市,下同

                                                                 图1

                                                           

2.中枢城市被攻陷,出现若干个分散的城市集合,如下图2

                                                                        图2

                                                             

 

分析:上图中,城市3被攻陷,此时端点含有3的道路全部断开,由此生成了4个分散的城市集合,如下图3

                                                                        图3

                                                               

显而易见,最少需要的道路数量就是 除被攻陷城市外分散的城市集合个数-1

那么解题流程如下:

1.读入原始数据,并生成原始图。

2.将端点含有被攻陷城市的道路切断。

3.统计切断后除被攻陷城市外分散的城市集合个数,减去1后输出

4.重新拷贝原始图后,循环执行2~3

三、代码

#include
using namespace std;
#define MAXN 1001bool vis[MAXN];void dfs(vector Adj[] , int x);int main(){int N , M , K;cin >> N >> M >> K;vector init[N + 1];for(int i = 0;i < M;i ++){int city1 , city2;cin >> city1 >> city2;//存入初始图init[city1].emplace_back(city2);init[city2].emplace_back(city1);}for(int i = 0;i < K;i ++){int lost; //被攻陷的城市cin >> lost;vector Adj[N + 1]; //存放切断道路后的图for(int j = 1;j <= N;j ++){if(j != lost){  Adj[j] = vector(init[j]); //不要放入被攻陷的城市}}for(int j = 1;j <= N;j ++){if(j != lost){auto it = find(Adj[j].begin() , Adj[j].end() , lost); //如果当前城市与被攻陷的城市之间有道路if(it != Adj[j].end()){//把和被攻陷城市连通的道路放到末尾并切断swap(*it , *(Adj[j].end() - 1));Adj[j].pop_back();}}}int connect_num = 0; //分散的城市集合的数量vector isConnect; //用来存放已经记录过的城市,或是与已经记录过的城市连通的城市for(int j = 1;j <= N;j ++){if(j != lost){//若当前城市还没有被记录,且不是被攻陷的城市if(find(isConnect.begin() , isConnect.end() , j) == isConnect.end()){int flag = 0; //判断是否全连通memset(vis , false , N + 1); //初始化访问dfs(Adj , j); //从当前城市开始访问for(int k = 1;k <= N;k ++){if(k != lost){if(!vis[k]){flag = 1;}else{isConnect.emplace_back(k); //将已经访问的城市记录。}}}if(flag == 1){//若没有全连通,则说明从该城市出发是一个未被记录的分散城市集合connect_num ++; }}}}printf("%d\n" , connect_num == 0 ? 0 : connect_num - 1);}return 0;
}void dfs(vector Adj[] , int x){vis[x] = true;for(int i = 0;i < Adj[x].size();i ++){int v = Adj[x][i];if(!vis[v]){dfs(Adj , v);}}
}

相关内容

热门资讯

50岁女性退休有什么创业项目,...         2016年12月20日,山西“钦州黄”小米(集团)有限公司召开股东大会,纪念母亲诞辰...
现代物流企业的经营模式,物流企...   来源 | 运联智库(ID:tucmedia),作者 | 贾艺超,编辑 | 小L      今年7...
研究生如何创业,硕士研究生回家...   武汉大学MBA第一批提前面试流程 一文教你搞定武汉大学MBA报考 林晨陪你考研         ...
创业失败负债,直播负债创业的网...   5月15日晚上8点,Tik Tok的直播间人头攒动,一张销售成绩单让所有网友大吃一惊!这真是太...
创业用什么电脑最好,创业老板用...   从CPU到显卡,从内存到硬盘,主板到系统.……PC产品的技术几乎都来自供应链,普通消费者也可以自...
怎样开店做生意才能赚钱,初次创...   #作者:庞刚谈生意/来源:原创      #起点站          qvj2l q49k ...
创业怎么签署就业协议,三方协议...   2021年,将涌入各行各业.将有900万毕业生对于毕业生来说,要注意自身的身份优势,才能更好地适...
创业培训师证怎么考,创业者都有...   现在世界已经进入知识经济时代,中国新一轮创业高潮中最成功的创业者都是高学历的人。但是,受教育程度...
创业中如何做好自己,创业心理如...   农村三人行的15元自助火锅通过扩店升级为18元自助火锅。后来,他成了甩手掌柜。店里的两个韩寒兄弟...
加盟算创业么,路边的小商贩算创...   “东风夜花千树。更吹,星如雨。宝马雕花车气味满路。笛声响起,玉壶转动,鱼龙共舞一夜。”这是800...