博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT_A1076#Forwards on Weibo
阅读量:6974 次
发布时间:2019-06-27

本文共 2549 字,大约阅读时间需要 8 分钟。

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 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 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 33 2 3 402 5 62 3 12 3 41 41 52 2 6

Sample Output:

45

Keys:

  • 广度优先搜索(Breadth First Search)
  • queue(C++ STL)

Attention:

  • 题目给的是关注列表,消息传播时的方向应该是相反的;
  • 有关层数的要用宽搜,刚开始直接用深搜去解了,答案居然还是对的,出题老师也是很心机-,-

Code:

1 /* 2 Data: 2019-06-20 16:47:58 3 Problem: PAT_A1076#Forwards on Weibo 4 AC: 15:23 5  6 题目大意: 7 在微博上,当一位用户发了一条动态,关注他的人可以查看和转发这条动态; 8 现在你需要统计出某条动态最多可以被转发的次数(转发层级不超过L) 9 输入:10 第一行给出,人数N<=1e3(编号从1~N),转发层级L<=611 接下来N行,用户i关注的总人数W[i]及其各个编号12 接下来一行,给出查询次数K,和要查询的各个编号13 输出;14 L层级内可获得的最大转发量15 16 基本思路:17 层次遍历18 */19 #include
20 #include
21 #include
22 #include
23 using namespace std;24 const int M=1e3+10,INF=1e9;25 int vis[M],layer[M],L,n;26 vector
adj[M];27 28 int BFS(int u)29 {30 fill(vis,vis+M,0);31 fill(layer,layer+M,1);32 queue
q;33 q.push(u);34 vis[u]=1;35 int sum=0;36 while(!q.empty())37 {38 u = q.front();39 q.pop();40 if(layer[u] > L)41 return sum;42 for(int i=0; i

 

转载于:https://www.cnblogs.com/blue-lin/p/10890553.html

你可能感兴趣的文章
struts2请求处理过程源代码分析(1)
查看>>
java中的char类型
查看>>
Windows XP下,JDK环境变量配置
查看>>
RabbitMQ (四) 路由选择 (Routing)
查看>>
关于ExtJS在使用下拉列表框的二级联动获取数据
查看>>
SPRING3.X JSON 406 和 中文乱码问题
查看>>
多个class相同的input标签 获取当前值!方法!
查看>>
模板方法模式与策略模式的区别
查看>>
html5地理定位数据
查看>>
《JAVA-枚举》
查看>>
使用photoshop 10.0制作符合社保要求的照片
查看>>
Python下使用tarfile模块来实现文件归档压缩与解压
查看>>
思科交换机各类型中字母的意思?
查看>>
linux基础命令
查看>>
我的友情链接
查看>>
Nutanix CE on Lenovo W520 初探
查看>>
make执行过程
查看>>
Ansible源码解析 Inventory组概念
查看>>
数据备份学习
查看>>
替换空格
查看>>