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:

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

Code:

 1 /*
 2 Data: 2019-05-19 18:01:34
 3 Problem: PAT_A1076#Forwards on Weibo
 4 AC: 47:39
 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 #include<cstdio>
18 #include<algorithm>
19 #include<queue>
20 using namespace std;
21 const int M=1e3+10,INF=1e9;
22 int grap[M][M],vis[M],n,L;
23 
24 void BFS(int u)
25 {
26     vis[u]=0;
27     queue<int> q;
28     q.push(u);
29     int ans=-1;
30     while(!q.empty())
31     {
32         u=q.front();q.pop();
33         if(vis[u]>L)
34             break;
35         ans++;
36         for(int v=1; v<=n; v++)
37         {
38             if(vis[v]==-1 && grap[u][v]!=INF)
39             {
40                 vis[v] = vis[u]+1;
41                 q.push(v);
42             }
43         }
44     }
45     printf("%d\n", ans);
46 }
47 
48 int main()
49 {
50 #ifdef  ONLINE_JUDGE
51 #else
52     freopen("Test.txt", "r", stdin);
53 #endif // ONLINE_JUDGE
54 
55     scanf("%d%d", &n,&L);
56     fill(grap[0],grap[0]+M*M,INF);
57     int v,k;
58     for(int i=1; i<=n; i++)
59     {
60         scanf("%d", &k);
61         for(int j=0; j<k; j++)
62         {
63             scanf("%d", &v);
64             grap[v][i]=1;
65         }
66     }
67     scanf("%d", &k);
68     for(int i=0; i<k; i++)
69     {
70         fill(vis,vis+M,-1);
71         scanf("%d", &v);
72         BFS(v);
73     }
74 
75     return 0;
76 }

猜你喜欢

转载自www.cnblogs.com/blue-lin/p/10890553.html
今日推荐