예제 #1
0
    static void Main()
    {
        var n   = int.Parse(Console.ReadLine());
        var map = Array.ConvertAll(new int[n], _ => Read().Skip(1).ToList());
        var qc  = int.Parse(Console.ReadLine());
        var qs  = Array.ConvertAll(new int[qc], _ => Read());

        var lca = new Lca(n, 0, map);

        Console.WriteLine(string.Join("\n", qs.Select(q => lca.GetLca(q[0], q[1]))));
    }
예제 #2
0
    static void Main()
    {
        var h = Read();
        int n = h[0], qc = h[1];
        var p  = Read();
        var qs = Array.ConvertAll(new bool[qc], _ => Read());

        var d   = p.Select((v, i) => (p: v, c: i + 1)).ToLookup(x => x.p, x => x.c);
        var map = Enumerable.Range(0, n).Select(i => d[i].ToList()).ToArray();

        var lca = new Lca(n, 0, map);

        Console.WriteLine(string.Join("\n", qs.Select(q => lca.GetLca(q[0], q[1]))));
    }
예제 #3
0
        /// <summary>
        /// Returns the list of permissions for a given permission type
        /// </summary>
        /// <param name="defaultId">The id of the default list</param>
        /// <param name="selectedId">The id of the selected list</param>
        /// <param name="permissionType">The permission type</param>
        /// <returns></returns>
        ///
        public List <PermissionListModel> GetPermissionResultListModel(long defaultId, long selectedId, PermissionTypeEnum permissionType, long?userId)
        {
            IQueryable <PermissionListModel> results = null;

            // get the default permissions and mark any as active if the active list contains identical records
            var query = (from defaults in this.Permissions
                         where defaults.PermissionTypeId == permissionType && defaults.ObjectId == defaultId

                         join selected in this.Permissions on new { a = selectedId, b = permissionType, c = defaults.ReferenceId } equals
                         new { a = selected.ObjectId, b = selected.PermissionTypeId, c = selected.ReferenceId } into Lca
                         from selected in Lca.DefaultIfEmpty()

                         select new { ReferenceId = defaults.ReferenceId, isActive = selected != null })

                        .Distinct();

            switch (permissionType)
            {
            case PermissionTypeEnum.CityArea:
                results = from perm in query
                          join reference in this.CityAreas on perm.ReferenceId equals reference.CityAreaId
                          select new PermissionListModel
                {
                    ReferenceId = perm.ReferenceId,
                    Description = reference.Name,
                    IsSelected  = perm.isActive
                };
                break;

            case PermissionTypeEnum.Brand:
                results = from perm in query
                          join reference in this.Brands on perm.ReferenceId equals reference.BrandId
                          select new PermissionListModel
                {
                    ReferenceId = perm.ReferenceId,
                    Description = reference.Name,
                    IsSelected  = perm.isActive
                };
                break;

            case PermissionTypeEnum.ProductFamily:
                results = from perm in query
                          join reference in this.ProductFamilies on perm.ReferenceId equals reference.ProductFamilyId
                          select new PermissionListModel
                {
                    ReferenceId = perm.ReferenceId,
                    Description = reference.Name,
                    IsSelected  = perm.isActive
                };
                break;

            case PermissionTypeEnum.Tool:
                results = from perm in query
                          join reference in this.Tools on perm.ReferenceId equals reference.ToolId
                          select new PermissionListModel
                {
                    ReferenceId = perm.ReferenceId,
                    Description = reference.Name,
                    IsSelected  = perm.isActive
                };
                break;

            case PermissionTypeEnum.SystemAccess:
                results = from perm in query
                          join reference in this.SystemAccesses on perm.ReferenceId equals reference.SystemAccessId
                          select new PermissionListModel
                {
                    ReferenceId = perm.ReferenceId,
                    Description = reference.Name,
                    IsSelected  = perm.isActive
                };
                break;
            }

            IQueryable <PermissionListModel> newResults = null;

            if (userId == null)
            {
                userId = selectedId;
            }

            if (permissionType == PermissionTypeEnum.ProductFamily)
            {
                var newQuery = (from defaults in this.Context.Permissions
                                where defaults.PermissionTypeId == permissionType && defaults.ObjectId == userId
                                select new
                {
                    ReferenceId = defaults.ReferenceId,
                    isActive = defaults != null
                }).Distinct();

                newResults = from q in newQuery
                             join reference in this.ProductFamilies on
                             q.ReferenceId equals reference.ProductFamilyId
                             select new PermissionListModel
                {
                    ReferenceId = q.ReferenceId,
                    Description = reference.Name,
                    IsSelected  = q.isActive
                };
            }

            List <PermissionListModel> values = new List <PermissionListModel>();

            values = results.ToList().OrderBy(o => o.ReferenceId).ToList();

            if (newResults != null)
            {
                foreach (var item in newResults)
                {
                    if (results.Where(r => r.ReferenceId == item.ReferenceId).Count() < 1)
                    {
                        values.Add(item);
                    }
                }
            }

            //return results.ToList().OrderBy(o => o.ReferenceId).ToList();
            return(values);
        }