BFS_1076 Forwards on Weibo (30 分)

1076 Forwards on Weibo (30 分)

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 triger, 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层(最初发出者的L层),并且给出的数据是,每个第i个人跟随的人,那么我们储存粉丝数据时需要反向存储。要求求最大的转发数量(不包括本身)

我们最大拓展三层,所以使用dfs搜索的话,会有很大限度,不如直接使用bfs进行搜索,每次增加判断:搜索的层数,如果大于最大限度同样进行返回,相反需要将其粉丝算入转达队列,进行BFS计算

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <vector>
#include <algorithm>
#define INF 0x3f3f3f3f

using namespace std;
const int maxn = 1e3+5;
int N,L;
vector<int> vec[maxn];
bool used[maxn];
struct Node
{
    int pos,deep;
};
int bfs(int p)
{
    int ans = 0;
    memset(used,false,sizeof(used));
    queue<Node> que;
    Node temp{p,0},a;
    que.push(temp);
    while(!que.empty())
    {
        temp = que.front();
        que.pop();
        if(used[temp.pos] || temp.deep > L)continue;   //已经不符合题目转发条件
        used[temp.pos] = true;
        ans ++;
        for(int i = 0;i < vec[temp.pos].size();i ++)
        {
            a.pos = vec[temp.pos][i],a.deep = temp.deep + 1;
            que.push(a);
        }
    }
    return ans;

}
int main()
{
    int m,x;
    scanf("%d%d",&N,&L);
    for(int i =1 ;i <= N;i ++)
    {
        scanf("%d",&m);
        for(int j =0 ;j < m;j ++)
        {
            scanf("%d",&x);
            vec[x].push_back(i);
        }
    }
    scanf("%d",&m);
    for(int i = 0;i < m;i ++)
    {
        scanf("%d",&x);
        printf("%d\n",bfs(x)-1);  //-1去除本身
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/li1615882553/article/details/86307950
今日推荐