木について。
TopCoderではときどき頭混乱させるような木のデータが与えられるので、まとめ。
//consider to be i = 0-based {-1, 0, 0, 2, 2, 4, 4} //i = 3ばんめの2は、2番目に親があるよってことを示してる. int start = 0; for(int i = 0; i < sz; i++) { if(heap[i] == -1) { start = i; continue; } arr[ heap[i] ].push_back(i); } //you can also use map-STL map<int, vector<int> > table; for(int i = 0; i < sz; i++) { table[ heap[i] ].push_back(i); } table[k] -->{1, 2, 3} //which means node 1, 2 and 3 are node-k's decendant.