1593E - Gardener and Tree - CodeForces Solution


brute force data structures dfs and similar greedy implementation trees *1600

Please click on ads to support us..

Python Code:

import sys

input = sys.stdin.readline
inf = float('inf')


def getInt():
    return int(input())


def getStr():
    return input().strip()


def getList(split=True):
    s = getStr()
    if split:
        s = s.split()
    return map(int, s)


t = getInt()


def solve():
    input()
    n, k = getList()
    from collections import deque

    q = deque()
    g = [[] for _ in range(n+1)]
    d = [0] * (n+1)
    for _ in range(n-1):
        u, v = getList()
        g[u].append(v)
        g[v].append(u)
    deg = list(map(len, g))
    for i in range(1, n+1):
        if len(g[i]) == 1:
            d[i] = 1
            q.append(i)

    while q:
        u = q.popleft()
        for v in g[u]:
            deg[v] -= 1
            if deg[v] == 1:
                d[v] = d[u]+1
                q.append(v)

    print(sum(d[i] > k for i in range(1, n+1)))


for _ in range(t):
    solve()

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define ba boolalpha
#define F first
using vi = vector<int>;
using vll = vector<ll>;
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()
vector<int> dx = {1, 1, -1, -1};
vector<int> dy = {1, -1, 1, -1};
const long long int mod = 1000000007;
#define loop(i,a,b) for(int i=a;i<b;i++)
ll gcd(ll a,ll b){if(b==0){return a;}return gcd(b,a%b);}
long long binpow(long long a, long long b, long long m) {  a %= m;long long res = 1; while (b > 0) { if (b & 1)res = res * a % m;a = a * a % m;  b >>= 1;} return res;}
long long sum_array(vector<ll>&ans){ll n = ans.size();ll x = 0;loop(i,0,n){x+=ans[i];}return x;}
ll phi(ll n){ll res = n;for(int i = 2;i*i<=n;i++){if(n%i==0){while(n%i==0){n=n/i;}res-=res/i;}}if(n>1)res=res-res/n;return res;}
ll extendedgcd(ll a, ll b, ll& x, ll& y){if(b==0){x=1;y=0;return a;}ll x1,y1; ll d = extendedgcd(b,a%b,x1,y1);x=y1;y=x1-y1*(a/b);return d;}
bool lineardiph(ll a,ll b,ll c,ll&x,ll&y,ll&g){g=extendedgcd(abs(a),abs(b),x,y); if(c%g!=0){return false;}; x*=c/g;y*=c/g;if(a<0){x=-x;}if(b<0){y=-y;}return true;}
void read(vll&a){loop(i,0,a.size())cin>>a[i];}
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;
vll pref_sum(vll&pref,vll&a){pref[0]=a[0];for(int i = 1;i<a.size();i++){pref[i]=pref[i-1]+a[i];}return pref;}
 long long NcR(int n, int r) {
    if(r > n - r) r = n - r; // because C(n, r) == C(n, n - r)
    long long ans = 1;
    int i;
 
    for(i = 1; i <= r; i++) {
        ans *= n - r + i;
        ans /= i;
    }
 
    return ans;
}
void solve(){
   ll n,k;
   cin>>n>>k;
   
   ll in[n+1]={0};
 
    vector<ll>adj[n+1];
   queue<ll>v;
   vector<bool>mark(n+1,false);
   for(int i = 0;i<n-1;i++){
    ll x,y;
    cin>>x>>y;
    adj[x].push_back(y);
    adj[y].push_back(x);
    in[x]++;
    in[y]++;
   }
   for(int i = 1;i<n+1;i++){
        if(in[i]<=1){
             v.push(i);
             mark[i]=true;
        }
   }
   
   ll tot = n;
   while(!v.empty() and k--){
    auto t =  v.size();
    for(int i=0;i<t;i++){
        auto fst = v.front();
        --tot;
        v.pop();
        for(auto x : adj[fst]){
           in[x]--;
           if(in[x]<=1 and mark[x]==false){
             mark[x]=true;
             v.push(x);
           }
        }
    }
   }
   cout<<max(tot,0LL)<<endl;
  return;

}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
    ll t;
    cin>>t;
    //t = 1;
    // for(int i = 1;i<=t;i++){
    //     cout<<"Case #"<<i<<": ";
    //     solve();
    // }
    while(t--){
        solve();
    }
 
}


Comments

Submit
0 Comments
More Questions

1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation
1360A - Minimal Square
467A - George and Accommodation
893C - Rumor
227B - Effective Approach
1534B - Histogram Ugliness
1611B - Team Composition Programmers and Mathematicians
110A - Nearly Lucky Number
1220B - Multiplication Table
1644A - Doors and Keys
1644B - Anti-Fibonacci Permutation