示例#1
0
    private bool CanSet(int prev, int target, int groupIdx)
    {
        if (Group[target] != 0)
        {
            return(false);
        }

        Group[target] = groupIdx;
        if (!Road.ContainsKey(target))
        {
            return(true);
        }

        bool ans = true;

        foreach (int next in Road[target].Keys)
        {
            if (next == prev)
            {
                continue;
            }
            if (!CanSet(target, next, groupIdx))
            {
                ans = false;
            }
        }
        return(ans);
    }
示例#2
0
    public void Proc()
    {
        int[] inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray();
        this.Group = new int[inpt[0]];

        int roadCount = inpt[1];

        for (int i = 0; i < roadCount; i++)
        {
            inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a) - 1).ToArray();

            if (!Road.ContainsKey(inpt[0]))
            {
                this.Road.Add(inpt[0], new Dictionary <int, bool>());
            }
            Road[inpt[0]][inpt[1]] = true;

            if (!Road.ContainsKey(inpt[1]))
            {
                this.Road.Add(inpt[1], new Dictionary <int, bool>());
            }
            Road[inpt[1]][inpt[0]] = true;
        }

        int ans = 0;

        for (int i = 0; i < Group.Length; i++)
        {
            if (Group[i] != 0)
            {
                continue;
            }
            if (CanSet(-1, i, i + 1))
            {
                ans++;
            }
        }
        Console.WriteLine(ans);
    }
示例#3
0
    private bool IsValid()
    {
        int[] inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray();
        this.PersonCount = inpt[0];
        int hintCount = inpt[1];

        this.PersonPos = new Nullable <long> [this.PersonCount];

        for (int i = 0; i < hintCount; i++)
        {
            inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray();
            int[] person = inpt.Take(2).Select(a => a - 1).ToArray();
            person.ToList().ForEach(a => {
                if (!Road.ContainsKey(a))
                {
                    Road.Add(a, new Dictionary <int, long>());
                }
            });
            if (Road[person[0]].ContainsKey(person[1]))
            {
                if (Road[person[0]][person[1]] != inpt[2])
                {
                    return(false);
                }
            }
            else
            {
                Road[person[0]][person[1]] = inpt[2];
            }
            if (Road[person[1]].ContainsKey(person[0]))
            {
                if (Road[person[1]][person[0]] != inpt[2] * -1)
                {
                    return(false);
                }
            }
            else
            {
                Road[person[1]][person[0]] = inpt[2] * -1;
            }
        }
        Stack <int> q = new Stack <int>();

        for (int i = PersonCount - 1; i >= 0; i--)
        {
            if (Road.ContainsKey(i))
            {
                q.Push(i);
            }
        }
        if (q.Count == 0)
        {
            return(true);
        }
        PersonPos[q.Peek()] = 0;
        while (q.Count > 0)
        {
            int current = q.Pop();
            if (PersonPos[current] == null)
            {
                PersonPos[current] = 0;
            }
            foreach (int next in Road[current].Keys)
            {
                if (PersonPos[next] == null)
                {
                    PersonPos[next] = PersonPos[current].Value + this.Road[current][next];
                    q.Push(next);
                    continue;
                }
                if (PersonPos[next].Value - PersonPos[current].Value != this.Road[current][next])
                {
                    return(false);
                }
            }
        }
        return(true);
    }
