PAT甲级——A1076 Forwards on Weibo
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的更多相关文章
- PAT甲级1076. Forwards on Weibo
PAT甲级1076. Forwards on Weibo 题意: 微博被称为中文版的Twitter.微博上的一位用户可能会有很多关注者,也可能会跟随许多其他用户.因此,社会网络与追随者的关系形成.当用 ...
- 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 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 ...
- PAT甲级 1124. Raffle for Weibo Followers (20)
1124. Raffle for Weibo Followers (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- 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 ...
- A1076. Forwards on Weibo
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...
- 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_A1076#Forwards on Weibo
Source: PAT A1076 Forwards on Weibo (30 分) Description: Weibo is known as the Chinese version of Twi ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
随机推荐
- python语法基础(类)
一.什么是类? 类是具有相同属性的一类事物 类还有功能和属性,属性就是这类事物的特征,而功能就是它能做什么,也是就是方法或者函数. 在python中类用关键词class来声明 二.类的声明 类的声明方 ...
- pdfkit
官方文档 0.准备 需要引入两个包,首先要npm install pdfkit安装pdfkit包 const PDF = require('pdfkit'); const fs = require(' ...
- CSIC_716_20191105【数字、字符串、列表】
python数据类型及其内置方法 一.整型:主要用于数学运算 其他进制----->转十进制 """ 其他进制转换为十进制 通过int('x进制数',x)实现 &qu ...
- JavaScript 数据值校验工具类
/** * 数据值校验工具类 */ var checkService = { // 不校验 none: function () { return true; }, //非空校验 isEmpty: fu ...
- Windows 获取控制台窗口句柄
详细信息 因为多个窗口可能具有相同的标题,您应该更改当前的控制台窗口标题为唯一的标题.这将有助于防止返回不正确的窗口句柄.使用 SetConsoleTitle() 来更改当前的控制台窗口标题.下面是此 ...
- 应用上云新模式,Aliware 全家桶亮相杭州云栖大会
全面上云带来的变化,不仅是上云企业数量上的攀升,也是企业对云的使用方式的转变,越来越多的企业用户不仅将云作为一种弹性资源,更是开始在云上部署架构和应用,借助 Serverless 等技术,开发人员只需 ...
- thinkphp 获取内容
如果需要获取渲染模板的输出内容而不是直接输出,可以使用fetch方法. fetch方法的用法和display基本一致(只是不需要指定输出编码和输出类型): 大理石平台规格 fetch('模板文件') ...
- flink提交文件出现java.io.IOException:unable to close file because the last block does not have enough number of replicas异常
当提交已经打包好的jar包时候,控制台出现以下的错误.
- 访问配置信息的URL与配置文件的映射关系
- 6_5.springboot2.x数据整合springData JPA
1.配置文件 pom.xml <dependencies> <dependency> <groupId>org.springframework.boot</g ...