結果
| 問題 | No.778 クリスマスツリー | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2018-12-26 15:47:56 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                MLE
                                 
                             | 
| 実行時間 | - | 
| コード長 | 900 bytes | 
| コンパイル時間 | 445 ms | 
| コンパイル使用メモリ | 65,572 KB | 
| 実行使用メモリ | 814,176 KB | 
| 最終ジャッジ日時 | 2024-10-01 14:43:18 | 
| 合計ジャッジ時間 | 3,374 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 3 MLE * 1 -- * 8 | 
ソースコード
#include <iostream>
#include <vector>
#include <list>
using namespace std;
struct Node {
	Node(){
		count=0;
	}
	int i;
	int parent;
	vector<int> children;
	int count;
};
void count(vector<Node> &nodes,Node &node,list<Node*> path)
{
	for(int i=0;i<node.children.size();i++){
		int c = node.children[i];
		Node &child = nodes[c];
		path.push_back(&node);
		count(nodes,child,path);
		path.pop_back();
	}
	for( auto &x: path){
		if(x->i<node.i){
			node.count++;
		}
	}
}
int main() {
	
	int N;
	cin >> N;
	vector<Node> nodes(N);
	nodes[0].i=0;
	nodes[0].parent=-1;
	for(int i=1;i<nodes.size();i++){
		int parent;
		cin >> parent;
		nodes[i].i = i;
		nodes[i].parent = parent;
		nodes[parent].children.push_back(i);
	}
	list<Node*> path;
	count(nodes,nodes[0],path);
	int ans = 0;
	for(int i=0;i<nodes.size();i++){
		ans += nodes[i].count;
	}
	cout << ans;
	// your code goes here
	return 0;
}
            
            
            
        