leedcode 928. 尽量减少恶意软件的传播 II (并查集)
测试链接:https://leetcode.cn/problems/minimize-malware-spread-ii/
思路
设置sz,inflect,virus[](该节点是否属于感染节点),father,cntans[](统计删除该感染节点可以拯救多少节点)
这道题目先使用并查集解决,如果相邻的节点都为正常节点则将其合并,等所有节点合并后考虑感染相邻节点,如果感染节点相邻节点还没有找出过源头,则此时,改感染节点更新为源头,如果这个相邻集合已经有源头了,则说明这个集合存在多个源头,只通过删除一个感染节点无法拯救,所以无需将inflect[i]设置-2(无效状态),我们初始化各自的集合为-1,如果有源头,则设置inflect[i]为感染节点
处理完各自集合的源头节点后开始统计各个感染节点删除后能拯救的节点数,统计完之后,将感染节点升序排序,更新答案,最后输出答案
题解
const int N=500;
class Solution {
public:
int father[N];
int sz[N];
bool virus[N];
int inflect[N];
int n;
int cntans[N];
void build()
{
for(int i=0;i<n;i++)
{
father[i]=i;
sz[i]=1;
inflect[i]=-1;
virus[i]=false;
}
}
int find(int x)
{
return x==father[x]?x:father[x]=find(father[x]);
}
void join(int x,int y)
{
int fx = find(x);
int fy = find(y);
if(fx!=fy)
{
father[fx]=fy;
sz[fy]+=sz[fx];
}
}
int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
n = graph.size();
build();
for(int i=0;i<initial.size();i++)
{
virus[initial[i]]=true;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(!virus[i]&&!virus[j]&&graph[i][j]==1)
{
join(i,j);
}
}
}
for(int i=0;i<initial.size();i++)
{
for(int j=0;j<n;j++)
{
if(initial[i]!=j&&!virus[j]&&graph[initial[i]][j])
{
int fn = find(j);
if(inflect[fn]==-1)
{
inflect[fn]=initial[i];
}
else if(inflect[fn]!=-2&&inflect[fn]!=initial[i])
{
inflect[fn]=-2;
}
}
}
}
for(int i=0;i<n;i++)
{
if(i==father[i]&&inflect[i]>=0)
{
cntans[inflect[i]]+=sz[i];
}
}
sort(initial.begin(),initial.end());
int maxans=cntans[initial[0]];
int ans=initial[0];
for(int i=0;i<initial.size();i++)
{
if(maxans<cntans[initial[i]])
{
maxans = cntans[initial[i]];
ans = initial[i];
}
}
return ans;
}
};
leedcode 928. 尽量减少恶意软件的传播 II (并查集)的更多相关文章
- [Swift]LeetCode928. 尽量减少恶意软件的传播 II | Minimize Malware Spread II
(This problem is the same as Minimize Malware Spread, with the differences bolded.) In a network of ...
- [Swift]LeetCode924.尽量减少恶意软件的传播 | Minimize Malware Spread
In a network of nodes, each node i is directly connected to another node j if and only if graph[i][j ...
- [LeetCode] 924. Minimize Malware Spread 最大程度上减少恶意软件的传播
In a network of nodes, each node i is directly connected to another node j if and only if graph[i][j ...
- Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环
D. Dividing Kingdom II Long time ago, there was a great kingdom and it was being ruled by The Grea ...
- [LeetCode] 928. Minimize Malware Spread II 最大程度上减少恶意软件的传播之二
(This problem is the same as Minimize Malware Spread, with the differences bolded.) In a network of ...
- hihocoder Counting Islands II(并查集)
Counting Islands II 描述 Country H is going to carry out a huge artificial islands project. The projec ...
- python 变量作用域 v.__sizeof__() python 深复制 一切皆对象 尽量减少内存消耗
python 深入理解 赋值.引用.拷贝.作用域 - 江召伟 - 博客园 https://www.cnblogs.com/jiangzhaowei/p/5740913.html a=[1,2,5]b= ...
- Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 暴力并查集
D. Dividing Kingdom II 题目连接: http://www.codeforces.com/contest/687/problem/D Description Long time a ...
- HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)
HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...
- HDU 3081 Marriage Match II (二分图,并查集)
HDU 3081 Marriage Match II (二分图,并查集) Description Presumably, you all have known the question of stab ...
随机推荐
- C++11 Lambda表达式(匿名函数)详解
使用STL时,往往会大量用到函数对象,为此要编写很多函数对象类.而有的函数对象类只用定义一个对象,而且这个对象也只使用一次,那编写这样一个函数对象就很浪费了.而且有时这定义函数对象类的地方和使用函数对 ...
- VS2019 配置 protobuf3.8.0
1.下载protobuf3.8.0 https://github.com/protocolbuffers/protobuf/releases/tag/v3.8.0 2.准备工作 解压文件并在同级目录建 ...
- LocalDateTime获取 年月日时分秒和判断日期大小
环境:java version "13.0.1". 创建一个DateUtils类,提供三个常用方法: String 转换 LocalDateTime的方法. 获取LocalDate ...
- 【2020.11.25提高组模拟】太空漫步(walking) 题解
[2020.11.25提高组模拟]太空漫步(walking) 题解 题目描述 Do not go gentle into that good night. Old age should burn an ...
- 编程记录:TypeScript中never类型的技巧
技巧1 当我们在一个项目中,可能会去改动一个在整个项目中应用很广泛的函数的参数类型,但是可能由于代码量比较庞大,我们不好排查改了之后哪些地方会出现问题,此时我们可以使用never类型来辅助我们的函数, ...
- OceanBase 正式上线 KubeSphere Marketplace
随着云原生与容器化浪潮全面重塑 IT 基础设施,数据库层的"云原生化"也逐渐从趋势演进为现实需求.近日,OceanBase 开源社区的扩展组件 OceanBase Operator ...
- Lustre 与 JuiceFS :架构设计、文件分布与特性比较
在 AI 模型训练.高性能计算等对 I/O 敏感的场景中,底层文件系统的架构和性能将直接影响训练效率.资源利用率与整体成本. Lustre 作为传统高性能文件系统,以极致性能著称:而 JuiceFS ...
- STM32学会要花费多长时间?一个从机械转行老程序员的血泪史
看到这个问题,我不禁想起了6年前那个拿着机械毕业证却被分配到电子部门的懵逼青年--没错,就是我. 当时坐在工位上,面对着桌上那块STM32F103的开发板,内心是崩溃的.我特么学了四年机械,研究生又搞 ...
- 一次说清楚:CAE软件可以做什么?
引言: 计算机辅助工程(CAE)软件是现代工程设计与分析的重要工具,它以计算机技术为基础,利用数值模拟和仿真方法来解决各种工程问题.本文将探讨CAE软件的广泛应用领域,以及它在工程设计与分析中的作用. ...
- Linux 查找Redis配置信息
前言 有时在使用Redis时密码或者配置信息经常忘记,应该怎么找回呢? 解决 如果设置了自启动,先查找服务状态(systemctl status redis服务名称) 根据服务可以找到服务的启动脚本, ...