Language: C++








//2つ目のdfs2
void dfs2(int a,int par){ 
  
  if( par == -1 ){ dp2[a] = 1LL; }
  
  vector<int> l_prod((int)to[a].size(),1);
  vector<int> r_prod((int)to[a].size(),1);
  
  for(int i = 0; i < (int)to[a].size(); i++){ 
    
    int res = 1LL;
    
    if( i-1 >= 0 ){ res *= l_prod[i-1];}
    
    if( to[a][i] != par ){ res *= dp1[to[a][i]];}
    
    res %= m;
    
    l_prod[i] = res;
    
  }
  
  
  for(int i = (int)to[a].size()-1; i >= 0; i--){ 
    
    int res = 1LL;
    
    if( to[a][i] != par ){ res *= dp1[to[a][i]];}
    
    if( i+1 < to[a].size()){ res  *= r_prod[i+1];}
    
    res %= m;
    
    r_prod[i] = res;
  }
  
  if(false){ 
    if( a == 0 && par == -1){ 
      cout << "L_prod check" << endl;
      for(int i = 0; i < (int)to[a].size(); i++){ 
        cout << l_prod[i] << " ";
      }
      cout << endl;
      cout << "R_prod check" << endl;
      for(int i = 0; i < (int)to[a].size(); i++){ 
        cout << r_prod[i] << " ";
      }
      
    }
    cout << endl;
    
  } 
  
  
  
  
  for(int j = 0; j  < (int)to[a].size(); j++){ 
    
    int res = 1LL;
    
    if( to[a][j] == par ){ continue;} 
    
    if( j-1 >= 0 ){ res *= l_prod[j-1];}
    if( j+1 < to[a].size()){ res *= r_prod[j+1];}
    
    res %= m;
    
    res = (res*dp2[a])%m;
    
    res = ( res+1 ) % m;
    
    dp2[to[a][j]] = res;
    //+1済み
    
    dfs2(to[a][j],a);
    
  }
  
  return ;
}
    

//３つ目のdfs 
void dfs3(int a, int par){ 
  
  int res = dp2[a];
  
  for(auto ne : to[a] ){ 
    
    if( ne == par ){ continue;} 
    
    res *= dp1[ne];
    res %= m;
    
    dfs3(ne,a);
    
  }
  
  ans[a] = res%m;
  
  return ;
}
  

