Language: C++

#include "bits/stdc++.h"
using namespace std;
vector<int> to,head,next;
vector<long long> f,g;
vector<vector<long long> > tmp;
vector<vector<long long> > pre,bac;
int mod,tot=0;
void addedge(int u,int v)
{
    tot++;
    next[tot]=head[u];
    to[tot]=v;
    head[u]=tot;
}
void dfs(int index,int father)
{
    int i,len;
    f[index]=1;
    for(i=head[index];i;i=next[i])
    {
	if(to[i]==father)
	{
	    continue;
	}
	dfs(to[i],index);
	f[index]=f[index]*(f[to[i]]+1)%mod;
        tmp[index].push_back(f[to[i]]);
    }
    len=tmp[index].size();
    if(len==0)
    {
	return;
    }
    pre[index].resize(len+1);
    bac[index].resize(len+1);
    pre[index][0]=1;
    for(i=1;i<len;i++)
    {
	pre[index][i]=pre[index][i-1]*(tmp[index][i-1]+1)%mod;
    }
    bac[index][len-1]=1;
    for(i=len-2;i>=0;i--)
    {
	bac[index][i]=bac[index][i+1]*(tmp[index][i+1]+1)%mod;
    }
}

void dfs1(int index,int father,long long value)
{
    int len,i,j=0;
    long long ret;
    len=tmp[index].size();
    for(i=head[index];i;i=next[i],j++)
    {
	if(to[i]==father)
	{
	    j--;
	    continue;
	}
	g[to[i]]=((value*pre[index][j])%mod*bac[index][j]+1)%mod;
	dfs1(to[i],index,g[to[i]]);
    }
} 
int main()
{
    int n,m;
    int i,x,y;
    scanf("%d %d",&n,&mod);
    m=n-1;
    tmp.resize(n+1);
    f.resize(n+1);
    g.resize(n+1);
    to.resize(2*m+1);
    next.resize(2*m+1);
    head.resize(n+1);
    pre.resize(n+1);
    bac.resize(n+1);
    for(i=1;i<=m;i++)
    {
	scanf("%d %d",&x,&y);	
        addedge(x,y);
        addedge(y,x);
    }
    dfs(1,0);
    g[1]=1;
    dfs1(1,0,1);
    for(i=1;i<=n;i++)
    {
        printf("%lld\n",f[i]*g[i]%mod);
    }
    return 0;
}

Compiler output:
a.cc: In function 'void addedge(int, int)':
a.cc:11:5: error: reference to 'next' is ambiguous
   11 |     next[tot]=head[u];
      |     ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 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:3:21: note:                 'std::vector<int> next'
    3 | vector<int> to,head,next;
      |                     ^~~~
a.cc: In function 'void dfs(int, int)':
a.cc:19:27: error: reference to 'next' is ambiguous
   19 |     for(i=head[index];i;i=next[i])
      |                           ^~~~
/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:3:21: note:                 'std::vector<int> next'
    3 | vector<int> to,head,next;
      |                     ^~~~
a.cc: In function 'void dfs1(int, int, long long int)':
a.cc:53:27: error: reference to 'next' is ambiguous
   53 |     for(i=head[index];i;i=next[i],j++)
      |                           ^~~~
/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:3:21: note:                 'std::vector<int> next'
    3 | vector<int> to,head,next;
      |                     ^~~~
a.cc: In function 'int main()':
a.cc:74:5: error: reference to 'next' is ambiguous
   74 |     next.resize(2*m+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:3:21: note:                 'std::vector<int> next'
    3 | vector<int> to,head,next;
      |                     ^~~~