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 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 trigger, 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 <stdio.h>
#include <algorithm>
#include <set>
//#include <string.h>
#include <vector>
//#include <math.h>
#include <queue>
#include <iostream>
#include <string>
using namespace std;
const int maxn = ;
const int inf = ;
int n,k,m,l; bool vis[maxn];
struct node{
int id,lvl;
};
vector<node> adj[maxn];
int bfs(int root){
fill(vis,vis+maxn,false);
int num=;
queue<node> q;
node st;
st.id = root;
st.lvl = ;
q.push(st);
vis[root]=true;
while(!q.empty()){
node now = q.front();
q.pop();
int u=now.id;
for(int i=;i<adj[u].size();i++){
node next = adj[u][i];
next.lvl = now.lvl+;
if(vis[adj[u][i].id]==false && next.lvl<=l){
q.push(next);
vis[next.id]=true;
num++;
}
}
}
return num;
}
int main(){
scanf("%d %d",&n,&l);
for(int i=;i<=n;i++){
scanf("%d",&k);
node user;
user.id = i;
for(int j=;j<k;j++){
scanf("%d",&m);
adj[m].push_back(user);
}
}
scanf("%d",&k);
for(int j=;j<k;j++){
scanf("%d",&m);
int res=bfs(m);
printf("%d\n",res);
}
}

注意点:图的多次bfs,要注意初始化vis,和dfs不同的是bfs的vis是入队一次就要设为true,而不是访问了才设为true,否则会多次访问到。记录层数需要结构体,和A1106 Lowest Price In Supply Chain 不同,1106需要两个队列来完成一层层保存,因为他是二叉树,而图会存在环,两个队列也可以,不过这题没必要。

PAT A1076 Forwards on Weibo (30 分)——图的bfs的更多相关文章

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

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

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

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

  4. 1076. Forwards on Weibo (30) - 记录层的BFS改进

    题目如下: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, a ...

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

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

  7. PAT 甲级 1072 Gas Station (30 分)(dijstra)

    1072 Gas Station (30 分)   A gas station has to be built at such a location that the minimum distance ...

  8. PAT 甲级 1080 Graduate Admission (30 分) (简单,结构体排序模拟)

    1080 Graduate Admission (30 分)   It is said that in 2011, there are about 100 graduate schools ready ...

  9. PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***

    1049 Counting Ones (30 分)   The task is simple: given any positive integer N, you are supposed to co ...

随机推荐

  1. 【协议】1、tcp,http,socket协议介绍

    1.TCP连接 手机能够使用联网功能是因为手机底层实现了TCP/IP协议,可以使手机终端通过无线网络建立TCP连接.TCP协议可以对上层网络提供接口,使上层网络数据的传输建立在“无差别”的网络之上. ...

  2. 漫画 | Servlet属于线程安全的吗?

    Servlet属于线程安全的吗? Servlet不是线程安全的 在JSP中,只有一行代码:<%=A+B %>,运行结果如何? jsp和servlet有什么关系? jsp一般被用在view层 ...

  3. linux学习笔记-目录结构(2)

    2./usr的意义与内容 依据FHS的基本定义,/usr里面放置的数据属于可分享的与不可变动的. usr是UNIX Software Resource的缩写,即UNIX操作系统软件资源所放置的目录,而 ...

  4. 【读书笔记】iOS-PhoneGap

    以前,用PhoneGap平台创建的应用在提交到AppStore中的时候可能会遇到一些问题,不过PhoneGap 0.8.0版本已经很好地解决了这个问题,而且苹果公司也允许将通过PhoneGap构建的应 ...

  5. SD从零开始67-70 后勤信息系统中的标准分析, 信息结构, 信息的更新规则, 建立统计数据

    SD从零开始67 后勤信息系统中的标准分析 标准分析中的报表Reporting in Standard Analyses 标准分析为高质量的表达和分析LIS中的数据基础提供了大量的功能: 当你决定了一 ...

  6. Nginx 反向代理工作原理简介与配置详解

    Nginx反向代理工作原理简介与配置详解   by:授客  QQ:1033553122   测试环境 CentOS 6.5-x86_64 nginx-1.10.0 下载地址:http://nginx. ...

  7. 修复cocos2dx的Label,WP8下不能换行的问题

    注:2014年12月23日有内存/性能优化更新,内容在下面分割线后 搞了几个小时,这个头疼的问题,我给出代码吧. 找到 libcocos2d/platform/winrt/CCFreeTypeFont ...

  8. vmare连接远程服务器的问题

    测试环境:两端都是VMware Workstation 12 Pro 1.需要共享虚拟机 在虚拟机上点击右键 -> Manage -> Share 后面按照操作设置 2.远程服务器的443 ...

  9. java调用python的惨痛史(无法获取环境变量)

    环境:java,was,python2.6,红帽linux,oracle,python用cx_Oracle        事情是这样的,有个需求,需要对数据库进行处理,简单说就是把数据取出来,用pyt ...

  10. 第七章 鼠标(CHECKER4)

    /*--------------------------------------------- CHECKER4.C -- Mouse Hit-Test Demo Program No.4 (c) C ...