#1076. Forwards on Weibo【图论 + BFS】

原题链接

Problem Description:

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 L 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 N N ( ≤ 1000 \leq 1000 1000), the number of users; and L L L ( ≤ 6 \leq 6 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 N N. Then N N N lines follow, each in the format:

M[i] user_list[i]

where M[i] ( ≤ 100 \leq 100 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 K K is given, followed by K K 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 L 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

Problem Analysis:

首先要理解透彻题目的意思,题目将给定一些数据,表示每个人的关注情况,并且给定若干组询问,每组询问需要输出该人的微博的最大可能转发数量。

倘若一个人有若干个粉丝,这些粉丝将会是他的一级关注者,这些粉丝的粉丝是这个人的二级关注者,依次类推,我们需要找出这个人的 L L L 级关注者, 1 ∼ L 1\sim L 1L 级关注者的总人数就是需要输出的答案。

因此,建完图之后,每组询问我们都直接进行 BFS 遍历 L + 1 L + 1 L+1 层(因为询问的是 L L L 层关注者,所以需要统计 L + 1 L + 1 L+1 层),然后统计总人数输出即可。

Code

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

const int N = 1010, M = 100010;

int n, m;
int h[N], e[M], 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 start)
{
    
    
    queue<int> q;
    memset(st, 0, sizeof st);
    q.push(start);
    
    st[start] = true;

    int res = 0;
    for (int step = 0; step < m; step ++ ) // 只考虑 m 层
    {
    
    
        int sz = q.size(); // 上一层的人数
        res += sz;

        for (int i = 0; i < sz; i ++ ) // 把上一层所有能够扩展出来的点全部扩展进队列
        {
    
    
            int t = q.front();
            q.pop();

            for (int j = h[t]; ~j; j = ne[j])
            {
    
    
                int k = e[j];
                if (!st[k])
                {
    
    
                    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 cnt;
        scanf("%d", &cnt);
        while (cnt -- )
        {
    
    
            int x;
            scanf("%d", &x);
            add(x, i); // 表示 x 被 i 关注了,表示 x 的微博可能会被 i 转发
        }
    }

    int k;
    cin >> k;
    while (k -- )
    {
    
    
        int x;
        scanf("%d", &x);
        printf("%d\n", bfs(x));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/geraltofrivia123/article/details/121105500