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:

  1. 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:

  1. 7 3
  2. 3 2 3 4
  3. 0
  4. 2 5 6
  5. 2 3 1
  6. 2 3 4
  7. 1 4
  8. 1 5
  9. 2 2 6
 

Sample Output:

  1. 4
  2. 5

题意:

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

思路:

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

Code:

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

1076 Forwards on Weibo的更多相关文章

  1. PAT 1076 Forwards on Weibo[BFS][一般]

    1076 Forwards on Weibo (30)(30 分) Weibo is known as the Chinese version of Twitter. One user on Weib ...

  2. 1076 Forwards on Weibo (30 分)

    1076 Forwards on Weibo (30 分) Weibo is known as the Chinese version of Twitter. One user on Weibo ma ...

  3. PAT甲级1076. Forwards on Weibo

    PAT甲级1076. Forwards on Weibo 题意: 微博被称为中文版的Twitter.微博上的一位用户可能会有很多关注者,也可能会跟随许多其他用户.因此,社会网络与追随者的关系形成.当用 ...

  4. 1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise

    题目信息 1076. Forwards on Weibo (30) 时间限制3000 ms 内存限制65536 kB 代码长度限制16000 B Weibo is known as the Chine ...

  5. PAT 甲级 1076 Forwards on Weibo (30分)(bfs较简单)

    1076 Forwards on Weibo (30分)   Weibo is known as the Chinese version of Twitter. One user on Weibo m ...

  6. 1076. Forwards on Weibo (30)

    时间限制 3000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Weibo is known as the Chinese v ...

  7. PAT 1076. Forwards on Weibo (30)

    Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...

  8. 1076. Forwards on Weibo (30) - 记录层的BFS改进

    题目如下: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, a ...

  9. 1076 Forwards on Weibo (30)(30 分)

    Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...

  10. PAT Advanced 1076 Forwards on Weibo (30) [图的遍历,BFS,DFS]

    题目 Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and ...

随机推荐

  1. Svelte 极简入门

    ​弹指之间即可完成.   注意:原文发表于 2017-8-7,随着框架不断演进,部分内容可能已不适用.     Svelte 是一种新型框架.   以往我们要引入一个框架或者类库,可以通过在页面上放置 ...

  2. Qt update刷新之源码分析(二)

    大家好,我是IT文艺男,来自一线大厂的一线程序员 上次视频给大家从源码层面剖析了Qt update刷新机制的异步事件投递过程,这次视频主要从源码层面剖析Qt刷新事件(QEvent::UpdateReq ...

  3. 后端程序员之路 23、一个c++的api framework

    在"21.一个cgi的c++封装"中,我们封装了cgi,在这之上,我们可以再来封装一个webapi的framework.当然,前文的Casablanca是个不错的选择,但是它比较庞 ...

  4. 为 APK 文件增加右键菜单组实现快捷安装

    0.结果 1.需求 迫于每次都要打开 Powershell 手动敲 adb install xxx.apk 太麻烦,就想通过注册表搞一个右键菜单,实现快捷安装 apk 的功能. 最后决定先实现三个功能 ...

  5. POJ-1751(kruskal算法)

    Highways POJ-1751 注意这里的样例答案也是对的,只是输出顺序改变,但是这也没关系,因为题目加了特殊判断. #include<iostream> #include<cs ...

  6. 盘点Excel中的那些有趣的“bug”

    本文由葡萄城技术团队原创并首发 转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. Excel 1.0早在1985年正式进入市场,距今已经有36年了,虽然在推出 ...

  7. Linux速通04 用户、群组、权限

    用户及passwd文件 # /etc/passwd文件的功能:存储所有用户的相关信息,实际上是存放用户信息的数据库(database) # 各个字段的含义: * 第一个字段(列)记录的是这个用户的名字 ...

  8. C#的foreach遍历循环和隐式类型变量

    C#的foreach遍历循环和隐式类型变量 foreach遍历循环 foreach (<baseType> <name> in <array>>) { //c ...

  9. ASP.NET Core扩展库

    亲爱的.Neter们,在我们日复一日的编码过程中是不是会遇到一些让人烦恼的事情: 日志配置太过复杂,各种模板.参数也搞不清楚,每次都要去查看日志库的文档,还需要复制粘贴一些重复代码,好无赖 当需要类型 ...

  10. css实现0.5像素的底边框。

    由于设计图的1px在移动端开发中的像素比是2倍,在实际开发中却是需要1px的线条,虽然最直接的方式是将线条设置为0.5px,但有些移动端对于0.5px的解析为0,变成了无边框的显示.因此处理该需求我们 ...