给大家两份代码,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];
}
原因自己看吧,友情提醒各位注意细节问题。