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> ...
随机推荐
- GPS部标平台的架构设计(十)-基于Asp.NET MVC构建GPS部标平台
在当前很多的GPS平台当中,有很多是基于asp.NET+siverlight开发的遗留项目,代码混乱而又难以维护,各种耦合和关联,要命的是界面也没见到比Javascript做的控件有多好看,随着需求的 ...
- Spark&Hadoop:scala编写spark任务jar包,运行无法识别main函数,怎么办?
昨晚和同事一起看一个scala写的程序,程序都写完了,且在idea上debug运行是ok的.但我们不能调试的方式部署在客户机器上,于是打包吧.打包时,我们是采用把外部引入的五个包(spark-asse ...
- IIS无法加载字体文件(*.woff,*.svg)的解决办法
在编写前端代码的过程中经常会遇到使用特定的字体(*.woff,*.svg),此时在加载字体时请求会被返回 Failed to load resource: the server responded w ...
- SD卡读写一些函数
/SPI2 读写一个字节 //TxData:要写入的字节 //返回值:读取到的字节 u8 SPI2_ReadWriteByte(u8 TxData) { u16 retry=0; while((S ...
- Leetcode: Unique Substrings in Wraparound String
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- 一张图轻松搞懂javascript event对象的clientX,offsetX,screenX,pageX区别
总是会被javascript的event对象的clientX,offsetX,screenX,pageX 弄得头晕,于是决定做个图来区分一下(画得我手那个酸呀....) 先总结下区别: event.c ...
- Excel 导入 导出 Microsoft
导出: private void exportExcel() { if (saveFileDialog1.ShowDialog() == DialogResult.OK) { Application. ...
- Could not load type 'System.ServiceModel.Activation.HttpModule' from assembly 'System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
Could not load type 'System.ServiceModel.Activation.HttpModule' from assembly 'System.ServiceModel, ...
- Linux:-防火墙iptables如何个性化定制?
身份标签/usr/local/etc/identity,主脚本iptables.sh,附属目录functions/iptables.d ├── iptables.sh ├── functions│ ...
- django关系对象映射(Object Relational Mapping,简称ORM)
Model 创建数据库,设计表结构和字段 django中遵循 Code Frist 的原则,即:根据代码中定义的类来自动生成数据库表 from django.db import models clas ...