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. 直接获取任意对象的 $('.xx').css('x') 值都是0

    <!-- 任意对象,直接获取他们的 x , y 都是为0: $('#xxx').css('x','y'); --> <!DOCTYPE html> <html lang= ...

  2. js数值的添加与删除

    js中数组元素的添加和删除 js中数组元素常用添加方法是直接添加.push方法以及unshift方法 删除方法则是delete.pop.shift 集修改方法为一身的则是splice 1.添加: (1 ...

  3. Kafka 基础操作

    cd /root/kafka/kafka_2.10-0.8.2.2/bin 1.查看kafka topic kafka-topics.sh --list --zookeeper 172.16.100. ...

  4. 数据结构实验之排序二:交换排序 (SDUT 3399)

    #include <iostream> #include <bits/stdc++.h> using namespace std; typedef long long ll; ...

  5. 一次docker镜像的迁移

    docker 镜像迁移 背景,本地测试环境要切到线上测试,镜像下载或编译都需要时间. 所以直接scp镜像过去来节省时间. save 相对于export会占用更多存储空间 被迁移服务器导出所有镜像 do ...

  6. cs 与 bs 架构

    C/S和B/S各有优势,C/S在图形的表现能力上以及运行的速度上肯定是强于B/S模式的,不过缺点就是他需要运行专门的客户端,而且更重要的是它不能跨平台,用c++在windows下写的程序肯定是不能在l ...

  7. jquery中$.get()如何让跨域请求携带cookie?

    在这个get请求前面加上这个就好了~~~~

  8. MySQL数据库入门——多实例配置

    前面介绍了相关的基础命令操作,所有的操作都是基于单实例的,mysql多实例在实际生产环境也是非常实用的,因为必须要掌握 1.什么是多实例 多实例就是一台服务器上开启多个不同的服务端口(默认3306), ...

  9. NAT+PAT+OSPF+设备互连地址详解

  10. Open vSwitch系列实验(二):Open vSwitch的GRE隧道实验网络

    一.实验目的 了解GRE协议及原理 理解 Open vSwitch如何配置GRE隧道 二.实验原理 Open vSwitch创建GRE原理很简单,就是把对GRE头和外部IP头的一些操作从原来的代码中抽 ...