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 (≤), 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 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:
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
题意:
给出一个社交网络,在规定的层级内,一个博主发了博文之后,共有多少人能够看到这篇文章?假设每一个follower看到发表的文章之后都会转发。
思路:
这道题就是一道有向图的问题,样例中:M[i] user_list[i]代表的意思应该是,user_list中的人发表了文章,i能够看到并进行转发。然后用邻接矩阵表示有向图,用层序遍历的方法来遍历满足要求的结点。注意遍历的过程中不要重复出现。
Code:
1 #include <bits/stdc++.h>
2
3 using namespace std;
4
5 vector<int> grap[1005];
6
7 int helper(int query, int level) {
8 queue<int> que;
9 que.push(query);
10 que.push(-1);
11 int count = 0;
12 vector<bool> visited(1005, false);
13 visited[query] = true;
14 while (!que.empty() && level >= 0) {
15 int temp = que.front();
16 que.pop();
17 if (temp != -1) count++;
18 if (temp == -1) {
19 level--;
20 if (que.empty()) break;
21 que.push(-1);
22 } else {
23 for (int i = 0; i < grap[temp].size(); ++i) {
24 if (!visited[grap[temp][i]]) {
25 visited[grap[temp][i]] = true;
26 que.push(grap[temp][i]);
27 }
28 }
29 }
30 }
31 return count - 1;
32 }
33
34 int main() {
35 int n, l, k, t;
36 cin >> n >> l;
37 for (int i = 1; i <= n; ++i) {
38 cin >> k;
39 for (int j = 0; j < k; ++j) {
40 cin >> t;
41 grap[t].push_back(i);
42 }
43 }
44 cin >> k;
45 for (int i = 0; i < k; ++i) {
46 cin >> t;
47 cout << helper(t, l) << endl;
48 }
49
50 return 0;
51 }
1076 Forwards on Weibo的更多相关文章
- PAT 1076 Forwards on Weibo[BFS][一般]
1076 Forwards on Weibo (30)(30 分) Weibo is known as the Chinese version of Twitter. One user on Weib ...
- 1076 Forwards on Weibo (30 分)
1076 Forwards on Weibo (30 分) Weibo is known as the Chinese version of Twitter. One user on Weibo ma ...
- PAT甲级1076. Forwards on Weibo
PAT甲级1076. Forwards on Weibo 题意: 微博被称为中文版的Twitter.微博上的一位用户可能会有很多关注者,也可能会跟随许多其他用户.因此,社会网络与追随者的关系形成.当用 ...
- 1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise
题目信息 1076. Forwards on Weibo (30) 时间限制3000 ms 内存限制65536 kB 代码长度限制16000 B Weibo is known as the Chine ...
- PAT 甲级 1076 Forwards on Weibo (30分)(bfs较简单)
1076 Forwards on Weibo (30分) Weibo is known as the Chinese version of Twitter. One user on Weibo m ...
- 1076. Forwards on Weibo (30)
时间限制 3000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Weibo is known as the Chinese v ...
- PAT 1076. Forwards on Weibo (30)
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...
- 1076. Forwards on Weibo (30) - 记录层的BFS改进
题目如下: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, a ...
- 1076 Forwards on Weibo (30)(30 分)
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...
- PAT Advanced 1076 Forwards on Weibo (30) [图的遍历,BFS,DFS]
题目 Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and ...
随机推荐
- HTTP 请求URL中不能含有空格
如果含有空格 会报 不合法参数异常 正确做法是将其encode URLEncoder.encode(targetString, "utf-8").replaceAll(" ...
- SpringCloud(四):服务注册中心Eureka Eureka高可用集群搭建 Eureka自我保护机制
第四章:服务注册中心 Eureka 4-1. Eureka 注册中心高可用集群概述在微服务架构的这种分布式系统中,我们要充分考虑各个微服务组件的高可用性 问题,不能有单点故障,由于注册中心 eurek ...
- Asp.NET Core 限流控制-AspNetCoreRateLimit
起因: 近期项目中,提供了一些调用频率较高的api接口,需要保障服务器的稳定运行:需要对提供的接口进行限流控制.避免因客户端频繁的请求导致服务器的压力. 一.AspNetCoreRateLimit 介 ...
- Kubernetes-7.Ingress
docker version:20.10.2 kubernetes version:1.20.1 本文概述Kubernetes Ingress基本原理和官方维护的Nginx-Ingress的基本安装使 ...
- JUC-ThreadLocal
目录 ThreadLocal ThreadLocal测试 ThreadLocal类结构 前言 多线程访问同一个共享变量的时候也别容易出现并发问题,特别是在多线程需要对一个共享变量进行写入的时候.为了保 ...
- Docker安装Openvas
目录 安装 在本机内运行 在局域网内运行 关闭 参考 安装 ➜ ~ docker search openvas NAME DESCRIPTION STARS OFFICIAL AUTOMATED mi ...
- MySql_176. 第二高的薪水 + limit + distinct + null
MySql_176. 第二高的薪水 LeetCode_MySql_176 题目描述 题解分析 代码实现 # Write your MySQL query statement below select( ...
- apicloud打包的ios证书的获取方法
apicloud云编译的时候,需要测试证书或者正式证书进行编译. 那么这个证书是怎么来的呢?通过什么渠道可以获取呢? 这里我介绍下使用香蕉云编这个在线工具来生成: 1.登录香蕉云编,生成证书的csr文 ...
- 【Azure Developer】Python 获取Micrisoft Graph API资源的Access Token, 并调用Microsoft Graph API servicePrincipals接口获取应用ID
问题描述 在Azure开发中,我们时常面临获取Authorization问题,需要使用代码获取到Access Token后,在调用对应的API,如servicePrincipals接口. 如果是直接调 ...
- BZOJ_2844 albus就是要第一个出场 【线性基】
一.题目 albus就是要第一个出场 二.分析 非常有助于理解线性基的一题. 构造线性基$B$后,如果$|A| > |B|$,那么就意味着有些数可以由$B$中的数异或出来,而多的数可以取或者不取 ...