Beispiel #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);
        }
Beispiel #2
0
        public static void ElectrificationPlan()
        {
            var input         = Console.ReadLine().Split();
            var n             = Int32.Parse(input[0]);
            var k             = Int32.Parse(input[1]);
            var dj            = new DisjointSet(n);
            var edges         = new List <Edge <int> >();
            var matrix        = new double[n, n];
            var builtStations = Console.ReadLine().Split(' ').Select(Int32.Parse).ToList();

            for (int i = 0; i < n; i++)
            {
                var inp = Console.ReadLine().Split();
                for (int j = 0; j < n; j++)
                {
                    matrix[i, j] = Int32.Parse(inp[j]);
                }
            }

            foreach (var item in builtStations)
            {
                for (int j = 1; j <= n; j++)
                {
                    if (item == j || builtStations.Contains(j))
                    {
                        continue;
                    }
                    edges.Add(new Edge <int>(item, j, matrix[item - 1, j - 1]));
                }
            }

            for (int i = 1; i <= n; i++)
            {
                if (builtStations.Contains(i))
                {
                    continue;
                }
                for (int j = 1; j <= n; j++)
                {
                    if (edges.Any(x => x.To == i && x.From == j) || i == j)
                    {
                        continue;
                    }
                    edges.Add(new Edge <int>(i, j, matrix[i - 1, j - 1]));
                }
            }
            var a = dj.KruskalMTS(edges, builtStations);

            double sum = a.Sum(t => t.Weight);

            Console.WriteLine(sum);
        }
Beispiel #3
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);
        }
Beispiel #4
0
        public static void Network()
        {
            var nm    = Console.ReadLine().Split(' ').Select(Int32.Parse).ToArray();
            var n     = nm[0];
            var m     = nm[1];
            var edges = new List <Edge <int> >();
            var dj    = new DisjointSet(n);

            for (int i = 1; i <= m; i++)
            {
                var input = Console.ReadLine().Split().Select(Int32.Parse).ToArray();
                edges.Add(new Edge <int>(input[0], input[1], input[2]));
            }

            var k = dj.KruskalMTS(edges);

            Console.WriteLine(k.Max(x => x.Weight));
            Console.WriteLine(k.Count);
            foreach (var k0 in k)
            {
                Console.WriteLine(k0.From + " " + k0.To);
            }
        }
Beispiel #5
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");
        }