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
看懂题意很重要,要计算 L层以内的follower的数量,bfs+邻接表(省时间)。
代码:
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <iostream>
using namespace std;
int n,l;
vector<int> v[];///邻接表
typedef pair<int,int> pa;///方便队列使用
int bfs(int d) {
queue<pa> q;
q.push(pa(d,));
int c = ;
bool vis[] = {false};
vis[d] = true;
while(!q.empty()) {
int level = q.front().second;
int present = q.front().first;
q.pop();
for(int i = ;i < v[present].size();i ++) {
if(!vis[v[present][i]]) {
if(level + <= l) {///满足
c ++;
vis[v[present][i]] = ;
q.push(pa(v[present][i],level + ));
}
}
}
}
return c;
}
int main() {
int m,d;
scanf("%d%d",&n,&l);
for(int i = ;i <= n;i ++) {
scanf("%d",&m);
for(int j = ;j <= m;j ++) {
scanf("%d",&d);
v[d].push_back(i);
}
}
int k;
scanf("%d",&k);
for(int i = ;i < k;i ++) {
scanf("%d",&d);
printf("%d\n",bfs(d));
}
}

1076 Forwards on Weibo (30)(30 分)的更多相关文章

  1. 1076 Forwards on Weibo (30 分)

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

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

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

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

  5. PAT甲级1076. Forwards on Weibo

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

  6. 【PAT甲级】1076 Forwards on Weibo (30 分)

    题意: 输入两个正整数N和L(N<=1000,L<=6),接着输入N行数据每行包括它关注人数(<=100)和关注的人的序号,接着输入一行包含一个正整数K和K个序号.输出每次询问的人发 ...

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

  8. 1076. Forwards on Weibo (30)

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

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

随机推荐

  1. dm8148 videoM3 link源代码解析

    样例:从A8送一帧jpeg图片到videoM3解码,然后在将解码的数据传递到A8, 这个流程涉及的link源代码例如以下: dm8148 link之间数据传递 1)在A8上调用IpcBitsOutLi ...

  2. 以使用QSqlQuery向数据库中插入数据为例,做一个小结

    背景: 最近在使用Qt+SQLite写一个本地数据库管理程序(使用者不懂SQL),在写向数据库中插入数据的 相关的函数时,我遇到了几个问题(暂时就这些): 1.向指定字段插入指定数据时,读取到的数据都 ...

  3. eclipse不能添加tomcat

    有时候原来能新建server,删掉后就不能新建了 1.退出eclipse 2.到[工程目录下 workspace ]/.metadata/.plugins/org.eclipse.core.runti ...

  4. centos 6.9 x86 安装搭建hadoop集群环境

    又来折腾hadoop了 文件准备: centos 6.9 x86 minimal版本 163的源 下软件的时候可能会用到 jdk-8u144-linux-i586.tar.gz ftp工具 putty ...

  5. POJ 1787 Charlie&#39;s Change

    多重背包 可行性+路径记录 题意是说你要用很多其它的零钱去买咖啡.最后输出你分别要用的 1,5 ,10 .25 的钱的数量. 多重背包二进制分解.然后记录下 这个状态.最后逆向推就可以. #inclu ...

  6. beego介绍

    beego 简介 beego 是一个快速开发 Go 应用的 HTTP 框架,他可以用来快速开发 API.Web 及后端服务等各种应用,是一个 RESTful 的框架,主要设计灵感来源于 tornado ...

  7. git for windows 无法结束node进程(windows下杀进程)

    问题 windows 系统下,如果用CMD命令行启动node服务,Ctrl + C 即可结束命令 git bash 用起来比命令行方便,但是Ctrl + C 并不会结束node服务,再次启动会报如下错 ...

  8. cesium学习--初识

    一.Cesium 官方介绍:CesiumJS是一个开源的JavaScript库,用于世界级的3D地球仪和地图.任务是为静态和时间动态的内容创建领先的3D地球和地图,具有最好的性能.精度.视觉质量.平台 ...

  9. Continuously Integrate

     Continuously Integrate David Bartlett THE Build AS A "Big BAng" EvEnT in project develop ...

  10. map和string的使用方法

    这个是别人写的map使用方法比較好能够看一下 http://www.cnblogs.com/anywei/archive/2011/10/27/2226830.html 怎样向数组中插入内容 http ...