• PAT甲级——A1076 Forwards on Weibo


    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

    使用广度优先遍历BFS
     1 #include <iostream>
     2 #include <vector>
     3 #include <queue>
     4 using namespace std;
     5 bool people[1010][1010];
     6 int N, L, K;
     7 int BFS(int v)//广度优先搜索
     8 {
     9     int num = 0;
    10     vector<int>level(N + 1, 0);
    11     vector<bool>visit(N + 1, false);
    12     queue<int>q;
    13     q.push(v);
    14     visit[v] = true;
    15     while (!q.empty())
    16     {
    17         v = q.front();
    18         q.pop();
    19         for (int i = 1; i <= N; ++i)
    20         {
    21             if (visit[i] == false && people[v][i] == true && level[v] < L)
    22             {
    23                 num++;
    24                 visit[i] = true;
    25                 level[i] = level[v] + 1;
    26                 q.push(i);
    27             }
    28         }
    29     }
    30     return num;
    31 }
    32 
    33 int main()
    34 {
    35     cin >> N >> L;
    36     fill(people[0], people[0] + 1010 * 1010, false);
    37     for (int i = 1; i <= N; ++i)
    38     {
    39         int a, m;
    40         cin >> m;
    41         for (int j = 1; j <= m; ++j)
    42         {
    43             cin >> a;
    44             people[a][i] = true;//请记住,存的是a的粉丝
    45         }
    46     }
    47     cin >> K;
    48     vector<int>test(K);
    49     for (int i = 0; i < K; ++i)
    50     {
    51         int a;
    52         cin >> a;
    53         cout << BFS(a) << endl;//使用广度优先搜索,即使用层序遍历
    54     }
    55     return 0;
    56 }

    使用深度遍历DFS

     1 #include <iostream>
     2 #include <vector>
     3 using namespace std;
     4 int N, L, K;
     5 vector<int> graph[1005];//
     6 bool visit[1005];//visit表示是否已被访问,person用于最后统计
     7 int layer[1005];//储存每个结点被遍历到时的层数
     8 void DFS(int v, int level) 
     9 {//深度优先遍历
    10     visit[v] = true;//当前结点置为已访问
    11     layer[v] = level;//更新被遍历时所处层数
    12     if (level != L)//还没有遍历到层数上限
    13         for (int i : graph[v])//遍历当前结点能够到达的结点
    14             if (!visit[i] || layer[i] > level + 1)//这个节点以前从未访问过或者这个节点当前被访问时的层数<layer数组中对应的层数
    15                 DFS(i, level + 1);//继续深度优先遍历
    16 }
    17 int main()
    18 {
    19     scanf("%d%d", &N, &L);
    20     for (int i = 1; i <= N; ++i) 
    21     {
    22         int num, a;
    23         scanf("%d", &num);
    24         while (num--) 
    25         {
    26             scanf("%d", &a);
    27             graph[a].push_back(i);
    28         }
    29     }
    30     scanf("%d", &K);
    31     while (K--) 
    32     {
    33         fill(visit + 1, visit + N + 1, false);
    34         fill(layer + 1, layer + N + 1, -1);
    35         int a, num = 0;
    36         scanf("%d", &a);
    37         DFS(a, 0);
    38         for (int i = 1; i < N + 1; ++i)//遍历layer数组,元素>0的即为符合条件的人,进行递增
    39             num += layer[i] > 0 ? 1 : 0;
    40         printf("%d
    ", num);//输出
    41     }
    42     return 0;
    43 }
  • 相关阅读:
    11.2
    11.1
    10.31JS中级
    10.24
    动画运动
    操作js的样式
    js
    js元素属性
    js轮播
    js计时器
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11319974.html
Copyright © 2020-2023  润新知