1076 Forwards on Weibo (30分)
 

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
作者: CHEN, Yue
单位: 浙江大学
时间限制: 3000 ms
内存限制: 64 MB
代码长度限制: 16 KB

题意:

微博在中国非常盛行,我们可以被其他人所关注,也可以关注其他人(注此处应用有向图),我们只能浏览被关注者所发表的微博。问题:当一位用户发表一篇微博时,他的每一位粉丝都会转发其内容,而粉丝的粉丝也会转发,以此类推,问,L次后,这篇微博的内容会被多少人浏览?
输入N,L(N为微博用户数,L为关系层数)。然后输入每个用户(随后第i行对应对第i位)的关注的用户数目n,再输入n位被关注者的id(对应行号i)。最后,输入数字K,同时,输入K位你要查询用户的ID(即序号)。
输出要查询的用户每发一篇微博,经过L轮转发后的浏览次数。
思路分析:通过输入构建有向图,经过广度优先搜索BFS,遍历经过L层的粉丝。
————————————————
版权声明:本文为CSDN博主「yzh1994414」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/yzh1994414/article/details/78229568

题解:

bfs,队列里存结构体,包含层数d,用f数组标记是否以算过。

自己要标记,但不用计数 。

没有算过的才需要放入队列,否则会内存超限。

AC代码:

#include<bits/stdc++.h>
using namespace std;
int n,l;
struct node{
int p;
int d;
};
int f[];
int s=;
queue<node>q;
vector<int>v[];
int num;
int main(){
cin>>n>>l;
for(int i=;i<=n;i++){
cin>>num;
for(int j=;j<=num;j++){
int x;
cin>>x;
v[x].push_back(i);
}
}
cin>>num;
for(int i=;i<=num;i++){
node a;
cin>>a.p;
s=;
a.d=;
memset(f,,sizeof(f));
f[a.p]=;//自己要标记,但不用计数
q.push(a);
while(!q.empty()){
a=q.front();
q.pop();
if(a.d>=l) continue;
for(int j=;j<v[a.p].size();j++){
int x=v[a.p].at(j);
if(!f[x]){//没有算过的才需要放入数组,否则会内存
f[x]++;
s++;
node b;
b.d=a.d+;
b.p=x;
q.push(b);
}
}
}
cout<<s<<endl;
}
return ;
}

最新文章

  1. 快捷键_Mac
  2. (转)各种排序算法的分析及java实现
  3. SQLite数据库中获取新插入数据的自增长ID
  4. python的全局变量玩法还挺特别的
  5. iScroll 4.2.5 中文API
  6. 链表c语言实现
  7. json 多重嵌套反序列化和序列化
  8. Unity3D在NGUI中使用mask
  9. ASP.NET 导出excel文件出现乱码的解决办法
  10. spring框架总结(04)----介绍的是Spring中的JDBC模板
  11. 剑指Offer——全排列递归思路
  12. 071、如何定制calico网络的IP池(2019-04-16 周二)
  13. Android中的广播基本实现及回调方法的理解
  14. shell脚本学习-练习写一个脚本1
  15. 同一个机器 安装多个版本Chrome浏览器的方法
  16. NOIP2008双栈排序(贪心)
  17. L3-002 特殊堆栈 (30 分) 模拟stl
  18. 检测Linux服务器端口是否开通
  19. Bigtable:一个分布式的结构化数据存储系统
  20. 关于scut PersonalCacheStruct&lt;&gt;.foreach

热门文章

  1. 微信小程序~模板template引用
  2. Spring容器、BeanFactory和ApplicationContext,及3种装配Bean的方式
  3. 在windows系统和kali中通过sqlmap注入
  4. NPM——npm|cnpm如何升级
  5. Java 14 周作业
  6. Python 3的 bytes 数据类型
  7. python OOP
  8. LeetCode 1087. Brace Expansion
  9. 如何用okr做好目标规划
  10. Minidumps 和 modules匹配