Exemplo n.º 1
0
    public static void Main()
    {
        var nk = Console.ReadLine().Split().Select(int.Parse).ToArray();
        var n  = nk[0];
        var k  = nk[1];
        ReRooting <Tuple <long, int[]> > hoge = new ReRooting <Tuple <long, int[]> >(
            n,
            Enumerable.Repeat(0, n - 1).Select(_ => Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToArray()).ToArray(),
            new Tuple <long, int[]>(0, new int[k]),
            (x, y) =>
        {
            return(new Tuple <long, int[]>(x.Item1 + y.Item1,
                                           (x.Item2.Zip(y.Item2, (X, Y) => X + Y).ToArray())
                                           ));
        },
            (x, y) =>
        {
            var res = new int[k];
            for (int i = 0; i < k - 1; i++)
            {
                res[i + 1] = x.Item2[i];
            }
            res[0] = x.Item2[k - 1] + 1;
            return(new Tuple <long, int[]>(x.Item1 + res[0], res));
        }
            );

        Console.WriteLine((Enumerable.Range(0, n).Sum(x => hoge.Query(x).Item1 - hoge.Query(x).Item2[0])) / 2);
    }
Exemplo n.º 2
0
    public static void Main()
    {
        var n     = int.Parse(Console.ReadLine());
        var edges = Enumerable.Repeat(0, n - 1).Select(_ => Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToArray()).ToArray();

        ReRooting <Data> res =
            new ReRooting <Data>(
                n,
                edges,
                new Data()
        {
            Combination = 1, Count = 0
        },
                (x, y) =>
        {
            var newCount = x.Count + y.Count;
            return(new Data()
            {
                Combination = x.Combination * y.Combination * Factorial(newCount) / Factorial(x.Count) / Factorial(y.Count),
                Count = newCount
            });
        },
                (x, _) => new Data()
        {
            Combination = x.Combination,
            Count       = x.Count + 1
        });

        for (int i = 0; i < n; i++)
        {
            Console.WriteLine(res.Query(i).Combination);
        }
    }
Exemplo n.º 3
0
    static void Main()
    {
        var n     = int.Parse(Console.ReadLine());
        var edges = Enumerable.Repeat(0, n - 1).Select(_ => Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToArray()).ToArray();
        int k     = int.Parse(Console.ReadLine());

        Node[] nodes = new Node[n];
        for (int i = 0; i < k; i++)
        {
            var vp = Console.ReadLine().Split().Select(int.Parse).ToArray();
            nodes[vp[0] - 1] = new Node(vp[1] % 2 == 0 ? Parity.Even : Parity.Odd, vp[1], vp[1]);
        }
        var IdentityElement        = new Node(Parity.Both, int.MinValue / 2, int.MaxValue / 2);
        ReRooting <Node> rerooting = new ReRooting <Node>(n, edges, IdentityElement, Node.Merge, Node.Next, (x, index) => Node.Merge(x, nodes[index] ?? IdentityElement));
        var res = new Node[n];

        for (int i = 0; i < n; i++)
        {
            var query = rerooting.Query(i);
            if (query.Parity == Parity.None || query.LowerBound > query.UpperBound || (query.LowerBound == query.UpperBound && ((query.Parity == Parity.Even) ^ query.LowerBound % 2 == 0)))
            {
                goto invalid;
            }
            res[i] = query;
        }
        Console.WriteLine("Yes");
        Console.WriteLine(string.Join("\n", res.Select(x => x.UpperBound)));
        return;

        invalid :;
        Console.WriteLine("No");
    }
Exemplo n.º 4
0
    public static void Main()
    {
        var n = int.Parse(Console.ReadLine());
        ReRooting <Data> rerooting = new ReRooting <Data>(
            n,
            Enumerable.Repeat(0, n - 1).Select(_ => Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToArray()).ToArray(),
            new Data()
        {
            Available = 0, Unavailable = 0
        },
            Data.Merge,
            (node, _) => node,
            (node, _) => Data.AddEdge(node)
            );
        int res = 0;

        for (int i = 0; i < n; i++)
        {
            var query = rerooting.Query(i);
            if (query.Available == query.Unavailable)
            {
                res++;
            }
        }
        Console.WriteLine(res);
    }
Exemplo n.º 5
0
    static void Main()
    {
        int n = int.Parse(Console.ReadLine());

        List <int>[] neighbours = Enumerable.Repeat(0, n).Select(_ => new List <int>()).ToArray();
        int[][]      edges      = Enumerable.Repeat(0, n - 1).Select(_ => Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToArray()).ToArray();
        ReRooting    treeDP     = new ReRooting(n, edges);

        var dicts   = treeDP.Neighbours.Select(x => x.Select((y, z) => new Tuple <int, int>(y, z)).ToDictionary(y => y.Item1, y => y.Item2)).ToArray();
        var sums    = Console.ReadLine().Split().Select(long.Parse).ToArray();
        int eqIndex = -1;

        long[] res = new long[edges.Length];

        BigInteger sum = new BigInteger(0);

        for (int i = 0; i < sums.Length; i++)
        {
            sum += sums[i];
        }

        for (int i = 0; i < edges.Length; i++)
        {
            var  edge = edges[i];
            long atob = treeDP.Query(edge[0], dicts[edge[0]][edge[1]]);
            long btoa = treeDP.Query(edge[1], dicts[edge[1]][edge[0]]);
            if (atob == btoa)
            {
                res[i]  = 0;
                eqIndex = i;
            }
            else
            {
                res[i] = (Abs(Abs(sums[edge[0]] - sums[edge[1]]) / (Abs(atob - btoa))));
                sum   -= (2 * res[i] * new BigInteger(atob) * btoa);
            }
        }
        if (eqIndex >= 0)
        {
            BigInteger zeroatob = treeDP.Query(edges[eqIndex][0], dicts[edges[eqIndex][0]][edges[eqIndex][1]]);
            BigInteger zerobtoa = treeDP.Query(edges[eqIndex][1], dicts[edges[eqIndex][1]][edges[eqIndex][0]]);
            res[eqIndex] = (long)(sum / (zeroatob * zerobtoa * 2));
        }
        Console.WriteLine(string.Join("\n", res));
    }
