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 ...
随机推荐
- IntPtr 来把指针转换为 Int
由于想得到指针的值,这个时候,不能把指针强制转换为 integer 因为 integer 只适合32位的系统,64位的系统下,需要用 int64, 通过这个函数来转换,就可以屏蔽掉系统是32位 还是 ...
- 时间减去一个小时怎么做 delphi 很方便 看代码
procedure TfrmTongBuTid.Button1Click(Sender: TObject); begin TopStartModifiedTime_dtp.Time := TopSta ...
- windows-网页流视频下载
一.m3u8 视频在线提取工具 https://blog.luckly-mjw.cn/tool-show/m3u8-downloader/index.html
- Pandas—to_csv()写入函数参数详解
1. to_csv函数的参数 DataFrame.to_csv(path_or_buf=None, sep=',', na_rep='', float_format=None, columns=Non ...
- 华为云GaussDB支撑农行超级网银业务,性能和稳定性备受认可
数据库作为数据驱动业务创新和智慧银行建设的关键基础设施,在银行数字化变革中具有举足轻重的作用. 在金融科技发展和国家政策引领下,银行纷纷加快推进数字化转型时代,正在经历一场以科技引领.数据赋能.数字经 ...
- java 如何计算两个汉字的相似度?如何获得一个汉字的相似汉字?
计算汉字相似度 情景 有时候我们希望计算两个汉字的相似度,比如文本的 OCR 等场景.用于识别纠正. 实现 引入 maven <dependency> <groupId>com ...
- vivo 短视频体验与成本优化实践
作者:来自 vivo 互联网短视频研发团队 本文根据蔡创业.马运杰老师在"2023 vivo开发者大会"现场演讲内容整理而成. 在线点播场景,播放体验提升与成本优化是同等重要的两件 ...
- Mysql一张表可以存储多少数据
Mysql一张表可以存储多少数据 在操作系统中,我们知道为了跟磁盘交互,内存也是分页的,一页大小4KB.同样的在MySQL中为了提高吞吐率,数据也是分页的,不过MySQL的数据页大小是16KB.(确切 ...
- [BUUCTF][WEB][极客大挑战 2019]Upload 1
打开靶机url,看到一个页面可以上传文件 上传一个图片试一下,发现上传的路径是 http://a7661b03-4852-41de-9ea4-d48c47cb50f0.node4.buuoj.cn:8 ...
- 麒麟系统开发笔记(五):制作安装麒麟系统的启动U盘、物理机安装麒麟系统以及搭建Qt开发环境
前言 电脑从U盘装麒麟系统,搭建实机Qt开发运行环境. 制作麒麟系统U盘(使用LiveUSB) 步骤一:先准备个至少8GB的U盘 之前购买的一批联想U盘,如下图: 查看U盘: 步 ...