PAT - 1076 Forwards on Weibo (30 分) 图的BFS遍历

40 篇文章 0 订阅
35 篇文章 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 followed by user[i]. It is guaranteed that no one can follow oneself. All the numbers are separated by a space.

读这一句话读了好久才读懂,i是从1开始递增的,M[i]代表第i个数follow的people, user_list[i]则代表是M[i]个用户,user[i] follow的用户。这里给出了三个数组,M[]是总数量数组,user_list[]是follower数组,存储着user follow的每一个用户,user[i]代表某一个用户。

其实意思很简单,先给出第i个数follow的用户总数量,再分别列出这些用户。注意,是M[i] follow的用户。

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

这道题基本上是自己直接写出来就AC的,非常的激动!!!! 

  1. BFS与DFS的区别: BFS是在队列里面循环,DFS是一层一层进去递归。
  2. Node里面加一个参数,并在每一层入队的时候直接更新。
  3. 由于是有向图,输入的时候指定方向为G[tmp][i]代表,tmp更新的时候,i会收到通知,这样比较直观(与follow的方向相反,是i follow tmp)
  4. 在每次BFS开始前,要重置inq数组和totalcnt。
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;

const int maxn = 1010;
int G[maxn][maxn] = {0};
bool inq[maxn] = {false};
int N, K;
int totalcnt = 0;
struct Node{
    int node, layer;
    Node(int _node, int _layer): node(_node), layer(_layer){}
};

void BFS(Node node){
    queue<Node> q;
    q.push(node);
    inq[node.node] = true;
    while(!q.empty()){
        int front = q.front().node;
        int curlayer = q.front().layer;
        if(curlayer >= K)
            break;
        q.pop();
        for(int i = 1 ;i <= N; i++){
            if(inq[i] == false && G[front][i] == 1 ){
                q.push(Node(i, curlayer + 1));
                inq[i] = true;
                totalcnt++;
            }
        }
    }
}


int main(){
    cin>>N>>K;
    for(int i = 1; i <= N; i++){
        int num;
        cin>>num;
        for(int j = 0; j < num;j++){
            int tmp;
            cin>>tmp;
            G[tmp][i] = 1;
        }
    }
    int outNum;
    cin>>outNum;
    for(int i = 0; i < outNum; i++)
    {
        int tmp;
        cin>>tmp;
        
       // for(int i = 1; i <= N;i++) //注意是数字序号是从1开始计数的!!! ‘AC的一瞬间,全身激动!!!太爽了’
        //    inq[i] = false;
        memset(inq, 0, sizeof(inq));
        totalcnt = 0;
        
        BFS(Node(tmp, 0));
        cout<<totalcnt<<endl;
        }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值