pat-a1076. Forwards on Weibo (30)

本文通过广度优先搜索(BFS)算法解决了一个关于微博社交网络中计算特定用户最大潜在转发数量的问题。给定用户间的关注关系及关注层级限制L,程序计算在L层级内的间接关注者能产生的最大转发数。

这个题我开始用深搜一直有两个点过不了。最后一个点是wa不是超时,不是很明白我深搜哪里写的不对。深搜限制层数计数访问过的节点。网上都是广搜写的。换成广搜就直接过了。后面如果有时间再看看深搜的问题。

#include<cstdio>
#include<queue>
#include<map>
#include<vector>
#include<cstring>
using namespace std;
map<int,vector<int> >g;
int l,ans=0;
int visit[1010];
struct node{
	int n,l;
	node(int a=0,int b=0):n(a),l(b){
	}
};
void bfs(int a){
	visit[a]=1;
	queue<node> q;
	q.push(node(a,0));
	while(!q.empty()){
		node temp=q.front();
		q.pop();
		int k=temp.n;
		int level=temp.l;
		if(level<l){
			int len=g[k].size();
			for(int i=0;i<len;++i){
				if(visit[g[k][i]]==0){
					q.push(node(g[k][i],level+1));
					ans++;
					visit[g[k][i]]=1;
				}
			}
		}
	}
}
int main(){
	int n,t,a;
	scanf("%d%d",&n,&l);
	for(int i=1;i<=n;++i){
		scanf("%d",&t);
		while(t--){
			scanf("%d",&a);
			g[a].push_back(i);
		}
	}
	scanf("%d",&n);
	while(n--){
		ans=0;
		memset(visit,0,sizeof(visit));
		scanf("%d",&a);
		bfs(a);
		printf("%d\n",ans);
	}
	return 0;
} 

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


<think>我们正在处理一个Git推送错误:non-fast-forward错误。用户尝试推送本地提交到远程仓库时被拒绝,因为远程分支包含本地没有的新提交,且Git默认要求快进式合并(fast-forward)。这意味着只有当本地分支直接基于远程分支的最新提交时,才能推送成功。否则,需要先整合远程更改。 解决方案通常有两种: 1. 先拉取(pull)远程更改,合并(merge)到本地分支,解决可能出现的合并冲突,然后再推送。 2. 如果确定要覆盖远程分支(例如,在个人分支上或者重置历史),可以使用强制推送(force push),但需谨慎,因为它会覆盖远程分支的历史。 根据引用[2]中的示例,强制推送可以使用`git push origin +dev:master`(其中`+`表示强制推送)或者`git push -f`(强制推送当前分支)。 但是,强制推送会重写远程分支的历史,如果该分支被其他人使用,可能会造成混乱。因此,在共享分支上,通常推荐先拉取合并。 步骤: 方案一:合并后推送(推荐在共享分支上使用) 1. 拉取远程分支:`git pull origin <branch-name>` 2. 解决可能出现的合并冲突(如果有的话)。 3. 提交合并结果:`git commit`(解决冲突后) 4. 推送:`git push origin <branch-name>` 方案二:强制推送(谨慎使用,适用于个人分支或重置历史) 1. 强制推送:`git push -f origin <branch-name>` 或 `git push origin +<branch-name>` 注意:强制推送会覆盖远程分支,如果其他人已经基于原来的提交工作,他们的工作可能会受到影响。因此,在共享分支上应避免使用强制推送,除非团队允许。 另外,还有一种情况:如果用户希望将本地分支推送到远程的不同分支(如引用[2]中的例子:将本地的dev分支推送到远程的master分支),并且允许非快进更新,则可以使用: `git push origin +dev:master` 但是,用户的问题中并没有提到分支名称,所以需要根据实际情况替换`<branch-name>`。 根据用户的问题描述,错误信息是“non-fast-forward”,因此我们给出上述两种解决方案。 生成相关问题: 1. 如何避免Git推送时的非快进错误? 2. 强制推送会有什么风险? 3. 如何撤销一次强制推送?</think>### Git推送遇到非快进错误的解决方案 当出现`non-fast-forward`错误时,意味着远程分支包含本地没有的新提交,Git默认禁止覆盖历史。以下是两种解决方案: --- #### ✅ 方案一:合并远程变更(推荐) 1. **拉取远程更新** ```bash git pull origin <分支名> ``` 这会自动尝试合并远程修改(相当于`git fetch` + `git merge`)。 2. **解决合并冲突**(如有) 编辑冲突文件后标记为已解决: ```bash git add <冲突文件> git commit -m "解决合并冲突" ``` 3. **重新推送** ```bash git push origin <分支名> ``` --- #### ⚠ 方案二:强制推送(谨慎使用) > 适用于个人分支或明确需要覆盖历史的情况,但会**覆盖远程提交**: > ```bash > git push -f origin <分支名> > # 或 > git push origin +<分支名> > ``` > 强制推送后,远程分支将完全匹配本地分支,原远程提交将变为"悬空提交"[^2]。 --- #### 💡 关键差异 | **方法** | 操作原理 | 风险等级 | 适用场景 | |----------------|--------------------------|----------|------------------------| | 合并远程变更 | 保留双方提交历史 | 低 | 协作分支、共享代码库 | | 强制推送 | 用本地历史覆盖远程历史 | 高 | 个人分支、历史重置 | --- #### 最佳实践建议 1. 协作开发时优先使用`git pull --rebase`(变基替代合并),保持提交线形整洁 2. 执行强制推送前,用`git log origin/<分支名>..<分支名>`检查将被覆盖的提交 3. 受保护分支(如`main`)需在Git仓库设置中禁用强制推送 > 例如:当远程分支有提交`A-B-C`而本地有`A-B-D`时,合并会生成新提交`E`(包含`C`和`D`),而强制推送会使远程直接变为`A-B-D`[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值