結果

問題 No.3217 Shiki no Shiki
ユーザー aqua
提出日時 2025-08-01 22:06:11
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 968 bytes
コンパイル時間 3,130 ms
コンパイル使用メモリ 284,452 KB
実行使用メモリ 9,856 KB
最終ジャッジ日時 2025-08-01 22:06:27
合計ジャッジ時間 4,666 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 5 WA * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

bool chmin(auto &a, auto b) { return a > b ? a = b, true : false; }
bool chmax(auto &a, auto b) { return a < b ? a = b, true : false; }

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

    int N; cin >> N;
    vector<vector<int>> G(N);
    vector<int> cnt(N);
    for(int i=0; i<N; ++i) {
        int P; cin >> P;
        if(!P) continue;
        --P;
        G[i].emplace_back(P);
        ++cnt[P];
    }
    vector<int> dist(N, -1);
    queue<int> que;
    for(int i=0; i<N; ++i) {
        if(cnt[i] == 0) {
            dist[i] = 0;
            que.push(i);
        }
    }
    while(!que.empty()) {
        int v = que.front(); que.pop();
        for(int i:G[v]) {
            if(dist[i] >= 2) continue;
            dist[i] = dist[v] + 1;
            que.push(i);
        }
    }
    int ans = 0;
    for(int i=0; i<N; ++i) if(dist[i] == 2) ++ans;
    cout << ans << endl;
}
0