徐子烊

  • 主页
  • 语雀
所有文章 关于我

徐子烊

  • 主页
  • 语雀

1076 Forwards on Weibo (30 分)

2019-04-07

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:

1
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
2
3
4
5
6
7
8
9
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:

1
2
4
5

作者: CHEN, Yue

单位: 浙江大学

时间限制: 3000 ms

内存限制: 64 MB

代码长度限制: 16 KB

题目大意

给出一个微博的关注关系网,一个用户发微博时,他的关注者都会转发,关注者的关注者继续转发,求l层内的转发数

分析

虽然30分的题,但是比较简单,用bfs即可。我这里用的是邻接表的结构。

代码

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
#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
#include <map>
#include <set>
#include <cstring>
#include <cmath>
#include <sstream>

using namespace std;

vector<int> users[1010];
int see[1010];
int n,l,mi,tmp,k;
deque<int> q;
void cal(int root,int layer){
q.push_back(root);
int cnt=0;
fill(see,see+1010,0);
see[root]=1;
while(layer--){
int qsize=q.size();
while(qsize--) {
int cur = q.front();
q.pop_front();
for (int u:users[cur]) {
if (!see[u]) {
see[u] = 1;
cnt++;
q.push_back(u);
}
}
}
}
cout<<cnt<<endl;
}
int main() {
cin>>n>>l;
for(int i=1;i<=n;i++){
cin>>mi;
for(int j=0;j<mi;j++){
cin>>tmp;
users[tmp].push_back(i);
}
}
cin>>k;
for(int i=0;i<k;i++){
cin>>tmp;
q.clear();
cal(tmp,l);
}
return 0;
}
  • PAT
  • bfs
  • PAT题解

扫一扫,分享到微信

微信分享二维码
1077 Kuchiguse (20 分)
[天坑]1075 PAT Judge (25 分)
  1. 1. 1076 Forwards on Weibo (30 分)
    1. 1.1. Input Specification:
    2. 1.2. Output Specification:
    3. 1.3. Sample Input:
    4. 1.4. Sample Output:
  2. 2. 题目大意
  3. 3. 分析
  4. 4. 代码
© 2020 徐子烊
Hexo Theme Yilia by Litten
  • 所有文章
  • 关于我

tag:

  • PAT
  • 多项式
  • dijkstra
  • 图
  • 树
  • 最大子序列
  • 进制转换
  • 二分
  • dfs
  • 连通分量
  • 模拟
  • STL
  • 大整数
  • 需复习
  • 链表
  • 贪心
  • 动态规划
  • 子序列
  • 二叉树
  • LCS
  • 栈
  • 排序
  • 质因数
  • 树状数组
  • 完全二叉树
  • 整数溢出
  • AVL树
  • 深搜
  • 哈希
  • bfs
  • 并查集
  • 红黑树
  • LCA
  • 拓扑排序
  • lca
  • 实习
  • bat
  • Leetcode
  • Java并发
  • 线程池
  • 6.824
  • 分布式
  • mapreduce
  • caffe
  • epoll
  • Raft
  • schnorr
  • 密码学
  • 设计模式
  • 单例模式
  • 原型模式
  • 工厂模式
  • 建造者模式
  • 抽象工厂
  • 桥接模式
  • 模版模式
  • 组合模式
  • 策略模式
  • 装饰器模式
  • 迭代器模式
  • 适配器模式

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

很惭愧

只做了一点微小的工作