使用前初始化 ;

int sum, n, rt;
int sz[N], mx[N], vis[N];
struct _ {int to,w;};
vector<_> g[N];

void getrt(int x, int fa) {
    mx[x]=0, sz[x]=1;
    for (_ e:g[x]) if (!vis[e.to]&&e.to!=fa) {
		int y = e.to;
        getrt(y,x),sz[x]+=sz[y];
        mx[x]=max(mx[x],sz[y]);
    }
    mx[x]=max(mx[x],sum-sz[x]);
    if (mx[rt]>mx[x]) rt=x;
}
void solve(int x) {
	vis[x] = 1;
	for (_ e:g[x]) if (!vis[e.to]) {
		int y = e.to;
	}
	for (_ e:g[x]) if (!vis[e.to]) {
		mx[rt=0]=n,sum=sz[e.to];
		getrt(e.to,0), solve(rt);
	}
}

 

相关文章:

  • 2021-09-18
  • 2021-07-13
  • 2021-06-25
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-02-27
  • 2021-10-09
  • 2021-08-08
  • 2021-09-22
相关资源
相似解决方案