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. spring声明式事务(@Transactional)开发常犯的几个错误及解决办法

    spring声明式事务(@Transactional)开发常犯的几个错误及解决办法 目前JAVA的微服务项目基本都是SSM结构(即:springCloud +springMVC+Mybatis),而其 ...

  2. RPA AI .NET Core 与未来--学习笔记

    摘要 当下 RPA 平台主要采用 .NET Framework 框架,也限制了 RPA 只用于 Windows 平台..NET Core 的开放与跨平台特性赋予 RPA 更大发展空间. RPA是什么 ...

  3. CentOS7 卸载/home 扩大/root空间

    卸载home 备份home分区文件 # 新系统无视 tar cvf /tmp/home.tar /home 修改fstab(这一步非常重要,千万别漏了) 准备卸载/home文件系统,centos启动时 ...

  4. JS 前序遍历、中序遍历、后序遍历、层序遍历详解,深度优先与广度优先区别,附leetcode例题题解答案

    壹 ❀ 引 按照一天一题的速度,不知不觉已经刷了快两多月的leetcode了,因为本人较为笨拙,一道简单的题有时候也会研究很久,看着提交了两百多次,其实也才解决了70来道简单题,对于二分法,双指针等也 ...

  5. Javascript操作对象数组实现增删改查

    1.介绍 最近帮朋友弄一个简单的针对json数组的增删改成页面,正好涉及到了js去操作对象数组实现增删改查功能.我估计很多朋友应该也会遇到这类操作,所以记录一下以便分享. 2.数据准备 这里我就以学生 ...

  6. 使用SYS_CONTEXT

    使用SYS_CONTEXT 1.什么是SYS_CONTEXT? SYS_CONTEXT 函数是Oracle提供的一个获取环境上下文信息的预定义函数. 该函数用来返回一个指定namespace下的par ...

  7. Maven如何打包可执行jar包

    假设我有一个maven项目叫:hello-world 新建一个HelloWorld类: package com.dylan.mvnbook.helloworld; public class Hello ...

  8. win32 - 写入安全日志(AuthzRegisterSecurityEventSource和AuthzReportSecurityEvent)

    微软文档介绍说, 安全日志在其他两个重要方面与其他日志不同.首先,在默认配置中,它受到强大的访问控制列表(ACL)和特权检查的保护,这将可以读取其内容的个人的范围限制为本地系统,管理员和安全特权的持有 ...

  9. [BUUCTF][Web][HCTF 2018]WarmUp 1

    这题已经标识为php 代码审计题,那么需要搞到源码才行 打开靶机对应的url,展示的是一张笑脸图片 右键查看网页源代码 <!DOCTYPE html> <html lang=&quo ...

  10. RN运行ios报错No matching function for call to 'RCTBridgeModuleNameForClass'

    xcode更新12.5后,ios运行报错No matching function for call to 'RCTBridgeModuleNameForClass' 解决方法: 在ios/Podfil ...