PAT-A 真题- 1076. Forwards on Weibo

发布于 / PAT-甲级 / 0 条评论

原题干:

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

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

题目大意:

在微博中,每个用户可能被多个用户所关注。当该用户发布一条消息的时候,他的直系粉丝就会看到,并且会转发。如果这个人转发了,转发者的粉丝又会转发.......

在这里,我们称直系粉丝为博主第一层粉丝;直系粉丝的粉丝为博主的第二层粉丝;直系粉丝的粉丝的粉丝为第三层......

第一行给出总用户数N和层数限制L,接着给出N行,分别是

关注者的数量  关注者1  关注者2  ......

最后一行给出

要查询的数量  查询者1  查询者2

要求输出在层数限制内转发的人数。

乍一眼一看,可以逐行读入数据,像这样:

a[1] = [2, 3, 4];
a[2] = [];
a[3] = [5, 6];
......

但是如果这样读数据,在搜索的时候就会很麻烦。这道题给出的是用户与关注者的关系,如果按照

"博主->可以转发自己微博的粉丝->可以转发自己微博的粉丝的粉丝"

这样的顺序进行建表,问题会简单很多。

因为和层次有较强的关系,这道题建议使用BFS算法。

代码如下:

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define MAXN 1010
typedef struct{
    int id, layer;  //编号、层号
} Node;
vector<Node> G[MAXN];
int user_cnt, layer_limit;
bool visited[MAXN] = {0};
/*BFS算法会修改数组信息,这里要初始化数组。*/
void init(){
    for(int i = 1; i <= user_cnt; i++)
        visited[i] = 0;
}

int BFS(int query){
    int cnt = 0;
    queue<Node> Q;
    Q.push({query, 0});
    visited[query] = true;
    while(!Q.empty()){
        Node top = Q.front();
        Q.pop();
        for(int i = 0; i < G[top.id].size(); i++){
            int now_id = top.id;
            Node next = G[now_id][i];
            next.layer = top.layer + 1;
            if((!visited[next.id]) && (next.layer <= layer_limit)){
                Q.push(next);
                visited[next.id] = true;
                cnt++;
            }
        }
    }
    return cnt;
}

int main(){
    cin >> user_cnt >> layer_limit;
    for(int i = 1; i <= user_cnt; i++){
        int cnt, tmp;
        cin >> cnt;
        while(cnt--){
            cin >> tmp;
            G[tmp].push_back({i,0});
        }
    }
    int cnt, tmp;
    cin >> cnt;
    while(cnt--){
        init();
        cin >> tmp;
        cout << BFS(tmp) << endl;
    }
    return 0;
}

转载原创文章请注明,转载自: 斐斐のBlog » PAT-A 真题- 1076. Forwards on Weibo
目前还没有评论,快来抢沙发吧~