1013. Battle Over Cities (25)
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
//深度优先搜索
#include<cstdio> #include<vector> using namespace std; const int maxn = 1010; vector<int> G[maxn]; bool vis[maxn]; int n,m,k; int current; void DFS(int v){ if(v == current) return; vis[v] = true; for(int i = 0; i < G[v].size(); i++){ if(vis[G[v][i]] == false ){ DFS(G[v][i]); } } } int main(){ scanf("%d%d%d",&n,&m,&k);// 城市数量,铁路数量,要检查城市数量 int u,v; for(int i = 0; i < m; i++){ scanf("%d%d",&u,&v); G[u].push_back(v); G[v].push_back(u); } for(int i = 0; i < k; i++){ scanf("%d",¤t); memset(vis,false,sizeof(vis)); int block = 0; for(int j = 1; j <= n; j++){ //如果j从0到n-1 if(j != current && vis[j] == false){ DFS(j); block++; } } printf("%d\n",block - 1); } return 0; }
//并查集
#include<cstdio> #include<vector> using namespace std; const int maxn = 1010; vector<int> G[maxn]; bool vis[maxn]; int father[maxn]; int n,m,k; int current; int findFather(int x){ int a = x; while(x != father[x]){ x = father[x]; } while(a != father[a]){ int z = a; a = father[a]; father[z] = x; } return x; } void Union(int a,int b){ int faA = findFather(a); int faB = findFather(b); if(faA != faB) father[faA] = faB; } void init(){ for(int i = 0; i < n; i++){ father[i] = i; vis[i] = false; } } int main(){ scanf("%d%d%d",&n,&m,&k);// 城市数量,铁路数量,要检查城市数量 int u,v; int i,j; for(i = 0; i < m; i++){ scanf("%d%d",&u,&v); G[u].push_back(v); G[v].push_back(u); } int qurey; for(int query = 0; query < k; query++){ scanf("%d",¤t); init(); for(i = 1; i <= n; i++){ for(j = 0; j < G[i].size(); j++){ int u = i; v = G[i][j]; if(u == current || v == current) continue; Union(u,v); } } int block = 0; for(i = 1; i <= n; i++){ if(i == current) continue; int fa_i = findFather(i); if(vis[fa_i] == false){ block++; vis[fa_i] = true; } } printf("%d\n",block - 1); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)