SorahISA

[TOI 2020 1st] B. 惡魔島死刑犯釋放事件

Apr 23rd, 2020
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. /* https://drive.google.com/open?id=1UvjGj27JG7t4xFl3hjKBCIHYSoYkLlzD */
  2. /* O(n^2/32) AC */
  3.  
  4. // #pragma GCC target("avx2")
  5. #pragma GCC optimize("O3", "unroll-loops")
  6.  
  7. // #include <bits/extc++.h>
  8. // using namespace __gnu_pbds;
  9.  
  10. #include <bits/stdc++.h>
  11. using namespace std;
  12.  
  13. #define int long long
  14. #define double long double
  15. // template <typename T>
  16. // using pbds_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  17. using pii = pair<int, int>;
  18. template<typename T>
  19. using prior = priority_queue<T, vector<T>, greater<T>>;
  20. template<typename T>
  21. using Prior = priority_queue<T>;
  22.  
  23. #define X first
  24. #define Y second
  25. #define ALL(x) (x).begin(), (x).end()
  26. #define eb emplace_back
  27. #define pb push_back
  28.  
  29. #define fastIO() ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
  30. #define RANDOM() random_device __rd; \
  31.                  mt19937 __gen = mt19937(__rd()); \
  32.                  uniform_int_distribution<int> __dis(0, 1); \
  33.                  auto rnd = bind(__dis, __gen);
  34.  
  35. const int INF = 1E18;
  36. const int mod = 1E9 + 7;
  37. const int maxn = 40000 + 5;
  38.  
  39. int n, k;
  40. vector<int> adj[maxn], subSize(maxn, 0);
  41. bitset<maxn> mods[maxn], tmp;
  42.  
  43. void dfs_init(int now, int lst) {
  44.     subSize[now] = 1;
  45.     for (auto x : adj[now]) {
  46.         if (x == lst) continue;
  47.         dfs_init(x, now);
  48.         subSize[now] += subSize[x];
  49.     }
  50. }
  51.  
  52. void dfs(int now, int lst) {
  53.     int lstX = 0;
  54.     for (auto x : adj[now]) {
  55.         if (x == lst) continue;
  56.         if (subSize[x] == subSize[lstX]) {mods[x] = mods[lstX], dfs(x, now); continue;}
  57.         lstX = x;
  58.         mods[x] = mods[now] << 1;
  59.        
  60.         for (auto y : adj[now]) {
  61.             if (y == lst or y == x) continue;
  62.             tmp = mods[x]; tmp <<= subSize[y];
  63.             mods[x] |= tmp;
  64.         }
  65.        
  66.         dfs(x, now);
  67.     }
  68. }
  69.  
  70. int32_t main() {
  71.     fastIO();
  72.    
  73.     int u, v;
  74.     cin >> n >> k;
  75.    
  76.     for (int i = 0; i < n-1; ++i) cin >> u >> v, adj[u].eb(v), adj[v].eb(u);
  77.    
  78.     if (k == 1) {
  79.         string _ans(n, 'Y');
  80.         cout << _ans << "\n";
  81.         return 0;
  82.     }
  83.    
  84.     dfs_init(1, -1);
  85.     for (int i = 1; i <= n; ++i) {
  86.         sort(ALL(adj[i]), [](auto a, auto b) {
  87.             return subSize[a] < subSize[b];
  88.         });
  89.     }
  90.     mods[1][1] = 1, dfs(1, -1);
  91.    
  92.     char ans[n + 1], *res = "NY";
  93.     ans[n] = '\0';
  94.     for (int i = 1; i <= n; ++i) {
  95.         for (int j = k; j <= n; j += k) mods[i][0] = mods[i][0] | mods[i][j];
  96.         ans[i - 1] = res[mods[i][0]];
  97.     }
  98.     cout << ans << "\n";
  99.    
  100.     return 0;
  101. }
Add Comment
Please, Sign In to add comment