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. 在windows下安装apidocjs

    1. 下载Node.js官方Windows版程序:   https://nodejs.org/download/   从0.6.1开始,Node.js在Windows平台上提供了两种安装方式,一是.M ...

  2. Sping Boot入门到实战之实战篇(二):一些常用功能的Spring Boot Starters

    包含功能 阿里云消息服务MNS 阿里云消息队列服务(即时消息.延迟消息.事务消息) AOP日志 基于MyBatis通用Mapper及DRUID的数据库访问 dubbo支持 错误处理 七牛图片服务 re ...

  3. mybatis的拦截器及分页机制

    https://blog.csdn.net/ssuperlg/article/details/79847889

  4. 深入Asyncio(九)异步生成器

    Async Generators:yield inside async def functions 如果在async def中使用yield会发生什么,答案就是生成一个异步生成器函数,如果有生成器.协 ...

  5. 知识复习(LDT+TSS+GATE+INTERRUPT)

    [1]README 1.0)由于实现进程的切换任务,其功能涉及到 LDT + TSS +GATE + INTERRUPT:下面我们对这些内容进行复习: 1.1) source code from or ...

  6. KVC基本使用

    首先,创建两个类.person类和book类.如图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/ ...

  7. 设计TCP服务器的规则

    设计TCP服务器,采用如下规则: 1.正等待连接请求的一端有一个固定长度的连接队列,该队列中的连接已被TCP接受(完成三次握手),但还没有被应用层接受.注意:TCP接受一个连接是将其放入这个队列,而应 ...

  8. mysql系列之1.mysql基础

    非关系型(NOSQL)数据库 键值存储数据库: memcached  /  redis  /  memcachedb  /  Berkeley db 列存储数据库: Cassandra  /  Hba ...

  9. 十分钟git-服务器搭建ssh登陆

    QQ820688215 微信公众号: 1首先,创建一个操作系统用户 git,并为其建立一个 .ssh 目录. $ sudo adduser git $ su git $ cd $ mkdir .ssh ...

  10. Bitmaps

    核心知识点: 1.Bitmaps是一种特殊的“数据结构”,实质上是一个字符串,操作单元是位. 2.命令: a.setbit:设置值,只能存储0和1,适用二元判断类型 b.getbit:获取值 c.bi ...