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但却被遗漏。

A1076. Forwards on Weibo的更多相关文章

  1. PAT A1076 Forwards on Weibo (30 分)——图的bfs

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

  2. PAT甲级——A1076 Forwards on Weibo

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

  3. PAT_A1076#Forwards on Weibo

    Source: PAT A1076 Forwards on Weibo (30 分) Description: Weibo is known as the Chinese version of Twi ...

  4. 1076 Forwards on Weibo (30 分)

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

  5. 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 ...

  6. PAT甲级1076. Forwards on Weibo

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

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

  8. 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 ...

  9. 1076. Forwards on Weibo (30)

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

随机推荐

  1. Angular 基本指令

    <!DOCTYPE html><html ng-app><head lang="en"> <meta charset="UTF- ...

  2. Android——MaterialDesign之一Toolbar

    Toolbar 由于ActionBar设计原因只能存在活动的顶部,从而不能实现MaterialDesign的效果,现在推荐使用Toolbar,继承Actionbar,但是比起它更加的灵活. 设置主题: ...

  3. 关于IWMS后台登录问题总结

    一.登录后台,点击登录无反应: 1.是因为网站文件夹没有权限,需要右击文件夹,将只读勾选去掉 2.在安全中加入Everyone对象. 二.登录后台后,左边显示不全,是因为会员权限不够,需要给权限.

  4. vscode git设置

    vscode只能打开一下界面: 在setting.path增加git.path选项,再使用linux的方法配置路径,就是使用D:/../bin/git.exe而不是\\ 重启vscode,git设置即 ...

  5. Lodop的JS模版代码、文档式模版 生成加载赋值博文索引

    Lodop获取全部JS代码,传统JS模版的生成.LODOP设置打印设计返回JS代码是变量 LodopJS代码模版的加载和赋值 Lodop生成文档式模版 LodopJS文档式模版的加载和赋值 由于加载J ...

  6. [洛谷日报第62期]Splay简易教程 (转载)

    本文发布于洛谷日报,特约作者:tiger0132 原地址 分割线下为copy的内容 [洛谷日报第62期]Splay简易教程 洛谷科技 18-10-0223:31 简介 二叉排序树(Binary Sor ...

  7. 在没有 Emacs 的情况下使用 Org 模式

    导读 每到年初似乎总有这么一个疯狂的冲动来寻找提高生产率的方法.新年决心,正确地开始一年的冲动,以及“向前看”的态度都是这种冲动的表现.软件推荐通常都会选择闭源和专利软件.但这不是必须的. 这是我 2 ...

  8. Elasticsearch 5.x 字段折叠的使用

    在Elasticsearch 5.x  之前,如果实现一个数据折叠的功能是非常复杂的,随着5.X的更新,这一问题变得简单,找到了一遍技术文章,对这个问题描述的非常清楚,收藏下. 参考:https:// ...

  9. Day24-ModelForm操作及验证

    Day23内容回顾--缺失,遗憾成狗. 一:Model(2个功能) -数据库操作: -验证,只有一个clean方法可以作为钩子. 二:Form(专门来做验证的)--------根据form里面写的类, ...

  10. 爬虫_拉勾网(解析ajax)

    拉勾网反爬虫做的比较严,请求头多添加几个参数才能不被网站识别 找到真正的请求网址,返回的是一个json串,解析这个json串即可,而且注意是post传值 通过改变data中pn的值来控制翻页 job_ ...