Language: C++

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<string>
#include<stack>
#include<cstdio>
#include<cmath>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<int,P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i,x) for(int i=0;i<x;i++)
#define rep1(i,x) for(int i=1;i<=x;i++)
#define rrep(i,x) for(int i=x-1;i>=0;i--)
#define rrep1(i,x) for(int i=x;i>0;i--)
#define sor(v) sort(v.begin(),v.end())
#define rev(s) reverse(s.begin(),s.end())
#define lb(vec,a) lower_bound(vec.begin(),vec.end(),a)
#define ub(vec,a) upper_bound(vec.begin(),vec.end(),a)
#define uniq(vec) vec.erase(unique(vec.begin(),vec.end()),vec.end())
#define mp1(a,b,c) P1(a,P(b,c))

const int INF=1000000000;
const int dir_4[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
const int dir_8[8][2]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};

vector<int> nullvi;

struct UF{
	vector<int> par,r;
	void init(int n){
		rep(i,n+1){
			par.pb(i);
			r.pb(0);
		}
	}
	int find(int x){
		if(par[x] == x)return x;
		return par[x] = find(par[x]);
	}
	bool same(int x,int y){
		return find(x) == find(y);
	}
	void unit(int x,int y){
		if(same(x,y))return;
		x = find(x);
		y = find(y);
		if(r[x] < r[y]){
			par[x] = y;
		}
		else {
			par[y] = x;
			if(r[x] == r[y]){
				r[x] ++;
			}
		}
	}
};

struct HL{
	int V;
	vector<vector<int>> G;
	int x,y;
	vector<int> d[2];
	int D;
	vector<int> id;
	vector<P> loc;
	UF uf;
	vector<HL*> ch;
	vector<vector<int>> ch_ids;
	vector<int> par,root;
	void init(int _V){
		V = _V;
		rep(i,V+1){
			G.push_back(nullvi);
			d[0].push_back(-1);
			d[1].push_back(-1);
			loc.push_back(P(-1,-1));
		}
		//G = vector<vector<int>>(V+1);
	}
	void add_edge(int a,int b){
		G[a].pb(b); G[b].pb(a);
	}
	void dfs(int v,int p,int t){
		d[t][v] = d[t][p]+1;
		for(int u: G[v]){
			if(u == p)continue;
			dfs(u,v,t);
		}
	}
	void dia(){
		x = 1;
		d[0][0] = -1;
		dfs(x,0,0);
		rep1(i,V)if(d[0][i]>d[0][x])x=i;
		d[1][0] = -1;
		dfs(x,0,0);
		y = 1;
		rep1(i,V)if(d[0][i]>d[0][y])y=i;
		dfs(y,0,1);
	}
	void run_HL(){
		/*cerr << "==========" << endl;
		cerr << V << endl;
		rep1(i,V){
			for(int j: G[i]){
				cerr << i << " " << j << endl;
			}
		}*/
		dia();
		D = d[0][y];
		rep(i,D+1){
			id.push_back(-1);
		}
		rep1(i,V){
			if(d[0][i]+d[1][i] == D){
				id[d[0][i]] = i;
				loc[i] = P(-1,d[0][i]);
			}
			else loc[i] = P(-2,0);
		}
		uf.init(V);
		rep1(i,V){
			if(loc[i].fr != -2)continue;
			for(int j: G[i]){
				if(loc[j].fr == -2)uf.unit(i,j);
			}
		}
		rep1(i,V){
			if(loc[i].fr == -1)continue;
			if(loc[uf.find(i)].fr == -2){
				loc[uf.find(i)].fr = ch_ids.size();
				ch.push_back(new HL);
				ch_ids.push_back(nullvi);
				par.push_back(0);
				root.push_back(0);
				ch_ids[loc[uf.find(i)].fr].push_back(uf.find(i));
				loc[uf.find(i)].sc = 1;
			}
			if(uf.find(i) != i){
				loc[i].fr = loc[uf.find(i)].fr;
				ch_ids[loc[i].fr].push_back(i);
				loc[i].sc = ch_ids[uf.find(i)].size();
			}
		}
		rep(i,ch_ids.size()){
			ch[i]->init(ch_ids[i].size());
			int _v = ch_ids[i][0];
			par[i] = id[d[0][_v]-(d[0][_v]+d[1][_v]-D)/2];
			for(int v: ch_ids[i]){
				for(int u: G[v]){
					if(u == par[i])root[i] = v;
					else if(loc[v].sc < loc[u].sc)ch[i]->add_edge(loc[v].sc,loc[u].sc);
				}
			}
			ch[i]->run_HL();
		}
		run_adhoc();
	}
	void run_adhoc(){
		
	void out(){
		cerr << "diameter:";
		for(int v: id)cerr << v << " ";
		cerr << endl;
		rep1(i,V){
			cerr << i << ":" << loc[i].fr << " " << loc[i].sc << endl;
		}
		rep(i,ch.size()){
			cerr << "=========ch[" << i << "]==========" << endl;
			cerr << "par=" << par[i] << ",root=" << root[i] << endl;
			ch[i]->out();
		}
	}
};

int main(){
	static int n;
	static int a[100010],b[100010];
	static int c[100010],d[100010];
	scanf("%d",&n);
	rep1(i,n-1)scanf("%d%d",&a[i],&b[i]);
	rep1(i,n-1)scanf("%d%d",&c[i],&d[i]);
	
	static HL* tree = new HL;
	tree->init(n);
	rep1(i,n-1){
		tree->add_edge(a[i],b[i]);
	}
	tree->run_HL();
	//tree->out();
}


Compiler output:
a.cc:203:2: error: expected '}' at end of input
  203 | }
      |  ^
a.cc:70:10: note: to match this '{'
   70 | struct HL{
      |          ^
a.cc: In member function 'void HL::run_adhoc()':
a.cc:173:19: error: a function-definition is not allowed here before '{' token
  173 |         void out(){
      |                   ^
a.cc: In member function 'int HL::main()':
a.cc:203:1: warning: no return statement in function returning non-void [-Wreturn-type]
  203 | }
      | ^
a.cc: At global scope:
a.cc:203:2: error: expected unqualified-id at end of input
  203 | }
      |  ^