fork download
  1. #include <bits/stdc++.h>
  2. #define rep(_i, _j) for(int _i = 1; _i <= _j; ++_i)
  3. const int inf = 0x3f3f3f3f;
  4. typedef long long LL;
  5. typedef double DB;
  6. using namespace std;
  7.  
  8. const int maxn = 10000 + 100;
  9. int n, b, k;
  10. int color[maxn], fa[maxn], size[maxn];
  11. vector<int> to[maxn];
  12. vector<pair<int, int> > headquater;
  13. stack<int> subtree;
  14. void dfs(int u) {
  15. int cnt = 0;
  16. subtree.push(u);
  17. for(size_t i = 0; i < to[u].size(); ++i) {
  18. if(to[u][i] != fa[u]) {
  19. fa[to[u][i]] = u;
  20. dfs(to[u][i]);
  21. cnt += size[to[u][i]];
  22. if(cnt < b) {
  23. continue;
  24. } else {
  25. ++k;
  26. headquater.push_back(make_pair(u, cnt));
  27. while(subtree.top() != u) {
  28. color[subtree.top()] = k;
  29. subtree.pop();
  30. }
  31. cnt = 0;
  32. }
  33. }
  34. }
  35. size[u] = 1 + cnt;
  36. }
  37.  
  38. int main() {
  39. scanf("%d%d", &n, &b);
  40. for(int i = 1; i < n; ++i) {
  41. int u, v;
  42. scanf("%d%d", &u, &v);
  43. to[u].push_back(v);
  44. to[v].push_back(u);
  45. }
  46. fa[1] = 0;
  47. dfs(1);
  48. if(n < b) {
  49. puts("0");
  50. } else if(n <= 3 * b) {
  51. puts("1");
  52. for(int i = 1; i <= n; ++i) {
  53. printf("%d ", 1);
  54. }
  55. puts("");
  56. puts("1");
  57. } else {
  58. for(int i = 1; i <= n; ++i) {
  59. if(color[i] == 0) {
  60. color[i] = k;
  61. ++headquater[k - 1].second;
  62. }
  63. }
  64. for(size_t i = 0; i < headquater.size(); ++i) {
  65. if(headquater[i].second < b || b * 3 < headquater[i].second) {
  66. puts("0");
  67. return 0;
  68. }
  69. }
  70. printf("%d\n", k);
  71. for(int i = 1; i <= n; ++i) {
  72. printf("%d ", color[i]);
  73. }
  74. puts("");
  75. for(size_t i = 0; i < headquater.size(); ++i) {
  76. printf("%d ", headquater[i].first);
  77. }
  78. puts("");
  79. }
  80.  
  81.  
  82. return 0;
  83. }
  84.  
Success #stdin #stdout 0s 3716KB
stdin
8 2 
1 2 
2 3 
1 8 
8 7 
8 6 
4 6 
6 5 
stdout
3
3 1 1 2 2 3 3 3 
1 6 8