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 ;
}

最新文章

  1. 1.2输出100以内的素数&amp;输出前100个素数。
  2. json和对象、list互转
  3. 基于PBOC电子钱包的消费过程详解
  4. 【转载】React初学者入门须知
  5. Maven : 将Jar安装到本地仓库和Jar上传到私服 转
  6. Python 错误和异常
  7. 道可叨 | Python 标准库 urllib2 的使用细节
  8. EF Codefirst 初步学习(二)—— 程序管理命令 更新数据库
  9. C++算法接口使用参考
  10. css3 样式过度器 Transition
  11. 微信小程序-解决下拉刷新报错
  12. Oracle:查询各组最新的一条记录
  13. Spring概况(一)
  14. 【项目 &#183; Wonderland】会议一 &#183; 可达鸭
  15. shiro框架
  16. mysql 字符串分割 和 动态执行拼接sql
  17. Python处理数据
  18. Linux命令行获取本机外网IP地址
  19. Sql语句里面调用变量
  20. ASP.NET Web API实践系列05,消息处理管道

热门文章

  1. 【POJ】1611 The Suspects
  2. Soci介绍
  3. StringBuilder 和 StringBuffer类
  4. SpringCloud学习笔记《---04 Feign---》基础篇
  5. arm-linux-objdump 的使用
  6. UOJ67 新年的毒瘤【Tarjan,割点】
  7. linux 将子文件夹的文件复制到 当前目录中
  8. Batch - Windows Batch 常用命令
  9. thinkphp 模块化设计
  10. thinkphp 分布式数据库支持