Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*input
- 4 2
- 1 2
- 1 3
- 3 4
- */
- #include<bits/stdc++.h>
- using namespace std;
- const int N=5e5 + 100;
- const int mod=1e9 + 7;
- #define int long long
- const int inf=1e18;
- #define pii pair<int, int>
- #define f first
- #define s second
- #define mp make_pair
- #define FOR(i, n) for(int i=1;i<=n;i++)
- #define TRACE(x) cerr << #x << " = " << x << endl
- //Trace prints the name of the variable and the value.
- int dp[N], n, k;vector<int> adjlist[N];
- void dfs(int i, int p)
- {
- dp[i]=0;
- for(auto j:adjlist[i])
- {
- if(j==p) continue;
- dfs(j, i);dp[i]=max(dp[j], dp[i]);
- }
- dp[i]++;
- }
- void solve()
- {
- cin>>n>>k;
- for(int i=1;i<=n;i++) adjlist[i].clear();
- for(int i=1;i<n;i++)
- {
- int u, v;cin>>u>>v;adjlist[u].push_back(v);adjlist[v].push_back(u);
- }
- dfs(1, -1);int ans=0;
- for(int i=1;i<=n;i++) ans+=(dp[i]<=k)?1:0;
- cout<<ans<<endl;
- }
- signed main()
- {
- ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
- int t;cin>>t;
- while(t--) solve();
- }
Add Comment
Please, Sign In to add comment