示例#4
0
    public void Proc()
    {
        int[] inpt      = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray();
        int   matiCount = inpt[0];
        int   roadCount = inpt[1];
        int   railCount = inpt[2];

        this.RoadMatiList = new TreeNode[matiCount];
        this.RailMatiList = new TreeNode[matiCount];

        for (int i = 0; i < roadCount; i++)
        {
            inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a) - 1).ToArray();
            if (!Road.ContainsKey(inpt[0]))
            {
                Road.Add(inpt[0], new Dictionary <int, bool>());
            }
            if (!this.Road.ContainsKey(inpt[1]))
            {
                this.Road.Add(inpt[1], new Dictionary <int, bool>());
            }
            this.Road[inpt[0]][inpt[1]] = true;
            this.Road[inpt[1]][inpt[0]] = true;
        }
        for (int i = 0; i < railCount; i++)
        {
            inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a) - 1).ToArray();
            if (!this.Rail.ContainsKey(inpt[0]))
            {
                this.Rail.Add(inpt[0], new Dictionary <int, bool>());
            }
            if (!this.Rail.ContainsKey(inpt[1]))
            {
                this.Rail.Add(inpt[1], new Dictionary <int, bool>());
            }
            this.Rail[inpt[0]][inpt[1]] = true;
            this.Rail[inpt[1]][inpt[0]] = true;
        }
        for (int i = 0; i < matiCount; i++)
        {
            if (this.RoadMatiList[i] != null)
            {
                continue;
            }
            this.RoadMatiList[i] = new TreeNode(i);
            Queue <TreeNode> que = new Queue <TreeNode>();
            que.Enqueue(this.RoadMatiList[i]);
            while (que.Count > 0)
            {
                TreeNode curr = que.Dequeue();
                if (!this.Road.ContainsKey(curr.ID))
                {
                    continue;
                }
                foreach (int next in this.Road[curr.ID].Keys)
                {
                    if (curr.Parent != null && curr.Parent.ID == next)
                    {
                        continue;
                    }
                    if (RoadMatiList[next] != null)
                    {
                        continue;
                    }
                    RoadMatiList[next] = curr.AddChild(next);
                    que.Enqueue(RoadMatiList[next]);
                }
            }
        }
        for (int i = 0; i < matiCount; i++)
        {
            if (this.RailMatiList[i] != null)
            {
                continue;
            }
            this.RailMatiList[i] = new TreeNode(i);
            Queue <TreeNode> que = new Queue <TreeNode>();
            que.Enqueue(this.RailMatiList[i]);
            while (que.Count > 0)
            {
                TreeNode curr = que.Dequeue();
                if (!this.Rail.ContainsKey(curr.ID))
                {
                    continue;
                }
                foreach (int next in this.Rail[curr.ID].Keys)
                {
                    if (RailMatiList[next] != null)
                    {
                        continue;
                    }
                    RailMatiList[next] = curr.AddChild(next);
                    que.Enqueue(RailMatiList[next]);
                }
            }
        }

        Dictionary <int, Dictionary <int, int> > groupCount = new Dictionary <int, Dictionary <int, int> >();

        for (int i = 0; i < matiCount; i++)
        {
            int roadGroup = this.RoadMatiList[i].Root.ID;
            int railGroup = this.RailMatiList[i].Root.ID;
            if (!groupCount.ContainsKey(roadGroup))
            {
                groupCount.Add(roadGroup, new Dictionary <int, int>());
            }
            if (groupCount[roadGroup].ContainsKey(railGroup))
            {
                groupCount[roadGroup][railGroup]++;
            }
            else
            {
                groupCount[roadGroup][railGroup] = 1;
            }
        }

        int[] cnt = new int[matiCount];
        for (int i = 0; i < matiCount; i++)
        {
            int roadGroup = this.RoadMatiList[i].Root.ID;
            int railGroup = this.RailMatiList[i].Root.ID;
            cnt[i] = groupCount[roadGroup][railGroup];
        }
        string ans = string.Join(" ", cnt);

        Console.WriteLine(ans);
    }
示例#5
0
    public void Proc()
    {
        long[] inpt = Reader.ReadLine().Split(' ').Select(a => long.Parse(a)).ToArray();

        int  matiCount = (int)inpt[0];
        int  roadCount = (int)inpt[1];
        long timeLimit = inpt[2];

        long[] okane = Reader.ReadLine().Split(' ').Select(a => long.Parse(a)).ToArray();

        for (int i = 0; i < roadCount; i++)
        {
            inpt = Reader.ReadLine().Split(' ').Select(a => long.Parse(a)).ToArray();
            int  matiFrom = (int)inpt[0] - 1;
            int  matiTo   = (int)inpt[1] - 1;
            long cost     = inpt[2];
            if (!Road.ContainsKey(matiFrom))
            {
                this.Road.Add(matiFrom, new Dictionary <int, long>());
            }
            this.Road[matiFrom].Add(matiTo, cost);
            if (!RoadRev.ContainsKey(matiTo))
            {
                this.RoadRev.Add(matiTo, new Dictionary <int, long>());
            }
            this.RoadRev[matiTo].Add(matiFrom, cost);
        }

        long[]      FromStart = new long[matiCount];
        long[]      ToStart   = new long[matiCount];
        Queue <int> task      = new Queue <int>();

        task.Enqueue(0);
        while (task.Count > 0)
        {
            int  cur  = task.Dequeue();
            long step = FromStart[cur];
            if (!this.Road.ContainsKey(cur))
            {
                continue;
            }
            foreach (int next in Road[cur].Keys)
            {
                if ((next != 0 && FromStart[next] == 0) || FromStart[next] > step + Road[cur][next])
                {
                    FromStart[next] = step + Road[cur][next];
                    task.Enqueue(next);
                }
            }
        }
        task.Enqueue(0);
        while (task.Count() > 0)
        {
            int  cur  = task.Dequeue();
            long step = ToStart[cur];
            if (!this.RoadRev.ContainsKey(cur))
            {
                continue;
            }
            foreach (int next in RoadRev[cur].Keys)
            {
                if ((next != 0 && ToStart[next] == 0) || ToStart[next] > step + RoadRev[cur][next])
                {
                    ToStart[next] = step + RoadRev[cur][next];
                    task.Enqueue(next);
                }
            }
        }

        long ans = okane[0] * timeLimit;

        for (int i = 1; i < matiCount; i++)
        {
            if (FromStart[i] == 0 || ToStart[i] == 0)
            {
                continue;
            }
            long tm = FromStart[i] + ToStart[i];
            ans = Math.Max(ans, okane[i] * (timeLimit - tm));
        }
        Console.WriteLine(ans);
    }