http://pat.zju.edu.cn/contests/pat-practise/1013

 

1013. Battle Over Cities (25)

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

[cpp]  view plain copy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<memory.h>  
  4. #include<algorithm>  
  5. #include<cstring>  
  6. #include<queue>  
  7. #include<cmath>  
  8. #include<cstdlib>  
  9. #include<iomanip>  
  10. #include<string>  
  11. using namespace std;  
  12. int map[1001][1001];  
  13. int visit[1001];  
  14. int n;  
  15. void dfs(int x){  
  16.     visit[x] = 1;  
  17.     for(int i=1;i<=n;++i){  
  18.         if(map[x][i] && !visit[i]){  
  19.             dfs(i);  
  20.         }  
  21.     }  
  22. }  
  23. int getblock(){  
  24.     int ans = 0;  
  25.     for(int i=1;i<=n;++i){  
  26.         if(!visit[i]){  
  27.             ans++;  
  28.             dfs(i);  
  29.         }  
  30.     }  
  31.     return ans;  
  32. }  
  33.   
  34. int main(){  
  35.   
  36.     //freopen("in.txt", "r", stdin);  
  37.   
  38.     int m, k;  
  39.     int a, b;  
  40.     int tmp;  
  41.     while(cin>>n>>m>>k){  
  42.         memset(map, 0, sizeof(map));  
  43.         for(int i=0;i<m;++i){  
  44.             scanf("%d%d", &a, &b);  
  45.             map[a][b] = map[b][a] = 1;  
  46.         }  
  47.           
  48.         for(int i=0;i<k;++i){  
  49.             memset(visit, 0, sizeof(visit));  
  50.             scanf("%d", &tmp);  
  51.             visit[tmp] = 1;  
  52.             int ans = getblock();  
  53.             printf("%d\n", ans-1);  
  54.         }  
  55.     }  
  56.       
  57.     //fclose(stdin);  
  58.     return 0;  
  59. }  
  60.       
──(root㉿xcs)-[/home/xcs/桌面] └─# gpg --show-keys /etc/apt/trusted.gpg.d/kali-archive-keyring.gpg gpg: 目录‘/root/.gnupg’已创建 gpg: 钥匙箱‘/root/.gnupg/pubring.kbx’已创建 pub rsa4096 2025-04-17 [SC] [有效至:2028-04-17] 827C8569F2518CC677FECA1AED65462EC8D5E4C5 uid Kali Linux Archive Automatic Signing Key (2025) <devel@kali.org> ┌──(root㉿xcs)-[/home/xcs/桌面] └─# apt update 错误:1 http://mirrors.aliyun.com/kali kali-rolling InRelease 403 Forbidden [IP: 124.225.96.42 80] 命中:2 http://http.kali.org/kali kali-rolling InRelease 获取:3 http://http.kali.org/kali kali-rolling/main i386 Packages [20.4 MB] 获取:4 http://http.kali.org/kali kali-rolling/main i386 Contents (deb) [47.7 MB] 获取:5 http://http.kali.org/kali kali-rolling/contrib i386 Packages [97.1 kB] 获取:6 http://http.kali.org/kali kali-rolling/contrib i386 Contents (deb) [183 kB] 获取:7 http://http.kali.org/kali kali-rolling/non-free i386 Packages [147 kB] 获取:8 http://mirrors.neusoft.edu.cn/kali kali-rolling/non-free i386 Contents (deb) [859 kB] 错误: 无法下载 http://mirrors.aliyun.com/kali/dists/kali-rolling/InRelease 403 Forbidden [IP: 124.225.96.42 80] 错误: 仓库 “http://mirrors.aliyun.com/kali kali-rolling InRelease” 没有数字签名。 注意: 无法安全地用该源进行更新,所以默认禁用该源。 注意: 参见 apt-secure(8) 手册以了解仓库创建和用户配置方面的细节。 ┌──(root㉿xcs)-[/home/xcs/桌面] └─#
最新发布
07-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值