Advertisement
RaFiN_

count how many nodes are of prime distance in a tree by cdco

Mar 10th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.49 KB | None | 0 0
  1. ///#include <stdio.h>
  2. ///#include <iostream>
  3. ///#include <cstring>
  4. ///#include <cmath>
  5. ///#include <algorithm>
  6. ///#include <string>
  7. ///#include <vector>
  8. ///#include <map>
  9. ///#include <set>
  10. ///#include <queue>
  11. ///#include <cstdlib>
  12. ///#include <limits>
  13. ///#include <iostream>
  14. ///#include <sstream>
  15. ///#include <unordered_set>
  16. ///#include <unordered_map>
  17. ///#include <random>
  18. #include <bits/stdc++.h>
  19. ///#include <bits/stdtr1c++.h>
  20. ///#include <ext/pb_ds/assoc_container.hpp>
  21. ///#include <ext/pb_ds/tree_policy.hpp>
  22. using namespace std;
  23. ///using namespace __gnu_pbds;
  24.  
  25. #define              MAX             200005
  26. #define              EPS             1e-9
  27. #define              ull             unsigned long long
  28. #define              ll              long long
  29. #define              inf             INT_MAX
  30. #define              pi              acos(-1.0)
  31. #define              vi              vector<int>
  32. #define              vl              vector<long long>
  33. #define              pii             pair<int,int>
  34. #define              pll             pair<long long,long long>
  35. #define              mp              make_pair
  36. #define              mem(a, v)       memset(a, v, sizeof (a))
  37. #define              mod             1000000007
  38. #define              all(a)          a.begin(),a.end()
  39. #define              rall(a)         a.rbegin(),a.rend()
  40. #define              ff              first
  41. #define              ss              second
  42. #define              arsort(ar,n)    sort(ar,ar+n);
  43. #define              vsort(v)        sort(v.begin(),v.end())
  44. #define              vrev(v)         reverse(v.begin(),v.end())
  45. #define              arrev(ar,n)     reverse(ar,ar+n)
  46. #define              pb              push_back
  47. #define              popb            pop_back
  48. #define              booster()       ios_base::sync_with_stdio(0); cin.tie(0);
  49. #define              read(f)         freopen(f, "r", stdin)
  50. #define              scani(x)        scanf("%d",&x)
  51. #define              scanl(x)        scanf("%I64d",&x)
  52. #define              scand(x)        scanf("%lf",&x)
  53. #define              scans(x)        scanf("%s",x)
  54. #define              OUT(x)          printf("%d\n",x)
  55. #define              OUTS(x)         printf("%d ",x)
  56. #define              min3(a,b,c)     min(a,min(b,c))
  57. #define              max3(a,b,c)     max(a,max(b,c))
  58. #define              min4(a,b,c,d)   min(min(a,b),min(c,d))
  59. #define              max4(a,b,c,d)   max(max(a,b),max(c,d))
  60. #define              max5(a,b,c,d,e) max(max3(a,b,c),max(d,e))
  61. #define              min5(a,b,c,d,e) min(min3(a,b,c),min(d,e))
  62.  
  63. #define              bitCheck(a,k)     ((bool)(a&(1<<(k))))
  64. #define              bitOff(a,k)       (a&(~(1<<(k))))
  65. #define              bitOn(a,k)        (a|(1<<(k)))
  66. #define              bitFlip(a,k)      (a^(1<<(k)))
  67.  
  68. #define              POPCOUNT           __builtin_popcount
  69. #define              POPCOUNTLL         __builtin_popcountll
  70. #define              RIGHTMOST          __builtin_ctzll
  71. #define              LEFTMOST(x)        (63-__builtin_clzll((x)))
  72.  
  73.  
  74.  
  75. #define scani2(a,b) scani(a) , scani(b)
  76. #define scani3(a,b,c) scani(a), scani(b), scani(c)
  77. #define scani4(a,b,c,d) scani(a), scani(b), scani(c), scani(d)
  78. #define scani5(a,b,c,d,e) scani(a), scani(b), scani(c), scani(d) , scani(e)
  79. #define rnd mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  80.  
  81.  
  82.  
  83. /*********************Ordered Multiset*************************/
  84.  
  85.  
  86. /*** Needs C++11 or C++14 ***/
  87. /// Treap supporting duplicating values in set
  88. /// Maximum value of treap * ADD must fit in long long
  89.  
  90. /*
  91.  
  92. struct Treap{ /// hash = 96814
  93.     int len;
  94.     const int ADD = 1000010;
  95.     const int MAXVAL = 1000000010;
  96.     tr1::unordered_map <long long, int> mp; /// Change to int if only int in treap
  97.     tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> T;
  98.     Treap(){
  99.         len = 0;
  100.         T.clear(), mp.clear();
  101.     }
  102.     inline void clear(){
  103.         len = 0;
  104.         T.clear(), mp.clear();
  105.     }
  106.     inline void insert(long long x){
  107.         len++, x += MAXVAL;
  108.         int c = mp[x]++;
  109.         T.insert((x * ADD) + c);
  110.     }
  111.     inline void erase(long long x){
  112.         x += MAXVAL;
  113.         int c = mp[x];
  114.         if (c){
  115.             c--, mp[x]--, len--;
  116.             T.erase((x * ADD) + c);
  117.         }
  118.     }
  119.     /// 1-based index, returns the K'th element in the treap, -1 if none exists
  120.     inline long long kth(int k){
  121.         if (k < 1 || k > len) return -1;
  122.         auto it = T.find_by_order(--k);
  123.         return ((*it) / ADD) - MAXVAL;
  124.     }
  125.     /// Count of value < x in treap
  126.     inline int count(long long x){
  127.         x += MAXVAL;
  128.         int c = mp[--x];
  129.         return (T.order_of_key((x * ADD) + c));
  130.     }
  131.     /// Number of elements in treap
  132.     inline int size(){
  133.         return len;
  134.     }
  135. };
  136.  
  137.  
  138.  
  139. typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;///*X.find_by_order();X.order_of_key();
  140. template <class T> inline bool isLeap(T y) {return (y % 400 == 0) || (y % 100 ? y % 4 == 0 : false); }*/
  141. template< class T > inline T gcd(T a, T b) { return (b) == 0 ? (a) : gcd((b), ((a) % (b))); }
  142. template< class T > inline T lcm(T a, T b) { return ((a) / gcd((a), (b)) * (b)); }
  143. template <class T> inline T BigMod(T Base,T power,T M=mod){if(power==0)return 1;if(power&1)return ((Base%M)*(BigMod(Base,power-1,M)%M))%M;else{T y=BigMod(Base,power/2,M)%M;return (y*y)%M;}}
  144. template <class T> inline T ModInv (T A,T M = mod){return BigMod(A,M-2,M);}
  145.  
  146. int fx[] = {-1,+0,+1,+0,+1,+1,-1,-1,+0};
  147. int fy[] = {+0,-1,+0,+1,+1,-1,+1,-1,+0};
  148. int day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  149.  
  150.  
  151. /*********************** let the play begin() ***********************************/
  152.  
  153. int n,pp,prime[MAX],arr[MAX];vi v[MAX];int totalnode = 0,del[MAX];ll ans;
  154.  
  155. ll dd[50005];int Subtree[MAX];
  156.  
  157. void dfs(int s,int p = -1)
  158. {
  159.     totalnode++;
  160.     Subtree[s] = 1;
  161.     for(auto it : v[s])
  162.     {
  163.         if(it==p||del[it])continue;
  164.         dfs(it,s);
  165.         Subtree[s]+=Subtree[it];
  166.     }
  167. }
  168.  
  169. int Getcenter(int s,int p = -1)
  170. {
  171.     for(auto it : v[s])
  172.     {
  173.         if(del[it]||it==p)continue;
  174.         if(Subtree[it]>totalnode/2)return Getcenter(it,s);
  175.     }
  176.     return s;
  177. }
  178.  
  179. void update(int s,int p,int dis)
  180. {
  181.     dd[dis]++;
  182.     for(auto it : v[s])
  183.     {
  184.         if(it==p||del[it])continue;
  185.         update(it,s,dis+1);
  186.     }
  187. }
  188.  
  189. void calcu(int s,int p,int dist)
  190. {
  191.     int x = lower_bound(prime,prime+pp,dist) - prime;
  192.     for(int indx = x;indx<pp;indx++)ans+=dd[prime[indx]-dist];
  193.     for(auto it : v[s])
  194.     {
  195.         if(it==p||del[it])continue;
  196.         calcu(it,s,dist+1);
  197.     }
  198. }
  199.  
  200. void decompose(int s,int p = -1)
  201. {
  202.     totalnode = 0;
  203.     dfs(s,p);
  204.     int x = Getcenter(s,-1);
  205.     del[x] = 1;
  206.     mem(dd,0);
  207.     dd[0] = 1;
  208.     for(auto it : v[x])
  209.     {
  210.         if(del[it])continue;
  211.         calcu(it,x,1);
  212.         update(it,x,1);
  213.     }
  214.     for(auto it : v[x])
  215.     {
  216.         if(del[it])continue;
  217.         decompose(it,x);
  218.     }
  219. }
  220.  
  221.  
  222.  
  223. void sieve(int nn)
  224. {
  225.     double z = sqrt(nn);
  226.     for(int i = 3;i<=z;i+=2)
  227.     {
  228.         if(!arr[i])
  229.         {
  230.             for(ll j = i*i;j<=nn;j+=i+i)
  231.             {
  232.                 ///cout<<j<<endl;
  233.                 arr[j] = 1;
  234.             }
  235.         }
  236.     }
  237.     prime[pp++] = 2;
  238.     for(int i = 3;i<=nn;i+=2)if(!arr[i])prime[pp++] = i;
  239. }
  240.  
  241.  
  242. int main()
  243. {
  244.     ///booster()
  245.     sieve(50002);
  246.     ///for(int i = 0;i<100;i++)cout<<prime[i]<<" ";
  247.     ///read("input.txt");
  248.     scani(n);
  249.     for(int i = 0;i<n-1;i++)
  250.     {
  251.         int a,b;
  252.         scani2(a,b);
  253.         v[a].pb(b);
  254.         v[b].pb(a);
  255.     }
  256.  
  257.  
  258.     ///cout<<x<<endl;
  259.     decompose(1);
  260.     double x = (double)n;
  261.     double y = (double)ans;
  262.     double ddd = (x*(x-1))/2.0;
  263.     printf("%.7f\n",ans/ddd);
  264.  
  265.     return 0;
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement