Esempio n. 1
0
        public override IEnumerable <object> Solve(TextReader inputStream)
        {
            Modular.InitializeCombinationTable();
            var nodesCount = inputStream.ReadInt();

            graph = new BasicGraph(nodesCount);

            for (int i = 0; i < nodesCount - 1; i++)
            {
                var(a, b) = inputStream.ReadValue <int, int>();
                a--;
                b--;
                graph.AddEdge(new BasicEdge(a, b));
                graph.AddEdge(new BasicEdge(b, a));
            }

            dpStates = new DPState[nodesCount];

            for (int i = 0; i < dpStates.Length; i++)
            {
                dpStates[i] = new DPState(new Modular(1), 0);
            }

            var rerooting = new Rerooting <BasicNode, BasicEdge, DPState>(graph);
            var results   = rerooting.Solve().Select(r => r.Count.Value);

            foreach (var result in results)
            {
                yield return(result);
            }
        }
        public void RerootingTestABC160ExampleF()
        {
            var inputs = @"8
1 2
2 3
3 4
3 5
3 6
6 7
6 8".Split(Environment.NewLine);

            var n     = int.Parse(inputs[0]);
            var graph = new BasicGraph(n);

            foreach (var input in inputs.Skip(1).Select(s => s.Split(' ').Select(s => int.Parse(s) - 1).ToArray()))
            {
                graph.AddEdge(input[0], input[1]);
                graph.AddEdge(input[1], input[0]);
            }

            var rerooting = new Rerooting <BasicEdge, DPState>(graph);
            var result    = rerooting.Solve().Select(r => r.Count.Value);

            var expected = new int[] { 40, 280, 840, 120, 120, 504, 72, 72 };

            Assert.Equal(expected, result);
        }
Esempio n. 3
0
        public override IEnumerable <object> Solve(TextReader inputStream)
        {
            var nodesCount = inputStream.ReadInt();

            edges = Enumerable.Repeat(0, nodesCount).Select(_ => new List <int>()).ToArray();

            for (int i = 0; i < nodesCount - 1; i++)
            {
                var ab = inputStream.ReadIntArray().Select(j => j - 1).ToArray();
                edges[ab[0]].Add(ab[1]);
                edges[ab[1]].Add(ab[0]);
            }

            dpStates = new DPState[nodesCount];

            for (int i = 0; i < dpStates.Length; i++)
            {
                dpStates[i] = new DPState(new Modular(1), 0);
            }

            var rerooting = new Rerooting <DPState>(edges);
            var results   = rerooting.Solve().Select(r => r.Count.Value);

            foreach (var result in results)
            {
                yield return(result);
            }
        }
Esempio n. 4
0
        public override void Solve(IOManager io)
        {
            Modular.InitializeCombinationTable();
            var n    = io.ReadInt();
            var tree = new BasicGraph(n);

            for (int i = 0; i < n - 1; i++)
            {
                var a = io.ReadInt() - 1;
                var b = io.ReadInt() - 1;
                tree.AddEdge(a, b);
                tree.AddEdge(b, a);
            }

            var rerooting = new Rerooting <BasicEdge, CountAndWay>(tree);

            var results = rerooting.Solve();

            foreach (var r in results)
            {
                io.WriteLine(r.Way);
            }
        }
Esempio n. 5
0
        public override IEnumerable <object> Solve(TextReader inputStream)
        {
            Modular.InitializeCombinationTable();
            var nodeCount = inputStream.ReadInt();
            var graph     = new BasicGraph(nodeCount);

            for (int i = 0; i < nodeCount - 1; i++)
            {
                var(u, v) = inputStream.ReadValue <int, int>();
                u--;
                v--;
                graph.AddEdge(new BasicEdge(u, v));
                graph.AddEdge(new BasicEdge(v, u));
            }

            var rerooting = new Rerooting <BasicNode, BasicEdge, CountAndWay>(graph);
            var results   = rerooting.Solve();

            foreach (var result in results)
            {
                yield return(result.Way);
            }
        }
Esempio n. 6
0
        public override IEnumerable <object> Solve(TextReader inputStream)
        {
            var n          = inputStream.ReadInt();
            var brightness = inputStream.ReadIntArray();
            var graph      = Enumerable.Repeat(0, n).Select(_ => new List <int>()).ToArray();

            for (int i = 0; i < n - 1; i++)
            {
                var ab = inputStream.ReadIntArray();
                var a  = ab[0] - 1;
                var b  = ab[1] - 1;
                graph[a].Add(b);
                graph[b].Add(a);
            }

            var rerooting = new Rerooting <BeautifulnessAndBrightness>(graph, brightness.Select(b => new BeautifulnessAndBrightness(0, b)).ToArray());

            var result = rerooting.Solve();

            for (int i = 0; i < result.Length; i++)
            {
                yield return(result[i].Beautifulness);
            }
        }