PTA甲级 1076 Forwards on Weibo (30分)

强烈推荐,刷PTA的朋友都认识一下柳神–PTA解法大佬

本文由参考于柳神博客写成

柳神的CSDN博客,这个可以搜索文章

柳神的个人博客,这个没有广告,但是不能搜索

还有就是非常非常有用的 算法笔记 全名是

算法笔记  上级训练实战指南		//这本都是PTA的题解
算法笔记

PS 今天也要加油鸭

在这里插入图片描述

题目原文

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

生词如下:

PS:关键词没有看懂就GG,大意就是给你一个图,和连接关系.

然后再给出查询的结点.问你?(我没有看懂的地方)

关键句子:

maximum potential amount of forwards for any specific user, assuming that only L levels of indirect followers are counted.

假设仅计算L级间接关注者,则任何特定用户的最大潜在转发量

最大的潜在转发量只计算L级的关注者.

indirect 间接

trigger 触发

题目大意:

就是问你一个有向图中,已某个点当中心,它下面L层一共有多少结点.

思路如下:

BFS或者DFS都可以简单的解决问题

DFS比较麻烦一点.

要注意这种情况

5 3 
0 
1 1 
1 2 
2 1 3 
1 4
1 1 

正确答案要输出

4

因为我们在遍历了 1->2->3->4之后,5 就会被弹出.

但是在1-4-5的情况下是可以的.

我们需要对DFS的代码做修改

修改后的DFS代码如下:

#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
const int Max = 1024;
int n, l, fan, id, query = 0, t,SumRoot=0, level[Max];
bool vis[Max];
vector<int> G[Max];
void dfsTrave(int v, int depth) {
	vis[v] = true;
	level[v] = depth;
	if (depth < l) {		//剪枝的一个方法level[G[v][i]] > depth + 1 就是当出现
		for (int i = 0; i < G[v].size(); ++i) {
			if (!vis[G[v][i]] || level[G[v][i]] > depth + 1)	dfsTrave(G[v][i], depth + 1);
		}
	}
	return;
}
int main(void) {
	scanf("%d%d", &n, &l);
	for (int i = 1; i <= n; ++i) {
		scanf("%d", &fan);
		for (int j = 0; j < fan; ++j) {
			scanf("%d", &id);
			G[id].push_back(i);
		}
	}
	scanf("%d", &query);
	for (int i = 0; i < query; ++i) {
		scanf("%d", &t);
		SumRoot = 0;
		memset(vis, false, sizeof(vis));
		memset(level, -1, sizeof(level));
		dfsTrave(t, 0);
		for(int i=1;i<=n;++i)	 SumRoot+= level[i] > 0 ? 1 : 0;
		printf("%d\n", SumRoot);
	}
	return 0;
}

BFS代码如下:

#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
const int Max = 1024;
int n, l, fan, id, query = 0, t, SumRoot = 0, idlevel[Max];
bool vis[Max];
vector<int> G[Max];
void bfs(int v) {
	queue<int> q;
	vis[v] = true;
	q.push(v);
	idlevel[v] = 0;
	while (q.size()) {
		int u = q.front();
		q.pop();
		int next = idlevel[u] + 1;
		if (next > l)return;
		for (int i = 0; i < G[u].size(); ++i) {
			if (!vis[G[u][i]]){
				SumRoot++;
				vis[G[u][i]] = true;
				idlevel[G[u][i]] = next;
				q.push(G[u][i]);
			}
		}
	}
}
int main(void) {
	scanf("%d%d", &n, &l);
	for (int i = 1; i <= n; ++i) {
		scanf("%d", &fan);
		for (int j = 0; j < fan; ++j) {
			scanf("%d", &id);
			G[id].push_back(i);
		}
	}
	scanf("%d", &query);
	for (int i = 0; i < query; ++i) {
		scanf("%d", &t);
		SumRoot = 0;
		memset(vis, false, sizeof(vis));
		bfs(t);
		printf("%d\n", SumRoot);
	}
	return 0;
}

如果这篇文章对你有张帮助的话,可以用你高贵的小手给我点一个免费的赞吗

相信我,你也能变成光.

在这里插入图片描述

如果你有任何建议,或者是发现了我的错误,欢迎评论留言指出.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值