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 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 #include20 #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