为什么我得到这个输出?

时间:2016-08-07 02:51:46

标签: c# algorithm tree

我正在研究一个基本上是"找到可以从树中删除的最大边数以获得偶数量的子树的问题。"

我的解决方案就像

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

class Solution
{
    static int DescendantCount(List<int>[] relNodes, int i)
    {
        int count = 0;
        List<int> descendants = relNodes[i];
        foreach(int j in descendants)
        {
            count += (1 + DescendantCount(relNodes, j));   
        }
        return count;
    }

    static void Main(String[] args)
    {
        int[] line = Array.ConvertAll(Console.ReadLine().Split(' '), Int32.Parse);
        int m = line[0], n = line[1];
        List<int>[] relationships = Enumerable.Repeat(new List<int>(), m).ToArray();
        for(int i = 0; i < n; ++i)
        {
            int[] rel = Array.ConvertAll(Console.ReadLine().Split(' '), Int32.Parse);
            int ancestor = rel[1], descendant = rel[0];
            relationships[ancestor - 1].Add(descendant - 1);
        }
        /*
        List<int> rootDescendants = relationships[0];
        int numOddDesendants = rootDescendants.Count(i => (DescendantCount(relationships, i) & 1) == 1);      
        Console.WriteLine(numOddDesendants);*/
        for(int i = 0; i < relationships.Length; ++i)
        {
            Console.WriteLine("i = {0}, descendants = {1}", i.ToString(), string.Join(",",relationships[i].ToArray()));
        }
    }
}

而我无法弄清楚的是我获得输出的原因

i = 0, descendants = 1,2,3,4,5,6,7,8,9
i = 1, descendants = 1,2,3,4,5,6,7,8,9
i = 2, descendants = 1,2,3,4,5,6,7,8,9
i = 3, descendants = 1,2,3,4,5,6,7,8,9
i = 4, descendants = 1,2,3,4,5,6,7,8,9
i = 5, descendants = 1,2,3,4,5,6,7,8,9
i = 6, descendants = 1,2,3,4,5,6,7,8,9
i = 7, descendants = 1,2,3,4,5,6,7,8,9
i = 8, descendants = 1,2,3,4,5,6,7,8,9
i = 9, descendants = 1,2,3,4,5,6,7,8,9

输入

10 9
2 1
3 1
4 3
5 2
6 1
7 2
8 6
9 8
10 8

表示"2 is a descendant of 1, 3 is a descendant of 1, 4 is a descendant of 3, ..."

我如何得到所有这些"descendants = 1,2,3,4,5,6,7,8,9" ????

0 个答案:

没有答案