Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.00 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp> // Common file
  3. #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
  4. #define ff first
  5. #define ss second
  6. #define pb push_back
  7. #define pf push_front
  8. #define eb emplace_back
  9. #define emp emplace
  10. #define ins insert
  11. #define mp make_pair
  12. #define mt make_tuple
  13. #define sz(s) (int)s.size()
  14. #define forp(i,a,b) for( int i=a;i<=b;i++)
  15. #define rep(i,n)    for( int i=0;i<n;i++)
  16. #define ren(i,n)    for( int i=n-1;i>=0;i--)
  17. #define forn(i,a,b) for( int i=a;i>=b;i--)
  18. #define w(t) while(t)
  19. #define on cout<<"\n"
  20. #define os cout <<" "
  21. #define o2(a,b) cout<<a<<" "<<b
  22. #define o(a) cout << a
  23. #define bitcount __builtin_popcount
  24. #define gcd __gcd
  25. #define all(v) v.begin(),v.end()
  26. #define mem(n,m) memset(n,m,sizeof(n))
  27. #define pii pair<int,int>
  28. #define tiii tuple<int, int, int>
  29. #define pll pair<long long,long long>
  30. #define sii set<int>
  31. #define sll set<long long>
  32. #define vii vector<int>
  33. #define vll vector<long long>
  34. #define mii map<int,int>
  35. #define mll map<long long, long long>
  36. #define lob lower_bound
  37. #define upb upper_bound
  38. #define ret return
  39. #define present(s,x) (s.find(x) != s.end())
  40. #define cpresent(s,x) (find(all(s),x) != s.end())
  41. #define ford(container, it) for(auto it = container.begin(); it != container.end(); it++)
  42. #define fors(container, it, a, b) for(auto it = a; it != b; it++)
  43. using namespace __gnu_pbds;
  44. #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  45. #define MOD 1000000007
  46. #define EPSILON 1e-9
  47. #define PI acos(-1)
  48. #define inf 1e18
  49. #define siz 100005
  50. #define SIZ 1000005
  51. #define SIZE 200005
  52. #define int long long
  53. #define debug(a) cerr<<#a<<": ";for(auto i:a)cerr<<i<<" ";cerr<<'\n';
  54. #define trace(a) cerr<<#a<<": "<<a<<"\n"
  55. #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  56.  
  57.  
  58. typedef long long  ll;
  59. typedef long double  ldo;
  60. typedef double  db ;
  61. using namespace std;
  62. auto start = std::chrono::system_clock::now();
  63. inline void skj()
  64. {
  65.   std::chrono::time_point<std::chrono::system_clock> end;
  66.   end = std::chrono::system_clock::now();
  67.   std::chrono::duration<double> elapsed_seconds = end - start;
  68.   std::time_t end_time = std::chrono::system_clock::to_time_t(end);
  69.   cerr<<"Time taken " << elapsed_seconds.count()*1000<<"\n";
  70. }
  71.  
  72. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  73. //uniform_int_distribution<int> ud(1, 100); use this for random number generator
  74.  
  75.     //custom hash for unordered map
  76. struct custom_hash {
  77.     static uint64_t splitmix64(uint64_t x) {
  78.         // http://xorshift.di.unimi.it/splitmix64.c
  79.         x += 0x9e3779b97f4a7c15;
  80.         x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
  81.         x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
  82.         return x ^ (x >> 31);
  83.     }
  84.      
  85.     size_t operator()(uint64_t x) const {
  86.         static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
  87.         return splitmix64(x + FIXED_RANDOM);
  88.     }
  89. };
  90. inline int binexp(int a,int b,int m)
  91. {
  92.     int res=1;
  93.     a%=m;
  94.     while(b)
  95.     {
  96.         if(b&1)
  97.             res=(res*1ll*a)%m;
  98.         a=(a*1ll*a)%m;
  99.         b>>=1;
  100.     }
  101.     return res;
  102. }
  103. int lp[siz];
  104. inline void seive() {
  105.     forp(i, 2, siz - 1) {
  106.         if(lp[i] == 0) {
  107.             for(int j = i; j < siz; j += i) {
  108.                 if(lp[j] == 0) {
  109.                     lp[j] = i;
  110.                 }
  111.             }
  112.         }
  113.     }
  114. }
  115. int n, par[siz], rnk[siz];
  116. inline void init(int n) {
  117.     forp(i, 1, n) {
  118.         par[i] = i;
  119.         rnk[i] = 1;
  120.     }
  121. }
  122. inline int find(int a) {
  123.     ret a == par[a] ? a : par[a] = find(par[a]);
  124. }
  125. inline void Union(int a, int b) {
  126.     a = find(a), b = find(b);
  127.     if(rnk[a] > rnk[b]) {
  128.         swap(a, b);
  129.     }
  130.     rnk[b] += rnk[a];
  131.     par[a] = b;
  132. }
  133. inline void comb(int a, int b, mii &M) {
  134.     w(a > 1) {
  135.         int p = lp[a], c = 0;
  136.         w(a % p == 0) {
  137.             c++, a /= p;
  138.         }
  139.         M[p] += b * c;
  140.     }
  141. }
  142. mii M, m;
  143. inline int doit(vector<pair<int, pii>> &edges, mii &M) {
  144.     init(n);
  145.     int p = 1;
  146.     for(auto &it : edges) {
  147.         int u = it.ss.ff, v = it.ss.ss;
  148.         u = find(u), v = find(v);
  149.         comb(it.ff, rnk[u] * rnk[v], M);
  150.         p = (p * binexp(it.ff, rnk[u] * rnk[v], MOD)) % MOD;
  151.         Union(u, v);
  152.     }
  153.     ret p;
  154. }
  155. inline void solve_me_senpai() {
  156.     cin >> n;
  157.     assert(1 <= n && n <= 1e5);
  158.     vector<pair<int, pii>> edges;
  159.     rep(i, n - 1) {
  160.         int u, v, w;
  161.         cin >> u >> v >> w;
  162.         assert(1 <= u && u <= n);
  163.         assert(1 <= v && v <= n);
  164.         assert(1 <= w && w <= 1e5);
  165.         edges.pb({w, {u, v}});
  166.     }
  167.     sort(all(edges));
  168.     int p = doit(edges, M);
  169.     sort(all(edges), greater<pair<int, pii>>());
  170.     int q = doit(edges, m);
  171.     int res = inf;
  172.     for(auto i : m) {
  173.         res = min(res, M[i.ff] / m[i.ff]);
  174.     }
  175.     o(p);on;
  176.     o(q);on;
  177.     o(res);on;
  178. }
  179.  
  180. signed main()
  181. {
  182.     boost
  183.     #ifndef ONLINE_JUDGE
  184.     freopen("input.txt", "r",stdin);
  185.     freopen("ot.txt", "w", stdout);
  186.     #endif
  187.     seive();
  188.     int t = 1;
  189.     //cin >> t;
  190.     //int a = 1;
  191.     while(t--) {
  192.       //  cout << "Case #" << a  << ": ";
  193.         solve_me_senpai();
  194.         //a++;
  195.     }
  196.     skj();
  197.     return 0;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement