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
使用广度优先遍历BFS
 #include <iostream>
#include <vector>
#include <queue>
using namespace std;
bool people[][];
int N, L, K;
int BFS(int v)//广度优先搜索
{
int num = ;
vector<int>level(N + , );
vector<bool>visit(N + , false);
queue<int>q;
q.push(v);
visit[v] = true;
while (!q.empty())
{
v = q.front();
q.pop();
for (int i = ; i <= N; ++i)
{
if (visit[i] == false && people[v][i] == true && level[v] < L)
{
num++;
visit[i] = true;
level[i] = level[v] + ;
q.push(i);
}
}
}
return num;
} int main()
{
cin >> N >> L;
fill(people[], people[] + * , false);
for (int i = ; i <= N; ++i)
{
int a, m;
cin >> m;
for (int j = ; j <= m; ++j)
{
cin >> a;
people[a][i] = true;//请记住,存的是a的粉丝
}
}
cin >> K;
vector<int>test(K);
for (int i = ; i < K; ++i)
{
int a;
cin >> a;
cout << BFS(a) << endl;//使用广度优先搜索,即使用层序遍历
}
return ;
}

使用深度遍历DFS

 #include <iostream>
#include <vector>
using namespace std;
int N, L, K;
vector<int> graph[];//图
bool visit[];//visit表示是否已被访问,person用于最后统计
int layer[];//储存每个结点被遍历到时的层数
void DFS(int v, int level)
{//深度优先遍历
visit[v] = true;//当前结点置为已访问
layer[v] = level;//更新被遍历时所处层数
if (level != L)//还没有遍历到层数上限
for (int i : graph[v])//遍历当前结点能够到达的结点
if (!visit[i] || layer[i] > level + )//这个节点以前从未访问过或者这个节点当前被访问时的层数<layer数组中对应的层数
DFS(i, level + );//继续深度优先遍历
}
int main()
{
scanf("%d%d", &N, &L);
for (int i = ; i <= N; ++i)
{
int num, a;
scanf("%d", &num);
while (num--)
{
scanf("%d", &a);
graph[a].push_back(i);
}
}
scanf("%d", &K);
while (K--)
{
fill(visit + , visit + N + , false);
fill(layer + , layer + N + , -);
int a, num = ;
scanf("%d", &a);
DFS(a, );
for (int i = ; i < N + ; ++i)//遍历layer数组,元素>0的即为符合条件的人,进行递增
num += layer[i] > ? : ;
printf("%d\n", num);//输出
}
return ;
}

PAT甲级——A1076 Forwards on Weibo的更多相关文章

  1. PAT甲级1076. Forwards on Weibo

    PAT甲级1076. Forwards on Weibo 题意: 微博被称为中文版的Twitter.微博上的一位用户可能会有很多关注者,也可能会跟随许多其他用户.因此,社会网络与追随者的关系形成.当用 ...

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

  3. PAT A1076 Forwards on Weibo (30 分)——图的bfs

    Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...

  4. PAT甲级 1124. Raffle for Weibo Followers (20)

    1124. Raffle for Weibo Followers (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

  5. PAT甲级——A1124 Raffle for Weibo Followers

    John got a full mark on PAT. He was so happy that he decided to hold a raffle(抽奖) for his followers ...

  6. A1076. Forwards on Weibo

    Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...

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

  8. PAT_A1076#Forwards on Weibo

    Source: PAT A1076 Forwards on Weibo (30 分) Description: Weibo is known as the Chinese version of Twi ...

  9. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

随机推荐

  1. opencv3中surfDetector中使用

    https://www.cnblogs.com/anqiang1995/p/7398218.html opencv3中SurfFeatureDetector.SurfDescriptorExtract ...

  2. JS事件 什么是事件?JavaScript 创建动态页面。事件是可以被 JavaScript 侦测到的行为。 网页中的每个元素都可以产生某些可以触发 JavaScript 函数或程序的事件。

    什么是事件 JavaScript 创建动态页面.事件是可以被 JavaScript 侦测到的行为. 网页中的每个元素都可以产生某些可以触发 JavaScript 函数或程序的事件. 比如说,当用户单击 ...

  3. android发编译

    普通的反编译http://blog.csdn.net/vipzjyno1/article/details/21039349/ 反编译os http://gikir.com/blog/?p=115 ht ...

  4. CSIC_716_20191106【列表、元组、字典、集合】

    python的数据类型及其内置方法 一.列表(续) list.count( ).list.index( ) list = ['1', '2', '3', '2', 'a', 'b', 'c', 'a' ...

  5. IPsec分析/测试/

    一   局域网拓扑图 局域网环境搭建步骤: (升级最新版本 ,恢复出厂设置后) 1 两台网关wan口直连,分别接两台pc , 2 局域网网络测试,正常情况下PC1 和PC2 互通 ,测试通过在进行ip ...

  6. Tomcat - 基本知识

    基本概念 Tomcat是接收和解析http请求,并将结果返回客户端的应用程序 轻量级的web应用服务器 适用于并发性不是很高的系统中 开发和调试jsp的首选 类似的应用程序:Jetty, JBoss/ ...

  7. python实现全局配置和用户配置文件

    一.文件目录格式 二.代码 1.conf.__init__.py import importlib import os from conf import gsettings class Setting ...

  8. axios HTTP 400后,error没有详细信息

    参考网址:axios怎么获取到error中的状态值,具体信息 error.response

  9. day 53 Django基础二之URL路由系统

    Django基础二之URL路由系统   本节目录 一 URL配置 二 正则表达式详解 三 分组命名匹配 四 命名URL(别名)和URL反向解析 五 命名空间模式 一 URL配置 Django 1.11 ...

  10. 清晰化算法在DSP上的实现

    清晰化算法在DSP TIDM642上的实现,之前的部分工作摘要于此. 1 DSP平台的选择 1.1 DM642 Evolution Module 选择现有的DM642 Evolution Module ...