Source:

PAT A1076 Forwards on Weibo (30 分)

Description:

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 (≤), the number of users; and L (≤), 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] (≤) 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

Keys:

Attention:

  • 题目给的是关注列表,消息传播时的方向应该是相反的;
  • 最后一个测试点的数据量很大,用DFS会爆栈,BFS剪枝才能通过;

Code:

 /*
Data: 2019-06-20 16:47:58
Problem: PAT_A1076#Forwards on Weibo
AC: 15:23 题目大意:
在微博上,当一位用户发了一条动态,关注他的人可以查看和转发这条动态;
现在你需要统计出某条动态最多可以被转发的次数(转发层级不超过L)
输入:
第一行给出,人数N<=1e3(编号从1~N),转发层级L<=6
接下来N行,用户i关注的总人数W[i]及其各个编号
接下来一行,给出查询次数K,和要查询的各个编号
输出;
L层级内可获得的最大转发量 基本思路:
层次遍历
*/
#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int M=1e3+,INF=1e9;
int vis[M],layer[M],L,n;
vector<int> adj[M]; int BFS(int u)
{
fill(vis,vis+M,);
fill(layer,layer+M,);
queue<int> q;
q.push(u);
vis[u]=;
int sum=;
while(!q.empty())
{
u = q.front();
q.pop();
if(layer[u] > L)
return sum;
for(int i=; i<adj[u].size(); i++){
if(vis[adj[u][i]]==){
vis[adj[u][i]]=;
q.push(adj[u][i]);
layer[adj[u][i]]=layer[u]+;
sum++;
}
}
}
return sum;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int k,v;
scanf("%d%d", &n,&L);
for(int i=; i<=n; i++)
{
scanf("%d", &k);
for(int j=; j<k; j++)
{
scanf("%d", &v);
adj[v].push_back(i);
}
}
scanf("%d", &k);
while(k--)
{
scanf("%d", &v);
printf("%d\n", BFS(v));
} return ;
}

PAT_A1076#Forwards on Weibo的更多相关文章

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

  2. 1076 Forwards on Weibo (30 分)

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

  3. PAT甲级1076. Forwards on Weibo

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

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

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

  6. 1076. Forwards on Weibo (30)

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

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

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

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

  9. A1076. Forwards on Weibo

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

随机推荐

  1. js 实现栈的结构

    js实现一个栈的数据结构 首先了解一下什么是栈,栈是一个后进先出的一种数据结构,执行起来效率比较高. 对于栈主要包括一些方法,弹出栈pop(),弹出栈顶元素,并删除该元素:压入栈push(),向栈中压 ...

  2. maven 的plugin 的使用

    mvn [plugin-name]:[goal-name] mvn compiler:compile 这里写的十分详细: https://www.tutorialspoint.com/maven/ma ...

  3. WebGIS开发技术杂谈

    WebGIS项目的开发主要是B/S架构.最流行的是clientjavascript,server端java. 另外还有flexclient. client主要完毕用户交互.向server端发送请求并传 ...

  4. 【iOS开发系列】NSObject方法介绍

    NSObject是OC中的基类,全部类都继承于此,这里面也给我们提供了非常多与"类"和"方法"相关的方法,本文将解说几个非常有用的方法. 正文: Person. ...

  5. EJB是什么鸟东西?

    到底EJB是什么 到底EJB是什么?被口口相传的神神秘秘的,百度一番,总觉得没有讲清楚的,仍觉得一头雾水.百度了很久,也从网络的文章的只言片语中,渐渐有了头绪. 用通俗话说,EJB就是:"把 ...

  6. ssdb底层实现——ssdb底层是leveldb,leveldb根本上是skiplist(例如为存储多个list items,必然有多个item key,而非暴力string cat),用它来做redis的list和set等,势必在数据结构和算法层面上有诸多不适

    我已经在用ssdb的hash结构,存储了很多数据了,但是我现在的用法正确吗? 我使用hash结构合理吗? 1. ssdb数据库说是类似redis,而且他们都有hash结构,但是他们的命名有点不同,ss ...

  7. pjax使用小结

    简介 虽然传统的ajax方式可以异步无刷新改变页面内容,但无法改变页面URL,因此有种方案是在内容发生改变后通过改变URL的hash的方式获得更好的可访问性(如https://liyu365.gith ...

  8. BZOJ 4753 二分+树形DP

    思路: 先二分答案 f[x][j]表示在x的子树里选j个点 f[x][j+k]=max(f[x][j+k],f[x][j]+f[v[i]][k]); 初始化 x!=0 -> f[x][1]=p[ ...

  9. maven添加本地jar包的方法

    1.将一个本地的jar包随便放在一个放入本地文件夹中 (文件夹位置 和 jar包名称都随意) 例:F:\java\repository\a 文件夹下,名称为:icepdf-core-6.0.jar 2 ...

  10. RabbitMQ 官方NET教程(二)【工作队列】

    这篇中我们将会创建一个工作队列用来在工作者(consumer)间分发耗时任务. 工作队列的主要任务是:避免立刻执行资源密集型任务和避免必须等待其完成.相反地,我们进行任务调度:我们把任务封装为消息发送 ...