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 ...
随机推荐
- F - True Liars 带权并查集
After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally cast ...
- 循环A表,根据查询结果,更新A表字段
create or replace procedure prc_user_xtzx_match(p_flag out varchar2) IS xingming_match_loginname ); ...
- HDU 5241 上海大都会 F题
留意到,每一种语言的情况其实是独立的,也就是说,每一种语言的集合的包含的情况都是符合要求的.一种语言在图上可以有32种情况(由数据2知),所以,总的数就是32^n import java.util.* ...
- 【转】php ob_start()、ob_end_flush和ob_end_clean()多级缓冲
原文:https://my.oschina.net/CuZn/blog/68650 当php.ini配置文件中的 设置开启的时候,就相当于PHP已经打开了最顶层的 一级缓存 (等价于调用了一次 ob ...
- CSS3:box-sizing:不再为盒子模型而烦恼
题外话: W3C奉行的标准,就是content-box,就是须要计算边框,填充还有内容的;可是就我个人而言, 比較喜欢的是传统IE6时候的怪异模式,不用考虑容器是否会被撑开(打乱布局); 盒子模型差异 ...
- WCF学习笔记——不支持内容类型 text/xml; charset=utf-8
我在使用WCF的时候,客户端运行报错: 不支持内容类型 text/xml; charset=utf-8 原因是WCF服务做了修改.刷新客户端的服务引用,问题消失 =================== ...
- B5090 组题 二分答案
bzoj有毒,看不了自己哪错了...根本没法debug. 我到现在还是不知道自己代码为什么会T,二分次数也加限制了,但是还是T...救命啊!!! 题干: Description 著名出题人小Q的备忘录 ...
- bzoj4197 [Noi2015]寿司晚宴——状压DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4197 首先,两个人选的数都互质可以看作是一个人选了一个数,就相当于选了一个质因数集合,另一个 ...
- Human Gene Functions(dp)
http://poj.org/problem?id=1080 #include <stdio.h> #include <stdlib.h> #include <strin ...
- css3 选择器 权重问题 (第一部分)
其实我现在写的这些博文笔记都是我很早之前学习的时候所写的笔记,只不过之前是没有写博客的习惯,所以都是写在word文档中个人需要的时候看而已.最近刚刚开了博客,所以想将自己的笔记贴到博客. 但是现在看来 ...