Language: C++

#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef pair<int,int> pii;

const int N=100010;

int fa[N],deg[N],next[4*N],b[4*N],head[N],tail[N];
map<pii,int> a;
queue<pii> q;

int find(int x)
{
    if (x!=fa[x]) fa[x]=find(fa[x]);
    return fa[x];
}

int main()
{
    int n,m=0;scanf("%d",&n);
    for (int i=1;i<n;i++)
    {
        int x,y;scanf("%d%d",&x,&y);
        if (x>y) swap(x,y);a[make_pair(x,y)]++;
        q.push(make_pair(x,y));deg[x]++;deg[y]++;
        b[++m]=y;if (!head[x]) head[x]=m; else next[tail[x]]=m;tail[x]=m;
        b[++m]=x;if (!head[y]) head[y]=m; else next[tail[y]]=m;tail[y]=m;
    }
    for (int i=1;i<n;i++)
    {
        int x,y;scanf("%d%d",&x,&y);
        if (x>y) swap(x,y);a[make_pair(x,y)]++;
        q.push(make_pair(x,y));deg[x]++;deg[y]++;
        b[++m]=y;if (!head[x]) head[x]=m; else next[tail[x]]=m;tail[x]=m;
        b[++m]=x;if (!head[y]) head[y]=m; else next[tail[y]]=m;tail[y]=m;
    }
    for (int i=1;i<=n;i++) fa[i]=i;
    for (int i=1;i<n;i++)
    {
        while (!q.empty())
        {
            pii t=q.front();
            int x=t.first,y=t.second;
            if (x>y) swap(x,y);
            if (a[make_pair(x,y)]!=2||find(x)==find(y)) {q.pop();continue;}
            if (deg[x]>deg[y]) swap(x,y);
            int p=head[x];
            while (p)
            {
                int u=find(b[p]),v=x;
                if (u>v) swap(u,v);
                a[make_pair(u,v)]--;
                u=find(b[p]);v=y;
                if (u>v) swap(u,v);
                a[make_pair(u,v)]++;
                q.push(make_pair(u,v));
                p=next[p];
            }
            deg[y]+=deg[x];fa[x]=y;
            next[tail[y]]=head[x];tail[y]=tail[x];
            break;
        }
        if (q.empty()) {puts("NO");return 0;}
    }
    puts("YES");
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:32:48: error: reference to 'next' is ambiguous
   32 |         b[++m]=y;if (!head[x]) head[x]=m; else next[tail[x]]=m;tail[x]=m;
      |                                                ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/bits/stl_tree.h:63,
                 from /usr/include/c++/14/map:62,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:14:18: note:                 'int next [400040]'
   14 | int fa[N],deg[N],next[4*N],b[4*N],head[N],tail[N];
      |                  ^~~~
a.cc:33:48: error: reference to 'next' is ambiguous
   33 |         b[++m]=x;if (!head[y]) head[y]=m; else next[tail[y]]=m;tail[y]=m;
      |                                                ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:14:18: note:                 'int next [400040]'
   14 | int fa[N],deg[N],next[4*N],b[4*N],head[N],tail[N];
      |                  ^~~~
a.cc:40:48: error: reference to 'next' is ambiguous
   40 |         b[++m]=y;if (!head[x]) head[x]=m; else next[tail[x]]=m;tail[x]=m;
      |                                                ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:14:18: note:                 'int next [400040]'
   14 | int fa[N],deg[N],next[4*N],b[4*N],head[N],tail[N];
      |                  ^~~~
a.cc:41:48: error: reference to 'next' is ambiguous
   41 |         b[++m]=x;if (!head[y]) head[y]=m; else next[tail[y]]=m;tail[y]=m;
      |                                                ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:14:18: note:                 'int next [400040]'
   14 | int fa[N],deg[N],next[4*N],b[4*N],head[N],tail[N];
      |                  ^~~~
a.cc:63:19: error: reference to 'next' is ambiguous
   63 |                 p=next[p];
      |                   ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:14:18: note:                 'int next [400040]'
   14 | int fa[N],deg[N],next[4*N],b[4*N],head[N],tail[N];
      |                  ^~~~
a.cc:66:13: error: reference to 'next' is ambiguous
   66 |             next[tail[y]]=head[x];tail[y]=tail[x];
      |             ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:14:18: note:                 'int next [400040]'
   14 | int fa[N],deg[N],next[4*N],b[4*N],head[N],tail[N];
      |                  ^~~~