Advertisement
Josif_tepe

Untitled

Jul 1st, 2023
843
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. //#include <bits/stdc++.h>
  4. using namespace std;
  5.  
  6. const int maxn=1e5+15;
  7. typedef long long ll;
  8. const ll MOD = 1e9 + 7;
  9. vector<int>g[maxn];
  10. ll dp[maxn][2]; // dp[node][0] -> white color,
  11. //                  dp[node][1] -> black color
  12. void dfs(int node, int par) {
  13.     dp[node][0] = 1;
  14.     dp[node][1] = 1;
  15.     for(int i = 0; i < (int) g[node].size(); i++) {
  16.         int neighbour = g[node][i];
  17.         if(neighbour != par) {
  18.             dfs(neighbour, node);
  19.            
  20.             dp[node][0] *= (dp[neighbour][0] + dp[neighbour][1]) % MOD;
  21.             dp[node][1] *= (dp[neighbour][0]) % MOD;
  22.            
  23.             dp[node][0] %= MOD;
  24.             dp[node][1] %= MOD;
  25.            
  26.         }
  27.     }
  28.  
  29. }
  30.  
  31. int main()
  32. {
  33.     ios::sync_with_stdio(false);
  34.     int n;
  35.     cin >> n;
  36.     for(int i = 1; i < n; i++) {
  37.         int a, b;
  38.         cin >> a >> b;
  39.         a--; b--;
  40.         g[a].push_back(b);
  41.         g[b].push_back(a);
  42.     }
  43.     dfs(0, -1);
  44.     cout << (dp[0][0] + dp[0][1]) % MOD << endl;
  45.     return 0;
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement