結果

問題 No.778 クリスマスツリー
ユーザー hiro71687khiro71687k
提出日時 2023-04-06 04:07:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 167 ms / 2,000 ms
コード長 872 bytes
コンパイル時間 4,529 ms
コンパイル使用メモリ 257,672 KB
実行使用メモリ 43,344 KB
最終ジャッジ日時 2024-04-10 04:44:54
合計ジャッジ時間 6,702 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 125 ms
43,344 KB
testcase_07 AC 68 ms
16,940 KB
testcase_08 AC 167 ms
29,128 KB
testcase_09 AC 145 ms
18,900 KB
testcase_10 AC 144 ms
18,944 KB
testcase_11 AC 151 ms
18,936 KB
testcase_12 AC 149 ms
18,916 KB
testcase_13 AC 108 ms
18,284 KB
testcase_14 AC 127 ms
43,244 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll=long long;
using ld=long double;
ld pie=3.141592653589793;
ll inf=144499999999994;
ll mod=1000000007;
ll op(ll a,ll b){
    return a+b;
}
ll e(){
    return 0;
}
ll ans=0;
void dfs(ll now,segtree<ll,op,e>&seg,vector<vector<ll>>&g,vector<ll>&memo){
    memo[now]=1;
    ans+=seg.prod(0,now);
    seg.set(now,1);
    for (ll i = 0; i < g[now].size(); i++)
    {
        if (memo[g[now][i]]==0)
        {
            dfs(g[now][i],seg,g,memo);
        }
    }
    seg.set(now,0);
}
int main(){
    ll n;
    cin >> n;
    vector<ll>a(n);
    vector<vector<ll>>g(n);
    for (ll i = 0; i < n-1; i++)
    {
        cin >> a[i];
        g[a[i]].push_back(i+1);
    }
    vector<ll>memo(n+10,0);
    segtree<ll,op,e>seg(memo);
    dfs(0,seg,g,memo);
    cout << ans << endl;
}
0