PAT_A1076#Forwards on Weibo

Source:

PAT A1076 Forwards on Weibo (30 分)

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 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:

Attention:

  • 题目给的是关注列表,消息传播时的方向应该是相反的;
  • 最后一个测试点的数据量很大,用DFS会爆栈,BFS剪枝才能通过;

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<=6
11 接下来N行,用户i关注的总人数W[i]及其各个编号
12 接下来一行,给出查询次数K,和要查询的各个编号
13 输出;
14 L层级内可获得的最大转发量
15 
16 基本思路:
17 层次遍历
18 */
19 #include<cstdio>
20 #include<queue>
21 #include<vector>
22 #include<algorithm>
23 using namespace std;
24 const int M=1e3+10,INF=1e9;
25 int vis[M],layer[M],L,n;
26 vector<int> adj[M];
27 
28 int BFS(int u)
29 {
30     fill(vis,vis+M,0);
31     fill(layer,layer+M,1);
32     queue<int> 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<adj[u].size(); i++){
43             if(vis[adj[u][i]]==0){
44                 vis[adj[u][i]]=1;
45                 q.push(adj[u][i]);
46                 layer[adj[u][i]]=layer[u]+1;
47                 sum++;
48             }
49         }
50     }
51     return sum;
52 }
53 
54 int main()
55 {
56 #ifdef ONLINE_JUDGE
57 #else
58     freopen("Test.txt", "r", stdin);
59 #endif // ONLINE_JUDGE
60 
61     int k,v;
62     scanf("%d%d", &n,&L);
63     for(int i=1; i<=n; i++)
64     {
65         scanf("%d", &k);
66         for(int j=0; j<k; j++)
67         {
68             scanf("%d", &v);
69             adj[v].push_back(i);
70         }
71     }
72     scanf("%d", &k);
73     while(k--)
74     {
75         scanf("%d", &v);
76         printf("%d\n", BFS(v));
77     }
78 
79     return 0;
80 }

 

posted @ 2019-05-19 20:09  林東雨  阅读(254)  评论(0编辑  收藏  举报