Problem:
C: ツイート数
問題
AORイカちゃんが利用するSNSであるイカったーでは、投稿のことをツイートと呼ぶ。
そして、イカったーでは、ツイートへの返信が多くなると視認性が悪くなることが懸念されるため、あるツイートが次の規則のいずれかを満たすときに画面にそのツイートを表示する仕様になっている。
規則１. どのツイートへも返信していない
規則２. どのツイートからも返信されていない
規則３. 規則２が適用されたツイートから返信先を順に辿ったとき、 $K$ 回未満で辿り着ける
なお、同じツイートは重複して表示されることはない。
いま、 $N$ 個のツイートがあり、 $A_i$ が $0$ のとき $i$ 番目のツイートは返信でないツイートで、 $A_i$ が $0$ でないとき $i$ 番目のツイートは $A_i$ 番目のツイートへの返信のツイートである。
画面に表示されるツイート数を答えよ。
制約
$1 \le N \le 10^5$
$1 \le K \le 10^5$
$0 \le A_i \lt i (i = 1, 2, \dots, N)$
ツイートは時系列順に与えられる。
入力は全て整数で与えられる。
入力形式
入力は以下の形式で与えられる。
$N \ K$
$A_1$
$A_2$
$\vdots$
$A_N$
出力
画面に表示されるツイート数を出力せよ。また、末尾に改行も出力せよ。
サンプル
サンプル入力 1
6 3
0
0
2
3
4
5
サンプル出力 1
5
この時、表示されるツイートは、図の青いツイートである。 よって、この例の解は $5$ である。
サンプル入力 2
12 2
0
1
0
3
4
3
6
7
0
9
10
11
サンプル出力 2
10
サンプル入力 3
5 10
0
0
0
0
0
サンプル出力 3
5

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    int n, k, v;
    cin >> n >> k;
    vector<vector<int>> g(n);
    vector<int> R, H(n);
    for(int i = 0; i < n; i++){
        cin >> v;
        if(v == 0){
            R.push_back(i);
            continue;
        }
        g[v - 1].push_back(i);
    }
    function<int(int)> dfs = [&](int v){
        int res = 1 << 30;
        for(auto &&u:g[v]){
            res = min(res, dfs(u) + 1);
        }
        if(res >> 30 & 1)res = 0;
        return H[v] = res;
    };
    int ans = 0;
    for(auto &&v:R){
        dfs(v);
        if(H[v] >= k)ans++;
    }
    ans += count_if(H.begin(), H.end(), [&](int v){return v < k;});
    cout << ans << '\n';
}
accuracy: 1
time_ms: 20
memory_kb: 15236
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=200005,INF=1<<30;

vector<int> G[MAX],rG[MAX];
int dis[MAX];

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int N,K;cin>>N>>K;
    for(int i=0;i<N;i++){
        int x;cin>>x;
        x--;
        if(x>=0){
            G[i].push_back(x);
            rG[x].push_back(i);
        }
    }
    
    for(int i=0;i<N;i++) dis[i]=INF;
    queue<int> Q;
    for(int i=0;i<N;i++){
        if(si(rG[i])==0){
            dis[i]=0;
            Q.push(i);
        }
    }
    
    while(!Q.empty()){
        int u=Q.front();Q.pop();
        for(int to:G[u]){
            if(chmin(dis[to],dis[u]+1)){
                Q.push(to);
            }
        }
    }
    
    int ans=0;
    
    for(int i=0;i<N;i++){
        if(si(G[i])==0||dis[i]<K) ans++;
    }
    
    cout<<ans<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 19456
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;

ll N, K;
vector<ll> A, I;

