Пример #1
0
        public double[] CalcEquation(IList <IList <string> > equations, double[] values, IList <IList <string> > queries)
        {
            var uf = new UnionFind();

            for (int i = 0; i < equations.Count; i++)
            {
                uf.Union(equations[i][0], equations[i][1], values[i]);
            }

            var result = new double[queries.Count];

            for (int i = 0; i < queries.Count; i++)
            {
                var x = queries[i][0];
                var y = queries[i][1];
                if (uf.Exist(x) && uf.Exist(y))
                {
                    var px = uf.Find(x);
                    var py = uf.Find(y);

                    if (px.ParentIndex == py.ParentIndex)
                    {
                        result[i] = px.Value / py.Value;
                        continue;
                    }
                }

                result[i] = -1;
            }

            return(result);
        }
Пример #2
0
        int GetMinCostToConstruct(int numTotalAvailableCities,
                                  int numTotalAvailableRoads,
                                  List <List <int> > roadsAvailable,
                                  int numNewRoadsConstruct,
                                  List <List <int> > costNewRoadsConstruct
                                  )
        {
            if (numTotalAvailableCities < 2 || numTotalAvailableRoads >= numTotalAvailableCities - 1)
            {
                return(0);
            }
            UnionFind uf = new UnionFind(numTotalAvailableCities);

            int avblRdCnt = 0;

            foreach (List <int> r in roadsAvailable)
            {
                int c1 = r[0];
                int c2 = r[1];

                if (!uf.Find(c1, c2))
                {
                    uf.Union(c1, c2);
                    avblRdCnt++;
                }
            }
            HashSet <Connetction> connection_hs = ListToHashsetConverter(costNewRoadsConstruct);

            ArrayList mst = new ArrayList();

            connection_hs.OrderBy(x => x.Cost);
            while (connection_hs.Count > 0 && mst.Count + avblRdCnt < numTotalAvailableCities - 1)
            {
                Connetction temp = connection_hs.FirstOrDefault();
                int         c1   = temp.City1;
                int         c2   = temp.City2;
                if (!uf.Find(c1, c2))
                {
                    uf.Union(c1, c2);
                    mst.Add(temp);
                }
                connection_hs.Remove(temp);
            }
            if (mst.Count + avblRdCnt < numTotalAvailableCities - 1)
            {
                return(-1);
            }
            int sum = 0;

            foreach (Connetction c in mst)
            {
                sum += c.Cost;
            }
            return(sum);
        }
Пример #3
0
        public int LargestComponentSize(int[] A)
        {
            int maxValue = A.Max();
            var uf       = new UnionFind(maxValue + 1);

            var numFactorMap = new Dictionary <int, int>();

            foreach (int num in A)
            {
                var primeFactors = PrimeDecompose(num);

                numFactorMap[num] = primeFactors[0];
                for (int i = 0; i < primeFactors.Count - 1; ++i)
                {
                    uf.Union(primeFactors[i], primeFactors[i + 1]);
                }
            }

            // count the size of group one by one
            int maxGroupSize = 0;
            var groupCount   = new Dictionary <int, int>();

            foreach (int num in A)
            {
                var groupId = uf.Find(numFactorMap[num]);
                groupCount.TryGetValue(groupId, out int count);
                groupCount[groupId] = count + 1;

                maxGroupSize = Math.Max(maxGroupSize, count + 1);
            }

            return(maxGroupSize);
        }
Пример #4
0
        public IList <IList <string> > AccountsMerge(IList <IList <string> > accounts)
        {
            var uf          = new UnionFind();
            var emailToName = new Dictionary <string, string>();
            var emailToId   = new Dictionary <string, int>();

            var id = 0;

            foreach (var account in accounts)
            {
                var name = string.Empty;
                foreach (var str in account)
                {
                    if (name == string.Empty)
                    {
                        name = str;
                        continue;
                    }

                    emailToName[str] = name;
                    if (!emailToId.ContainsKey(str))
                    {
                        emailToId[str] = id++;
                    }
                    uf.Union(emailToId[account[1]], emailToId[str]);
                }
            }

            var map = new Dictionary <int, IList <string> >();

            foreach (var email in emailToName.Keys)
            {
                var index = uf.Find(emailToId[email]);
                if (!map.ContainsKey(index))
                {
                    map[index] = new List <string>();
                }
                map[index].Add(email);
            }

            return(map.Values.Select(list =>
            {
                IList <string> temp = list.OrderBy(e => e, StringComparer.Ordinal).ToList();
                temp.Insert(0, emailToName[list[0]]);
                return temp;
            }).ToList());
        }
Пример #5
0
        public string SmallestEquivalentString(string A, string B, string S)
        {
            var uf = new UnionFind(26);

            for (int i = 0; i < A.Length; i++)
            {
                uf.Union(A[i] - 'a', B[i] - 'a');
            }

            var sb = new StringBuilder(S.Length);

            foreach (var ch in S)
            {
                sb.Append((char)(uf.Find(ch - 'a') + 'a'));
            }

            return(sb.ToString());
        }