测试链接: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 (并查集)的更多相关文章

  1. [Swift]LeetCode928. 尽量减少恶意软件的传播 II | Minimize Malware Spread II

    (This problem is the same as Minimize Malware Spread, with the differences bolded.) In a network of ...

  2. [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 ...

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

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

  5. [LeetCode] 928. Minimize Malware Spread II 最大程度上减少恶意软件的传播之二

    (This problem is the same as Minimize Malware Spread, with the differences bolded.) In a network of ...

  6. hihocoder Counting Islands II(并查集)

    Counting Islands II 描述 Country H is going to carry out a huge artificial islands project. The projec ...

  7. python 变量作用域 v.__sizeof__() python 深复制 一切皆对象 尽量减少内存消耗

    python 深入理解 赋值.引用.拷贝.作用域 - 江召伟 - 博客园 https://www.cnblogs.com/jiangzhaowei/p/5740913.html a=[1,2,5]b= ...

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

  9. HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)

    HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...

  10. HDU 3081 Marriage Match II (二分图,并查集)

    HDU 3081 Marriage Match II (二分图,并查集) Description Presumably, you all have known the question of stab ...

随机推荐

  1. 球体的顶点与索引创建方法----以WebGL为例

    上图,左图为一个球体的三维图,其中一个圆面以θ角(范围为[0,PI])的方式确定,该圆面在x-z坐标平面投影如右图,其中圆面上任意一点又由α确定(范围为[0,2PI]). 假定该球体半径为r,那么球面 ...

  2. PINN做脆性材料裂纹扩展的损失函数设计

  3. 用网页计数器来说明application和session

    jsp的代码: 1 <body> 2 <h1>网页计数器</h1> 3 <% 4 //第一次访问数据为空 5 Object obj=application.g ...

  4. 《经验分享——在CSDN编写文章时如何实现空格、空行》

    经验分享--在CSDN编写文章时如何实现空格.空行 一.富文本编辑器: 1.空格: 按空格键 2.空行 先按Tab,再按回车键 二.Markdown编辑器: 1. 空格: 按空格键 2.空行: 输入& ...

  5. helmfile调试

    说明 我们在调试helmfile编排的chart时,对于helm chart正常的情况下,我们编排的helmfile渲染模版的值出了问题,可能会存在以下的报错: helmfile template E ...

  6. PHP数据结构当中的栈

    本文由 ChatMoney团队出品 栈(Stack)是一种后进先出(Last In First Out, LIFO)的数据结构,它只允许在一端(称为栈顶)进行插入和删除操作.栈的应用非常广泛,例如在编 ...

  7. Just:告别 Makefile 的现代命令行任务运行器

    本文推荐的一个轻量级命令行工具--Just,它提供了一种简单高效的方式来管理项目任务,类似于传统的 Make 工具,但具有更简洁的语法和更现代化的功能. 我目前在一些小项目中开始使用它来管理一些日常的 ...

  8. 数栈大数据组件:Hive优化之配置参数的优化

    Hive是大数据领域常用的组件之一,主要用于大数据离线数仓的运算,关于Hive的性能调优在日常工作和面试中是经常涉及的一个点,因此掌握一些Hive调优是必不可少的一项技能.影响Hive效率的主要因素有 ...

  9. GitHub MCP Server - 无缝集成GitHub API的自动化工具

    GitHub MCP Server GitHub MCP Server是一个Model Context Protocol(MCP)服务器,提供与GitHub API的无缝集成,使开发者和工具能够实现高 ...

  10. 从Rust想到C#

    近几年,RUST语言越来越受大家的喜爱,排除去一些跟风者,大部分的人喜欢RUST的内存安全性和高效的性能.但编译速度始终是它的短板. 这几天,突然有一个想法,如果C#或者说.NET的编译器也做成RUS ...