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. 计算机与linux操作系统的发展

    一.计算机 (一)计算机的概念 1.概念:计算机(computer)俗称电脑,是一种用于高速计算的电子计算机器,可以进行数值计算,又可以进行逻辑计算,还具有存储记忆功能.是能够按照程序运行,自动.高速 ...

  2. HTML5:防止页面在移动设备上缩放

    在制作网页时,如果对移动设备有做兼容设计的话,通常是不希望页面在移动设备能够被缩放.这样可以防止原先设计好的样式被破坏.要做到这一点,只需要在网页的head部分加入如下语句即可: <!-- 屏蔽 ...

  3. HDU 5393

    [background] 保研的事终于告一段落了,之后去北京折腾了一段时间,本以为会在那里实习一个月,谁知道刚去ICT,心中就各种反感,可能是因为LP的态度吧,否则我可能会留在那里读研也说不定.花了两 ...

  4. wait()方法写在while循环中可以在线程接到通知后再一次判断条件

    wait()方法写在while循环中可以在线程接到通知后再一次判断条件 synchronized public String pop() { String returnValue = "&q ...

  5. $().attr()的使用方法 &amp;&amp; $().html()与$().text()的差别

    <1>$().attr()的使用方法 </pre><pre class="html" name="code"><htm ...

  6. ant+jmeter中build.xml配置详解

  7. mongoDB学习笔记——在C#中查询

    1.下载安装 想要在C#中使用MongoDB,首先得要有个MongoDB支持的C#版的驱动.C#版的驱动貌似有很多种,如官方提供的samus. 实现思路大都类似.这里我们用官方提供的mongo-csh ...

  8. CMDBuild安装

    近日来,老板要在内部部署一套IT资产管理系统,要笔者去调研一下,测试了GLPI.OCSNG(没记错吧)和CMDBuild之后,发现还是CMDBuild的功能较为强大,虽然暂时不具备SNMP之类的工具, ...

  9. SVM中的线性分类器

    线性分类器: 首先给出一个非常非常简单的分类问题(线性可分),我们要用一条直线,将下图中黑色的点和白色的点分开,很显然,图上的这条直线就是我们要求的直线之一(可以有无数条这样的直线)     假如说, ...

  10. Python开发利器PyCharm 2.7附注册码

    PyCharm 2.7 下载 http://download.jetbrains.com/python/pycharm-2.7.2.exe 激活注册 user name:EMBRACE key: 14 ...