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 (<=1000), the number of users; and L (<=6), 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] (<=100) is the total number of people that user[i] follows; and user_list[i] is a list of the M[i] users that are 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 triger, 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
 #include<cstdio>
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
typedef struct NODE{
int data, layer;
}node;
vector<node> G[];
int visit[];
int N, L, K, Mi;
int bfs(int vt){
int cnt = -;
node first = {vt, };
visit[vt] = ;
queue<node> Q;
Q.push(first);
while(Q.empty() == false){
node temp = Q.front();
Q.pop();
cnt++;
int len = G[temp.data].size();
if(temp.layer < L){
for(int i = ; i < len; i++){
if(visit[G[temp.data][i].data] == ){
visit[G[temp.data][i].data] = ;
G[temp.data][i].layer = temp.layer + ;
Q.push(G[temp.data][i]);
}
}
}
}
return cnt;
}
int main(){
scanf("%d%d", &N, &L);
for(int i = ; i <= N; i++){
scanf("%d", &Mi);
for(int j = ; j < Mi; j++){
int temp;
scanf("%d", &temp);
node nd = {i, };
G[temp].push_back(nd);
}
}
scanf("%d", &K);
int vt, ans;
for(int i = ; i < K; i++){
for(int j = ; j < ; j++)
visit[j] = ;
scanf("%d", &vt);
ans = bfs(vt);
printf("%d\n", ans);
}
cin >> N;
return ;
}

总结:

1、题意:求据距离点A的距离不超过L的节点的个数(A本身不计入)。

2、bfs的visit数组记录的是是否曾加入过队列,而非是否被访问。

3、本题是有向图,最好用邻接表存储。此外,题目给出的数据是A关注的人的列表,而微博消息应该是从关注列表里的人到A的方向,注意不要弄错图的边。

4、由于dfs的原则是访问过的节点就不再访问,所以求得的节点有可能不是最短的转发距离,从而有可能漏掉一些情况。如图,当使用深搜,L = 2。从A开始,A->B->C,距离达到了2开始回溯到A,而由于C已被visit标记,所以无法再次被访问,D距离A也是2但却被遗漏。

最新文章

  1. 淡蓝风格的手机登录HTML模板
  2. Percona Server 5.6.13-61.0 首个 GA 版本发布
  3. ASP.NET Web API 上传文件
  4. 《30天自制操作系统》15_day_学习笔记
  5. linux 软件安装
  6. PMP-产品范围与项目范围区别
  7. c# 将PPT转换成HTML
  8. POJ2299--树状数组求逆序数
  9. 给Ocelot做一个Docker 镜像
  10. WebGL&amp;Three.js工作原理
  11. Python爬虫7-Cookie &amp; Session
  12. Spring Security(三十):9.5 Access-Control (Authorization) in Spring Security
  13. Nginx ServerName指令
  14. Django 学习第十一天——中间键和上下文处理器
  15. ArrayList, LinkedList, Vector - dudu:史上最详解
  16. 冒泡 MS Azure 不便宜。。。
  17. js 深度复制deepClone
  18. C++ 获取字符串中的所有汉字
  19. Win7 qt-windows-x86-msvc2015-5.6.0 DLL依赖库打包
  20. Delphi XE 新功能试用:多种皮肤样式静、动态设置方法

热门文章

  1. APP-SERVICE-SDK:setStorageSync:fail;at page/near/pages/shops/shops page lifeCycleMethod onUnload function
  2. CLOUD SQL跟踪
  3. python之路--初识html前端
  4. admin快速搭建后台管理系统
  5. Stream、FileStream、MemoryStream的区别
  6. npm火速上手
  7. Visual Studio for Mac中的ASP.NET Core
  8. WSS Process On Causal LTI System
  9. Ubuntu 16.04配置Java Web开发环境
  10. Go语言类型(布尔、整型、数组、切片、map等)