Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all his/her followers can view and forward his/her post, which can then be forwarded again by their followers. Now given a social network, you are supposed to calculate the maximum potential amount of forwards for any specific user, assuming that only L levels of indirect followers are counted.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers: N (≤), the number of users; and L (≤), the number of levels of indirect followers that are counted. Hence it is assumed that all the users are numbered from 1 to N. Then N lines follow, each in the format:

M[i] user_list[i]
 

where M[i] (≤) is the total number of people that user[i] follows; and user_list[i] is a list of the M[i] users that followed by user[i]. It is guaranteed that no one can follow oneself. All the numbers are separated by a space.

Then finally a positive K is given, followed by K UserID's for query.

Output Specification:

For each UserID, you are supposed to print in one line the maximum potential amount of forwards this user can trigger, assuming that everyone who can view the initial post will forward it once, and that only L levels of indirect followers are counted.

Sample Input:

7 3
3 2 3 4
0
2 5 6
2 3 1
2 3 4
1 4
1 5
2 2 6
 

Sample Output:

4
5

题意:

  给出一个社交网络,在规定的层级内,一个博主发了博文之后,共有多少人能够看到这篇文章?假设每一个follower看到发表的文章之后都会转发。

思路:

  这道题就是一道有向图的问题,样例中:M[i] user_list[i]代表的意思应该是,user_list中的人发表了文章,i能够看到并进行转发。然后用邻接矩阵表示有向图,用层序遍历的方法来遍历满足要求的结点。注意遍历的过程中不要重复出现。

Code:

 1 #include <bits/stdc++.h>
2
3 using namespace std;
4
5 vector<int> grap[1005];
6
7 int helper(int query, int level) {
8 queue<int> que;
9 que.push(query);
10 que.push(-1);
11 int count = 0;
12 vector<bool> visited(1005, false);
13 visited[query] = true;
14 while (!que.empty() && level >= 0) {
15 int temp = que.front();
16 que.pop();
17 if (temp != -1) count++;
18 if (temp == -1) {
19 level--;
20 if (que.empty()) break;
21 que.push(-1);
22 } else {
23 for (int i = 0; i < grap[temp].size(); ++i) {
24 if (!visited[grap[temp][i]]) {
25 visited[grap[temp][i]] = true;
26 que.push(grap[temp][i]);
27 }
28 }
29 }
30 }
31 return count - 1;
32 }
33
34 int main() {
35 int n, l, k, t;
36 cin >> n >> l;
37 for (int i = 1; i <= n; ++i) {
38 cin >> k;
39 for (int j = 0; j < k; ++j) {
40 cin >> t;
41 grap[t].push_back(i);
42 }
43 }
44 cin >> k;
45 for (int i = 0; i < k; ++i) {
46 cin >> t;
47 cout << helper(t, l) << endl;
48 }
49
50 return 0;
51 }

最新文章

  1. ashx中Response.ContentType的常用类型
  2. Qt基本框架介绍
  3. Hadoop集群datanode磁盘不均衡的解决方案
  4. 汇编语言进阶和Makefile进阶---第二天
  5. 拉面馆中的移动互联网——无线KPI探讨
  6. UBoot常用命令手册
  7. Windows 8.1 (64bit) 下搭建 Scrapy 0.22 环境
  8. python属性查找
  9. hdu 4619 二分图最大匹配 ——最大独立集
  10. 基于VMware的eCos环境编译redboot(脚本配置redboot)
  11. 「设计模式」JavaScript - 设计模式之单例模式与场景实践
  12. JS中JSON对象的定义和取值
  13. mongodb副本集配置
  14. js 从一个函数中传递值到另一个函数
  15. linux vi命令详解
  16. linux系统常用的基本命令分类
  17. Oracle修改表结构字段名和字段长度
  18. [protocol]GO enrichment analysis
  19. Flask-SQLAlchemy基本操作
  20. Kafka 0.8 sever:源代码High level分析

热门文章

  1. IVMS-5000海康平台安装
  2. 后端程序员之路 38、Scala入门
  3. CCF(棋局评估)博弈论+对抗搜索+DFS
  4. go-优雅地关机或重启
  5. Linux系统用户与用户组管理
  6. MyBatis(十一):MyBatis架构流程浅析
  7. java mvc 及其缓存
  8. ECharts系列 (01):地图三级下钻
  9. Linux wget 使用笔记
  10. io流(文件字符流(FileReader,FileWriter文件的复制))