A1076 Forwards on Weibo (30 point(s))

BFS应用,计算L层内个数

1. 原文

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 (≤1000), the number of users; and L (≤6), 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:

1
M[i] user_list[i]

where M[i] (≤100) 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 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:

1
2
3
4
5
6
7
8
9
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:

1
2
4
5

2. 解析

i : 1-> N , 记录的是关注的人 v[a].push_back(i);

层序,在l层内统计个数

1
2
3
4
if (level<=l)
{
cnt++;
}

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
vector<int> v[1010];
bool visit[1010]={false};
int l;
int bfs(int root){
queue<int> q;
q.push(root);
int level=0;
int lastnode=root;
int nownode=0;
int cnt=0;
visit[root]=true;
while(!q.empty()){
int temp=q.front();
//printf("%d %d\n", temp,level);
q.pop();
for (int i = 0; i < v[temp].size(); ++i)
{
if (!visit[v[temp][i]])
{
q.push(v[temp][i]);
visit[v[temp][i]]=true;
nownode=v[temp][i];
}
}
if (level<=l)
{
cnt++;
}
if (lastnode==temp)
{
level++;
lastnode=nownode;
}
}
return cnt;
}
int main()
{
int n;
scanf("%d%d",&n,&l);
for (int i = 1; i <= n; ++i)
{
int m;
scanf("%d",&m);
for (int j = 0; j < m; ++j)
{
int a;
scanf("%d",&a);
v[a].push_back(i);
}
}
int k;
scanf("%d",&k);
int a;
for (int i = 0; i < k; ++i)
{
scanf("%d",&a);
fill(visit,visit+1010,false);
printf("%d\n",bfs(a)-1);
}

return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1076/
  • 发布时间: 2019-09-03 12:32
  • 更新时间: 2021-10-29 14:04
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!