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

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

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

另外,图中只是显示了理想情况,可能会出现两人之间互相关注的情况,我们在 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;
}


目录
相关文章
|
C++
【PAT甲级 - C++题解】1040 Longest Symmetric String
【PAT甲级 - C++题解】1040 Longest Symmetric String
76 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
109 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
95 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
97 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
86 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
90 0
|
3天前
|
编译器 C语言 C++
类和对象的简述(c++篇)
类和对象的简述(c++篇)
|
1月前
|
C++ 芯片
【C++面向对象——类与对象】Computer类(头歌实践教学平台习题)【合集】
声明一个简单的Computer类,含有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,以及两个公有成员函数run、stop。只能在类的内部访问。这是一种数据隐藏的机制,用于保护类的数据不被外部随意修改。根据提示,在右侧编辑器补充代码,平台会对你编写的代码进行测试。成员可以在派生类(继承该类的子类)中访问。成员,在类的外部不能直接访问。可以在类的外部直接访问。为了完成本关任务,你需要掌握。
69 19
|
1月前
|
存储 编译器 数据安全/隐私保护
【C++面向对象——类与对象】CPU类(头歌实践教学平台习题)【合集】
声明一个CPU类,包含等级(rank)、频率(frequency)、电压(voltage)等属性,以及两个公有成员函数run、stop。根据提示,在右侧编辑器补充代码,平台会对你编写的代码进行测试。​ 相关知识 类的声明和使用。 类的声明和对象的声明。 构造函数和析构函数的执行。 一、类的声明和使用 1.类的声明基础 在C++中,类是创建对象的蓝图。类的声明定义了类的成员,包括数据成员(变量)和成员函数(方法)。一个简单的类声明示例如下: classMyClass{ public: int
51 13
|
1月前
|
编译器 数据安全/隐私保护 C++
【C++面向对象——继承与派生】派生类的应用(头歌实践教学平台习题)【合集】
本实验旨在学习类的继承关系、不同继承方式下的访问控制及利用虚基类解决二义性问题。主要内容包括: 1. **类的继承关系基础概念**:介绍继承的定义及声明派生类的语法。 2. **不同继承方式下对基类成员的访问控制**:详细说明`public`、`private`和`protected`继承方式对基类成员的访问权限影响。 3. **利用虚基类解决二义性问题**:解释多继承中可能出现的二义性及其解决方案——虚基类。 实验任务要求从`people`类派生出`student`、`teacher`、`graduate`和`TA`类,添加特定属性并测试这些类的功能。最终通过创建教师和助教实例,验证代码
53 5