【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
201 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
245 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
244 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
230 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
146 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
166 0
|
12月前
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
10月前
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
394 12
|
8月前
|
人工智能 机器人 编译器
c++模板初阶----函数模板与类模板
class 类模板名private://类内成员声明class Apublic:A(T val):a(val){}private:T a;return 0;运行结果:注意:类模板中的成员函数若是放在类外定义时,需要加模板参数列表。return 0;
210 0
|
8月前
|
存储 编译器 程序员
c++的类(附含explicit关键字,友元,内部类)
本文介绍了C++中类的核心概念与用法,涵盖封装、继承、多态三大特性。重点讲解了类的定义(`class`与`struct`)、访问限定符(`private`、`public`、`protected`)、类的作用域及成员函数的声明与定义分离。同时深入探讨了类的大小计算、`this`指针、默认成员函数(构造函数、析构函数、拷贝构造、赋值重载)以及运算符重载等内容。 文章还详细分析了`explicit`关键字的作用、静态成员(变量与函数)、友元(友元函数与友元类)的概念及其使用场景,并简要介绍了内部类的特性。
336 0