PAT甲级 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 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
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 1001
#define Vertex int

typedef struct AdjVNode *PtrToAdjVNode;
struct AdjVNode{
	Vertex AdjV;
	PtrToAdjVNode Next;
};

typedef struct VNode{
	PtrToAdjVNode FirstEdge;
}AdjList[MaxSize];

typedef struct GNode *_Graph;
struct GNode{
	int Nv;
	int Ne;
	AdjList Graph;
};
int Visited[MaxSize]={};

typedef struct QNode *Queue;
struct QNode{
	int front;
	int rear;
	int Size;
	Vertex *_Q;
};

void ReSet(int *A);
_Graph BuiltG(int N);
_Graph CreatG(int N);
void InsertEdge(_Graph G,Vertex W,Vertex V);
int BFS(_Graph G,Vertex V,int L);
Queue CreatQ(int N);
void AddQ(Queue Q,Vertex V);
Vertex DeleteQ(Queue Q);
int IsEmpty(Queue Q);

int main()
{
	_Graph G;
	int N,L,K;
	Vertex i,V;
	int Num;
	scanf("%d %d",&N,&L);
	G=BuiltG(N);
	scanf("%d",&K);
	for(i=1;i<=K;i++)
	{
		scanf("%d",&V);
		Num=BFS(G,V,L);
		printf("%d\n",Num);
		ReSet(Visited);
	}
	return 0;
}

void ReSet(int *A)
{
	int i;
	for(i=0;i<MaxSize;i++)
	{
		A[i]=0;
	}
}

_Graph BuiltG(int N)
{
	_Graph G;
	G=CreatG(N);
	Vertex V,W;
	int i,M;
	for(V=1;V<=N;V++)
	{
		scanf("%d",&M);
		if(M)
		{
			for(i=0;i<M;i++)
			{
				scanf("%d",&W);
				InsertEdge(G,W,V); 
			}
		}
	}
	return G;
}

_Graph CreatG(int N)
{
	Vertex V;
	_Graph G=(_Graph)malloc(sizeof(struct GNode));
	G->Nv=N;
	for(V=0;V<=N;V++)
		G->Graph[V].FirstEdge=NULL;
	return G;
}

void InsertEdge(_Graph G,Vertex W,Vertex V)
{
	PtrToAdjVNode NewNode;
	NewNode=(PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
	NewNode->AdjV=V;
	NewNode->Next=G->Graph[W].FirstEdge;
	G->Graph[W].FirstEdge=NewNode;
}

int BFS(_Graph G,Vertex V,int L)
{
	int count=0,level=0;
	Vertex last=V,tail;
	Vertex S;
	Queue Q;
	Q=CreatQ(G->Nv);
	Visited[V]=1;
	AddQ(Q,V);
	while(!IsEmpty(Q))
	{
		S=DeleteQ(Q);
		PtrToAdjVNode P;
		for(P=G->Graph[S].FirstEdge;P;P=P->Next)
		{
			if(!Visited[P->AdjV])
			{
				Visited[P->AdjV]=1;
				AddQ(Q,P->AdjV);
				tail=P->AdjV;
				count++;
			}
		}
		if(last==S)
		{
			level++;
			last=tail;
		}
		if(level==L)
			break;
	}
	return count;
}

Queue CreatQ(int N)
{
	Queue Q=(Queue)malloc(sizeof(struct QNode));
	Q->_Q=(Vertex *)malloc((N+1)*sizeof(Vertex));
	Q->front=Q->rear=-1;
	Q->Size=N+1;
	return Q;
}

void AddQ(Queue Q,Vertex V)
{
	Q->_Q[(++Q->rear)%(Q->Size)]=V;
}

Vertex DeleteQ(Queue Q)
{
	return Q->_Q [(++Q->front )%(Q->Size )];
}

int IsEmpty(Queue Q)
{
	return Q->front==Q->rear;
}

猜你喜欢

转载自blog.csdn.net/weixin_42348049/article/details/81178105