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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章