GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — LeetCode/BFS/928.Minimize-Malware-Spread-II at master · wisdompeak/LeetCode

Expand file tree

/

928.Minimize-Malware-Spread-II

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

928.Minimize-Malware-Spread-II

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
928.Minimize-Malware-Spread-II.cpp
928.Minimize-Malware-Spread-II.cpp
 
 
Readme.md
Readme.md
 
 

Readme.md

928.Minimize-Malware-Spread-II

本题的切入点是,什么样的node我们可以挽救?答案是邻接的感染源数目为1的node才行。如果某个node邻接的直接感染源(指不需要经过其他感染源就可以到达)的数目为2或以上,说明任何remove的操作都无法征求这个node,总会被其他的感染源所扩散到。

所以,我们对于某个node,如果它的直接感染源是source且唯一,那么说明我们只要remove source的话,就可以使得这个node免受感染。即这个node是source的“直接保护对象”。如果我们对于每个source,都可以计算出它的直接保护对象的数目。自然答案就是取“直接保护对象”最多的那个感染源。

怎么高效地求出每个source的“保护对象”的个数呢?这个逻辑有点绕。我们需要先从每个source出发,看看它能直接覆盖到的node(不需要经过其他感染源就可以到达)是哪些,这个用BFS即可。依次类推,重复K次BFS(K是感染源的个数)。最终可以记录下每个node都可以被哪些感染源“直接覆盖”。如果某个node的邻近感染源的数目是1时,才给对应的source的“直接保护对象”的计数器加一。于是就解决了这个问题。

Leetcode Link

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.