题目信息

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代码

  1. #include <cstdio>
  2. #include <vector>
  3. #include <queue>
  4. using namespace std;
  5. int n, level, tn, t, m;
  6. vector<int> node[1005];
  7. int find(int root){
  8. int s = 0;
  9. vector<bool> vis(n+1, false);
  10. queue<pair<int, int> > que;
  11. que.push(make_pair(root, 0));
  12. vis[root] = true;
  13. while (!que.empty()){
  14. int id = que.front().first;
  15. int lv = que.front().second;
  16. que.pop();
  17. if (lv >= level) continue;
  18. for (int i = 0; i < node[id].size(); ++i){
  19. if (!vis[node[id][i]]){
  20. vis[node[id][i]] = true;
  21. que.push(make_pair(node[id][i], lv + 1));
  22. ++s;
  23. }
  24. }
  25. }
  26. return s;
  27. }
  28. int main()
  29. {
  30. scanf("%d%d", &n, &level);
  31. for (int i = 1; i <= n; ++i){
  32. scanf("%d", &tn);
  33. while (tn--) {
  34. scanf("%d", &t);
  35. if (t != i) node[t].push_back(i);
  36. }
  37. }
  38. scanf("%d", &m);
  39. while (m--){
  40. scanf("%d", &t);
  41. printf("%d\n", find(t));
  42. }
  43. return 0;
  44. }

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 in action读书笔记(20)第十五章 处理缺失数据的高级方法

    处理缺失数据的高级方法 15.1 处理缺失值的步骤 一个完整的处理方法通常包含以下几个步骤: (1) 识别缺失数据: (2) 检查导致数据缺失的原因: (3) 删除包含缺失值的实例或用合理的数值代替( ...

  2. mysql中int(1)与int(10)的区别

    INT[(M)] [UNSIGNED] [ZEROFILL] 普通大小的整数.带符号的范围是-2147483648到2147483647.无符号的范围是0到4294967295. INT(1) 和 I ...

  3. 洛谷 P2801 教主的魔法

    题目描述 教主最近学会了一种神奇的魔法,能够使人长高.于是他准备演示给XMYZ信息组每个英雄看.于是N个英雄们又一次聚集在了一起,这次他们排成了一列,被编号为1.2.…….N. 每个人的身高一开始都是 ...

  4. hibernate4+spring4+struts2的Maven中的pom.xml文件的配置

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  5. 解决dom4j加载xml文件性能慢的问题

    在代码中使用: 1: DocumentHelper.parseText 2: SAXReader reader = new SAXReader(); Document extdocument = re ...

  6. intellij idea集成github

    IDEA配置github并上传项目 http://www.cnblogs.com/jinjiyese153/p/6796668.html github ssl验证 https://www.cnblog ...

  7. SQL server 表操作语句(原创)

    CREATE TABLE [dbo].[test] ([id11] int NOT NULL ,[as] varchar(1) COLLATE Chinese_PRC_CI_AS NULL ,[asd ...

  8. python compare with other language

    java http://dirtsimple.org/2004/12/python-is-not-java.htmlhttp://twistedmatrix.com/users/glyph/rant/ ...

  9. Spring Boot . 3 -- Spring Boot Auto_configuration 是如何实现的?

    配置是Spring 框架的重要核心之一,所以Spring 应用能够正常的跑起来肯定是需要配置的,但是使用的Spring Boot 后很多配置没有做,那么AUTO-CONFIGURATION 到底是怎么 ...

  10. Spring Data Redis入门示例:Hash操作(七)

    将对象存为Redis中的hash类型,可以有两种方式,将每个对象实例作为一个hash进行存储,则实例的每个属性作为hash的field:同种类型的对象实例存储为一个hash,每个实例分配一个field ...