MAGCARI

TOI19 Explorer

Jun 5th, 2023
808
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. /*
  2.     Task    : _example
  3.     Author  : Phumipat C. [MAGCARI]
  4.     Language: C++
  5.     Created : 05 June 2023 [19:35]
  6. */
  7. #include<bits/stdc++.h>
  8. #define MOD (long long )(1e9 + 7)
  9. using namespace std;
  10. vector<int > g[500010];
  11. long long fact[500010];
  12. long long dfs(int currentNode, int parent){
  13.     if(g[currentNode].size() == 0)
  14.         return 1;
  15.     if(g[currentNode].size() == 1 && g[currentNode][0] == parent)
  16.         return 1;
  17.     long long ret = 1;
  18.     int cnt = 0;
  19.     for(auto x:g[currentNode]){
  20.         if(x == parent) continue;
  21.         cnt++;
  22.         ret*=dfs(x,currentNode);
  23.         ret%=MOD;
  24.     }
  25.     ret = (ret*fact[cnt])%MOD;
  26.     return ret;
  27. }
  28. int main(){
  29.     cin.tie(0)->sync_with_stdio(0);
  30.     cin.exceptions(cin.failbit);
  31.     int n,last = 0,num;
  32.     cin >> n >> last;
  33.     for(int i=2;i<=2*n-1;i++){
  34.         cin >> num;
  35.         g[last].push_back(num);
  36.         last = num;
  37.     }
  38.     fact[0] = fact[1] = 1;
  39.     for(int i=2;i<=n;i++){
  40.         fact[i] = (fact[i-1]*i)%MOD;
  41.     }
  42.     cout << dfs(last,0) << '\n';
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment