Advertisement
Naxocist

toi19_explorer

May 27th, 2023
904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 5e5 + 3, mod = 1e9 + 7;
  4. using ll = long long;
  5. vector<int> adj[N];
  6. ll dp[N], fac[N];
  7.  
  8. void dfs(int u, int p) {
  9.  
  10.     dp[u] = 1;
  11.     int child = 0;
  12.     for(int v : adj[u]) {
  13.         if(v == p) continue ;
  14.         dfs(v, u);
  15.         dp[u] *= dp[v], dp[u] %= mod;
  16.         child ++;
  17.     }
  18.  
  19.     dp[u] *= fac[child], dp[u] %= mod;
  20. }
  21.  
  22. int main() {
  23.     cin.tie(nullptr)->sync_with_stdio(false);
  24.     fac[0] = 1;
  25.     for(int i=1; i<N; ++i) fac[i] = fac[i-1] * i, fac[i] %= mod;
  26.     int n; cin >> n;
  27.     unordered_set<int> s;
  28.     int p = -1;
  29.     for(int i=0; i<2*n-1; ++i) {
  30.         int u; cin >> u;
  31.         if(s.find(u) == s.end() and i) adj[u].emplace_back(p), adj[p].emplace_back(u);
  32.         s.insert(u);
  33.         p = u;
  34.     }
  35.     dfs(p, -1);
  36.     cout << dp[p];
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement