Advertisement
Asif_Anwar

WA khaitsi ekhon :)

Mar 29th, 2021
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. #define pb push_back
  5. #define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  6. #define F first
  7. #define S second
  8. typedef long long ll;
  9. typedef vector< int > vi;
  10. typedef map<int, int > mp;
  11. #define debug cout << -1 << endl;
  12. #define REP(i, a, b) for(int i=a; i<b; i++)
  13. #define pop pop_back
  14. const ll MOD = 1000000007;
  15. int cc = 1;
  16. const int maxN = 2001;
  17. vector< int > adj[maxN];
  18. vector< int > child[maxN];
  19. bool isVis[maxN] = {false};
  20. bool isConn[maxN] = {false};
  21. map< int , int > m;
  22. bool cmp(int a, int b)
  23. {
  24. if(child[a].size()==child[b].size()) return a<b;
  25. return child[a].size() > child[b].size();
  26. }
  27.  
  28. void dfs(int s, int k)
  29. {
  30. //track.pb(s);
  31. isConn[s] = true;
  32. isVis[s] = true;
  33. for(auto x: adj[s]) {
  34. if(!isVis[x]) {
  35. child[s].pb(x);
  36. dfs(x, k);
  37. }
  38. }
  39.  
  40. sort(child[s].begin(), child[s].end(), cmp);
  41. int sz = child[s].size();
  42. if(sz<k && sz!=0){
  43. //cout << "** " << s << " ** " << sz << endl;
  44. //isConn[s];
  45. for(auto x: child[s]) {
  46. isConn[x] = false;
  47. }
  48. child[s].clear();
  49. m[s]++;
  50. //cout << s << " lol " << endl;
  51. //temp++;
  52. }
  53. else if(sz>k) {
  54. for(int i=sz; i>=k; i--) {
  55. isConn[child[s][i]] = false;
  56. child[s].pop();
  57. }
  58. }
  59.  
  60. return;
  61. }
  62.  
  63. void solve()
  64. {
  65. int n, k;
  66. cin >> n >> k;
  67. for(int i=0; i<n-1; i++) {
  68. int a, b;
  69. cin >> a >> b;
  70. adj[a].pb(b);
  71. adj[b].pb(a);
  72. }
  73. ll sum = 0;
  74. dfs(1, k);
  75. for(int i=1; i<maxN; i++) {
  76. //debug;
  77. if(isConn[i]) {
  78. //debug;
  79. //cout << i << " ";
  80. ll sz = (ll)child[i].size();
  81. if(sz==0) {
  82. if(m[i]) sz++;
  83. }
  84. sum += sz;
  85. }
  86. }
  87. cout << "Case " << cc++ << ": ";
  88. cout << sum << endl;
  89. }
  90. int main()
  91. {
  92. FastIO;
  93. //freopen("tttt.in", "r", stdin);
  94. //freopen("tttt.out", "w", stdout);
  95. int t;
  96. //t = 1;
  97. cin >> t;
  98. while(t--){
  99. for(int i=0; i<maxN; i++) {
  100. isConn[i] = false;
  101. isVis[i] = false;
  102. adj[i].clear();
  103. child[i].clear();
  104. }
  105. solve();
  106. }
  107. return 0;
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement