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 ...
随机推荐
- fatal: Authentication failed for 问题解决
执行以下code git config --system --unset credential.helper 参考地址: https://www.jianshu.com/p/8a7f257e07b8
- PHP serialize() 序列化函数
PHP serialize() 序列化函数 定义和用法 — 语法 string serialize ( mixed $value ) serialize() 返回字符串,此字符串包含了表示 value ...
- select * from a, b的意思
其结果中总记录数为a表记录数乘以b表记录数,a的每条记录都会重复列出b记录总数次 很多晦涩的sql都要通过测试具体的数据来理解
- STL中unique的使用
作用 unique函数可以删除有序数组中的重复元素,即去重(并不是真正的删除,后面会讲) 定义在头文件<algorithm>中 函数原型 1.只有两个参数,且参数类型都是迭代器: iter ...
- Java方法注释模板
普通方法 /** * ${todo} * @author: SYJP * @version 创建时间:${date} */ 覆盖方法 /** * @Title: ${enclosing_method} ...
- java匹配http或https的url的正则表达式20180912
package org.jimmy.autosearch20180821.test; import java.util.regex.Matcher; import java.util.regex.Pa ...
- vue多视图
第一步 在app.vue中 <router-view class="b" name="header"> </router-view> ...
- node.js 的介绍
1.node.js是什么? (1)node.js不是一门编程语言, 是一个开发平台,就像Java开发平台,Net平台,PHP开发平台,Apple开发平台.(何为开发平台?有对应的编程语言,有语言运行时 ...
- 初识 Spring 框架
初识 Spring 框架可以帮助我们构建规范的.优秀的应用程序,简化烦琐的编码过程. Spring 是一个非常著名的轻量级的企业级开源框架,Spring 的目标是使 Java EE 更易用并促进良好的 ...
- HashTable的C++实现
由哈希表的定义,采用C++完成了一个学生成绩存储系统,分析过程如下: 由于哈希表是按KEY值存储,我们假设KEY值为一个字符串.hash算法为字符串的前两位大写字母所对应的数字对一个质数的模运算. i ...