PAT 甲级【1013 Battle Over Cities】
- 本题就是dfs.连通图个数-2;
- 但是java慢,最后一个case 超时
import java.io.*;
import java.util.HashSet;
import java.util.Set; public class Main {
@SuppressWarnings("uncheck")
public static void main(String[] args) throws IOException {
StreamTokenizer st = new StreamTokenizer(new InputStreamReader(System.in));
int n, m, k;
st.nextToken();
n = (int) st.nval;
st.nextToken();
m = (int) st.nval;
st.nextToken();
k = (int) st.nval;
Set<Integer>[] g = new HashSet[n + 1];
for (int i = 0; i <= n; i++) {
g[i] = new HashSet<>();
}
for (int i = 0; i < m; i++) {
st.nextToken();
int city1 = (int) st.nval;
st.nextToken();
int city2 = (int) st.nval;
g[city1].add(city2);
g[city2].add(city1);
}
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
for (int i = 0; i < k; i++) {
st.nextToken();
int city = (int) st.nval;
Set<Integer> connected = new HashSet<>(g[city]);
visited = new boolean[n + 1];
g[city] = new HashSet<>();
for (int connectedcity : connected) {
g[connectedcity].remove(city);
} int cnt = travel(g, n); out.println(cnt - 2); for (int connectedcity : connected) {
g[connectedcity].add(city);
}
g[city] = connected;
}
out.flush();
} static boolean[] visited; public static int travel(Set<Integer>[] g, int n) {
int count = 0;
for (int i = 1; i <= n; i++) {
if (!visited[i]) {
travel(i, g);
count++;
}
}
return count;
} public static void travel(int start, Set<Integer>[] g) {
visited[start] = true;
for (int city : g[start]) {
if (!visited[city]) {
travel(city, g);
}
}
}
}
PAT 甲级【1013 Battle Over Cities】的更多相关文章
- PAT甲级1013. Battle Over Cities
PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...
- 图论 - 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 ...
- PAT A 1013. Battle Over Cities (25)【并查集】
https://www.patest.cn/contests/pat-a-practise/1013 思路:并查集合并 #include<set> #include<map> ...
- PAT甲级——A1013 Battle Over Cities
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
- 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 ...
- 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(并查集)
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 ...
随机推荐
- go中的 位预算,反码、补码、原码
https://baike.baidu.com/item/%E4%BD%8D%E8%BF%90%E7%AE%97/6888804 首先关于"位运算",看下百度百科就行了. 总结:在 ...
- .NET 云原生架构师训练营(模块二 基础巩固 依赖注入)--学习笔记
2.2.1 核心模块--依赖注入 什么是依赖注入 .NET Core DI 生命周期 服务设计 服务范围检查 ASP.NET Core 依赖注入:https://docs.microsoft.com/ ...
- NC23413 小A买彩票
题目链接 题目 题目描述 小A最近开始沉迷买彩票,并且希望能够通过买彩票发家致富.已知购买一张彩票需要3元,而彩票中奖的金额分别为1,2,3,4元,并且比较独特的是这个彩票中奖的各种金额都是等可能的. ...
- eslint+prettier 统一代码风格
1.实现效果 Eslint校验代码语法,prettier统一格式化代码,按下保存自动修复eslint错误,自动格式化代码. 2.安装vscode插件 Vetur ESLint Prettier - C ...
- OsgEarth开发笔记(二):Osg3.6.3+OsgEarth3.1+vs2019x64开发环境搭建(中)
上一篇:<OsgEarth开发笔记(一):Osg3.6.3+OsgEarth3.1+vs2019x64开发环境搭建(上)>下一篇:敬请期待- 前言 上一篇编译了osg和osgCurl ...
- pycharm中自定义函数补全
在 PyCharm 中,你可以通过以下步骤实现这一目标: 打开 PyCharm,点击顶部菜单的 "File"(文件) -> "Settings"(设置). ...
- linux下virtualenvwrapper安装
安装 pip3 install virtualenvwrapper -i https://pypi.douban.com/simple 设置linux的用户配置文件配置virtualenvwrappe ...
- java+mysql实现的公益管理系统
一功能 1.管理员的登录 2.公益项目的增删改查 3.负责人的增删改查 4.捐款人的增删改查 5.志愿者增删改查 二界面展示 1.欢迎界面 2.登录界面 3.系统首页 4.项目管理 5.负责人管理 6 ...
- yolov5项目cuda错误解决
CUDA报错解决 # 报错详情 AssertionError: CUDA unavailable, invalid device 0 requested 查看cuda版本 先看一下电脑是否支持GPU, ...
- MIG是如何向DDR中写入数据的
1.1 先来看看信号线的描述 我们以X16的器件为例,下面的截图来自 镁光的官方手册 https://media-www.micron.com/-/media/client/global/doc ...