int main(){
    cin >> N >> K;
    A.resize(N);
    I.resize(N);
    vector<bool> rule1(N), rule2(N, true);
    for(ll i=0; i<N; i++) {
        auto& a = A[i];
        cin >> a;
        a--;
        if (a==-1) rule1[i] = true;
        else rule2[a] = false;
    }
    for(ll i=N-1; i>=0; i--){
        if (A[i]==-1)continue;
        if (rule2[i]) I[i] = K;
        ll a = A[i];
        if (a != -1) {
            if (I[a] < I[i]-1) I[a] = I[i]-1;
        }
    }
    ll ans = 0;
    for(ll i=0; i<N; i++){
        if (rule1[i] || I[i]>0) ans++;
        //cout << i << " " << rule2[i] << endl;
    }
    cout << ans << endl;


}
accuracy: 1
time_ms: 10
memory_kb: 4700
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using uint = unsigned;
using pcc = pair<char, char>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tuplis = array<ll, 3>;
template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;
const ll LINF=0x1fffffffffffffff;
const ll MINF=0x7fffffffffff;
const int INF=0x3fffffff;
const int MOD=1000000007;
const int MODD=998244353;
const ld DINF=numeric_limits<ld>::infinity();
const ld EPS=1e-9;
const ld PI=3.1415926535897932;
const ll dx[] = {0, 1, 0, -1, 1, -1, 1, -1};
const ll dy[] = {1, 0, -1, 0, 1, 1, -1, -1};
#define overload4(_1,_2,_3,_4,name,...) name
#define overload3(_1,_2,_3,name,...) name
#define rep1(n) for(ll i=0;i<n;++i)
#define rep2(i,n) for(ll i=0;i<n;++i)
#define rep3(i,a,b) for(ll i=a;i<b;++i)
#define rep4(i,a,b,c) for(ll i=a;i<b;i+=c)
#define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)
#define rrep1(n) for(ll i=n;i--;)
#define rrep2(i,n) for(ll i=n;i--;)
#define rrep3(i,a,b) for(ll i=b;i-->(a);)
#define rrep4(i,a,b,c) for(ll i=(a)+((b)-(a)-1)/(c)*(c);i>=(a);i-=c)
#define rrep(...) overload4(__VA_ARGS__,rrep4,rrep3,rrep2,rrep1)(__VA_ARGS__)
#define each1(i,a) for(auto&&i:a)
#define each2(x,y,a) for(auto&&[x,y]:a)
#define each3(x,y,z,a) for(auto&&[x,y,z]:a)
#define each(...) overload4(__VA_ARGS__,each3,each2,each1)(__VA_ARGS__)
#define all1(i) begin(i),end(i)
#define all2(i,a) begin(i),begin(i)+a
#define all3(i,a,b) begin(i)+a,begin(i)+b
#define all(...) overload3(__VA_ARGS__,all3,all2,all1)(__VA_ARGS__)
#define rall1(i) (i).rbegin(),(i).rend()
#define rall2(i,k) (i).rbegin(),(i).rbegin()+k
#define rall3(i,a,b) (i).rbegin()+a,(i).rbegin()+b
#define rall(...) overload3(__VA_ARGS__,rall3,rall2,rall1)(__VA_ARGS__)
#define sum(...) accumulate(all(__VA_ARGS__),0LL)
#define dsum(...) accumulate(all(__VA_ARGS__),0.0L)
#define Msum(...) accumulate(all(__VA_ARGS__),0_M)
#define elif else if
#define unless(a) if(!(a))
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__;in(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;in(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;in(__VA_ARGS__)
#define Sort(a) sort(all(a))
#define Rev(a) reverse(all(a))
#define Uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))
#define vec(type,name,...) vector<type>name(__VA_ARGS__)
#define VEC(type,name,size) vector<type>name(size);in(name)
#define vv(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define VV(type,name,h,w) vector<vector<type>>name(h,vector<type>(w));in(name)
#define vvv(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
template<class T> auto min(const T& a){ return *min_element(all(a)); }
template<class T> auto max(const T& a){ return *max_element(all(a)); }
inline ll popcnt(ull a){ return __builtin_popcountll(a); }
ll gcd(ll a, ll b){ while(b){ ll c = b; b = a % b; a = c; } return a; }
ll lcm(ll a, ll b){ if(!a || !b) return 0; return a * b / gcd(a, b); }
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
template<class T> bool chmin(T& a, const T& b){ if(a > b){ a = b; return 1; } return 0; }
template<class T> bool chmax(T& a, const T& b){ if(a < b){ a = b; return 1; } return 0; }
template<class T, class U> bool chmin(T& a, const U& b){ if(a > T(b)){ a = b; return 1; } return 0; }
template<class T, class U> bool chmax(T& a, const U& b){ if(a < T(b)){ a = b; return 1; } return 0; }
vector<ll> iota(ll n){ vector<ll> a(n); iota(a.begin(), a.end(), 0); return a; }
vector<pll> factor(ull x){ vector<pll> ans; for(ull i = 2; i * i <= x; i++) if(x % i == 0){ ans.push_back({i, 1}); while((x /= i) % i == 0) ans.back().second++; } if(x != 1) ans.push_back({x, 1}); return ans; }
map<ll,ll> factor_map(ull x){ map<ll,ll> ans; for(ull i = 2; i * i <= x; i++) if(x % i == 0){ ans[i] = 1; while((x /= i) % i == 0) ans[i]++; } if(x != 1) ans[x] = 1; return ans; }
vector<ll> divisor(ull x){ vector<ll> ans; for(ull i = 1; i * i <= x; i++) if(x % i == 0) ans.push_back(i); rrep(ans.size() - (ans.back() * ans.back() == x)) ans.push_back(x / ans[i]); return ans; }
template<class T> unordered_map<T, ll> press(vector<T> a){ Uniq(a); unordered_map<T, ll> ans; rep(a.size()) ans[a[i]] = i; return ans; }
template<class T> map<T, ll> press_map(vector<T> a){ Uniq(a); map<T, ll> ans; rep(a.size()) ans[a[i]] = i; return ans; }
int scan(){ return getchar(); }
void scan(int& a){ scanf("%d", &a); }
void scan(unsigned& a){ scanf("%u", &a); }
void scan(long& a){ scanf("%ld", &a); }
void scan(long long& a){ scanf("%lld", &a); }
void scan(unsigned long long& a){ scanf("%llu", &a); }
void scan(char& a){ do{ a = getchar(); }while(a == ' ' || a == '\n'); }
void scan(float& a){ scanf("%f", &a); }
void scan(double& a){ scanf("%lf", &a); }
void scan(long double& a){ scanf("%Lf", &a); }
void scan(vector<bool>& a){ for(unsigned i = 0; i < a.size(); i++){ int b; scan(b); a[i] = b; } }
void scan(char a[]){ scanf("%s", a); }
void scan(string& a){ cin >> a; }
template<class T> void scan(vector<T>&);
template<class T, size_t size> void scan(array<T, size>&);
template<class T, class L> void scan(pair<T, L>&);
template<class T, size_t size> void scan(T(&)[size]);
template<class T> void scan(vector<T>& a){ for(auto&& i : a) scan(i); }
template<class T> void scan(deque<T>& a){ for(auto&& i : a) scan(i); }
template<class T, size_t size> void scan(array<T, size>& a){ for(auto&& i : a) scan(i); }
template<class T, class L> void scan(pair<T, L>& p){ scan(p.first); scan(p.second); }
template<class T, size_t size> void scan(T (&a)[size]){ for(auto&& i : a) scan(i); }
template<class T> void scan(T& a){ cin >> a; }
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){ scan(head); in(tail...); }
void print(){ putchar(' '); }
void print(bool a){ printf("%d", a); }
void print(int a){ printf("%d", a); }
void print(unsigned a){ printf("%u", a); }
void print(long a){ printf("%ld", a); }
void print(long long a){ printf("%lld", a); }
void print(unsigned long long a){ printf("%llu", a); }
void print(char a){ printf("%c", a); }
void print(char a[]){ printf("%s", a); }
void print(const char a[]){ printf("%s", a); }
void print(float a){ printf("%.15f", a); }
void print(double a){ printf("%.15f", a); }
void print(long double a){ printf("%.15Lf", a); }
void print(const string& a){ for(auto&& i : a) print(i); }
template<class T> void print(const complex<T>& a){ if(a.real() >= 0) print('+'); print(a.real()); if(a.imag() >= 0) print('+'); print(a.imag()); print('i'); }
template<class T> void print(const vector<T>&);
template<class T, size_t size> void print(const array<T, size>&);
template<class T, class L> void print(const pair<T, L>& p);
template<class T, size_t size> void print(const T (&)[size]);
template<class T> void print(const vector<T>& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ putchar(' '); print(*i); } }
template<class T> void print(const deque<T>& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ putchar(' '); print(*i); } }
template<class T, size_t size> void print(const array<T, size>& a){ print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ putchar(' '); print(*i); } }
template<class T, class L> void print(const pair<T, L>& p){ print(p.first); putchar(' '); print(p.second); }
template<class T, size_t size> void print(const T (&a)[size]){ print(a[0]); for(auto i = a; ++i != end(a); ){ putchar(' '); print(*i); } }
template<class T> void print(const T& a){ cout << a; }
int out(){ putchar('\n'); return 0; }
template<class T> int out(const T& t){ print(t); putchar('\n'); return 0; }
template<class Head, class... Tail> int out(const Head& head, const Tail&... tail){ print(head); putchar(' '); out(tail...); return 0; }
#ifdef DEBUG
inline ll __lg(ull __n){ return sizeof(ull) * __CHAR_BIT__  - 1 - __builtin_clzll(__n); }
#define debug(...) { print(#__VA_ARGS__); print(":"); out(__VA_ARGS__); }
#else
#define debug(...) void(0)
#endif
int first(bool i = true){ return out(i?"first":"second"); }
int yes(bool i = true){ return out(i?"yes":"no"); }
int Yes(bool i = true){ return out(i?"Yes":"No"); }
int No(){ return out("No"); }
int YES(bool i = true){ return out(i?"YES":"NO"); }
int NO(){ return out("NO"); }
int Yay(bool i = true){ return out(i?"Yay!":":("); }
int possible(bool i = true){ return out(i?"possible":"impossible"); }
int Possible(bool i = true){ return out(i?"Possible":"Impossible"); }
int POSSIBLE(bool i = true){ return out(i?"POSSIBLE":"IMPOSSIBLE"); }
void Case(ll i){ printf("Case #%lld: ", i); }




