Ahmed_Negm

Untitled

Mar 28th, 2023
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. using namespace std;
  5. using namespace __gnu_pbds;
  6. #define ll long long
  7. #define OO 2'000'000'000
  8. #define ull unsigned long long
  9. #define nl '\n'
  10. #define sz(x) (ll)(x.size())
  11. #define all(x) x.begin(),x.end()
  12. #define rall(s)  s.rbegin(), s.rend()
  13. #define getline(s) getline(cin>>ws,s)
  14. #define ceill(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
  15. #define pi  3.141592653589793
  16. #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  17. #define multi_ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
  18.  
  19.  
  20. void Fast_IO(){
  21. ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  22. // freopen("filename.in", "r", stdin);
  23. // freopen("filename.txt", "w", stdout);
  24. #ifndef ONLINE_JUDGE
  25. freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  26. #endif
  27. }
  28.  
  29.  
  30.  
  31.  
  32. int dx[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
  33. int dy[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
  34.  
  35. ll n,k;
  36. vector<vector<ll>>adj;
  37.  
  38. ll dp[100005][4];
  39. vector<ll>paint;
  40. const ll mod = 1e9 + 7;
  41. void dfs(ll u,ll p){
  42.     if(paint[u]) dp[u][paint[u]] = 1;
  43.     else{
  44.         dp[u][1] = dp[u][2] = dp[u][3] = 1;
  45.     }
  46.     if(paint[p]) dp[u][paint[p]] = 0;
  47.  
  48.   for(auto &v:adj[u]){
  49.     if(v==p)continue;
  50.    
  51.     if(paint[v] && paint[v] == paint[u]){
  52.         cout<<0<<nl;
  53.         exit(0);
  54.     }
  55.    
  56.     dfs(v,u);
  57.     if(paint[u]){
  58.         dp[u][paint[u]] *= (dp[v][1] + dp[v][2] + dp[v][3] - dp[v][paint[u]]);
  59.         dp[u][paint[u]] %= mod;
  60.         continue;
  61.     }
  62.  
  63.     if(paint[u] == 0){
  64.         dp[u][1] = (dp[u][1] * (dp[v][2] + dp[v][3])) % mod;
  65.         dp[u][2] = (dp[u][2] * (dp[v][1] + dp[v][3])) % mod;
  66.         dp[u][3] = (dp[u][3] * (dp[v][1] + dp[v][2])) % mod;
  67.     }
  68.   }
  69. }
  70.  
  71.  
  72.  
  73. void solve(){
  74.   cin>>n>>k;
  75.   paint = vector<ll>(n+1);
  76.   adj = vector<vector<ll>>(n+1);
  77.   for(int i=1;i<n;i++){
  78.     ll u,v;
  79.     cin>>u>>v;
  80.     adj[u].push_back(v);
  81.     adj[v].push_back(u);
  82.   }
  83.   for(int i=0;i<k;i++){
  84.     ll x,c; cin>>x>>c;
  85.     paint[x] = c;
  86.   }
  87.    
  88.     dfs(1,0);
  89.  
  90.     cout<<(dp[1][1] + dp[1][2] + dp[1][3]) % mod;
  91.  
  92.  
  93.  
  94.  
  95. }
  96.  
  97. int main(){
  98.     Fast_IO();
  99. int t =1;
  100. //cin>>t;
  101. while(t--){
  102. solve();
  103. }
  104. return 0;
  105. }  
Add Comment
Please, Sign In to add comment