1076 Forwards on Weibo

本文介绍了一个基于BFS算法的微博转发潜力计算模型,该模型能够计算特定用户在微博社交网络中,考虑到L层级间接关注者时的最大转发潜力。通过输入用户数量、层级数以及用户关注关系,模型可以精确计算出每个查询用户的转发影响力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.youkuaiyun.com/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));
	}
}

 

### CSS `animation-fill-mode` 属性中的 `forwards` 值 当定义动画时,`animation-fill-mode` 属性指定了目标元素在动画执行之前或之后应用哪些样式。对于 `forwards` 值而言,在动画完成后,被动画影响的属性会保留其最后的关键帧值(而不是恢复到初始状态)。这意味着即使动画结束,最终的状态仍然会被保持。 #### 使用示例 下面是一个简单的例子来展示如何使用带有 `forwards` 的 `animation-fill-mode`: ```css /* 定义关键帧 */ @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } } /* 应用动画并设置 fill mode */ .fade-in-element { animation-name: fadeIn; animation-duration: 2s; animation-fill-mode: forwards; } ``` 在这个案例里,`.fade-in-element` 类下的 HTML 元素将会逐渐变得不透明,并且一旦动画完成,这些元素将继续维持完全可见的状态,而不会返回原来的不可见形式[^1]。 #### 实际应用场景 考虑一个按钮点击后显示提示框的情况。可以利用 `forwards` 来确保提示框在关闭前一直保持最新的视觉效果直到手动隐藏它为止。 ```html <button id="show-toast">Show Toast</button> <div class="toast hidden"></div> <style> .hidden { display:none;} .toast { /* 初始状态下隐藏 toast */ visibility:hidden; @keyframes slideInDown { from { transform: translateY(-100%); } to { transform: translateY(0); } } animation-name:slideInDown ; animation-duration:.5s; animation-fill-mode:forwards; /* 动画结束后不再隐藏 */ visibility:visible !important; } </style> <script> document.getElementById('show-toast').addEventListener('click', function() { document.querySelector('.toast').classList.remove('hidden'); }); </script> ``` 这段代码展示了通过移除 `.hidden` 类使提示框显现出来的同时启动下滑进入屏幕中心位置的效果;由于设置了 `forwards`, 提示框将在动画结束后停留在屏幕上直至进一步操作将其再次隐藏.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值