• 1076 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 (≤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 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:

    1. 7 3
    2. 3 2 3 4
    3. 0
    4. 2 5 6
    5. 2 3 1
    6. 2 3 4
    7. 1 4
    8. 1 5
    9. 2 2 6

    Sample Output:

    1. 4
    2. 5

    注意给的是关注列表,也就是说当前的第 i 人是接下来m[i] 人的粉丝,注意建图方向;

    dfs:有的节点会出现需要反复访问的情况,当在一次访问中的层数大于这一次访问的层数时,意味着这次访问有可能通过该节点可以访问到上次没有访问到的节点(因为有层数的限制),此时需要在vis[]已经等于1即标记过访问的情况下再次访问,并且该节点由于已经访问过所以不再计入ans++。

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. vector<int>g[1010];
    7. int n, l, k, x, ans, cnt;
    8. int m[1010], d[1010];
    9. bool vis[1010];
    10. void dfs(int i, int deep) {
    11. if (deep >= l || g[i].size() == 0) {
    12. return;
    13. }
    14. for (int j = 0; j < g[i].size(); j++) {
    15. if (!vis[g[i][j]] || vis[g[i][j]] && deep + 1 < d[g[i][j]]) {
    16. if (!vis[g[i][j]]) {
    17. ans++;
    18. }
    19. vis[g[i][j]] = 1;
    20. // //
    21. // cout << "kkk:" << g[i][j] << endl;
    22. // cout << "deep:" << deep + 1 << endl;
    23. d[g[i][j]] = deep + 1;
    24. dfs(g[i][j], deep + 1);
    25. }
    26. }
    27. }
    28. int main() {
    29. cin >> n >> l;
    30. for (int i = 1; i <= n; i++) {
    31. cin >> m[i];
    32. for (int j = 0; j < m[i]; j++) {
    33. cin >> x;
    34. g[x].push_back(i);
    35. }
    36. }
    37. cin >> k;
    38. while (k--) {
    39. memset(vis, 0, sizeof(vis));
    40. ans = 0;
    41. cin >> x;
    42. vis[x] = 1;
    43. dfs(x, 0);
    44. cout << ans << endl;
    45. }
    46. return 0;
    47. }

  • 相关阅读:
    带团队后的日常思考(十四)
    【MindSpore易点通】dump工具使用经验总结
    《21天精通TypeScript-5》类型注解与原始类型
    【JY】YJK前处理参数详解及常见问题分析:刚度系数(三)
    我要写整个中文互联网界最牛逼的JVM系列教程 | 「JVM与Java体系架构」章节:区分栈的指令集架构和寄存器的指令集架构
    数字孪生技术架构
    搭建自己的搜索引擎——oh-my-search使用
    Java线程池ThreadPoolExecutor极简教程
    嵌入式开发:成功进行代码审查的10个问题
    Java中数组的定义与使用(Java系列3)
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/126539951