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 ...
随机推荐
- [bzoj1598][Usaco08Mar]牛跑步_A*_Dijkstra
牛跑步 bzoj-1598 题目大意:给你n个点,m条边的有向图.求从1到n的严格的第k短路. 注释:$1\le n\le 1000$,$1\le m \le 10,000$,$1\le k \le ...
- codevs 1700 施工方案第二季
1700 施工方案第二季 2012年市队选拔赛北京 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description c国边防 ...
- 微信 创建自定义菜单 向微信发起的post请求
微信 创建自定义菜单 向微信发起的post请求 Map<String, Object> res = new HashMap<String, Object>(); try { S ...
- SSH整合开发时Scope为默认时现象与原理
1.前提知识 1)scope默认值 进行SSH整合开发时,Struts2的action须要用spring容器进行管理,仅仅要涉及到类以bean的形式入到spring容器中.无论是xml配置还是使用注解 ...
- MySql解压版使用
1.解压 2.配置环境变量 3.新建空目录data,修改ini配置文件,修改basedir和datadir 4.管理员运行cmd,进入bin目录 5.mysql -install,如果提示错误,先my ...
- SharePoint 2013 改动表单认证登录页面
前 言 之前的博客我们介绍了怎样为SharePoint配置表单登陆,可是.登陆页面是丑.非常丑.非常丑.特别非常丑! 我们如今就介绍一下怎样定制SharePoint表单登陆页面! SharePoint ...
- hibernate4中oracle,sqlserver,mysql数据库的sql方言配置(SQL Dialects)
hibernate4中oracle,mysql,sqlserver数据库的sql方言配置(SQL Dialects) 数据库类型 Hibernate sql方言 DB2 org.hibernate.d ...
- Android shape自定义形状,设置渐变色
<?xml version="1.0" encoding="utf-8"?> <!-- android:shape=["rect ...
- Android updater-scripts(Edify Script)各函数详细说明【转】
本文转载自:http://blog.csdn.net/kwuwei/article/details/40616909 这是Android系统来运行updater-scripts的Edify语言的基本介 ...
- Expression Trees (C# and Visual Basic)
https://msdn.microsoft.com/en-us/library/bb397951.aspx Expression trees represent code in a tree-lik ...