コード例 #1
0
        public static void LearningLanguages()
        {
            var nm        = Console.ReadLine().Split().Select(Int32.Parse).ToArray();
            var n         = nm[0];
            var k         = nm[1];
            var dj        = new DisjointSet(n + 1);
            var languages = new Dictionary <int, List <int> >();
            var noLang    = 0;

            for (int i = 0; i < n; i++)
            {
                var employee = Console.ReadLine().Split().Select(Int32.Parse).ToArray();
                if (employee[0] == 0)
                {
                    noLang++;
                }

                for (int j = 1; j < employee.Length; j++)
                {
                    if (languages.ContainsKey(employee[j]))
                    {
                        languages[employee[j]].Add(i);
                    }
                    else
                    {
                        languages.Add(employee[j], new List <int> {
                            i
                        });
                    }
                }
            }

            var amount = n;

            for (int i = 1; i <= k; i++)
            {
                if (!languages.ContainsKey(i))
                {
                    continue;
                }
                for (int j = 0; j < languages[i].Count && j + 1 < languages[i].Count; j++)
                {
                    if (dj.Union(languages[i][j], languages[i][j + 1]))
                    {
                        amount--;
                    }
                }
            }

            if (noLang == n)
            {
                amount++;
            }
            Console.WriteLine(amount - 1);
        }
コード例 #2
0
        public static void NonYekaterinburgSubway()
        {
            var nkm = Console.ReadLine().Split(' ').Select(Int32.Parse).ToArray();
            var n   = nkm[0];
            var k   = nkm[1];
            var m   = nkm[2];

            var dj = new DisjointSet(n);

            for (int i = 0; i < k; i++)
            {
                var xy = Console.ReadLine().Split().Select(Int32.Parse).ToArray();
                dj.Union(xy[0], xy[1]);
            }

            for (int i = 0; i < m; i++)
            {
                var xy = Console.ReadLine().Split().Select(Int32.Parse).ToArray();
            }

            Console.WriteLine(dj.SetCount - 1);
        }
コード例 #3
0
        static void BearAndFriendshipCondition()
        {
            var nm    = Console.ReadLine().Split().Select(Int32.Parse).ToArray();
            var n     = nm[0];
            var m     = nm[1];
            var dj    = new DisjointSet(n);
            var graph = new Dictionary <int, List <int> >();

            for (int i = 0; i < m; i++)
            {
                var input = Console.ReadLine().Split().Select(Int32.Parse).ToArray();
                dj.Union(input[0], input[1]);
                try
                {
                    graph[input[0]].Add(input[1]);
                }
                catch (Exception e)
                {
                    graph.Add(input[0], new List <int> {
                        input[1]
                    });
                }

                try
                {
                    graph[input[1]].Add(input[0]);
                }
                catch (Exception e)
                {
                    graph.Add(input[1], new List <int> {
                        input[0]
                    });
                }
                //if (graph.ContainsKey(input[0]))
                //    graph[input[0]].Add(input[1]);
                //else graph.Add(input[0], new List<int> { input[1] });
                //if (graph.ContainsKey(input[1]))
                //    graph[input[1]].Add(input[0]);
                //else graph.Add(input[1], new List<int> { input[0] });
            }

            var parents = new Dictionary <int, List <int> >();

            for (int i = 1; i <= n; i++)
            {
                var p = dj.FindRepresentative(i);
                try
                {
                    parents[p].Add(i);
                }
                catch (Exception e)
                {
                    parents.Add(p, new List <int> {
                        i
                    });
                }
                //if (parents.ContainsKey(p))
                //    parents[p].Add(i);
                //else parents.Add(p, new List<int> { i });
            }

            foreach (var g in graph)
            {
                if (!parents.ContainsKey(g.Key) || parents[g.Key].Count <= 0)
                {
                    continue;
                }
                var degree = parents[g.Key].Where(p => graph.ContainsKey(p)).Sum(p => graph[p].Count);
                var k      = parents[g.Key].Count;
                k = k * (k - 1) / 2;
                if (k == degree / 2)
                {
                    continue;
                }
                Console.WriteLine("NO");
                return;
            }

            Console.WriteLine("YES");
        }