PAT 甲级 1013 Battle Over Cities
发布日期:2021-07-01 03:08:47 浏览次数:2 分类:技术文章

本文共 2558 字,大约阅读时间需要 8 分钟。

1013 Battle Over Cities (25 point(s))

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 city​1​​-city​2​​ and city​1​​-city​3​​. Then if city​1​​ is occupied by the enemy, we must have 1 highway repaired, that is the highway city​2​​-city​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 Knumbers, 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 31 21 31 2 3

Sample Output:

100

Experiential Summing-up

This question is a simple and classical traversal of graph. Both DFS and BFS are right. You can select as your mood~~

(The purpose of using English to portray my solution is that to exercise the ability of my expression of English and accommodate PAT advanced level's style.We can make progress together by reading and comprehending it. Please forgive my basic grammar's and word's error. Of course, I would appreciated it if you can point out my grammar's and word's error in comment section.( •̀∀•́ ) Furthermore, Big Lao please don't laugh at me because I just a English beginner settle for CET6    _(:з」∠)_  )

Accepted Code

#include 
#include
#include
#include
using namespace std;const int INF=0x3fffffff;const int maxn=1010;bool G[maxn][maxn],C[maxn][maxn];bool flag[maxn];int n,f;void copy(){ for(int i=1;i<=n;++i) for(int j=1;j<=n;++j) C[i][j]=G[i][j];}void BFS(int s){ queue
q; flag[s]=true; q.push(s); while(q.size()) { int x=q.front(); q.pop(); for(int i=1;i<=n;++i) { if(flag[i]==false&&C[x][i]==1) { q.push(i); flag[i]=true; } } }}int main(){ int m,k,s,e; scanf("%d %d %d",&n,&m,&k); memset(G,0,sizeof(G)); for(int i=0;i

 

转载地址:https://meteorrain.blog.csdn.net/article/details/86544981 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:PAT 甲级 1014 Waiting in Line
下一篇:PAT 甲级 1012 The Best Rank

发表评论

最新留言

很好
[***.229.124.182]2024年04月08日 09时23分03秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章