存个板子,内容以后补
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+50;
int ecnt=-1,head[maxn];
int son[maxn],siz[maxn];
int tot,n,root;
bool vis[maxn];
struct mint
{
int nxt,v,w;
}e[maxn<<1];
struct node
{
vector<int> anc;
vector<int> child;
}nd[maxn];
inline void addline(int u,int v,int w)
{
e[++ecnt].nxt=head[u];
e[ecnt].v=v;
e[ecnt].w=w;
head[u]=ecnt;
}
void getG(int now,int fa)
{
siz[now]=1;
son[now]=0;
for(int i=head[now];~i;i=e[i].nxt)
{
int v=e[i].v;
if(v==fa || vis[v]) continue;
getG(v,now);
siz[now]+=siz[v];
son[now]=max(son[now],siz[v]);
}
son[now]=max(son[now],tot-siz[now]);
if(son[now]<maxp)
{
maxp=son[now];
root=now;
}
}
void build(int now,ntot)
{
tot=ntot;
maxp=0x7f7f7f7f;
getG(now,0);
int g=root;
vis[g]=1;
for(int i=head[g];~i;i=e[i].nxt)
{
int v=e[i].v;
if(vis[g]) continue;
int tmp=build(v,ntot-son[v]);
nd[tmp].fa=now;
nd[now].child.push_back(tmp);
}
return g;
}
int main()
{
memset(head,-1,sizeof(head));
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addline(u,v,w);
addline(v,u,w);
}
int virtroot=build(1,n);
return 0;
}