PAT-A-1076 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 (≤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

 emmm,前前后后两次在实训课上写的,第一天写完了大致思路,第二天细节找错,前前后后有一个多小时,换来的结果是一个内存超出限制,这个就没办法了,谁让使用的是vector呢、

大体思路,使用vector记录每个人的粉丝,但是输入的是每个人的关注列表,因此需要小小的变动一下。按照深度循环,使用vector模拟能够浏览的人,利用set数据不重复的性质保存结果。第一次循环 保存该用户的粉丝到vector下,纪录当先的size,第二次循环以后,遍历vector,将上一次保存到vector中的用户的粉丝添加到vector中,并删掉前size个元素,如此循环。最后输出的时候一定要判断,set中的用户id是否包含test本身,如果包含,结果减一。

#include <iostream>
#include <algorithm>
#include <set>
#include <queue>
#include <vector>
using namespace std;
int main(){
	vector<int> data[1001];
	int n,m;
	scanf("%d %d",&n,&m);
	for(int i=1,temp;i<=n;i++){
		scanf("%d",&temp);
		for(int j=1,a;j<=temp;j++){
			scanf("%d",&a);
			data[a].push_back(i);
		}
	}
	
	int sum;
	scanf("%d",&sum);
	for(int l=0,test;l<sum;l++){
		scanf("%d",&test);
		set<int> res;
		vector<int> info;
		int time=1;
		int length;
		while(true){
			if(time==1){
				info.insert(info.end(),data[test].begin(),data[test].end());
				for(int mm=0;mm<data[test].size();mm++){
					res.insert(data[test][mm]);
				}
				length=info.size();
			}
			else{
				for(int n1=0;n1<length;n1++){
					int n2=info[n1];
					info.insert(info.end(),data[n2].begin(),data[n2].end());
					for(int n3=0;n3<data[n2].size();n3++){
						res.insert(data[n2][n3]);
					}
					

				}
				info.erase(info.begin(),info.begin()+length);
				length=info.size();
			}
			if(time==m){
				break;
			}
			time++;
			
		}
		if(res.count(test)){
			cout<<res.size()-1<<endl;
		}
		else{
			cout<<res.size()<<endl;
		}
		
	}
	
	
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值