好久都没有做题了,从长沙回来之后一直就是看看QT,感觉自己真的要蠢死了><不开心不开心

题目大概意思就是从一个图里面去掉一个点,看看剩下多少个孤立点。

自己想了好大一会儿没有思路,看到网上一个代码,真是惊叹好神奇...><

用遍历的方式,如DFS,将去掉的点设为1,然后遍历一次看看剩下多少个没有被遍历到的点。

#include <iostream>
#include <cstring>
using namespace std; #define MAX_VERTEX_NUM 1005 int visit[MAX_VERTEX_NUM]; typedef struct
{
int vexs[MAX_VERTEX_NUM];
bool edges[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
int vexnum,edgenum;
}MGraph; MGraph g; void dfs(int from){
int i;
for(i=;i<=g.vexnum;i++){
if(i!=from&&visit[i]==&&g.edges[from][i]==){
visit[i]=;
dfs(i);
}
}
} int main()
{ int n,k,m,city,count;
cin>>n>>m>>k;
g.vexnum=n;
g.edgenum=m;
memset(g.edges,,sizeof(g.edges));
int c1,c2;
for(int i=;i<m;i++){
cin>>c1>>c2;
g.edges[c1][c2]=g.edges[c2][c1]=true;
} for(int i=;i<k;i++){
count=;
memset(visit,,sizeof(visit));
cin>>city;
visit[city]=;
for(int j=;j<=n;j++){
if(visit[j]==){
count++;
dfs(j);
}
}
count=count-;
cout<<count<<endl;
} return ;
}

1013. Battle Over Cities的更多相关文章

  1. PAT 解题报告 1013. Battle Over Cities (25)

    1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...

  2. PAT 1013 Battle Over Cities

    1013 Battle Over Cities (25 分)   It is vitally important to have all the cities connected by highway ...

  3. PAT甲级1013. Battle Over Cities

    PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...

  4. PAT 1013 Battle Over Cities(并查集)

    1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...

  5. pat 1013 Battle Over Cities(25 分) (并查集)

    1013 Battle Over Cities(25 分) It is vitally important to have all the cities connected by highways i ...

  6. 图论 - PAT甲级 1013 Battle Over Cities C++

    PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...

  7. PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)

    1013 Battle Over Cities (25 分)   It is vitally important to have all the cities connected by highway ...

  8. 1013 Battle Over Cities (25分) DFS | 并查集

    1013 Battle Over Cities (25分)   It is vitally important to have all the cities connected by highways ...

  9. PTA (Advanced Level) 1013 Battle Over Cities

    Battle Over Cities It is vitally important to have all the cities connected by highways in a war. If ...

  10. PAT A 1013. Battle Over Cities (25)【并查集】

    https://www.patest.cn/contests/pat-a-practise/1013 思路:并查集合并 #include<set> #include<map> ...

随机推荐

  1. AVD之PANIC: Could not open

    1 原因一:因为我们采用的是绝对路径定位,也就是说在环境变量里面把路径写死了,所以安装都不同,路径读不出来. 解决办法:①在环境变量中创建变量名:ANDROID_SDK_HOME,变量值:你当时安装S ...

  2. kali 下文件操作

    记得看到一片文章中说要学习linux 不要用kali.. 不感兴趣的东西,还指望我去搞个Ubuntu.... Ctrl+I 清屏 CD命令: cd 进入用户主目录: cd ~ 进入用户主目录: cd ...

  3. 使用IIS发布WCF服务

    上一篇是Windows服务为宿主的WCF服务,现在用IIS为宿主发布WCF服务. 第一步:肯定是新建一个WCF服务啦[是WCF服务应用程序],然后在解决方案上再次添加一个新项目[我们选择WCF服务库, ...

  4. DevExpress 为TextEdit设置水印文字

    设置水印代码: //设置水印值public static void SetWatermark(this TextEdit textEdit, string watermark) { textEdit. ...

  5. YUM源

    由于自己想做一个简单的博客玩玩,需要去搭建apache,mysql和php,如果只是用rpm安装包的话,安装的速度太慢不说,最主要的是包之间的关联太让人蛋疼了,所以最好还是是用yum来安装吧,当然这只 ...

  6. Extjs 一些配置以及方法

    1.例如想要实现以下功能,本来model中只有用户的firstname和lastname,但是在grid中展示还需要展示用户姓名,或者只展示用户姓名

  7. Ansible 学习笔记

    最近因为需要管理很多台机器,而这些机器又需要频繁重新安装,实在受不了Puppet需要在每个客户机上都安装一遍,于是转头开始学些Ansible.根据这段时间的使用,这个确实是神器,唯一的感觉就是相见恨晚 ...

  8. var ball0=new Ball("executing") 是怎样被执行的?

    function Ball(message){ alert(message); }; var ball0=new Ball("executing"); //var ball0=ne ...

  9. Sublime Text 3 高效编码快捷键

    Sublime Text 3 高效编码快捷键   1.快速跳到第20行 Ctrl+p 框中输入 “  :20 ”   2.在文件夹中查看文件 Ctrl+p 框中输入 “ index.html”  更快 ...

  10. java中常见的几种异常

    算术异常类:ArithmeticExecption空指针异常类:NullPointerException类型强制转换异常:ClassCastException数组负下标异常:NegativeArray ...