signed main(){
    LL(n,k);
    VEC(ll,a,n);
    each(i,a)i--;
    vec(ll,h,n,LINF);
    vec(ll,reps,n);
    vector<ll>q;
    rep(n)if(a[i]!=-1)reps[a[i]]++;
    rep(n)if(!reps[i]){
        h[i]=0;
        q.push_back(i);
    }
    rep(q.size()){
        const ll at=q[i];
        if(a[at]!=-1){
            chmin(h[a[at]],h[at]+1);
            if(!--reps[a[at]])q.push_back(a[at]);
        }
    }
    ll ans=0;
    rep(n)if(a[i]==-1||h[i]<k)ans++;
    out(ans);
}
accuracy: 1
time_ms: 10
memory_kb: 6300
final_rank: 3

code_snippet: #include<iostream>
#include<vector>
using namespace std;
using ll = long long;


int main(){
    int n,k;
    cin >> n >> k;
    vector<int> roots;
    vector<vector<int>> children(n, vector<int>());
    for(int i=0;i<n;i++){
        int a;
        cin >> a;
        if(a == 0){
            roots.push_back(i);
        }else{
            children[a-1].push_back(i);
        }
    }
    int ans = 0;
    auto dfs = [&children,&ans,k](int now,bool flag, auto dfs)-> int{
        int now_min = 1000000;
        if(children[now].size() == 0){
            ans++;
            return 0;
        }else{
            for(const int& nxt: children[now]){
                now_min = min(now_min, dfs(nxt,0, dfs) + 1);
            }
            if(now_min < k or flag){
                ans++;
            }
            return now_min;
        }
    };
    for(const int& root: roots){
        dfs(root,1,dfs);
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 11512
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
 
vector<vector<int>> v(100010);
vector<vector<int>> inv(100010);
int a[100010];

bool used[100010];
int ans = 0;
int n,k;


signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(20);
 
    cin>>n>>k;
    for(int i=0;i<n;i++){
        cin>>a[i];
        a[i]--;
        if(a[i]>=0){
            v[i].push_back(a[i]);
            inv[a[i]].push_back(i);
        }
    }
    queue<int> q;
    for(int i=0;i<n;i++){
        if(a[i]==-1){
            ans++;
            used[i] = true;
            continue;
        }
        if(inv[i].size()==0){
            q.push(i);
        }
    }
    int d[n] = {};
    while(q.size()){
        int t = q.front(); q.pop();
        if(!used[t]) ans++;
        used[t] = true;
        for(auto i:v[t]){
            if(!used[i] && d[t]+1<k){
                if(d[i]==0){
                    d[i] = d[t]+1;
                    q.push(i);
                }
            }
        }
    }
    
    cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 14700
final_rank: 15

code_snippet: #include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

int main(){
	int n,k;
	cin>>n>>k;
	vector<int> nxt(n);
	rep(i,n){
		cin>>nxt[i];
		--nxt[i];
	}
	vector<int> dp(n,inf);
	for(int i=n-1;i>=0;--i){
		if(dp[i]==inf)dp[i]=0;
		if(~nxt[i])dp[nxt[i]]=min(dp[nxt[i]],dp[i]+1);
		else dp[i]=0;
	}
	int ans=0;
	rep(i,n){
		if(dp[i]<k)++ans;
	}
	cout<<ans<<endl;
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3616
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (long long i = 0, max_i = (n); i < max_i; i++)
template <class T>
ostream& operator<<(ostream& s, vector<T>& d) {
    REP(i, d.size()) s << d[i] << (i == d.size() - 1 ? "" : " ");
    return s;
}
using Int = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    Int n, K;
    cin >> n >> K;
    vector<vector<Int>> g(n);
    vector<Int> ps(n);
    REP(i, n) {
        Int p;
        cin >> p;
        ps[i] = p;
        if (p) {
            g[p - 1].push_back(i);
        }
    }

    vector<bool> use(n);
    vector<Int> rem(n, -1);
    function<Int(Int)> dfs = [&](Int v) {
        if (g[v].size() == 0) return rem[v] = K;
        if (rem[v] != -1) return rem[v];
        Int ma = 0;
        for (Int s : g[v]) {
            ma = max(ma, dfs(s) - 1);
        }
        return rem[v] = ma;
    };
    REP(i, n) { dfs(i); }
    int ans = 0;
    REP(i, n) {
        ans += (rem[i] > 0 || ps[i] == 0);
        // cerr << ans << " " << i << endl;
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 15984
final_rank: 12

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define int long long

const int INF = 1 << 30;

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n,k; cin >> n >> k;
  vector<int> a(n),G[n];
  int ans = 0;
  for(int i = 0; i < n; i++){
    cin >> a[i];
    a[i]--;
    if(a[i] == -1) continue;
    G[a[i]].push_back(i);
  }

  vector<int> d(n,INF);
  function< int(int,int) > rec =
  [&](int u,int par){
    if(G[u].empty()) d[u] = 1;
    else {
      for(auto v : G[u]){
        d[u] = min(d[u],rec(v,0) + 1);
      }
    }
    if(par == -1 || d[u] <= k) ans++;
    return d[u];
  };

  for(int i = 0; i < n; i++){
    if(d[i] == INF){
      if(G[i].empty()) ans++;
      else rec(i,-1);
    }
  }
  cout << ans << endl;

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 17628
final_rank: 14

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#define REP(i,N) for(int i=0;i<(N);++i)
using namespace std;
class tree{
public:
  bool parent;
  vector<int> child;
};
vector<tree> t;
int N,K;
int ans;
int func(int place){
  if(t[place].child.empty()){
    ++ans;
    return 1;
  }
  if(t[place].parent){
    ++ans;
    REP(i,t[place].child.size()){
      func(t[place].child[i]);
    }
    return 0;
  }
  int dep = 1000000;
  REP(i,t[place].child.size()){
    dep = min(dep,func(t[place].child[i]));
  }
  if(dep < K)++ans;
  return dep + 1;
}
int main(){
  cin >> N >> K;
  t.resize(N+1);
  REP(i,N){
    int j = i + 1;
    int d;
    cin >> d;
    t[j].parent = d == 0;
    t[d].child.push_back(j);
  }
  REP(i,N){
    int j = i + 1;
    if(t[j].parent){
      func(j);
    }
  }
  cout << ans << endl;
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 12312
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define lp(i,n) for(int i=0;i<n;i++)
#define lps(i,j,n) for(int i=j;i<n;i++)
#define fordebug int hoge;cin>>hoge;
#define DEKAI 1000000007;
#define INF (1<<28)
#define int long long
#define double long double
#define floot10 cout<<fixed<<setprecision(10)
int check[101000];
int nex[101000];
int done[101000];

signed main(){
  int n,k;
  cin>>n>>k;
  int ans=0;
  lp(i,n){
    int a;
    cin>>a;
    if(a==0) ans++;
    else{
      nex[i+1]=a;
      check[a]=1;
    }
  }
  queue<pair<int,int> > q;
  lp(i,n+1){
    if(i==0) continue;
    if(check[i]==0){
      q.push({i,0});
    }
  }
  while(!q.empty()){
    int now=q.front().first;
    int count=q.front().second;
    q.pop();
    if(done[now]==1) continue;
    done[now]=1;
    if(count>=k) continue;
    if(nex[now]==0) continue;
    ans++;
    q.push({nex[now],count+1});
  }
  cout<<ans<<endl;
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 6200
final_rank: 6

code_snippet: #include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef pair<int,int>P;
#define F first
#define S second

int used[100009];
int jis[100009],n,k,a[100009];
vector<int>v[100009];

signed main(){
  cin>>n>>k;
  r(i,n){
    cin>>a[i];
    if(a[i]){
      v[i].push_back(a[i]-1);
      jis[a[i]-1]++;
    }
  }
  queue<int>q;
  r(i,n){
    if(jis[i]==0||a[i]==0){
      if(a[i])q.push(i);
      used[i]=1;
    }
  }
  while(!q.empty()){
    int x=q.front();q.pop();
   // cout<<x<<' '<<used[x]<<endl;
    if(used[x]==k)continue;
    r(i,v[x].size()){
      if(used[v[x][i]])continue;
      used[v[x][i]]=used[x]+1;
      q.push(v[x][i]);
    }
  }
  int s=0;
  r(i,n){
    if(used[i])s++;
  }
  cout<<s<<endl;
}
accuracy: 1
time_ms: 20
memory_kb: 9932
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;

//INSERT ABOVE HERE
signed main(){
  Int n,k;
  cin>>n>>k;
  
  vector<vector<Int> > G(n);
  vector<Int> a(n);
  for(Int i=0;i<n;i++){
    cin>>a[i];
    a[i]--;
    if(~a[i]) G[a[i]].emplace_back(i);
  }

  vector<Int> dist(n,-1);
  queue<Int> qu;
  for(Int i=0;i<n;i++){
    if(G[i].empty()){
      dist[i]=0;
      qu.emplace(i);
    }
  }
  
  while(!qu.empty()){
    Int v=qu.front();qu.pop();
    if(a[v]<0||~dist[a[v]]) continue;
    dist[a[v]]=dist[v]+1;
    qu.emplace(a[v]);
  }
  
  Int ans=0;
  for(Int i=0;i<n;i++){
    if(a[i]<0||dist[i]<k) ans++;    
  } 
  cout<<ans<<endl;
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 9972
final_rank: 5

code_snippet: #include<iostream>
#include<vector>
#include<queue>
#include<numeric>
using namespace std;

int main(){
    int n, k;
    cin >> n >> k;
    vector<int> a(n, 0), indeg(n, 0), v(n);
    for(int i = 0; i < n; i++){
        cin >> v[i];
        v[i]--;
        if(v[i] != -1)   indeg[v[i]]++;
    }
    priority_queue<pair<int,int>> pq;
    for(int i = 0; i < n; i++){
        if(indeg[i] == 0)   pq.push({k, i});
    }
    while(!pq.empty()){
        pair<int,int> p = pq.top(); pq.pop();
        int x = p.first, y = p.second;
        if(a[y] == 1)    continue;
        a[y] = 1;
        if(x == 1 || v[y] == -1)    continue;
        if(a[v[y]] == 0) pq.push({x-1, v[y]});
    }
    for(int i = 0; i < n; i++)  if(v[i] == -1)   a[i] = 1;
    cout << accumulate(a.begin(), a.end(), 0) << endl;
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 5048
final_rank: 7

code_snippet: #include<iostream>
#include<vector>
#include<queue>
#include<numeric>
using namespace std;

int main(){
    int n, k;
    cin >> n >> k;
    vector<int> a(n, 0), indeg(n, 0), v(n);
    for(int i = 0; i < n; i++){
        cin >> v[i];
        v[i]--;
        if(v[i] != -1)   indeg[v[i]]++;
    }
    priority_queue<pair<int,int>> pq;
    for(int i = 0; i < n; i++){
        if(k > 0 && indeg[i] == 0)   pq.push({k, i});
    }
    while(!pq.empty()){
        pair<int,int> p = pq.top(); pq.pop();
        if(a[p.second] == 1)    continue;
        a[p.second] = 1;
        if(p.first == 1 || v[p.second] == -1)    continue;
        if(a[v[p.second]] == 0) pq.push({p.first-1, v[p.second]});
    }
    for(int i = 0; i < n; i++)  if(v[i] == -1)   a[i] = 1;
    cout << accumulate(a.begin(), a.end(), 0) << endl;
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 5048
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(int)(n);i++)

int n,k;
int a[125252];
int outdeg[125252], indeg[125252];
int po[125252];
bool view[125252];
bool used[125252];

int main(){
  scanf("%d%d",&n,&k);
  REP(i,n)scanf("%d",a+i),a[i]--;
  REP(i,n){
    if(a[i]!=-1){
      outdeg[i]++;
      indeg[a[i]]++;
    }
  }
  REP(i,n)view[i] = (outdeg[i]==0) || (indeg[i]==0);
  stack<int> S;
  REP(i,n)if(indeg[i]==0){
    po[i] = k;
    S.push(i);
  }
  while(!S.empty()){
    int p = S.top(); S.pop();
    used[p] = true;
    int to = a[p];
    po[to] = max(po[to], po[p]-1);
    indeg[to]--;
    if(indeg[to] == 0){
      S.push(to);
    }
  }
  REP(i,n)view[i] = view[i] || (po[i]>0);
  int ans = 0;
  REP(i,n)ans+=view[i];
  printf("%d\n",ans);
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 5188
final_rank: 2

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = double;
const ld eps = 1e-9;
//
//using Graph = vector<vector<int>>;
//
//int dfs(const Graph&g, const int now, vector<int>&ch_cnts) {
//  ch_cnts[now]++;
//  for (auto&& e : g[now]) {
//      ch_cnts[now] += dfs(g, e, ch_cnts);
//  }
//  return ch_cnts[now];
//}
//
//struct HL_Edge {
//  int from;
//  int to;
//  bool heavy;
//};
//
//void dfs2(const Graph&g, vector<vector<HL_Edge>>&hl_tree, const int now, const vector<int>&ch_cnts) {
//
//  int max_ch_id = 1;
//  {
//      int max_ch_num = -1;
//      for (auto&&e : g[now]) {
//          if (max_ch_num < ch_cnts[e]) {
//              max_ch_num = ch_cnts[e];
//              max_ch_id = e;
//          }
//      }
//  }
//  for (auto e : g[now]) {
//      bool heavy = (e == max_ch_id);
//
//      hl_tree[now].push_back(HL_Edge{ now,e,heavy });
//  }
//  return;
//}
//
//void HL(const vector<vector<int>>&ro_tree) {
//  vector<int>ch_cnts(ro_tree.size());
//
//  dfs(ro_tree, 0, ch_cnts);
//
//  vector<vector<HL_Edge>>hl_tree(ro_tree.size());
//
//  dfs2(ro_tree, hl_tree, 0, ch_cnts);
//}
//
//namespace FastFourierTransform
//{
//  using C = complex< double >;
//
//  void DiscreteFourierTransform(vector< C > &F, bool rev)
//  {
//      const int N = (int)F.size();
//      const double PI = (rev ? -1 : 1) * acos(-1);
//      for (int i = 0, j = 1; j + 1 < N; j++) {
//          for (int k = N >> 1; k > (i ^= k); k >>= 1);
//          if (i > j) swap(F[i], F[j]);
//      }
//      C w, s, t;
//      for (int i = 1; i < N; i <<= 1) {
//          for (int k = 0; k < i; k++) {
//              w = polar(1.0, PI / i * k);
//              for (int j = 0; j < N; j += i * 2) {
//                  s = F[j + k];
//                  t = C(F[j + k + i].real() * w.real() - F[j + k + i].imag() * w.imag(),
//                      F[j + k + i].real() * w.imag() + F[j + k + i].imag() * w.real());
//                  F[j + k] = s + t, F[j + k + i] = s - t;
//              }
//          }
//      }
//      if (rev) for (int i = 0; i < N; i++) F[i] /= N;
//  }
//
//  vector< int> Multiply(const vector<int > &A, const vector<int > &B)
//  {
//      int sz = 1;
//      while (sz <= A.size() + B.size()) sz <<= 1;
//      vector< C > F(sz), G(sz);
//      for (int i = 0; i < A.size(); i++) F[i] = A[i];
//      for (int i = 0; i < B.size(); i++) G[i] = B[i];
//      DiscreteFourierTransform(F, false);
//      DiscreteFourierTransform(G, false);
//      for (int i = 0; i < sz; i++) F[i] *= G[i];
//      DiscreteFourierTransform(F, true);
//      vector< int > X(A.size() + B.size() - 1);
//      for (int i = 0; i < A.size() + B.size() - 1; i++) X[i] = F[i].real() + 0.5;
//      return (X);
//  }
//};

#define Seg_Max_N (1<<19) 

using Value = int;
const Value ini = 0;
struct segtree {
	int N;
	vector<Value>dat;

	segtree() {}
	segtree(int n) :dat(2 * Seg_Max_N) {
		N = 1;
		while (N < n) N *= 2;
		for (int i = 0; i < 2 * N - 1; i++) {
			dat[i] = ini;
		}
	}
	Value connect(const Value&l, const Value&r) {
		return l+r;
	}
	// update k th element
	void update(int k, Value a) {
		k += N - 1;
		dat[k] = a;

		while (k > 0) {
			k = (k - 1) / 2;
			const Value al(dat[k * 2 + 1]);
			const Value ar(dat[k * 2 + 2]);
			dat[k] = connect(al, ar);
		}
	}
	// min [a, b)
	Value  query(int a, int b) { return query(a, b, 0, 0, N); }
	Value  query(int a, int b, int k, int l, int r) {
		if (r <= a or b <= l) return ini;
		if (a <= l and r <= b) return dat[k];
		const int m = (l + r) / 2;
		const Value al(query(a, b, k * 2 + 1, l, m));
		const Value ar(query(a, b, k * 2 + 2, m, r));
		return connect(al, ar);
	}
};

pair<int, int>dfs(const vector<vector<int>>&edges, const int K,const int now) {

	int d=1e8;
	int sum=0;
	for (auto e : edges[now]) {
		auto p(dfs(edges,K,e));
		d=min(d,p.first);
		sum+=p.second;
	}
	if (edges[now].empty()) {
		d=0;
	}
	{
		if (d < K) {
			sum++;
		}
	}
	return make_pair(d+1,sum);
	
}


int main() {
	int N;cin>>N;
	int K;
	cin>>K;

	vector<vector<int>>edges(N);
	vector<int>comes(N);
	for (int i = 0; i < N; ++i) {
		int a;cin>>a;
		if (a) {
			edges[a-1].push_back(i);
			comes[i]++;
		}
	}
	int ans=0;
	for (int i = 0; i < N; ++i) {
		if (comes[i] == 0) {
			auto p=dfs(edges,K,i);
			if (p.first <= K) {

			}
			else {
				ans++;
			}
			ans+=p.second;
		}
	}
	cout<<ans<<endl;
	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 16568
final_rank: 20

code_snippet: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0; i<n;i++)
using namespace std;
#define int long long
signed main(){
    int n,k;
    typedef pair<int,int> pii;
    cin>>n>>k;
    vector<bool>flag(n+1,true);
    vector<bool>IsPrint(n+1,true);
    vector<pii>mp(n+1);
    int ans=0;
    rep(i,n){
        int temp; cin>>temp;
        if(temp==0){ans++; mp[i+1]=pii(0,0);flag[i+1]=false; continue;}
        mp[i+1]=pii(temp,mp[temp].second+1);
        flag[temp]=false;
    }
    vector<int>yebi;
    vector<int>dp(n+1,0);
    rep(i,n)if(flag[i+1])yebi.push_back(i+1);
    rep(z,yebi.size()){
        int i=yebi[z];
        ans++;
        IsPrint[i]=false;
        flag[i]=false;
        pii next=mp[i];
        for(int i=1;i<=k-1;i++){
            if(mp[next.first].second==0)break;
            if(IsPrint[next.first]==false){
                if(dp[next.first]>=k-1-i)break;
                dp[next.first]=max(dp[next.first],k-1-i);
                IsPrint[next.first]=false;
                next=mp[next.first];
                continue;
            }
            IsPrint[next.first]=false;
            flag[next.first]=false;
            ans++;
            next=mp[next.first];
        }
    }
    cout<<ans<<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 6300
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb emplace_back
typedef long long ll;
typedef pair<int,int> pint;

vector<pint> vp[100001];
int d[100001];
int INF=1000100010;
int n;
int dijkstra(int st){
    priority_queue<pint,vector<pint>,greater<pint> > pq;
    rep(i,n+1) d[i]=INF;
    d[st]=0;
    pq.push(make_pair(0,st));
    while(!pq.empty()){
        pint pi=pq.top();pq.pop();
        int t=pi.second;
        if(d[t]<pi.first) continue;
        for(auto it:vp[t]){
            if(d[it.second]>d[t]+it.first){
                d[it.second]=d[t]+it.first;
                pq.push(make_pair(d[it.second],it.second));
            }
        }
    }
}
int cnt[100001];
int main(){
    int k,ai;
    cin>>n>>k;
    rep(i,n){
        cin>>ai;
        if(ai==0) vp[0].pb(1,i+1);
        else vp[i+1].pb(1,ai);
        ++cnt[ai];
    }
    FOR(i,1,n+1){
        if(cnt[i]==0) vp[0].pb(1,i);
    }
    dijkstra(0);
    int ans=0;
    FOR(i,1,n+1)if(d[i]<=k) ++ans;
    cout<<ans<<endl;
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 11076
final_rank: 19

code_snippet: #include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
 
int n,k;
int a[100001];
vector<int> G[100005];
vector<int> rG[100005];
int dist[100005];
 
int main(void){
    scanf("%d%d",&n,&k);
    queue<int> que;
    memset(dist,-1,sizeof(dist));
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
        if(a[i]>0){
            G[a[i]-1].push_back(i);
            rG[i].push_back(a[i]-1);
        }
    }
    for(int i=0;i<n;i++){
        if(G[i].size()==0){
            que.push(i);
            dist[i]=0;
        }
    }
    while(que.size()){
        int v=que.front();
        que.pop();
        for(int i=0;i<rG[v].size();i++){
            if(dist[rG[v][i]]==-1){
                dist[rG[v][i]]=dist[v]+1;
                que.push(rG[v][i]);
            }
        }
    }
    int res=0;
    for(int i=0;i<n;i++){
        if(dist[i]<k || rG[i].size()==0)res++;
    }
    printf("%d\n",res);
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 15012
final_rank: 18