PAT 1076Forwards on Weibo(30分)

PAT 1076Forwards 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 onlyLlevels 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; andL(≤6), the number of levels of indirect followers that are counted. Hence it is assumed that all the users are numbered from 1 toN. ThenNlines follow, each in the format:

M[i] user_list[i]

whereM[i](≤100) is the total number of people thatuser[i]follows; anduser_list[i]is a list of theM[i]users that followed byuser[i]. It is guaranteed that no one can follow oneself. All the numbers are separated by a space.

Then finally a positiveKis given, followed byKUserID's for query.

Output Specification:

For eachUserID, 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 onlyLlevels 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

思路

  • 利用bfs,在其中加入一个对于l的判断即可。

代码

#pragma warning (disable:4996)
#include 
#include 
#include 
#include 
#include 

using namespace std;

const int maxn = 1100;

int n, l, k;
bool inq[maxn];
int num[maxn];
struct Node
{
    int v;
    int layer;
};
vector Adj[maxn];


int  bfs(int s)
{
    int ans = 0;
    queue q;
    Node start;
    start.v = s;
    start.layer = 0;
    q.push(start);
    inq[start.v] = true;
    while (!q.empty())
    {
        Node now = q.front();
        int u = now.v;
        q.pop();
        for (int i = 0; i < Adj[u].size(); i++)
        {
            Node next = Adj[u][i];
            next.layer = now.layer + 1;
            if (inq[next.v] == false)
            {
                if (next.layer <= l)
                {
                    ans++;
                }
                q.push(next);
                inq[next.v] = true;
            }
        }
    }
    return ans;
}


int main()
{
    //freopen("test.txt", "r", stdin);

    cin >> n >> l;
    int follow;
    for (int i = 1; i <= n; i++)
    {
        cin >> follow;
        int id;
        Node follower;
        follower.v = i;
        for (int j = 1; j <= follow; j++)
        {
            cin >> id;
            Adj[id].push_back(follower);
        }
    }
    cin >> k;
    int id;
    for (int i = 1; i <= k; i++)
    {
        memset(inq, false, sizeof inq);
        cin >> id;
        int ans = bfs(id);
        cout << ans << endl;
    }

    //for (int i = 1; i <= n; i++)
    //{
    //    if (num[i] != 0)
    //    {
    //        cout << num[i] << endl;
    //    }
    //}

    return 0;
}

你可能感兴趣的:(图,算法,c++)