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 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 are 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 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
解题思路
合适的结构构造成树再进行搜索
AC代码
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
int n, level, tn, t, m;
vector<int> node[1005];
int find(int root){
int s = 0;
vector<bool> vis(n+1, false);
queue<pair<int, int> > que;
que.push(make_pair(root, 0));
vis[root] = true;
while (!que.empty()){
int id = que.front().first;
int lv = que.front().second;
que.pop();
if (lv >= level) continue;
for (int i = 0; i < node[id].size(); ++i){
if (!vis[node[id][i]]){
vis[node[id][i]] = true;
que.push(make_pair(node[id][i], lv + 1));
++s;
}
}
}
return s;
}
int main()
{
scanf("%d%d", &n, &level);
for (int i = 1; i <= n; ++i){
scanf("%d", &tn);
while (tn--) {
scanf("%d", &t);
if (t != i) node[t].push_back(i);
}
}
scanf("%d", &m);
while (m--){
scanf("%d", &t);
printf("%d\n", find(t));
}
return 0;
}
1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- PAT (Advanced Level) 1076. Forwards on Weibo (30)
最短路. 每次询问的点当做起点,然后算一下点到其余点的最短路.然后统计一下最短路小于等于L的点有几个. #include<cstdio> #include<cstring> # ...
- PAT甲题题解-1076. Forwards on Weibo (30)-BFS
题目大意:给出每个用户id关注的人,和转发最多的层数L,求一个id发了条微博最多会有多少个人转发,每个人只考虑转发一次.用BFS,同时每个节点要记录下所在的层数,由于只能转发一次,所以每个节点要用vi ...
- 【PAT甲级】1076 Forwards on Weibo (30 分)
题意: 输入两个正整数N和L(N<=1000,L<=6),接着输入N行数据每行包括它关注人数(<=100)和关注的人的序号,接着输入一行包含一个正整数K和K个序号.输出每次询问的人发 ...
- 1076. Forwards on Weibo (30)
时间限制 3000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Weibo is known as the Chinese v ...
- 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 ...
随机推荐
- linux下redis安装访问
下载编译安装 wget http://download.redis.io/releases/redis-3.0.1.tar.gz tar xvf redis-3.0.1.tar.gz mv redis ...
- H.264学习笔记5——熵编码之CAVLC
H.264中,4x4的像素块经过变换和量化之后,低频信号集中在左上角,大量高频信号集中在右下角.左边的低频信号相对数值较大,而右下角的大量高频信号都被量化成0.1和-1:变换量化后的残差信息有一定的统 ...
- Swift 命名空间形式扩展的实现
Swift 的 extension 机制很强大,不仅可以针对自定义的类型,还能作用于系统库的类型,甚至基础类型比如 Int.当在对系统库做 extension 的时候,就会涉及到一个命名冲突的问题.O ...
- swift 语言评价
杂而不精,一团乱麻!模式乱套,不适合作为一门学习和研究语言. 谢谢 LZ 介绍,看完之后更不想用 Swift 了.从 C++那里抄个 V-Table 来很先进嘛?别跟 C++一样搞什么 STL 就好了 ...
- redisd的非持久化配置
如何关闭redis持久化?我的需求是只把redis当作缓存来用,所以持久化到硬盘对我的需求来说没有意义. 修改redis配置文件,redis.conf 第115行左右. 1.注释掉原来的持久化规则 # ...
- Ubuntu下systemd服务的配置
1. 在/lib/systemd/system目录下创建服务启动脚本testservice.service 2. 文件内容如下: [Unit] Description=TestService [Ser ...
- poj3134 Power Calculus
题目描述: 你现在有x^1,每动一步可以用当前存在的x^a和x^b获得x^(a+b)或x^(abs(a-b)).给出n(n<=1000),求最少多少步能得到x^n. 题解: IDDFS.枚举步数 ...
- Layui框架 中table解决日期格式问题
使用templet自定义模板(详细查看官方文https://www.layui.com) 1.对Date的扩展,将 Date 转化为指定格式的String ,创建一个js文件: (dataForma ...
- Python学习-初始列表
初始列表 列表中的排放是没有顺序的并且可以被修改 在列表中你可以放入任何的元素类型.字符串,数字,布尔值等等,甚至还可以进行列表的嵌套 列表的注意特征:用中括号包围,中间用逗号分隔开 list = [ ...
- 理解ZAB协议
ZAB协议 介绍 1.zab协议是为分布式协调服务zookpeer专门设计的一种支持崩溃恢复的原子广播协议 2.在zookeeper中主要依赖ZAB协议来实现数据一致性,基于该协议zk实现了一种主备模 ...