signed main(void){ 
  
  cin >> n >> m;
  to.resize(n);
  for(int i = 0; i < n-1; i++){ 
    int a,b; cin >> a >> b;
    a--; b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  
  dfs1(0,-1);
  
  if(true){ 
  dfs2(0,-1);
  }
  
  dfs3(0,-1);
  
  if(true){ 
  for(int i = 0; i < n; i++){ 
    cout << ans[i] << endl;
  }
    
  }
  
  if(false){ 
    
    cout << endl;
    
    for(int i = 0; i < n; i++){ 
      cout << dp1[i] << " ";
    }
    
    cout << endl;
    
    for(int i = 0; i < n; i++){ 
      cout << dp2[i] << " " ;
    }
    
    cout << endl;
  } 
  
  return 0;
}
  

Compiler output:
a.cc: In function 'void dfs2(int, int)':
a.cc:11:20: error: 'dp2' was not declared in this scope
   11 |   if( par == -1 ){ dp2[a] = 1LL; }
      |                    ^~~
a.cc:13:3: error: 'vector' was not declared in this scope
   13 |   vector<int> l_prod((int)to[a].size(),1);
      |   ^~~~~~
a.cc:13:10: error: expected primary-expression before 'int'
   13 |   vector<int> l_prod((int)to[a].size(),1);
      |          ^~~
a.cc:14:10: error: expected primary-expression before 'int'
   14 |   vector<int> r_prod((int)to[a].size(),1);
      |          ^~~
a.cc:16:27: error: 'to' was not declared in this scope
   16 |   for(int i = 0; i < (int)to[a].size(); i++){
      |                           ^~
a.cc:20:28: error: 'l_prod' was not declared in this scope
   20 |     if( i-1 >= 0 ){ res *= l_prod[i-1];}
      |                            ^~~~~~
a.cc:22:35: error: 'dp1' was not declared in this scope
   22 |     if( to[a][i] != par ){ res *= dp1[to[a][i]];}
      |                                   ^~~
a.cc:24:12: error: 'm' was not declared in this scope
   24 |     res %= m;
      |            ^
a.cc:26:5: error: 'l_prod' was not declared in this scope
   26 |     l_prod[i] = res;
      |     ^~~~~~
a.cc:31:20: error: 'to' was not declared in this scope
   31 |   for(int i = (int)to[a].size()-1; i >= 0; i--){
      |                    ^~
a.cc:35:35: error: 'dp1' was not declared in this scope
   35 |     if( to[a][i] != par ){ res *= dp1[to[a][i]];}
      |                                   ^~~
a.cc:37:38: error: 'r_prod' was not declared in this scope
   37 |     if( i+1 < to[a].size()){ res  *= r_prod[i+1];}
      |                                      ^~~~~~
a.cc:39:12: error: 'm' was not declared in this scope
   39 |     res %= m;
      |            ^
a.cc:41:5: error: 'r_prod' was not declared in this scope
   41 |     r_prod[i] = res;
      |     ^~~~~~
a.cc:46:7: error: 'cout' was not declared in this scope
   46 |       cout << "L_prod check" << endl;
      |       ^~~~
a.cc:46:33: error: 'endl' was not declared in this scope
   46 |       cout << "L_prod check" << endl;
      |                                 ^~~~
a.cc:47:31: error: 'to' was not declared in this scope
   47 |       for(int i = 0; i < (int)to[a].size(); i++){
      |                               ^~
a.cc:48:17: error: 'l_prod' was not declared in this scope
   48 |         cout << l_prod[i] << " ";
      |                 ^~~~~~
a.cc:52:31: error: 'to' was not declared in this scope
   52 |       for(int i = 0; i < (int)to[a].size(); i++){
      |                               ^~
a.cc:53:17: error: 'r_prod' was not declared in this scope
   53 |         cout << r_prod[i] << " ";
      |                 ^~~~~~
a.cc:57:5: error: 'cout' was not declared in this scope
   57 |     cout << endl;
      |     ^~~~
a.cc:57:13: error: 'endl' was not declared in this scope
   57 |     cout << endl;
      |             ^~~~
a.cc:64:28: error: 'to' was not declared in this scope
   64 |   for(int j = 0; j  < (int)to[a].size(); j++){
      |                            ^~
a.cc:70:28: error: 'l_prod' was not declared in this scope
   70 |     if( j-1 >= 0 ){ res *= l_prod[j-1];}
      |                            ^~~~~~
a.cc:71:37: error: 'r_prod' was not declared in this scope
   71 |     if( j+1 < to[a].size()){ res *= r_prod[j+1];}
      |                                     ^~~~~~
a.cc:73:12: error: 'm' was not declared in this scope
   73 |     res %= m;
      |            ^
a.cc:75:16: error: 'dp2' was not declared in this scope
   75 |     res = (res*dp2[a])%m;
      |                ^~~
a.cc: In function 'void dfs3(int, int)':
a.cc:93:13: error: 'dp2' was not declared in this scope
   93 |   int res = dp2[a];
      |             ^~~
a.cc:95:17: error: 'to' was not declared in this scope
   95 |   for(auto ne : to[a] ){
      |                 ^~
a.cc:99:12: error: 'dp1' was not declared in this scope
   99 |     res *= dp1[ne];
      |            ^~~
a.cc:100:12: error: 'm' was not declared in this scope
  100 |     res %= m;
      |            ^
a.cc:106:3: error: 'ans' was not declared in this scope
  106 |   ans[a] = res%m;
      |   ^~~
a.cc:106:16: error: 'm' was not declared in this scope
  106 |   ans[a] = res%m;
      |                ^
a.cc: In function 'int main()':
a.cc:114:3: error: 'cin' was not declared in this scope
  114 |   cin >> n >> m;
      |   ^~~
a.cc:114:10: error: 'n' was not declared in this scope
  114 |   cin >> n >> m;
      |          ^
a.cc:114:15: error: 'm' was not declared in this scope
  114 |   cin >> n >> m;
      |               ^
a.cc:115:3: error: 'to' was not declared in this scope
  115 |   to.resize(n);
      |   ^~
a.cc:123:3: error: 'dfs1' was not declared in this scope; did you mean 'dfs3'?
  123 |   dfs1(0,-1);
      |   ^~~~
      |   dfs3
a.cc:133:5: error: 'cout' was not declared in this scope
  133 |     cout << ans[i] << endl;
      |     ^~~~
a.cc:133:13: error: 'ans' was not declared in this scope
  133 |     cout << ans[i] << endl;
      |             ^~~
a.cc:133:23: error: 'endl' was not declared in this scope
  133 |     cout << ans[i] << endl;
      |                       ^~~~
a.cc:140:5: error: 'cout' was not declared in this scope
  140 |     cout << endl;
      |     ^~~~
a.cc:140:13: error: 'endl' was not declared in this scope
  140 |     cout << endl;
      |             ^~~~
a.cc:143:15: error: 'dp1' was not declared in this scope
  143 |       cout << dp1[i] << " ";
      |               ^~~
a.cc:149:15: error: 'dp2' was not declared in this scope
  149 |       cout << dp2[i] << " " ;
      |               ^~~