【剑指紫金港】1076 Forwards on Weibo BFS和DFS我都可

A 1076 Forwards on Weibo

题目链接

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 levels of indirect followers are counted.

Input

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

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

题目大意

给出n个人及其粉丝(后继节点)。一个人发微博,这条微博会被他的所有粉丝,以及他的粉丝的所有粉丝…以及…看到(以某个人为根节点,他的所有子树的节点都能看到他的微博)。然后k个查询,查询k个人每人在深度L内最多能看到多少个人的更新微博(每个人在层次深度L内最多有多少个不同的前驱节点)

解题思路

用vis做标记,访问过的的不用访问了(去重),并且我们希望第一次访问某个节点时,该节点的深度尽可能浅,用bfs最佳,一开始没反应过来,dfs+set做了半天最后一个样例一直超时。

AC代码(bfs版117ms)
在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;

struct node{
    int data,lvl;
};

int vis[maxn],N,L,m,k,cnt;
vector<int> pre[maxn];

int bfs(int x,int level){
    int num=0;
    node now = {x,level};
    queue<node> q;
    q.push(now);
    while(!q.empty()){
        node next = q.front();
        q.pop();
        int p=next.data,high=next.lvl;
        for(int i=0,len=pre[p].size();i<len;i++){
            if(vis[pre[p][i]]!=cnt && high<L){
                vis[pre[p][i]]=cnt;
                num++;
                q.push({pre[p][i],high+1});
            }
        }
    }
    return num;
}

int main(){
    fill(vis,vis+maxn,0);
    scanf("%d%d",&N,&L);
    for(int i=1;i<=N;i++){
        scanf("%d",&m);
        while(m--){
            scanf("%d",&k);
            pre[k].emplace_back(i);
        }
    }
    scanf("%d",&m);
    for(cnt=1;cnt<=m;cnt++){
        scanf("%d",&k);
        vis[k]=cnt;
        printf("%d\n",bfs(k,0));
    }
    return 0;
}

AC代码(dfs仿bfs剪枝版522ms)
在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
int vis[maxn],high[maxn],N,L,m,k,now,num,cnt;
vector<int> pre[maxn];

void dfs(int x,int level){
    for(int i=0,len=pre[x].size();i<len;i++){
        if(vis[pre[x][i]]!=cnt){
            vis[pre[x][i]]=cnt;
            num++;
            if(level<L){
                high[pre[x][i]]=min(high[pre[x][i]],level+1);
                dfs(pre[x][i],level+1);
            }
        }else{
            if(level<L && level+1<high[pre[x][i]]){
                high[pre[x][i]]=level+1;
                dfs(pre[x][i],level+1);
            }
        }
    }
    return ;
}

int main(){
    fill(vis,vis+maxn,0);
    scanf("%d%d",&N,&L);
    for(int i=1;i<=N;i++){
        scanf("%d",&m);
        while(m--){
            scanf("%d",&k);
            pre[k].emplace_back(i);
        }
    }
    scanf("%d",&m);
    for(cnt=1;cnt<=m;cnt++){
        scanf("%d",&k);
        num=0;
        vis[k]=cnt;
        fill(high,high+N+1,L+1);
        dfs(k,1);
        printf("%d\n",num);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

征服所有不服

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值