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. 应用处理器AP概述

    移动终端芯片其它部分见"一站式了解智能终端处理器". 功能机时代,扩展手机特性是在基带芯片上进行.手段包含:升级基带芯片获得更强的计算能力.电路进行又一次设计以添加功能如照相机和S ...

  2. sprint3 【每日scrum】 TD助手站立会议第四天

    站立会议 组员 昨天 今天 困难 签到 刘铸辉 (组长) 和楠哥学习了通过AlarmManager 来实现闹钟,由于要用到BroadcastReceiver广播协议,所以正在学习中,暂时只是按照教程写 ...

  3. 35:字符串单词倒排 ReverseWords

    题目描述:对字符串中的所有单词进行倒排. 说明: 1.每个单词是以26个大写或小写英文字母构成: 2.非构成单词的字符均视为单词间隔符: 3.要求倒排后的单词间隔符以一个空格表示:如果原字符串中相邻单 ...

  4. XTUOJ 1176 I Love Military Chess(模拟)

     I Love Military Chess Accepted : 45   Submit : 141 Time Limit : 1000 MS   Memory Limit : 65536 KB ...

  5. iOS界面-仿网易新闻左侧抽屉式交互 续(添加新闻内容页和评论页手势)

     本文转载至  http://blog.csdn.net/totogo2010/article/details/8637430       1.介绍 有的博友看了上篇博文iOS界面-仿网易新闻左侧抽屉 ...

  6. EasyNVR H5无插件摄像机直播解决方案前端解析之:监控实时直播的四分屏的前端展示

    完成运行效果图: 一.四分屏展示样式布局 1.通过html.css等来进行样式排版 根据需求的四分屏的样式:来合理的划分出四个大块,分别用于放置四个播放器: 以四等分结构为例进行前端的排版: html ...

  7. Dijkstra的堆优化

    先附上一个例题:P3371 [模板]单源最短路径 一眼扫去,最短路... spfa可行,但是今天的主题是Dijkstra: #include<iostream> #include<a ...

  8. ubuntun下安装sublime text

    Sublime Text 3 是一款轻量级.跨平台的文本编辑器.可安装在ubuntu,Windows和MAC OS X上高级文本编辑软件,有一个专有的许可证,但该程序也可以免费使用,无需做逆向工程.如 ...

  9. eclipse 安装tomcat

  10. 【C语言天天练(十)】结构体

    引言:数据常常以成组的形式存在.在C中,使用结构能够把不同类型的值存放在一起. 结构的声明有两种 1.struct SIMPLE{ int a; char b; float c; };然后用标签SIM ...