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

一种植物()
做了第二遍,思路还比较清晰,但是找了老长时间的错误了,就bfs,关键句是再bfs的过程中犯了错误,再队列中我用的int下标存的,就再这里坏事情的,在我的模拟过程中,会改变已经在队列中的数据,所以答案不对,找的过程总觉得没有错误,人了,不过,有问题也好,总比在考试中发现好

错误版本

#include
#include
#include
#include
#include
#include
#include
#include
const int N = 1015;
const int INF = 1000000000;
using namespace std;
int n,l;
bool vis[1005]={false};
typedef struct{
	vector<int> child;
	int layer; 
}people;
people peo[1005];
void bfs(int root){
	queue<int> q;
	peo[root].layer = 0;
	q.push(root);
	int num=0;
	vis[root] = true;
	while(!q.empty()){
	    int top = q.front();
		q.pop();
			for(int i=0;i<peo[top].child.size();i++){
				int id=peo[top].child[i];
				cout<<id<<' ';
				peo[id].layer = peo[top].layer + 1;
				cout<<	peo[id].layer<<' ';
				if(peo[id].layer <= l && vis[id] == false) {
					q.push(id);
					vis[id] = true;				
					num++;
				}
			}
			cout<<endl;
	}
	cout<<num<<endl;
}
int main(){
	cin>>n>>l;
	int num,t;	
	for(int i=1;i<=n;i++){
		scanf("%d",&num);
		for(int j=0;j<num;j++){
			scanf("%d",&t);
			peo[t].child.push_back(i);
		}
	}
	int k;
	scanf("%d",&k);
	while(k--){
		scanf("%d",&t);
		memset(vis,false,sizeof(vis));
		bfs(t);
	}
	return 0;
} 

修改过后

#include
#include
#include
#include
#include
#include
#include
#include
const int N = 1015;
const int INF = 1000000000;
using namespace std;
int n,L;
bool vis[1005]={false};
typedef struct{
	vector<int> child;
	int layer; 
}people;
people peo[1005];
void bfs(int root){
	queue<int> q;
	peo[root].layer = 0;
	q.push(root);
	int num=0;
	vis[root] = true;
	while(!q.empty()){
	    int top = q.front();
		q.pop();
			for(int i=0; i<peo[top].child.size() ;i++){
				int id = peo[top].child[i];
				//peo[id].layer = peo[top].layer + 1;
				if(peo[top].layer + 1 <= L && !vis[id] ){
					vis[id] = true;	
					peo[id].layer = peo[top].layer + 1;
					q.push(id);			
					num++;
				}
			}
	}
	cout<<num<<endl;
}
int main(){
	cin>>n>>L;
	int num,t;	
	for(int i=1;i<=n;i++){
		scanf("%d",&num);
		for(int j=0;j<num;j++){
			scanf("%d",&t);
			peo[t].child.push_back(i);
		}
	}
	int k;
	scanf("%d",&k);
	while(k--){
		scanf("%d",&t);
		memset(vis,false,sizeof(vis));
		bfs(t);
	}
	return 0;
} 

直接用结构体的,我有一篇博客记录过

你可能感兴趣的:(pat甲级)