[算法系列]-图的遍历-算题记录-1

158 阅读2分钟

这是我参与更文挑战的第16天,活动详情查看:更文挑战

Forwards on Weibo (30)

时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小)

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.

输入描述: 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.

输出描述: 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.

输入例子: 7 3 3 2 3 4 0 2 5 6 2 3 1 2 3 4 1 4 1 5 2 2 6

输出例子: 4 5

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

const int MAXN = 1010;

struct Node {
    int id;
    int layer;
};

vector<Node> adj[MAXN];
bool inque[MAXN] = {false};

int BFS(int s, int L){
    int numForwad = 0;
    queue<Node> que;
    Node start;
    start.id = s;
    start.layer = 0;
    que.push(start);
    inque[start.id] = true;
    while(!que.empty()){
        Node topNode = que.front();
        que.pop();
        int uid = topNode.id;
        for(int i = 0; i < adj[uid].size(); i++){
            Node next = adj[uid][i];
            next.layer = topNode.layer +1;
            if(inque[next.id] == false && next.layer <= L){
                que.push(next);
                inque[next.id] = true;
                numForwad++;
            }
        }
    }
    return numForwad;
}
int main(){
    Node user;
    int N, L, numFollow, idFollow;
    scanf("%d%d", &N,&L);
    for(int i = 1; i <= N; i++){
        user.id = i;
        scanf("%d", &numFollow);
        for(int j = 0; j < numFollow; j++){
            scanf("%d", &idFollow);
            adj[idFollow].push_back(user);
        }
    }
    int numQuery, s;
    scanf("%d", &numQuery);
    for(int i = 0; i < numQuery; i++){
        memset(inque, false, sizeof(inque));
        scanf("%d", &s);
        printf("%d\n", BFS(s, L));
    }
    return 0;
}

思考和总结

对于DFS和BFS其实已经没有什么好讲的了,每次都是做的时候忘了怎么做,看了之后有恍然大悟,说不过如此。 说到底,这还是个人的学习习惯问题,对于这种问题的方法只有一个,那就是保持复习的习惯,不管是背单词还是算法模板,只有隔一段时间就复习一遍才有可能真正的了然于心。尤其是对于算法这种抽象复杂的东西而言。