PAT 甲级 1013 Battle Over Cities (25 分)
直接跳跃到了1013 因为前面的没有啥问题就过了题目分析这是一道用来图的题目,初步就是使用BFS查找在删除了一个节点后剩下的 整块数num需要连接线的最少值就是 num-1;Code#include <stdio.h>#include <iostream>#include <map>#include <vector>#include <algo
·
直接跳跃到了1013 因为前面的没有啥问题就过了
题目
分析
这是一道用来图的题目,初步就是使用BFS查找在删除了一个节点后剩下的 整块数num
需要连接线的最少值就是 num-1;
Code
#include <stdio.h>
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int connect[1010][1010] = {0};
int n,m,c;
int check(int checknum){
int checkarr[1010] = {0}, res=0;
for (int i = 1; i <= n; i++)
{
if(i==checknum||checkarr[i]==1) continue;
// 判断联通
queue<int> q;
for (int j = 1; j <= n; j++){
if(connect[i][j]==1&&j!=checknum){
q.push(j);
}
}
while (!q.empty()){
int x = q.front();
q.pop();
if(x==checknum||checkarr[x]==1) continue;
checkarr[x] = 1;
for (int h = 1; h <= n; h++)
{
if(connect[x][h]==1){
q.push(h);
}
}
}
res++;
}
return res;
}
int main(){
cin>>n>>m>>c;
map<int,vector<int> > mp;
for (int i = 0; i < m; i++)
{
int from,to;
cin>>from>>to;
connect[from][to] = 1;
connect[to][from] =1;
}
for (int i = 0; i < c; i++)
{
int cc;
cin>>cc;
cout<<check(cc)-1<<endl;
}
}
结果

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)