1076. Forwards on Weibo (30)

1.题目给出用户follow了哪些用户,然后求在level层关系内最大的转发数。

2.该题需要采用合适的数据结构,本程序使用了用节点来进行存储,节点中有一个followers列表,记录了谁关注了该用户.

3.使用层次遍历,直到最大level。

4.需要注意,用一个hasForwarded数组记录哪些用户已经转发,每个用户只转发一次。

1076

时间限制
3000 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

 
AC代码:

//#include<string>
//#include <iomanip>
//#include<stack>
//#include<unordered_set>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<unordered_map>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include <algorithm>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
#include<stack>
using namespace std;
struct UserNode{
	vector<int> follower;
	UserNode() :follower(0){};
};
int main(void)
{
	int userSum, maxLevel;
	cin >> userSum >> maxLevel;
	vector<UserNode> user(userSum);
	for (int i = 0; i < user.size(); i++)
	{
		int followSum;
		scanf("%d", &followSum);
		for (int j = 0; j < followSum; j++)
		{//用户i的关注列表
			int follow;
			scanf("%d", &follow);
			user[follow - 1].follower.push_back(i);
		}
	}

	int querySum;
	scanf("%d", &querySum);
	vector<int> query(querySum);
	for (int i = 0; i < query.size(); i++)
	{
		scanf("%d", &query[i]);
		query[i]--;//题目命名为1~N,但是程序命名为0~N-1
	}
	for (int i = 0; i < query.size(); i++)
	{//对每个查询的用户进行层次遍历
		queue<int> q;
		q.push(query[i]);
		int count1 = 1;
		int count2 = 0;
		int level = 0;
		int totalForward = 0;
		vector<bool> hasForwarded(userSum, false);
		hasForwarded[query[i]] = true;
		while (!q.empty()&&level<maxLevel)
		{
			for (int j = 0; j < count1; j++)
			{
				int head = q.front();
				q.pop();
				for (int k = 0; k < user[head].follower.size(); k++)
				{
					if (!hasForwarded[user[head].follower[k]])
					{//每个人只转发一次,还没转发的人进行转发
						hasForwarded[user[head].follower[k]] = true;
						q.push(user[head].follower[k]);
						count2++;
						totalForward++;
					}
				}
			}
			level++;
			count1 = count2;
			count2 = 0;
		}
		printf("%d\n", totalForward);
	}

	return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注