1013. Battle Over Cities
好久都没有做题了,从长沙回来之后一直就是看看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的更多相关文章
- PAT 解题报告 1013. Battle Over Cities (25)
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
- PAT 1013 Battle Over Cities
1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highway ...
- PAT甲级1013. Battle Over Cities
PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...
- PAT 1013 Battle Over Cities(并查集)
1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...
- pat 1013 Battle Over Cities(25 分) (并查集)
1013 Battle Over Cities(25 分) It is vitally important to have all the cities connected by highways i ...
- 图论 - PAT甲级 1013 Battle Over Cities C++
PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...
- 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 ...
- 1013 Battle Over Cities (25分) DFS | 并查集
1013 Battle Over Cities (25分) It is vitally important to have all the cities connected by highways ...
- 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 ...
- PAT A 1013. Battle Over Cities (25)【并查集】
https://www.patest.cn/contests/pat-a-practise/1013 思路:并查集合并 #include<set> #include<map> ...
随机推荐
- Leetcode: Serialize and Deserialize BST
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- python3爬取网页
爬虫 python3爬取网页资源方式(1.最简单: import'http://www.baidu.com/'print2.通过request import'http://www.baidu.com' ...
- C++之路进阶——HDU1880(魔咒词典)
---恢复内容开始--- New~ 欢迎参加2016多校联合训练的同学们~ 魔咒词典 Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 3 ...
- Overview of the Oppia codebase(Oppia代码库总览)
Oppia is built with Google App Engine. Its backend is written in Python, and its frontend is written ...
- svm机器学习算法中文视频讲解
这个是李政軒Cheng-Hsuan Li的关于机器学习一些算法的中文视频教程:http://www.powercam.cc/chli. 一.KernelMethod(A Chinese Tutoria ...
- div水平居中和垂直居中
水平居中和垂直居中 水平居中包含两种情况: 块级元素的水平居中:margin:0px auto; 文字内容的水平居中:text-align: center; ...
- poj 2515 差分序列,排列组合
大神博客链接 http://blog.csdn.net/kksleric/article/details/8021276 这道题的差分序列从没看过,公式题. 先构造从0到m的第p阶差分序列,算出0^p ...
- .NET 泛型分析
.NET 泛型解析 一.问题背景 我们在编程的时候往往因为需要处理不同类型的数据或者对象,重复编写很多类似的代码,造成代码的冗余,代码也显得不那么优雅,泛型的出现,正好是为了解决这个问题,实现继承. ...
- 8139too.c网卡驱动简单分析
从事linux C开发工作以来,工作内容主要是在应用层,对nginx和unbound等软件有些了解,也常对这2个软件进行二次开发. 对网络这块一直比较有兴趣.也很好奇网卡到底是怎么接受到报文的,以及报 ...
- 20160113第一个ANDRIOD开发日志
今天开发了第一个andriod程序,测试录音和播放功能.源码是网上抄来的. 代码: unit Unit2; interface uses System.SysUtils, System.Types ...