Considering that I'ld would like to spread a promotion message across all people in twitter. Assuming the ideal case, if a person tweets a message, then every follower will re-tweet the message.

You need to find the minimum number of people to reach out (for example, who doesn't follow anyone etc) so that your promotion message is spread out across entire network in twitter.

Also, we need to consider loops like, if A follows B, B follows C, C follows D, D follows A (A -> B -> C -> D -> A) then reaching only one of them is sufficient to spread your message.

Input: A 2x2 matrix like below. In this case, a follows b, b follows c, c follows a.

    a b c
a 1 1 0
b 0 1 1
c 1 0 1

Output: List of people to be reached to spread out message across everyone in the network.

来源

F家

解法:

This is a very interesting graph problem, here is what I would do:

step 1. Build a directed graph based on the input people (nodes) and their relationship (edges).

step 2. Find strongly connected components (SCCs) in the graph. Let's use the wikipedia's graph example, in that case, there are 3 SCCs: (a, b, e)(c, d, h) and (f, g). There are two famous algorithms for getting the SCCs: Kosaraju's algorithm and Tarjan's algorithm.

step 3. Pick one of the nodes from the SCCs we get: a, c, f, now these 3 nodes form a DAG, we just need to do topological sort for them, eventually a is the root node in the path (or stack), and we can let a spread the message and guarantee all other people will get it.

Sometimes, there could be several topological paths, and the root nodes of those paths will be the minimum people to reach out to spread the message.

Create a directed graph which captures followee -> follower relationship
Now create the topological sort for the entire graph
For each unvisited node in the topological sort result, add it to the final result and then visit all the nodes in that tree
The reason this works is, in the topological sort order the node that appears first is the left most node in the given connected component. So you would use that to traverse the curr node and all its children node.

def min_people(num_people, follows):
from collections import defaultdict # in this graph we will store
# followee -> follower relation
graph = defaultdict(set) # a follows b
for a, b in follows:
graph[b].add(a) def topo(node, graph, visited, result):
visited.add(node)
for nei in graph[node]:
if nei not in visited:
topo(nei, graph, visited, result)
result.append(node) visited = set([])
result = []
for i in range(num_people):
if i not in visited:
topo(i, graph, visited, result)
result = list(reversed(result)) def visit(node, visited, graph):
visited.add(node)
for nei in graph[node]:
if nei not in visited:
visit(nei, visited, graph) visited = set([])
start_with = []
for r in result:
if r not in visited:
start_with.append(r)
visit(r, visited, graph) return start_with

  

类似题目:

[LeetCode] 323. Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数

Find minimum number of people to reach to spread a message across all people in twitter的更多相关文章

  1. [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  2. [LeetCode] 452 Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  3. Reorder array to construct the minimum number

    Construct minimum number by reordering a given non-negative integer array. Arrange them such that th ...

  4. Leetcode: Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  5. Lintcode: Interval Minimum Number

    Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...

  6. 452. Minimum Number of Arrows to Burst Balloons——排序+贪心算法

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  7. Codeforces 279D The Minimum Number of Variables 状压dp

    The Minimum Number of Variables 我们定义dp[ i ][ mask ]表示是否存在 处理完前 i 个a, b中存者 a存在的状态是mask 的情况. 然后用sosdp处 ...

  8. [Swift]LeetCode452. 用最少数量的箭引爆气球 | Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  9. [Swift]LeetCode995. K 连续位的最小翻转次数 | Minimum Number of K Consecutive Bit Flips

    In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray o ...

随机推荐

  1. Java【基础学习】向下转型和上转型例子

    Java小白应付期末考试QWQ class Animal{ public void move() { System.); } } class Dog extends Animal{ public vo ...

  2. Oracle CAST() 函数 数据类型的转换

    CAST()函数可以进行数据类型的转换. CAST()函数的参数有两部分,源值和目标数据类型,中间用AS关键字分隔. 以下例子均通过本人测试. 一.转换列或值 语法:cast( 列名/值 as 数据类 ...

  3. Deepgreen/Greenplum 删除节点步骤

    Deepgreen/Greenplum删除节点步骤 Greenplum和Deepgreen官方都没有给出删除节点的方法和建议,但实际上,我们可以对节点进行删除.由于不确定性,删除节点极有可能导致其他的 ...

  4. python - 使用psutils

    oshelper.py #encoding=utf-8 import psutil import datetime #查看cpu的信息 print u"CPU 个数 %s"%psu ...

  5. php+ tinymce粘贴word

    最近公司做项目需要实现一个功能,在网页富文本编辑器中实现粘贴Word图文的功能. 我们在网站中使用的Web编辑器比较多,都是根据用户需求来选择的.目前还没有固定哪一个编辑器 有时候用的是UEditor ...

  6. easyui增删改查

    easyui的crud(dialog,datagrid.form讲解)1.datagrid布局2.dialog布局3.form布局4.通用的JsonBaseDao增删改方法5.dao层6.web层7. ...

  7. 【JS】知识笔记

    一.JS文件位置 多个.JS文件最好合并到一个文件中,减少加载页面时发送的请求数量. 某个单独页面需要的js,最好放在HTML文档的最后,</body>之前,这样能使浏览器更快地加载页面. ...

  8. 生活 RH阴性血 AB型

    这个血型很稀有,外国多些,中国很少. ABO型:A.B.AB.O RH血型系统:阴性,阳性 RH阴性血,被称为熊猫血,估计是稀有吧,阴性血缺抗D,我老婆的血型抗原好像是:ccee,大部分汉族人都有抗D ...

  9. 玩好JDK[转]

    ref: https://www.cnblogs.com/zuoxiaolong/p/life53.html java-reference:https://docs.oracle.com/en/jav ...

  10. Angular实战项目(1)

    Angular 打造企业级协作平台 [外链图片转存失败(img-J0HrPiEG-1563902660799)(https://upload-images.jianshu.io/upload_imag ...