• PAT 甲级 1076 Forwards on Weibo (30分)(bfs较简单)


    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 (≤), the number of users; and L (≤), 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] (≤) 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 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
    
    作者: CHEN, Yue
    单位: 浙江大学
    时间限制: 3000 ms
    内存限制: 64 MB
    代码长度限制: 16 KB

    题意:

    微博在中国非常盛行,我们可以被其他人所关注,也可以关注其他人(注此处应用有向图),我们只能浏览被关注者所发表的微博。问题:当一位用户发表一篇微博时,他的每一位粉丝都会转发其内容,而粉丝的粉丝也会转发,以此类推,问,L次后,这篇微博的内容会被多少人浏览?
    输入N,L(N为微博用户数,L为关系层数)。然后输入每个用户(随后第i行对应对第i位)的关注的用户数目n,再输入n位被关注者的id(对应行号i)。最后,输入数字K,同时,输入K位你要查询用户的ID(即序号)。
    输出要查询的用户每发一篇微博,经过L轮转发后的浏览次数。
    思路分析:通过输入构建有向图,经过广度优先搜索BFS,遍历经过L层的粉丝。
    ————————————————
    版权声明:本文为CSDN博主「yzh1994414」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/yzh1994414/article/details/78229568

    题解:

    bfs,队列里存结构体,包含层数d,用f数组标记是否以算过。

    自己要标记,但不用计数 。

    没有算过的才需要放入队列,否则会内存超限。

    AC代码:

    #include<bits/stdc++.h>
    using namespace std;
    int n,l;
    struct node{
        int p;
        int d;
    };
    int f[1005];
    int s=0;
    queue<node>q;
    vector<int>v[1005];
    int num;
    int main(){
        cin>>n>>l;
        for(int i=1;i<=n;i++){
            cin>>num;
            for(int j=1;j<=num;j++){
                int x;
                cin>>x;
                v[x].push_back(i);
            }
        }
        cin>>num;
        for(int i=1;i<=num;i++){
            node a;
            cin>>a.p;
            s=0;
            a.d=0;
            memset(f,0,sizeof(f));
            f[a.p]=1;//自己要标记,但不用计数
            q.push(a);
            while(!q.empty()){
                a=q.front();
                q.pop();
                if(a.d>=l) continue;
                for(int j=0;j<v[a.p].size();j++){
                    int x=v[a.p].at(j);
                    if(!f[x]){//没有算过的才需要放入数组,否则会内存 
                        f[x]++;
                        s++;
                        node b;
                        b.d=a.d+1;
                        b.p=x;
                        q.push(b);
                    }
                }
            }
            cout<<s<<endl;
        }
        return 0;
    } 
  • 相关阅读:
    格式布局
    tp框架之文件上传
    tp框架之验证码
    tp框架之自动验证表单
    tp框架之留言板练习
    tp框架之session
    tp框架之登录验证
    tp框架之函数调用
    tp框架之分页与第三方类的应用
    tp框架之AJAX
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/11991344.html
Copyright © 2020-2023  润新知