PAT_A1076#Forwards on Weibo
Source:
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 thatuser[i]
follows; anduser_list[i]
is a list of theM[i]
users that followed byuser[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:
- 图的存储和遍历
- 广度优先搜索(Breadth First Search)
- queue(C++ STL)
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的更多相关文章
- 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 ...
- 1076 Forwards on Weibo (30 分)
1076 Forwards on Weibo (30 分) Weibo is known as the Chinese version of Twitter. One user on Weibo ma ...
- PAT甲级1076. Forwards on Weibo
PAT甲级1076. Forwards on Weibo 题意: 微博被称为中文版的Twitter.微博上的一位用户可能会有很多关注者,也可能会跟随许多其他用户.因此,社会网络与追随者的关系形成.当用 ...
- 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 ...
- 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 ...
- 1076. Forwards on Weibo (30)
时间限制 3000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Weibo is known as the Chinese v ...
- 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 ...
- 1076. Forwards on Weibo (30) - 记录层的BFS改进
题目如下: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, a ...
- A1076. Forwards on Weibo
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...
随机推荐
- 洛谷—— P2733 家的范围 Home on the Range
https://www.luogu.org/problem/show?pid=2733 题目背景 农民约翰在一片边长是N (2 <= N <= 250)英里的正方形牧场上放牧他的奶牛.(因 ...
- BZOJ——T 2097: [Usaco2010 Dec]Exercise 奶牛健美操
http://www.lydsy.com/JudgeOnline/problem.php?id=2097 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: ...
- DTrace Probes In MySQL 自定义探针
Inserting user-defined DTrace probes into MySQL source code is very useful to help user identify the ...
- 装饰器 转载自 http://www.cnblogs.com/huxi/archive/2011/03/01/1967600.html
今天来讨论一下装饰器.装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等.装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量函数中与函数 ...
- HDU 4360
题意很好理解. 由于点是可以重复到达的,但可能每次经过路径的标志不一样,所以可以设每个点有四种状态"L”,'O','V','E'.然后按这些状态进行求最短路,当然是SPFA了. #inclu ...
- java 效率编程 的一些小知识点
1.在程序中若出现字符串连接的情况.请使用StringBuffer取代String,这样能够降低多次创建String以及垃圾回收所带来的内存消耗 2.尽量使用局部变量. 调用方法时传递的參数以及调用中 ...
- Linux IPC之共享内存C 事例
Linux IPC之共享内存 标签: linuxrandomnull工作 2011-08-25 11:52 4123人阅读 评论(0) 收藏 举报 分类: Linux(3) 读书札记(3) 版权 ...
- Delphi7中的函数与过程(Function and Procedure)
1.锁住空间的位置,可以选择Edit--->Lock component ,也可以在窗体设计面板下面找到组件排版功能栏,第二排里面有个带锁的图标,表示组件可以被锁住.点击一下,组件的大小和位置就 ...
- B1607 [Usaco2008 Dec]Patting Heads 轻拍牛头 数学
今天净做水题了,这个题还不到十五分钟就搞定了,思路特别简单,就是直接按照线性求因子个数的思路就行了. 题干: Description 今天是贝茜的生日,为了庆祝自己的生日,贝茜邀你来玩一个游戏. 贝茜 ...
- 操作系统-虚拟机-百科:VM
ylbtech-操作系统-虚拟机-百科:VM 虚拟机(Virtual Machine)指通过软件模拟的具有完整硬件系统功能的.运行在一个完全隔离环境中的完整计算机系统. 虚拟系统通过生成现有操作系统的 ...