Advertisement
K_Y_M_bl_C

Untitled

Sep 21st, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. struct DSU
  2. {
  3.     vi p;
  4.     DSU() {};
  5.     DSU(int n)
  6.     {
  7.         p.resize(n);
  8.         for (int i = 0; i < n; ++i)
  9.             p[i] = i;
  10.     }
  11.     int get(int u)
  12.     {
  13.         if (u == p[u])
  14.             return u;
  15.         return p[u] = get(u);
  16.     }
  17.     void unit(int a, int b)
  18.     {
  19.         a = get(a);
  20.         b = get(b);
  21.         p[a] = b;
  22.     }
  23. };
  24.  
  25. map <int, int> cnt[100001];
  26. vi G[100001];
  27.  
  28. int solve()
  29. {
  30.     int n;
  31.     scanf("%d", &n);
  32.     for (int i = 0; i < n - 1; ++i)
  33.     {
  34.         int x, y;
  35.         scanf("%d %d", &x, &y);
  36.         --x, --y;
  37.         G[x].inb(y);
  38.     }
  39.     DSU T = DSU(n);
  40.     function<void(int)> dfs = [&](int u)
  41.     {
  42.         if (G[u].size() == 1 && !G[G[u][0]].empty())
  43.         {
  44.             T.unit(u, G[u][0]);
  45.         }
  46.         for (int to : G[u])
  47.         {
  48.             dfs(to);
  49.         }
  50.     };
  51.     dfs(0);
  52.     int root = T.get(0);
  53.     vi p(n, -1);
  54.     function<void(int)> mkgraph = [&](int u)
  55.     {
  56.         for (int v : G[u])
  57.         {
  58.             int to = T.get(v);
  59.             p[to] = u;
  60.             mkgraph(to);
  61.         }
  62.     };
  63.     mkgraph(root);
  64.     for (int i = 0; i < n; ++i)
  65.     {
  66.         if (G[i].empty())
  67.         {
  68.             int it = 0;
  69.             int x = T.get(i);
  70.             int u = p[x];
  71.             int cf = 1;
  72.             while (u != -1 && cf < 1000000)
  73.             {
  74.                 ++it;
  75.                 cf *= (int)G[u].size();
  76.                 cnt[u][cf]++;
  77.                 u = p[u];
  78.             }
  79.         }
  80.     }
  81.     int m;
  82.     scanf("%d", &m);
  83.     for (int it = 0; it < m; ++it)
  84.     {
  85.         int u, x;
  86.         scanf("%d %d", &u, &x);
  87.         int ans = x;
  88.         --u;
  89.         for (int i = 1; (int)i * i <= x; ++i)
  90.         {
  91.             if (x % i)
  92.                 continue;
  93.             {
  94.                 int cd = i;
  95.                 if (cnt[u].find(cd) != cnt[u].end())
  96.                     ans -= (x / cd) * cnt[u][cd];
  97.             }
  98.             if (x != i * i)
  99.             {
  100.                 int cd = x / i;
  101.                 if (cnt[u].find(cd) != cnt[u].end())
  102.                     ans -= (x / cd) * cnt[u][cd];
  103.             }
  104.         }
  105.         printf("%d\n", max(ans, 0));
  106.     }
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement