Language: C++

#include <bits/stdc++.h>
#define F first
#define S second
#define X real()
#define Y imag()
using namespace std;
typedef long long ll;
typedef long double ld;

struct Node {
	Node* c[2], *p;
	int id, rev;
	int v, mv;
	int av;
	int isr() {
		return !p||(p->c[0]!=this&&p->c[1]!=this);
	}
	int dir() {
		return p->c[1]==this;
	}
	void add(int vv){
		av+=vv;
		v+=vv;
		mv+=vv;
	}
	void push() {
		if (rev) {
			swap(c[0], c[1]);
			if (c[0]) c[0]->rev^=1;
			if (c[1]) c[1]->rev^=1;
			rev=0;
		}
		if (av!=0){
			if (c[0]) c[0]->add(av);
			if (c[1]) c[1]->add(av);
			av=0;
		}
	}
	void upd(){
		push();
		mv=v;
		if (c[0]) mv=min(mv, c[0]->mv);
		if (c[1]) mv=min(mv, c[1]->mv);
	}
	void setc(Node* s, int d) {
		c[d]=s;
		if (s) s->p=this;
		upd();
	}
	Node(int i, int val) : id(i), v(val), mv(val) {
		c[0]=0;c[1]=0;p=0;rev=0;
		av=0;
	}
};
struct LinkCut {
	void rot(Node* x) {
		Node* p=x->p;int d=x->dir();
		if (!p->isr()) p->p->setc(x, p->dir());
		else x->p=p->p;
		p->setc(x->c[!d], d);x->setc(p, !d);
	}
	void pp(Node* x) {
		if (!x->isr()) pp(x->p);
		x->push();
	}
	void splay(Node* x) {
		pp(x);
		while (!x->isr()) {
			if (x->p->isr()) rot(x);
			else if(x->dir()==x->p->dir()) {
				rot(x->p);rot(x);
			}
			else {
				rot(x);rot(x);
			}
		}
	}
	Node* expose(Node* x) {
		Node* q=0;
		for (;x;x=x->p) {
			splay(x);x->c[1]=q;x->upd();q=x;
		}
		return q;
	}
	void evert(Node* x) {
		x=expose(x);x->rev^=1;x->push();
	}
	void link(Node* x, Node* y) {
		evert(x);evert(y);splay(y);x->setc(y, 1);
	}
	void expose(Node* x, Node* y) {
		evert(x);expose(y);splay(x);
	}
	void cut(Node* x, Node* y) {
		expose(x, y);x->c[1]=0;x->upd();y->p=0;
	} 
	int rootid(Node* x) {
		expose(x);splay(x);
		while(x->c[0]) {
			x=x->c[0];x->push();
		}
		splay(x);
		return x->id;
	}
	Node* getMin(Node* x) {
		x->push();
		if (x->v==x->mv) {
			splay(x);
			return x;
		}
		if (x->c[0]&&x->c[0]->mv==x->mv) return getMin(x->c[0]);
		else return getMin(x->c[1]);
	}
	Node* getMinP(Node* x, Node* y) {
		expose(x, y);
		return getMin(x);
	}
	void addP(Node* x, Node* y, int v){
		expose(x, y);
		x->add(v);
	}
};

Node* nn[101010];
int u[101010];
int v[101010];
Node* en[101010];

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin>>n;
	LinkCut lc;
	for (int i=1;i<=n;i++){
		nn[i]=new Node(n+i, 10*n);
	}
	for (int i=0;i<n-1;i++){
		cin>>u[i]>>v[i];
		en[i]=new Node(i, 0);
		lc.link(en[i], nn[u[i]]);
		lc.link(en[i], nn[v[i]]);
	}
	set<pair<int, int> > ps;
	for (int i=0;i<n-1;i++){
		int a,b;
		cin>>a>>b;
		ps.insert({a, b});
		lc.addP(nn[a], nn[b], 1);
	}
	while (1){
		vector<pair<int, pair<int, int> > > nps;
		int f1=0;
		for (auto p:ps){
			auto miv=lc.getMinP(nn[p.F], nn[p.S]);
			if (miv->mv>0) nps.push_back({miv->mv, p});
			if (f1>10&&nps.size()>10000) break;
		}
		if (nps.size()==0){
			cout<<"YES"<<endl;
			return 0;
		}
		sort(nps.begin(), nps.end());
		if (nps[0].F>1){
			cout<<"NO"<<endl;
			return 0;
		}
		for (auto p:nps){
			auto miv=lc.getMinP(nn[p.S.F], nn[p.S.S]);
			if (miv->mv>1){
				np.push_back(p.S);
			}
			else if(miv->mv==1){
				lc.addP(nn[p.S.F], nn[p.S.S], -1);
				ps.erase(p.S);
			}
		}
	}
}

Compiler output:
a.cc: In function 'int main()':
a.cc:171:33: error: 'np' was not declared in this scope; did you mean 'p'?
  171 |                                 np.push_back(p.S);
      |                                 ^~
      |                                 p