寝癖頭の解法

学習中の覚え書きを投稿、更新していきます。

AtCoder Problems in C++ #committee - 委員会 (Committee)

2008年 日本情報オリンピック春合宿OJから、その提出コードの解答例です。
AtCoderとは、コンテストを通じて、プログラミングやアルゴリズムを学習するサービスです。
atcoder.jp
プログラミングコンテストとは、「与えられた問題をいかに素早く、正確に」解くことができるかを競うものです。
競技プログラミング」を略して、「競プロ」などと呼ばれています。

#committee - 委員会 (Committee)

https://www.ioi-jp.org/camp/2008/2008-sp-tasks/2008-sp_tr-day1_20.pdf

僕が作成、提出したコードは、以下のとおりです。

/*
AtCoder Problems in C++
#committee - 委員会 (Committee)
https://www.ioi-jp.org/camp/2008/2008-sp-tasks/2008-sp_tr-day1_20.pdf
提出コードの解答例
https://neguse-atama.hatenablog.com
*/
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
ll f(ll i,vector<ll> &c,vector<vector<ll>> &g,ll &ans){
  ll t=c[i];
  for(ll j : g[i]){
    t+=f(j,c,g,ans);
  }
  ans=max(ans,t);
  return max(0LL,t);
}
int main(void){
  ll n;
  cin>>n;
  vector<ll> c(n);
  vector<vector<ll>> g(n);
  ll ans=-1000000000;
  for(ll i=0;i<n;i++){
    ll t;
    cin>>t;
    if(t!=0){
      g[t-1].push_back(i);
    }
    cin>>t;
    c[i]=t;
    ans=max(ans,t);
  }
  f(0,c,g,ans);
  cout<<ans<<endl;
  return 0;
}