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;
} 

 

 

 

 

發佈了763 篇原創文章 · 獲贊 71 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章