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

       这道题跟PTA上的另一道题“六度空间”很像https://blog.csdn.net/Authur520/article/details/85064582,只不过那道题是无向图,而这道题是有向图,不过都是使用BFS,所以我就照搬之前写那道题的框架,然后修改了一下建立图的过程就完美解决了。我这里存储图使用了邻接表,其实使用邻接矩阵也可以。

      具体代码实现如下:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define TRUE 1
#define FALSE 0
#define MaxVertexNum 1005  //最大顶点数设为1005
typedef int Vertex;       //用顶点下标表示顶点,为整型

/* 边的定义 */
typedef struct ENode *PtrToENode;
struct ENode{
    Vertex V1, V2;
};
typedef PtrToENode Edge;

/* 邻接点的定义 */
typedef struct AdjVNode *PtrToAdjVNode; 
struct AdjVNode{
    Vertex AdjV;           /* 邻接点下标 */
    PtrToAdjVNode Next;    /* 指向下一个邻接点的指针 */
};

/* 顶点表头结点的定义 */
typedef struct Vnode{
    PtrToAdjVNode FirstEdge;  /* 边表头指针 */
} AdjList[MaxVertexNum];      /* AdjList是邻接表类型 */

/* 图结点的定义 */
typedef struct GNode *PtrToGNode;
struct GNode{  
    int Nv;     /* 顶点数 */
    int Ne;     /* 边数   */
    AdjList G;  /* 邻接表 */
};
typedef PtrToGNode LGraph; /* 以邻接表方式存储的图类型 */

Vertex Visited[MaxVertexNum] = {FALSE};

LGraph BuildGraph();
void Forwards_on_Weibo(LGraph Graph);

int L;

int main()
{
	LGraph graph;
	
	graph = BuildGraph(); 
	Forwards_on_Weibo(graph);
	
	return 0;
}

LGraph CreateGraph( int VertexNum )
{ /* 初始化一个有VertexNum个顶点但没有边的图 */
    Vertex V;
    LGraph Graph;
     
    Graph = (LGraph)malloc(sizeof(struct GNode)); /* 建立图 */
    Graph->Nv = VertexNum;
    Graph->Ne = 0;
    /* 初始化邻接表头指针 */
    for (V=1; V<=Graph->Nv; V++)
        Graph->G[V].FirstEdge = NULL;
             
    return Graph; 
}

void InsertEdge( LGraph Graph, Edge E )
{
    PtrToAdjVNode NewNode;
     
    /* 插入边 <V1, V2> */  //表示V2关注了V1 
    /* 为V2建立新的邻接点 */
    NewNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
    NewNode->AdjV = E->V2;
    /* 将V2插入V1的表头 */
    NewNode->Next = Graph->G[E->V1].FirstEdge;
    Graph->G[E->V1].FirstEdge = NewNode;
}

LGraph BuildGraph()
{
    LGraph Graph;
    Edge E;
    Vertex V;
    int N,i,j,num;
    
    scanf("%d %d",&N,&L);  //读入用户数及层级数
	Graph = CreateGraph(N); /*初始化有N个顶点但没有边的图*/ 
    
    for(j=1; j<=N; j++){ 
    	scanf("%d", &num);   /* 读入某个用户关注的人数 */
    	if ( num ) { /* 如果用户关注了人 */
        	E = (Edge)malloc( sizeof(struct ENode) ); /* 建立边结点 */
        	E->V2 = j;  //当前用户是关注者 
        	for (i=1; i<=num; i++) {
            	scanf("%d", &E->V1);   //读入被关注者 
            	InsertEdge(Graph, E);  
        	}
    	}
	}
 
    return Graph;
}

int BFS (LGraph Graph, Vertex S)
{   /* 以S为出发点对邻接表存储的图Graph进行BFS搜索 */
    Vertex V;
	PtrToAdjVNode W;
    Vertex a[MaxVertexNum];  //结点队列
    int head = 0,tail = 0;   //队列头尾指针 
    int count = 0,level = 0;  //count计数满足微博转发潜在接受者的结点数,level计数当前BFS的层数 
    int curlast,last;  //curlast为当前BFS访问层所访问的最后一个结点,last为上一BFS访问层所访问的最后一个结点 
 
    Visited[S] = TRUE; /* 标记S已访问 */
    a[tail++] = S; /* S入队列 */
    curlast = S;
	last = S;
     
    while ( head < tail ) {   //队列不为空时 
        V = a[head++];  /* 弹出V */
        for( W=Graph->G[V].FirstEdge; W; W=W->Next ){/* 对V的每个邻接点W->AdjV */
  			if ( !Visited[W->AdjV] ) {  /* 若W->AdjV未被访问 */
                Visited[W->AdjV] = TRUE; /* 标记W已访问 */
                count++;
                a[tail++] = W->AdjV; /* W入队列 */
                curlast = W->AdjV;  // 更新curlast 
            }
		}
        if(V == last){
        	level++;
        	last = curlast;
		}
		if(level == L)  break;
    } /* while结束*/
    
    return count;
}

void Forwards_on_Weibo(LGraph Graph)
{
	int cnt = 0;
	int i,check;  //要查询的人数 
	Vertex V;     //要查询的用户ID 
	
	scanf("%d",&check); 
	for(i=1; i<=check; i++){ 
	    scanf("%d",&V);
		cnt = BFS(Graph,V);
		printf("%d\n",cnt);
		memset(Visited,FALSE,sizeof(Visited));
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值