1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| int H_son[maxn],siz[maxn],fa[maxn],dep[maxn],in[maxn],out[maxn],top[maxn],times; void getH(int x){ siz[x]=1;dep[x]=dep[fa[x]]+1;H_son[x]=0; for (int j=lnk[x];j;j=nxt[j]) if (son[j]!=fa[x]){ fa[son[j]]=x; getH(son[j]); siz[x]+=siz[son[j]]; if (!H_son[x]||siz[H_son[x]]<siz[son[j]]) H_son[x]=son[j]; } } void dfs(int x,int lst){ top[x]=lst;in[x]=++times;id[times]=x; if (H_son[x]) dfs(H_son[x],lst); for (int j=lnk[x];j;j=nxt[j]) if (son[j]!=fa[x]&&son[j]!=H_son[x]) dfs(son[j],son[j]); out[x]=times; } void insert(int x,int y,int w){ while (top[x]!=top[y]){ if (dep[top[x]]<dep[top[y]]) swap(x,y); ist(1,1,N,in[top[x]],in[x],w); x=fa[top[x]]; } if (in[x]>in[y]) swap(x,y); ist(1,1,N,in[x],in[y],w); } int query(int x,int y){ int res=0; while (top[x]!=top[y]){ if (dep[top[x]]<dep[top[y]]) swap(x,y); res=max(res,qry(1,1,N,in[top[x]],in[x])); x=fa[top[x]]; } if (in[x]>in[y]) swap(x,y); return max(res,qry(1,1,N,in[x],in[y])); }
|