結果

問題 No.778 クリスマスツリー
ユーザー batasanblogbatasanblog
提出日時 2023-04-02 15:40:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 181 ms / 2,000 ms
コード長 1,348 bytes
コンパイル時間 4,893 ms
コンパイル使用メモリ 266,600 KB
実行使用メモリ 37,628 KB
最終ジャッジ日時 2023-10-25 04:55:53
合計ジャッジ時間 7,291 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 107 ms
37,628 KB
testcase_07 AC 62 ms
17,540 KB
testcase_08 AC 181 ms
29,180 KB
testcase_09 AC 158 ms
17,036 KB
testcase_10 AC 142 ms
17,036 KB
testcase_11 AC 138 ms
17,036 KB
testcase_12 AC 129 ms
17,036 KB
testcase_13 AC 93 ms
17,564 KB
testcase_14 AC 105 ms
37,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
//using mint = static_modint<998244353>;
//using mint = modint;
using mint = static_modint<1000000007>;
using vm = vector<mint>;
using vvm = vector<vm>;
ostream &operator<<(ostream &o,const mint &m){cout<<m.val();return o;}
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using pl = pair<ll,ll>;
#define rep(i,n) for(ll i=0;i<(ll)(n);++i)
#define reps(i,s,n) for(ll i=(s);i<(ll)(n);++i)
#define rep1(i,n) for(ll i=1;i<=(ll)(n);++i)
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
const long long INF = 1e18;

#ifdef DEBUG
#include <debug.hpp>
#endif

int main(){
#ifdef DEBUG
	cout << "--- Input ---" << endl;
#endif
	ll N;cin>>N;
	vvl g(N);
	reps(v,1,N){
		ll u;cin>>u;
		g.at(u).push_back(v);
		g.at(v).push_back(u);
	}
	fenwick_tree<ll> bit(N);
	ll ans=0;

	auto rec=[&](auto f,ll u,ll p=-1)->void{
#ifdef DEBUG
		cout<<"u="<<u<<" p="<<p<<endl;
#endif
		assert(u>=0&&u<N);
		ans+=bit.sum(0,u);
		bit.add(u,1);
		rep(v,g.at(u).size()){
			if(g.at(u).at(v)==p)continue;
			f(f,g.at(u).at(v),u);
		}
		bit.add(u,-1);
	};
#ifdef DEBUG
	cout<<g;
	cout << "--- Logic ---" << endl;
#endif
	rec(rec,0);	

#ifdef DEBUG
	cout << "--- Answer ---" << endl;
#endif
	cout<<ans<<endl;
	return 0;
}
//cout << fixed << setprecision(9);
0