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 ...
随机推荐
- python高级用法之命名元组namedtuple
1.tuple类型数据的获取 大家都知道,元组里面的数据获取只能通过下标的方式去获取,比如 :a = ('username', 'age', 'phone'),要获取username的话 ,就需要用a ...
- Pandas数据合并
目录 1) 在单个键上进行合并操作 2) 在多个键上进行合并操作 使用how参数合并 1) left join 2) right join 3) outer join(并集) 4) inner joi ...
- NC18985 数字权重
题目链接 题目 题目描述 小a有一个n位的数字,但是它忘了各个位上的数是什么,现在请你来确定各个位上的数字,满足以下条件: 设第i位的数为ai,其中a1为最高位,an为最低位,K为给定的数字 不含前导 ...
- 基于 junit5 实现 junitperf 源码分析
前言 上一节介绍了基于 junit4 实现 junitperf,但是可以发现定义变量的方式依然不够优雅. 那可以让用户使用起来更加自然一些吗? 有的,junit5 为我们带来了更加强大的功能. 拓展阅 ...
- docker启动nginx https自签名证书配置
启动测试应用时, 有时需要自己配置证书签名: 1 使用系统自带openssl openssl req \ > -x509 \ > -nodes \ > -days 365 \ > ...
- golang中协程&管道&锁
进程和线程 进程(Process)就是程序在操作系统中的一次执行过程,是系统进行资源分配和调度的基 本单位,进程是一个动态概念,是程序在执行过程中分配和管理资源的基本单位,每一个进 程都有一个自己的地 ...
- 彻底搞懂Java中的Runnable和Thread
写在前面 今天在阅读ThreadPoolExecutor源码的时候觉得有些地方理解起来似是而非,很别扭!最后才猛然发现,原来是我自己的问题:没有真正理解Runnable和Thread的含义! 我之前对 ...
- HttpURLConnection使用分析
在项目中遇到各种版本的httpClient,所以想了解一下httpClient的实现 首先查看的是JDK1.1中自带的HttpURLConnection,看一下最初的设计是怎么样的 代码分析 使用Ht ...
- 单表查询,多表查询,子查询---day37
1.单表查询 # ### part1 单表查询 # sql 查询语句的完整语法 '''select..from..where..group by..having..order by..limit..' ...
- error LNK2019: unresolved external symbol _CrtDbgReport referenced in function xxxx
现象 在用C++调试dll动态库的时候汇报上述错误,我用VS2015编译的DEBUG版本动态库,实际在VS2019中调试 解决方法 在工程属性中做出如下选择,代码生成->多线程调试DLL ,出现 ...