1076 Forwards on Weibo 30 ☆☆☆

github 地址:https://github.com/iofu728/PAT-A-by-iofu728 难度:☆☆☆ 关键词:图,BFS

题目

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

Then finally a positive K is given, followed by KUserID'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 个用户关系图,找出某个用户 k 层单向关系网内人数

思路

  1. 这是一个图的问题,第一步建图,vector<vector<int> > v;
  2. 找 k 层关系网内人数,BFS 遍历,带上层数 layer

code

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

using namespace std;
const int MAXN = 1009;
int m;

struct node {
  int id, layer;
};

vector<vector<int> > v;

int bfs(node start) {
  bool vis[MAXN] = {false};
  vis[start.id] = true;
  queue<node> q;
  q.push(start);
  int connectNum = 0;
  while (!q.empty()) {
    node top = q.front();
    q.pop();
    int topId = top.id;
    for (int i = 0; i < v[topId].size(); ++i) {
      node next = {v[topId][i], top.layer + 1};
      if (!vis[next.id] && next.layer <= m) {
        q.push(next);
        vis[next.id] = true;
        ++connectNum;
      }
    }
  }
  return connectNum;
}

int main(int argc, char const *argv[]) {
  int n, num, temp;
  cin >> n >> m;
  v.resize(n + 1);
  for (int i = 1; i < n + 1; ++i) {
    cin >> num;
    for (int j = 0; j < num; ++j) {
      cin >> temp;
      v[temp].push_back(i);
    }
  }
  cin >> num;
  for (int i = 0; i < num; ++i) {
    cin >> temp;
    node start = {temp, 0};
    cout << bfs(start) << endl;
  }
  return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
You can use this BibTex to reference this blog if you find it useful and want to quote it.