結果

問題 No.3217 Shiki no Shiki
ユーザー zawakasu
提出日時 2025-08-01 21:32:27
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
MLE  
実行時間 -
コード長 1,467 bytes
コンパイル時間 1,192 ms
コンパイル使用メモリ 133,928 KB
実行使用メモリ 606,116 KB
最終ジャッジ日時 2025-08-01 21:32:33
合計ジャッジ時間 4,905 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 1 MLE * 1 -- * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <cassert>
#include <vector>
#include <algorithm>
#include <utility>
#include <numeric>
#include <tuple>
#include <ranges>
namespace ranges = std::ranges;
namespace views = std::views;
// #include "Src/Number/IntegerDivision.hpp"
// #include "Src/Utility/BinarySearch.hpp"
// #include "Src/Sequence/CompressedSequence.hpp"
// #include "Src/Sequence/RunLengthEncoding.hpp"
// #include "Src/Algebra/Group/AdditiveGroup.hpp"
// #include "Src/DataStructure/FenwickTree/FenwickTree.hpp"
// #include "Src/DataStructure/SegmentTree/SegmentTree.hpp"
// #include "Src/DataStructure/DisjointSetUnion/DisjointSetUnion.hpp"
// using namespace zawa;
// #include "atcoder/modint"
// using mint = atcoder::modint998244353;
#include <set>
using namespace std;

int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    int N;
    cin >> N;
    vector<int> P(N), cnt(N);
    vector<vector<int>> g(N);
    for (int i = 0 ; i < N ; i++) {
        cin >> P[i];
        P[i]--;
        if (P[i] != -1) {
            cnt[P[i]]++;
            g[i].push_back(P[i]);
        }
    }
    vector<set<int>> dist(N);
    auto dfs = [&](auto dfs, int v, int d) -> void {
        dist[v].insert(d);
        for (int x : g[v]) dfs(dfs, x, d + 1);
    };
    for (int i = 0 ; i < N ; i++) if (cnt[i] == 0) dfs(dfs, i, 0);
    int ans = 0;
    for (int i = 0 ; i < N ; i++) if (dist[i].contains(2)) ans++;
    cout << ans << '\n';
}
0