【PAT甲级 - C++题解】1076 Forwards on Weibo

✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
📚专栏地址:PAT题解集合
📝原题地址:题目详情 - 1076 Forwards on Weibo (pintia.cn)
🔑中文翻译:微博转发
📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪

1076 Forwards on Weibo

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

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 来计算思路会更简洁一些,具体思路如下:

  1. 先输入每个用户的关注者,这里用链式前向星来存储邻接表。
  2. 对每个要查询的用户进行 bfs 计算。这里需要用到队列,初始化完后进行每一层的计算,因为最大计算 L 层,所以遍历次数不大于 L 。另外需要注意的是,发出帖子的那个人在计算过程中被当做第一层加入答案,而我们遍历次数不大于 L ,所以最终返回答案时要加上下一层的数量并减去 1 即不计算发帖的那个人。
  3. 最后,输出返回回来的最大转发数。

如下图所示,假设发帖的人的编号为 1 ,且最多只计算 2 层:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dHhlJEkp-1667120025380)(PAT 甲级辅导.assets/20.png)]

另外,图中只是显示了理想情况,可能会出现两人之间互相关注的情况,我们在 bfs 的过程中会有个 st 数组来记录哪些人已经被计算过,故不会出现重复计算的情况。

代码
#include<bits/stdc++.h>
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值