Language: C++

#include<stdio.h>
#include<vector>
#include<algorithm>
#define LL long long
using namespace std;
const int maxn=200005;
const int inf=1<<30;


struct NODE{
	int u,cost;
	bool operator<(const NODE&v)
	{
		return cost<v.cost;
	}
}d[maxn];

struct Edge{
	int from,to,w;
	
	bool operator<(const Edge&v)
	{
		return w<v.w;
	}
};

vector<Edge>g[maxn];
int f[maxn],size[maxn],rt,tot,cnt,del[maxn],w[maxn];
//f数组存储最大孩子，size是子树大小， 
void Getroot(int x,int fa)
{
	f[x]=0,size[x]=1;
	for(int i=0;i<g[x].size();i++)
	{
		Edge&e=g[x][i];
		if(del[e.to]||e.to==fa) continue;
		Getroot(e.to,x);
		f[x]=max(f[x],size[e.to]);
		size[x]+=size[e.to];
	}
	printf("x=%d f=%d sz=%d\n",x,f[x],size[x]);
	f[x]=max(f[x],tot-size[x]);//f[0]=0
	if(f[x]<f[rt]) rt=x;
} 


struct MST
{
	Edge edge[maxn*30];
	int p[maxn],ecnt;
	int find(int x){return x==p[x]?x:p[x]=find(p[x]);}
	void Addedge(int u,int v,int w) { edge[ecnt++]=Edge{u,v,w};}
	bool Union(int x,int y)
	{
		int px=find(x),py=find(y);
		if(px!=py)
			{
				p[px]=py;
				return true;
			}
		else return false;
	}
		
	
LL kruskal(int n)
{
		LL ans=0;
		for(int i=1;i<=n;i++) p[i]=i;
		sort(edge,edge+ecnt);
		for(int i=0;i<ecnt;i++)
		{
			if(Union(edge[i].from,edge[i].to))
				ans+=edge[i].w;
		}
		return ans;
	}
	
}mst;


void getnum(int o,int fa,int D)
{
	d[cnt++]=NODE{o,D+w[o]};
	for(int i=0;i<g[o].size();i++)
	{
		Edge&e=g[o][i];
		if(del[e.to]||e.to==fa) continue;
		getnum(e.to,o,D+e.w);
	}	
}

void getans(int o)
{
	cnt=0;
	getnum(o,-1,0);
	sort(d,d+cnt);
	for(int i=1;i<cnt;i++)
		mst.Addedge(d[0].u,d[i].u,d[0].cost+d[i].cost);
}


void solve(int o)
{
	del[o]=1,getans(o);
	for(int i=0;i<g[o].size();i++)
	{ 
		Edge&e=g[o][i]; 
		if(del[e.to]) continue;
		tot-=size[e.to],rt=0;
		Getroot(e.to,o),solve(rt);
	} 	
}



int main(){
  // freopen("C:\\Users\\deii\\Desktop\\test.txt","r",stdin);	
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&w[i]);
	for(int i=0;i<n-1;i++)
	{
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		g[u].push_back(Edge{u,v,w});
		g[v].push_back(Edge{v,u,w});	
	}
	f[0]=inf;
	tot=n,rt=0;
	mst.ecnt=0;
	Getroot(1,-1);
	printf("rt=%d\n",rt);
	solve(rt);
	printf("%lld",mst.kruskal(n));
	
	return 0;
}


Compiler output:
a.cc: In function 'void Getroot(int, int)':
a.cc:32:16: error: reference to 'size' is ambiguous
   32 |         f[x]=0,size[x]=1;
      |                ^~~~
In file included from /usr/include/c++/14/vector:69,
                 from a.cc:2:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:28:13: note:                 'int size [200005]'
   28 | int f[maxn],size[maxn],rt,tot,cnt,del[maxn],w[maxn];
      |             ^~~~
a.cc:38:31: error: reference to 'size' is ambiguous
   38 |                 f[x]=max(f[x],size[e.to]);
      |                               ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:28:13: note:                 'int size [200005]'
   28 | int f[maxn],size[maxn],rt,tot,cnt,del[maxn],w[maxn];
      |             ^~~~
a.cc:39:17: error: reference to 'size' is ambiguous
   39 |                 size[x]+=size[e.to];
      |                 ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:28:13: note:                 'int size [200005]'
   28 | int f[maxn],size[maxn],rt,tot,cnt,del[maxn],w[maxn];
      |             ^~~~
a.cc:39:26: error: reference to 'size' is ambiguous
   39 |                 size[x]+=size[e.to];
      |                          ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:28:13: note:                 'int size [200005]'
   28 | int f[maxn],size[maxn],rt,tot,cnt,del[maxn],w[maxn];
      |             ^~~~
a.cc:41:43: error: reference to 'size' is ambiguous
   41 |         printf("x=%d f=%d sz=%d\n",x,f[x],size[x]);
      |                                           ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:28:13: note:                 'int size [200005]'
   28 | int f[maxn],size[maxn],rt,tot,cnt,del[maxn],w[maxn];
      |             ^~~~
a.cc:42:27: error: reference to 'size' is ambiguous
   42 |         f[x]=max(f[x],tot-size[x]);//f[0]=0
      |                           ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:28:13: note:                 'int size [200005]'
   28 | int f[maxn],size[maxn],rt,tot,cnt,del[maxn],w[maxn];
      |             ^~~~
a.cc: In function 'void solve(int)':
a.cc:109:22: error: reference to 'size' is ambiguous
  109 |                 tot-=size[e.to],rt=0;
      |                      ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:28:13: note:                 'int size [200005]'
   28 | int f[maxn],size[maxn],rt,tot,cnt,del[maxn],w[maxn];
      |             ^~~~