关于85分的问题
查看原帖
关于85分的问题
177535
CDFLS_mao_zx楼主2020/10/2 22:34

给大家两份代码,85的可以参考一下:
100:

void dfs(int u)
{
	ans[u]+=ans[fa[u]];
	if(ch[u]=='(')
	{
		st[++top]=u;
		op[u]=1e9;
	}
	else
	{
		if(top)
		{
			op[u]=st[top--];
			c[u]=c[fa[op[u]]]+1;
			ans[u]+=c[u];
		}
	}
	for(int i=head[u];i;i=a[i].next)
	dfs(a[i].v);
	if(op[u]==1e9)
	top--;
	else
	if(op[u]!=0)
	st[++top]=op[u];
}

85分:

void dfs(int u)
{
	ans[u]+=ans[fa[u]];
	if(ch[u]=='(')
	{
		st[++top]=u;
		op[u]=1e9;
	}
	else
	{
		if(top)
		{
			op[u]=st[top--];
			c[u]=c[fa[op[u]]]+1;
			ans[u]+=c[u];
		}
	}
	for(int i=head[u];i;i=a[i].next)
	dfs(a[i].v);
	if(op[u]==1e9)
	top--;
	else
	st[++top]=op[u];
}

原因自己看吧,友情提醒各位注意细节问题。

2020/10/2 22:34
加载中...