import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
static final int NMAX = 100001;
static ArrayList<ArrayList<Integer>> tree = new ArrayList<ArrayList<Integer>> (NMAX);
static ArrayList<Integer> killed = new ArrayList<Integer> (NMAX);
static ArrayList<Boolean> used = new ArrayList<Boolean> (NMAX);
static ArrayList<Integer> dist = new ArrayList<Integer> (NMAX);
static int ans = 0;
public static void kills(int v, int p)
{
if (v == p) kills(tree.get(v).get(0), v);
if (tree.
get(v
).
size() == 2) for (Integer to
: tree.
get(v
)) if (to
!= p
) kills
(to, v
); if (tree.get(v).size() >= 3) killed.set(v, killed.get(v) + 1);
return;
}
public static void goup(int v, int p)
{
if (v == p)
{
for (Integer to
: tree.
get(v
)) if (dist.
get(to
) < dist.
get(v
)) goup
(to, v
); killed.set(v, 0);
return;
}
if (tree.
get(v
).
size() == 2) for (Integer to
: tree.
get(v
)) if (to
!= p
) goup
(to, v
); if (tree.get(v).size() >= 3)
{
killed.set(v, killed.get(v) + 1);
return;
}
}
public static void main
(String[] args
) { Scanner in
= new Scanner
(System.
in); for (int i=0;i<NMAX; i++)
{
tree.add(new ArrayList<Integer>());
dist.add(0);
used.add(false);
killed.add(0);
}
int n,v;
n = in.nextInt();
if (n
<= 5) {System.
out.
print(1); return;} // Частный случай tree.get(1).add(0); // Корень тоже должен быть "развилкой"
ArrayList<Integer> leaves = new ArrayList<Integer> ();
for (int i = 1; i < n; i++)
{
v = in.nextInt();
tree.get(v).add(i+1);
tree.get(i+1).add(v); //Заполнение графа в том виде, в котором его дают
}
for (int i = 1; i <= n; i++)
{
if (tree.get(i).size() == 1 || (i == 1 && tree.get(i).size() == 2)) leaves.add(i); //Запоминаем все листья
}
LinkedList<Integer> q = new LinkedList<>(); dist.set(1, 0);
q.offer(1); used.set(1, true);
while (!q.isEmpty())
{
int qv = q.poll();
used.set(qv, true);
for (Integer to
: tree.
get(qv
)) //BFS заполняющий вектор dist {
if (!used.get(to))
{
q.offer(to);
dist.set(to, dist.get(qv)+1);
}
}
}
{
kills(l, l); // Первый этап - запросы от каждого листа
}
int maxdist
= -1; for (int i
= 1; i
<= n
; i
++) maxdist
= Math.
max(maxdist, dist.
get(i
)); // Определение максимального "уровня" дерева int wentup = 1;
while (wentup != 0)
{
wentup = 0;
for (int l = maxdist; l > 0; l--)
{
for (int i = 2; i <= n; i++)
{
if (killed.get(i) == 1 && dist.get(i) == l)
{
goup(i, i); //Этап 2 - поднятие "недошедших" запросов
wentup++;
}
}
}
}
for (int i = 1; i <= n; i++) if (killed.get(i) >= 2) ans++; //Финальный подсчет "отравленных" развилок
}
}
aW1wb3J0IGphdmEudXRpbC5BcnJheUxpc3Q7CmltcG9ydCBqYXZhLnV0aWwuTGlua2VkTGlzdDsKaW1wb3J0IGphdmEudXRpbC5RdWV1ZTsKaW1wb3J0IGphdmEudXRpbC5TY2FubmVyOwoKcHVibGljIGNsYXNzIE1haW4gewogICAgc3RhdGljIGZpbmFsIGludCBOTUFYID0gMTAwMDAxOwogICAgc3RhdGljIEFycmF5TGlzdDxBcnJheUxpc3Q8SW50ZWdlcj4+IHRyZWUgPSBuZXcgQXJyYXlMaXN0PEFycmF5TGlzdDxJbnRlZ2VyPj4gKE5NQVgpOwogICAgc3RhdGljIEFycmF5TGlzdDxJbnRlZ2VyPiBraWxsZWQgPSBuZXcgQXJyYXlMaXN0PEludGVnZXI+IChOTUFYKTsKICAgIHN0YXRpYyBBcnJheUxpc3Q8Qm9vbGVhbj4gdXNlZCA9IG5ldyBBcnJheUxpc3Q8Qm9vbGVhbj4gKE5NQVgpOwogICAgc3RhdGljIEFycmF5TGlzdDxJbnRlZ2VyPiBkaXN0ID0gbmV3IEFycmF5TGlzdDxJbnRlZ2VyPiAoTk1BWCk7CiAgICBzdGF0aWMgaW50IGFucyA9IDA7CgogICAgcHVibGljIHN0YXRpYyB2b2lkIGtpbGxzKGludCB2LCBpbnQgcCkKICAgIHsKICAgICAgICBpZiAodiA9PSBwKSBraWxscyh0cmVlLmdldCh2KS5nZXQoMCksIHYpOwogICAgICAgIGlmICh0cmVlLmdldCh2KS5zaXplKCkgPT0gMikgZm9yIChJbnRlZ2VyIHRvIDogdHJlZS5nZXQodikpIGlmICh0byAhPSBwKSBraWxscyh0bywgdik7CiAgICAgICAgaWYgKHRyZWUuZ2V0KHYpLnNpemUoKSA+PSAzKSBraWxsZWQuc2V0KHYsIGtpbGxlZC5nZXQodikgKyAxKTsKICAgICAgICByZXR1cm47CiAgICB9CgogICAgcHVibGljIHN0YXRpYyB2b2lkIGdvdXAoaW50IHYsIGludCBwKQogICAgewogICAgICAgIGlmICh2ID09IHApCiAgICAgICAgewogICAgICAgICAgICBmb3IgKEludGVnZXIgdG8gOiB0cmVlLmdldCh2KSkgaWYgKGRpc3QuZ2V0KHRvKSA8IGRpc3QuZ2V0KHYpKSBnb3VwKHRvLCB2KTsKICAgICAgICAgICAga2lsbGVkLnNldCh2LCAwKTsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBpZiAodHJlZS5nZXQodikuc2l6ZSgpID09IDIpIGZvciAoSW50ZWdlciB0byA6IHRyZWUuZ2V0KHYpKSBpZiAodG8gIT0gcCkgZ291cCh0bywgdik7CiAgICAgICAgaWYgKHRyZWUuZ2V0KHYpLnNpemUoKSA+PSAzKQogICAgICAgIHsKICAgICAgICAgICAga2lsbGVkLnNldCh2LCBraWxsZWQuZ2V0KHYpICsgMSk7CiAgICAgICAgICAgIHJldHVybjsKICAgICAgICB9CiAgICB9CgogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIFNjYW5uZXIgaW4gPSBuZXcgU2Nhbm5lcihTeXN0ZW0uaW4pOwogICAgICAgIGZvciAoaW50IGk9MDtpPE5NQVg7IGkrKykKICAgICAgICB7CiAgICAgICAgICAgIHRyZWUuYWRkKG5ldyBBcnJheUxpc3Q8SW50ZWdlcj4oKSk7CiAgICAgICAgICAgIGRpc3QuYWRk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KTsgZGlzdC5zZXQoMSwgMCk7CiAgICAgICAgcS5vZmZlcigxKTsgdXNlZC5zZXQoMSwgdHJ1ZSk7CiAgICAgICAgd2hpbGUgKCFxLmlzRW1wdHkoKSkKICAgICAgICB7CiAgICAgICAgICAgIGludCBxdiA9IHEucG9sbCgpOwogICAgICAgICAgICB1c2VkLnNldChxdiwgdHJ1ZSk7CiAgICAgICAgICAgIGZvciAoSW50ZWdlciB0byA6IHRyZWUuZ2V0KHF2KSkgICAgICAgICAgICAgICAgLy9CRlMg0LfQsNC/0L7Qu9C90Y/RjtGJ0LjQuSDQstC10LrRgtC+0YAgZGlzdAogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBpZiAoIXVzZWQuZ2V0KHRvKSkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBxLm9mZmVyKHRvKTsKICAgICAgICAgICAgICAgICAgICBkaXN0LnNldCh0bywgZGlzdC5nZXQocXYpKzEpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICBmb3IgKEludGVnZXIgbCA6IGxlYXZlcykKICAgICAgICB7CiAgICAgICAgICAgIGtpbGxzKGwsIGwpOyAgICAgICAgLy8g0J/QtdGA0LLRi9C5INGN0YLQsNC/IC0g0LfQsNC/0YDQvtGB0Ysg0L7RgiDQutCw0LbQtNC+0LPQviDQu9C40YHRgtCwCiAgICAgICAgfQoKICAgICAgICBpbnQgbWF4ZGlzdCA9IC0xOyBmb3IgKGludCBpID0gMTsgaSA8PSBuOyBpKyspIG1heGRpc3QgPSBNYXRoLm1heChtYXhkaXN0LCBkaXN0LmdldChpKSk7IC8vINCe0L/RgNC10LTQtdC70LXQvdC40LUg0LzQsNC60YHQuNC80LDQu9GM0L3QvtCz0L4gItGD0YDQvtCy0L3RjyIg0LTQtdGA0LXQstCwCiAgICAgICAgaW50IHdlbnR1cCA9IDE7CiAgICAgICAgd2hpbGUgKHdlbnR1cCAhPSAwKQogICAgICAgIHsKICAgICAgICAgICAgd2VudHVwID0gMDsKICAgICAgICAgICAgZm9yIChpbnQgbCA9IG1heGRpc3Q7IGwgPiAwOyBsLS0pCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGZvciAoaW50IGkgPSAyOyBpIDw9IG47IGkrKykKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBpZiAoa2lsbGVkLmdldChpKSA9PSAxICYmIGRpc3QuZ2V0KGkpID09IGwpCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBnb3VwKGksIGkpOyAgICAgICAgICAgICAgICAgICAgLy/QrdGC0LDQvyAyIC0g0L/QvtC00L3Rj9GC0LjQtSAi0L3QtdC00L7RiNC10LTRiNC40YUiINC30LDQv9GA0L7RgdC+0LIKICAgICAgICAgICAgICAgICAgICAgICAgd2VudHVwKys7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG47IGkrKykgaWYgKGtpbGxlZC5nZXQoaSkgPj0gMikgYW5zKys7IC8v0KTQuNC90LDQu9GM0L3Ri9C5INC/0L7QtNGB0YfQtdGCICLQvtGC0YDQsNCy0LvQtdC90L3Ri9GFIiDRgNCw0LfQstC40LvQvtC6CiAgICAgICAgU3lzdGVtLm91dC5wcmludChhbnMpOwogICAgfQp9