PAT (Advanced Level) Practice 1076 Forwards on Weibo (30 分)

  • 编程题

1076 Forwards on Weibo (30 分)

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

题目大意:

给出节点总数和每个节点的多个单向目标节点,求某个节点的L层反向目标节点个数。

解题思路:

使用BFS广度优先搜索。

读入每个节点的单向目标节点并记录。

用book数组记录每个节点的层数。用广搜求一共有多少个符合要求的节点。

注意事项:

1、有关层数计算,广搜优于深搜。试了深搜,有两个测试点没过,还没找到原因。

2、在判断层数是否符合要求时,需要在每个节点的循环中判断,而不是在某个节点的循环完成后再判断。(如果每次循环完成后level++再判断,这个level值是错误的,答案也不就不对了。)对BFS的层序遍历还需要更加熟悉。

源代码:

#include<iostream>
#include<stdlib.h>
#include<vector>
#include<queue>
using namespace std;
struct node
{
	vector<int>follower;
};
vector<node>v;
int cnt, n, l, book[1002];
/*
void dfs(int i, int level)
{
	if (level == l)return;
	for (int j=0; j<v[i].follower.size(); j++)
	{
		if (book[v[i].follower[j]] == 0)
		{
			book[v[i].follower[j]] = 1;
			cnt++;
			dfs(v[i].follower[j], level + 1);
		}
	}
	return;
}
*/
void bfs(int i)
{
	queue<int>q;
	int level = 0;
	q.push(i);
	while (!q.empty())
	{
		int temp = q.front();
		for (int j = 0; j < v[temp].follower.size(); j++)
		{
			if (book[v[temp].follower[j]] == 0&&book[temp]<=l)
			{
				book[v[temp].follower[j]] = book[temp]+1;
				q.push(v[temp].follower[j]);
				cnt++;
			}
		}
		q.pop();
	}
	return;
}
int main()
{
	int i, j, k, fl;
	scanf("%d %d", &n, &l);
	v.resize(n + 1);
	for (i = 1; i <= n; i++)
	{
		scanf("%d", &k);
		for (j = 0; j < k; j++)
		{
			scanf("%d", &fl);
			v[fl].follower.push_back(i);
		}
	}
	scanf("%d", &k);
	for (i = 0; i < k; i++)
	{
		for (j = 1; j <= n; j++)book[j] = 0;
		scanf("%d", &fl);
		book[fl] = 1;
		cnt = 0;
//		dfs(fl, 0);
		bfs(fl);
		printf("%d\n", cnt);
	}
	system("pause");
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值