結果

問題 No.778 クリスマスツリー
ユーザー KINOKINOKINOKINO
提出日時 2018-12-26 16:10:32
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 998 bytes
コンパイル時間 747 ms
コンパイル使用メモリ 62,964 KB
実行使用メモリ 814,208 KB
最終ジャッジ日時 2024-04-09 02:54:26
合計ジャッジ時間 3,559 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 MLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <list>

using namespace std;


struct Node {
	Node(){
		count=0;
		finished=false;
	}
	int i;
	int parent;
	int count;
	bool finished;
};

void count(vector<Node> &nodes,Node &node,list<Node*> path)
{
	if(node.parent>=0)
	{
		Node &parent = nodes[node.parent];

		path.push_back(&node);
		count(nodes,parent,path);
		path.pop_back();
		node.finished=true;
	}

	for( Node *child: path){
		if(!child->finished && child->i > node.i){
			cout << child->i << ":" << node.i << endl;
			child->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;
	}

	list<Node*> path;
	for(int i=1;i<nodes.size();i++){
		Node &node = nodes[i];
		count(nodes,node,path);
	}
	int ans = 0;
	for(int i=0;i<nodes.size();i++){
		ans += nodes[i].count;
	}
	cout << ans;

	// your code goes here
	return 0;
}
0