Esempio n. 1
0
    public void ComputeSubTreeSize(int root)
    {
        const long X     = 1000000000;
        var        stack = new FastStack <long>(N + 1);

        stack.Push(root * X);
        par[root] = -1;
        d[root]   = 0;

        while (stack.Any())
        {
            var val = stack.Peek();
            var u   = (int)(val / X);
            var it  = (int)(val % X);

            if (it == G[u].Count)
            {
                stack.Pop();
                subTreeSize[u]++;
                if (par[u] >= 0)
                {
                    subTreeSize[par[u]] += subTreeSize[u];
                }
            }
            else
            {
                var to = G[u][it].to;
                stack.Last++;
                if (to == par[u])
                {
                    continue;
                }

                par[to] = u;
                d[to]   = d[u] + 1;
                stack.Push(to * X);
            }
        }
    }