結果
問題 | No.778 クリスマスツリー |
ユーザー |
![]() |
提出日時 | 2023-04-06 04:07:07 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 218 ms / 2,000 ms |
コード長 | 872 bytes |
コンパイル時間 | 4,174 ms |
コンパイル使用メモリ | 254,100 KB |
最終ジャッジ日時 | 2025-02-11 23:14:36 |
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 12 |
ソースコード
#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;}