1013 Battle Over Cities (25 分)(图的遍历or并查集)

这题用并查集或者dfs都可以做
dfs
#include<bits/stdc++.h> using namespace std;
const int N=1e3+;
bool mp[N][N];
int n,m,k;
bool vis[N];
void dfs(int v)
{
vis[v]=true;
for(int i=;i<=n;i++){
if(mp[v][i]&&!vis[i]){
dfs(i);
}
}
}
int main()
{
fill(mp[],mp[]+N*N,false);
scanf("%d %d %d",&n,&m,&k);
for(int i=;i<m;i++){
int a,b;
scanf("%d %d",&a,&b);
mp[a][b]=mp[b][a]=true;
}
while(k--){
fill(vis,vis+N,false);
int x;
scanf("%d",&x);
vis[x]=true;
int sum=;
for(int i=;i<=n;i++){ if(!vis[i]){
sum++;
dfs(i);
}
}
printf("%d\n",sum-);
}
return ;
}
并查集
#include<bits/stdc++.h> using namespace std; vector<pair<int,int> > edge;
int f[];
int n,m;
int findth(int x)
{
if(x==f[x]) return x;
return f[x]=findth(f[x]);
}
void join(int x,int y)
{
int fx,fy;
fx = findth(x);
fy = findth(y);
if (fx != fy)
f[fx] = fy;
}
void solve(int p)
{
for (int i = ; i <= n ; i++) f[i] = i;
for (int i = ; i < edge.size() ; i++){
if (edge[i].first == p || edge[i].second == p) continue;
join(edge[i].first,edge[i].second);
}
int cnt = ;
for (int i = ; i <= n ; i++){
if (i == p)
continue;
if(f[i]==i) cnt++;
}
printf("%d\n",cnt-);
}
int main()
{
int k;
scanf("%d %d %d",&n,&m,&k);
edge.resize(m);
for (int i = ; i < m ; i++)
{
int x,y;
scanf("%d %d",&x,&y);
edge[i] = make_pair(x,y);
}
for (int i = ; i <= k ; i++)
{
int q;
scanf("%d",&q);
solve(q);
}
return ;
}
1013 Battle Over Cities (25 分)(图的遍历or并查集)的更多相关文章
- 1013 Battle Over Cities (25分) 图的连通分量+DFS
题目 It is vitally important to have all the cities connected by highways in a war. If a city is occup ...
- 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 ...
- 1013 Battle Over Cities (25 分)
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
- 【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)
题意: 输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6).有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公 ...
- 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 Advanced 1013 Battle Over Cities (25) [图的遍历,统计连通分量的个数,DFS,BFS,并查集]
题目 It is vitally important to have all the cities connected by highways in a war. If a city is occup ...
- 1013. Battle Over Cities (25)
题目如下: It is vitally important to have all the cities connected by highways in a war. If a city is oc ...
- 1013 Battle Over Cities (25)(25 point(s))
problem It is vitally important to have all the cities connected by highways in a war. If a city is ...
- PTA 朋友圈 (25 分) 代码详解 (并查集)
1.题目要求: 某学校有N个学生,形成M个俱乐部.每个俱乐部里的学生有着一定相似的兴趣爱好,形成一个朋友圈.一个学生可以同时属于若干个不同的俱乐部.根据"我的朋友的朋友也是我的朋友" ...
随机推荐
- centos7下javac:未找到命令的问题
在linux下编译java程序,执行javac编译生成class文件时,在centos7终端输入如,javac hello.java 会提示未找到指令,但用java -verison测试环境变量 ...
- C#实现打印
C#实现导出pdf文件,打印 using System; using System.Collections.Generic; using System.Linq; using System.Web; ...
- 自动计算UITableViewCell高度2(CGRect约束)
1.先创建model .h #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface LBDNe ...
- Shiro登录中遇到了问题
Shiro登录中遇到了问题 记录二次开发中遇到的问题, 如果系统学习Shiro, 推荐跟我学Shrio. 问题 项目是要将验证从本地改为LDAP验证, 但是因为jeecms的验证和授权中, 用户和角色 ...
- mysql 导出数据字典
使用Navicat工具 查询: SELECT TABLE_SCHEMA AS '数据库', TABLE_NAME AS '表名', COLUMN_NAME AS '字段名', COLUMN_TYPE ...
- tp3.2报错;syntax error, unexpected 'function' (T_FUNCTION), expecting identifier (T_STRING) or \\ (T_NS_SEPARATOR)
出错原因:这个是php版本问题,laravel5.1的php版本要求是PHP >= 5.5.9,切换一下PHP版本就行.
- html5 获取和设置data-*属性值的四种方法讲解
1.获取id的对象 2.需要获取的就是data-id 和 dtat-vice-id的值 一:getAttribute()方法 const getId = document.getElementById ...
- CentOS下安装pip
CentOS下安装pip 通常情况下使用命令: yum -y install pip 也有可能报错,无法安装.这是应该使用第二种方法. 1.首先需要先安装扩展源EPEL: yum -y install ...
- STM32进阶之串口环形缓冲区实现(转载)
转载自微信公众号“玩转单片机”,感谢原作者“杰杰”. 队列的概念 在此之前,我们来回顾一下队列的基本概念:队列 (Queue):是一种先进先出(First In First Out ,简称 FIFO) ...
- MyEclipse 上使用sping+hibernate+mysql
以下为入门级别代码,高手请务见笑~ 我的MyEclipse 上spring最高版是3.* 而hibernate 最高版本是4.* 在做项目时用的都是最高版,于是代码写好之后调试报了个异常,居然 ...