Exemplo n.º 6
0
    static void Main()
    {
        var             n           = int.Parse(Console.ReadLine());
        var             edges       = Enumerable.Repeat(0, n - 1).Select(_ => Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToArray()).ToArray();
        ReRooting <int> getDiameter = new ReRooting <int>(n, edges, 0, Max, x => x + 1);
        var             diameter    = Enumerable.Range(0, n).Select(x => getDiameter.Query(x)).Max() - 1;

        Console.WriteLine(diameter % 3 == 1 ? "Second" : "First");
    }
Exemplo n.º 7
0
    static void Main()
    {
        int n = int.Parse(Console.ReadLine());

        List <int>[] neighbours = Enumerable.Repeat(0, n).Select(_ => new List <int>()).ToArray();
        int[][]      edges      = Enumerable.Repeat(0, n - 1).Select(_ => Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToArray()).ToArray();
        ReRooting    treeDP     = new ReRooting(n, edges);

        Console.WriteLine(string.Join("\n", Enumerable.Range(0, n).Select(x => treeDP.Query(x))));
    }
Exemplo n.º 8
0
    static void Main()
    {
        var nm = Console.ReadLine().Split().Select(int.Parse).ToArray();
        var n  = nm[0];

        Mod = nm[1];
        List <int>[]     neighbours = Enumerable.Repeat(0, n).Select(_ => new List <int>()).ToArray();
        int[][]          edges      = Enumerable.Repeat(0, n - 1).Select(_ => Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToArray()).ToArray();
        ReRooting <long> treeDP     = new ReRooting <long>(n, edges, 1, (x, y) => (x * y) % nm[1], x => x + 1);

        Console.WriteLine(string.Join("\n", Enumerable.Range(0, n).Select(x => treeDP.Query(x) - 1)));
    }
Exemplo n.º 9
0
    public static void Main()
    {
        int n = int.Parse(Console.ReadLine());

        List <int>[] graph = Enumerable.Repeat(0, n).Select(_ => new List <int>()).ToArray();
        int[][]      edges = new int[n - 1][];
        for (int i = 0; i < n - 1; i++)
        {
            var st = Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToArray();
            edges[i] = st;
            graph[st[0]].Add(st[1]);
            graph[st[1]].Add(st[0]);
        }

        ReRooting <int> diamator = new ReRooting <int>(n, edges, 0, Max, (x, _) => x + 1);
        var             max      = Enumerable.Range(0, n).Max(x => diamator.Query(x));
        var             firstInd = Enumerable.Range(0, n).TakeWhile(x => diamator.Query(x) != max).Count();

        Stack <int> stack = new Stack <int>();

        stack.Push(firstInd);
        int[] distances = new int[n];
        while (stack.Count > 0)
        {
            var elem = stack.Pop();
            foreach (var item in graph[elem])
            {
                if (item == firstInd || distances[item] != 0)
                {
                    continue;
                }
                distances[item] = distances[elem] + 1;
                stack.Push(item);
            }
        }
        var secondInd = distances.TakeWhile(x => x != max - 1).Count();

        Console.WriteLine($"{firstInd + 1} {secondInd + 1}");
    }
Exemplo n.º 10
0
    public static void Main()
    {
        int n = int.Parse(Console.ReadLine());

        var edges = Enumerable.Repeat(0, n - 1).Select(_ => Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToArray()).ToArray();

        List <int>[] graph = Enumerable.Repeat(0, n).Select(_ => new List <int>()).ToArray();
        for (int i = 0; i < n - 1; i++)
        {
            var st = edges[0];
            graph[st[0]].Add(st[1]);
            graph[st[1]].Add(st[0]);
        }

        long[] sums = new long[n];
        Dictionary <int, long>[] dics = Enumerable.Repeat(0, n).Select(_ => new Dictionary <int, long>()).ToArray();
        int q = int.Parse(Console.ReadLine());

        for (int i = 0; i < q; i++)
        {
            var tex = Console.ReadLine().Split().Select(int.Parse).ToArray();
            var ind = tex[1] - 1;
            var(node, par) = tex[0] == 1 ? (edges[ind][0], edges[ind][1]) : (edges[ind][1], edges[ind][0]);
            var x = tex[2];
            if (!dics[node].ContainsKey(par))
            {
                dics[node][par] = 0;
            }
            sums[node]      += x;
            dics[node][par] += x;
        }

        ReRooting <long> rerooting = new ReRooting <long>(n, edges, 0, (x, y) => x + y, (val, node, par) => val + sums[node] - (dics[node].ContainsKey(par) ? dics[node][par] : 0));

        for (int i = 0; i < n; i++)
        {
            Console.WriteLine(rerooting.Query(i));
        }
    }