【PAT】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.

翻译:微博被认为是中国版推特。一个微博用户可能有许多粉丝,并且也会粉其他用户。这样粉丝之间的关系就组成了一张社交网。当一个用户在微博上发表了一篇博文,所有他/她的粉丝都可以浏览和转发他/她的博文,这样他们的粉丝也可以继续转发。现在给你一张社交网,你需要计算任何一个用户的最大潜在转发次数。假设只有L层间接转发者会被计算。

INPUT FORMAT

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 are 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.

每个输入文件包含一组测试数据。对于每组输入数据,第一行包括两个正整数:N(<=1000),用户数;和L(<=6),代表只有L层间接转发者会被计算。因此假设所有的用户被标记为1到N。接着N行,每行按照以下格式:
M[i] user_list[i]
M[i] (<=100)代表user[i]粉的总人数;user_list[i]代表他粉的人的序列。数据保证没有人可以自粉。所有数字之间用空格隔开。

OUTPUT FORMAT

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.

翻译:对于每个UserID,你需要输出一行该用户可以触发的最大潜在转发次数,假设所有人最初看到博文时都会立刻转发,并且只有L层间接转发者会被计算。


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


解题思路

这道题用宽度优先搜索最好,可以避免出现重复搜索的问题,很贴合题目要求。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#define INF 99999999
using namespace std;
int N,L,K;
vector<int>G[1010];
typedef pair<int,int> P;
int v[1010];
int bfs(int s){
    queue<P>q;
    int ccount=0;
    memset(v,0,sizeof(v));
    q.push(P(0,s));
    v[s]=1;
    while(!q.empty()){
        P temp=q.front();q.pop();
        int time=temp.first,next=temp.second;
        if(time>=L)break;
        for(int i=0;i<G[next].size();i++){
            if(!v[G[next][i]]){
                v[G[next][i]]=1;
                ccount++;
                q.push(P(time+1,G[next][i]));
            }
        }   
    } 
    return ccount;
}
int main(){
    scanf("%d%d",&N,&L);
    int M,a;
    for(int i=1;i<=N;i++){
        scanf("%d",&M);
        for(int j=0;j<M;j++){
            scanf("%d",&a);
            G[a].push_back(i); 
        } 
    }
    scanf("%d",&K);
    for(int i=0;i<K;i++){
        scanf("%d",&a);
        printf("%d\n",bfs(a));
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值