题目信息

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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. PAT (Advanced Level) 1076. Forwards on Weibo (30)

    最短路. 每次询问的点当做起点,然后算一下点到其余点的最短路.然后统计一下最短路小于等于L的点有几个. #include<cstdio> #include<cstring> # ...

  5. PAT甲题题解-1076. Forwards on Weibo (30)-BFS

    题目大意:给出每个用户id关注的人,和转发最多的层数L,求一个id发了条微博最多会有多少个人转发,每个人只考虑转发一次.用BFS,同时每个节点要记录下所在的层数,由于只能转发一次,所以每个节点要用vi ...

  6. 【PAT甲级】1076 Forwards on Weibo (30 分)

    题意: 输入两个正整数N和L(N<=1000,L<=6),接着输入N行数据每行包括它关注人数(<=100)和关注的人的序号,接着输入一行包含一个正整数K和K个序号.输出每次询问的人发 ...

  7. 1076. Forwards on Weibo (30)

    时间限制 3000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Weibo is known as the Chinese v ...

  8. 1076. Forwards on Weibo (30) - 记录层的BFS改进

    题目如下: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, a ...

  9. 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 ...

随机推荐

  1. R Programming week2 Functions and Scoping Rules

    A Diversion on Binding Values to Symbol When R tries to bind a value to a symbol,it searches through ...

  2. iOS-UI控件之UIImageView

    contentMode属性 带有scale单词的:图片有可能会拉伸 UIViewContentModeScaleToFill 将图片拉伸至填充整个imageView 图片显示的尺寸跟imageView ...

  3. 【C++】模板简述(一):模板的引入

    我们在介绍模板之前,首先想象有这么一个场景: 我们需要通过C++写出一个通用的加法程序,那么有如下几种方法: 方法一:C++的函数重载 //int int int int Add(int l,int ...

  4. qt5.8 链接mysql错误:driver not load

    转载请注明出处:http://www.cnblogs.com/dachen408/p/7155858.html 问题:qt5.8 链接mysql错误:driver not load. 解决方案:1.安 ...

  5. http升级https改造方案

    一.解决方案 1.httpClient请求https版蓝鲸接口 (1).原理 https与http最大的区别在于SSL加密传输协议的使用.在自己写的JAVA HttpClient程序,想手动验证证书, ...

  6. KMP算法介绍

    简介 KMP算法是D.E.Knuth.J.H.Morris和V.R.Pratt共同提出的,称之为Knuth-Morris-Pratt算法,简称KMP算法.该算法与Brute-Force算法相比有较大改 ...

  7. 6-Java-C(小题答案)

    1.15 2.36 3.0.58198 4.return v.size()-v.indexOf(n) 5."%"+(width-s.length()-2)/2+"s%s% ...

  8. winpcap编程设置过滤器之指定获取某个网站的数据

    下面,我将以 乱世隋唐页游 为例,通过编码获取这里面的数据. 游戏图: 我是乱世隋唐的网址是:www.917st.com 这个是官网网址的服务器地址.  42.62.0.14 我玩的游戏服是84区.网 ...

  9. Linux下MySQL 5.7的初始化

    要用管理员账号运行. systemctl start mysql#启动MySQL服务 mysqld_safe --user=mysql &#启动MySQL服务(安全方式) mysql -u r ...

  10. No-8.循环

    01. 程序的三大流程 在程序开发中,一共有三种流程方式: 顺序 —— 从上向下,顺序执行代码 分支 —— 根据条件判断,决定执行代码的 分支 循环 —— 让 特定代码 重复 执行 02. while ...