✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
专栏地址:PAT题解集合
原题地址:题目详情 - 1076 Forwards on Weibo (pintia.cn)
中文翻译:微博转发
专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
❤️如果有收获的话,欢迎点赞收藏,您的支持就是我创作的最大动力
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:
M[i] user_list[i]
where
M[i]
(≤100) 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 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
当用户在微博上发布帖子时,他/她的所有关注者都可以查看并转发他/她的帖子,然后这些人的关注者可以对内容再次转发…
现在给定一个社交网络,假设只考虑 L
层关注者,请你计算某些用户的帖子的最大可能转发量。
这题数据量稍微比较大,所以我们用邻接表来存每条边,并且用 bfs
来计算思路会更简洁一些,具体思路如下:
bfs
计算。这里需要用到队列,初始化完后进行每一层的计算,因为最大计算 L
层,所以遍历次数不大于 L
。另外需要注意的是,发出帖子的那个人在计算过程中被当做第一层加入答案,而我们遍历次数不大于 L
,所以最终返回答案时要加上下一层的数量并减去 1
即不计算发帖的那个人。如下图所示,假设发帖的人的编号为 1
,且最多只计算 2
层:
另外,图中只是显示了理想情况,可能会出现两人之间互相关注的情况,我们在 bfs
的过程中会有个 st
数组来记录哪些人已经被计算过,故不会出现重复计算的情况。
#include
using namespace std;
const int N = 1010, M = 100010;
int n, m;
int e[M], h[N], ne[M], idx;
bool st[N];
//链式前向星存储邻接表
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
//深搜求最大转发数
int bfs(int x)
{
//初始化
memset(st, 0, sizeof st);
st[x] = true;
queue<int> q;
q.push(x);
//最多转发m层
int res = 0;
for (int i = 0; i < m; i++)
{
int sz = q.size();
res += sz; //一层一层人数的加
//再遍历该层每个人的关注者
while (sz--)
{
int u = q.front();
q.pop();
for (int j = h[u]; ~j; j = ne[j])
{
int k = e[j];
if (st[k]) continue;
st[k] = true;
q.push(k);
}
}
}
//第一层即发帖子的人是不算进转发数的,故第一层不算
return res + q.size() - 1;
}
int main()
{
scanf("%d%d", &n, &m);
//输入每个用户关注的信息
memset(h, -1, sizeof h);
for (int i = 1; i <= n; i++)
{
int k;
scanf("%d", &k);
while (k--)
{
int x;
scanf("%d", &x);
add(x, i);
}
}
//进行查询
int k;
scanf("%d", &k);
while (k--)
{
int x;
scanf("%d", &x);
printf("%d\n", bfs(x));
}
return 0;
}