Advertisement
Kevin_Zhang

Untitled

Apr 24th, 2020
743
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define pb emplace_back
  3. using namespace std;
  4. using ll = long long;
  5. const int maxn = 40001;
  6. const int bs = maxn;
  7. //const int bs = 10;
  8. int n, k;
  9. vector<int> edge[maxn];
  10. bitset<bs> dp[2][maxn];
  11. int sz[maxn];
  12. void add(int& a, int v) {
  13.     a += v-k;
  14.     a += (a>>31)&k;
  15. }
  16. void gs(int now, int last = 0) {
  17.     sz[now] = 1;
  18.     if (auto it = find(edge[now].begin(), edge[now].end(), last); it != edge[now].end()) {
  19.         swap(*it, edge[now].back());
  20.         edge[now].pop_back();
  21.     }
  22.     for (int u : edge[now])
  23.         gs(u, now), add(sz[now], sz[u]);
  24. }
  25. bool ans[maxn];
  26. void dfs(int now) {
  27.     static int d;
  28.     add(d, 1);
  29. #define cd d
  30.     //int cd = d;
  31.     ans[now] = (!cd || (((dp[0][now] << cd) | (dp[0][now] >> (k-cd))) & (dp[1][now])).any());
  32.     //ans[now] = (dp[0][now] & dp[1][now])[ k - cd ];
  33.     auto &e = edge[now];
  34.     if (e.size()) {
  35. #define GL(u) (sz[u] ? dp[0][u] | (dp[0][u] << sz[u]) | (dp[0][u] >> (k-sz[u])) : dp[0][u])
  36. #define GR(u) (sz[u] ? dp[1][u] | (dp[1][u] >> sz[u]) | (dp[1][u] << (k-sz[u])) : dp[1][u]);
  37.         dp[0][ e[0] ] = dp[0][now];
  38.         dp[1][ e.back()] = dp[1][now];
  39.         for (int i = 0;i+1 < e.size();++i)
  40.             dp[0][e[i+1]] = GL(e[i]);
  41.         for (int i = e.size()-1;i > 0;--i)
  42.             dp[1][e[i-1]] = GR(e[i]);
  43.  
  44.         for (int u : edge[now])
  45.             dfs(u);
  46.     }
  47.     --d;
  48.     d += (d>>31)&k;
  49. }
  50.  
  51. inline char readchar(){static const size_t bufsize = 1 << 16; static size_t p = 0, end = 0; static char buf[bufsize]; if (p == end) end = fread_unlocked(buf, sizeof(char), bufsize, stdin), p = 0; return p == end ? EOF : buf[p++]; }
  52. inline void read(char &a){while(isspace(a = readchar()));}
  53. inline void read(char *a){while(isspace(*a = readchar()));++a; while(isgraph(*a = readchar()))++a;}
  54. inline void read(string &a){static char p; while(isspace(p = readchar())); a = p; while(isgraph(p = readchar()))a.push_back(p);}
  55. template<typename T> inline void read(T& a){static char p; static bool b;while (!isdigit(p = readchar()))b = p == '-'; a = p ^ '0'; while (isdigit(p = readchar())) a *= 10, a += p ^ '0'; if(b)a *= -1, b = false; }
  56. template<typename T1, typename T2> inline void read(pair<T1, T2> &a){read(a.first), read(a.second);}
  57. template<typename T> inline void read(T begin, const T &end){while(begin != end)read(*begin), ++begin;}
  58. inline void R(){} template<class T1, class... T2> inline void R(T1 &h, T2 &... e){read(h), R(e...);}
  59. signed main(){
  60.     ios_base::sync_with_stdio(0), cin.tie(0);
  61.     R(n, k);
  62.     //cin >> n >> k;
  63.     for (int i = 1, a, b;i < n;++i) {
  64.         R(a, b);
  65.         //cin >> a >> b;
  66.         edge[a].pb(b);
  67.         edge[b].pb(a);
  68.     }
  69.     dp[0][1][k] = dp[0][1][0] = dp[1][1][0] = dp[1][1][k] = true;
  70.     gs(1);
  71.     dfs(1);
  72.     for (int i = 1;i <= n;++i)
  73.         putchar(ans[i]?'Y':'N');
  74.     putchar('\n');
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement