1. 本题就是dfs.连通图个数-2;
  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】的更多相关文章

  1. PAT甲级1013. Battle Over Cities

    PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...

  2. 图论 - PAT甲级 1013 Battle Over Cities C++

    PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...

  3. 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 ...

  4. PAT A 1013. Battle Over Cities (25)【并查集】

    https://www.patest.cn/contests/pat-a-practise/1013 思路:并查集合并 #include<set> #include<map> ...

  5. 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 ...

  6. 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 ...

  7. PAT 解题报告 1013. Battle Over Cities (25)

    1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...

  8. PAT 1013 Battle Over Cities

    1013 Battle Over Cities (25 分)   It is vitally important to have all the cities connected by highway ...

  9. PAT 1013 Battle Over Cities(并查集)

    1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...

  10. pat 1013 Battle Over Cities(25 分) (并查集)

    1013 Battle Over Cities(25 分) It is vitally important to have all the cities connected by highways i ...

随机推荐

  1. 小知识:RMAN备份当前控制文件报错ORA-245

    在一个备份的case上遇到备份控制文件报错ORA-245,最终通过修改snapshot controlfile默认位置到ASM磁盘组后解决. 1.问题复现 回来后就想快速记录下这个小知识点,打开尘封的 ...

  2. Windows、MacOs上 gif 录像软件 LICEcap

    LICEcap 官网:https://www.cockos.com/licecap/ 适用于 windows macos 我自己保持的一版,下载地址:https://files-cdn.cnblogs ...

  3. LTspice XVII使用笔记

    安装 前往官网下载 https://www.analog.com/cn/design-center/design-tools-and-calculators/ltspice-simulator.htm ...

  4. 【OpenGL ES】绘制立方体

    1 前言 ​ 本文主要介绍使用 OpenGL ES 绘制立方体,读者如果对 OpenGL ES 不太熟悉,请回顾以下内容: 绘制三角形 绘制彩色三角形 绘制正方形 绘制圆形 ​ 在绘制立方体的过程中, ...

  5. string 字符串转为 char *

    std::string str = "abc"; const char *p = str.data(); 更多: https://www.cnblogs.com/devilmayc ...

  6. golang微服务实践:分布式链路追踪系统-jaeger安装与简单使用

    简介 jaeger是一个比较有名的分布式链路追踪系统,底层用golang实现,兼容opentracing标准. 文档地址:docs github地址:github 官网:website blog:bl ...

  7. 【Android 逆向】【ARM汇编】 堆栈

    arm 四种栈 1 空栈 栈指针指向空位,每次存入时可以直接存入然后栈指针移动一格:而取出时需要先移动一格才能取出 2 满栈 栈指针指向栈中最后一格数据.每次存入时需要先移动栈指针一格再存入.取出时可 ...

  8. Python输出日志信息

    在Python中要输出日志信息有2种方式: 1.调用内置的print()方法,该方式只能将信息输出到控制台 2.使用logging模块将日志信息输出到文件中(logging模块默认也是输出到控制台:标 ...

  9. auth模块的一些方法

    auth模块 auth模块是cookie和session的升级版,auth模块是对登录认证方法的一种封装,之前我们获取用户输入的用户名及密码后需要自己从user表里查询有没有用户名和密码符合的对象,而 ...

  10. java图书管理系统界面版本+mysql数据库

    描述: java+mysql数据库实现的图书管理系统,实现了图书添加,图书删除,图书修改,图书查看,图书列表查看等,用户增加,用户删除.用户修改,用户查看等 功能: 图书管理功能:图书添加,图书删除, ...