好久都没有做题了,从长沙回来之后一直就是看看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. XPath 语法

    XPath 使用路径表达式来选取 XML 文档中的节点或节点集.节点是通过沿着路径 (path) 或者步 (steps) 来选取的. XML 实例文档 我们将在下面的例子中使用这个 XML 文档. & ...

  2. poj 2376 Cleaning Shifts

    http://poj.org/problem?id=2376 Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  3. Centos 7 mysql Buffered warning: Changed limits: max_connections: 214 解决方法

    Everytime I restart MySQL I have this warning: [Warning] Buffered warning: Changed limits: max_conne ...

  4. vi编辑器的使用

    在命令模式下进入编辑模式,输入字母"a","A","i","I","o","O" ...

  5. 编写ros串口节点,使用官方serial包

    参考http://www.roswiki.com/read.php?tid=557&fid=39 1.通过sudo apt-get install ros-<distro>-ser ...

  6. html5,视频的兼容

    <video controls="controls" width="500" >        <source src="1.mp4 ...

  7. Mount DVD on CentOS

    Mount DVD on CentOS need to mount CD/DVD on CentOS Temporarily or Permanently? Here’s the Process Us ...

  8. AsyncTask 异步处理

    1 package com.ch.day8_asynctaskdemo; 2 3 import java.util.ArrayList; 4 5 import com.ch.myutils.NetWo ...

  9. OC-01 编译链接的作用

    编译:检测代码的语法合法性,随后生成.o文件. 链接:把项目中所有的.out合并,生成一个可执行文件. OC编译连接过程 .m---->.o---->.out  . 检测源文件的语法合法性 ...

  10. Tomcat重启脚本restart.sh停止脚本stop.sh

    Tomcat重启脚本restart.sh停止脚本stop.sh Tomcat本身提供了 startup.sh(启动)shutdown.sh(关闭)脚本,我们在部署中经常会出现死进程形象,无法杀掉进程需 ...