結果

問題 No.812 Change of Class
ユーザー Tomohiko HasegawaTomohiko Hasegawa
提出日時 2019-04-13 17:22:47
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 2,674 ms / 4,000 ms
コード長 6,207 bytes
コンパイル時間 4,596 ms
コンパイル使用メモリ 115,000 KB
実行使用メモリ 68,076 KB
最終ジャッジ日時 2024-06-12 20:40:05
合計ジャッジ時間 50,760 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 360 ms
59,020 KB
testcase_01 AC 76 ms
32,688 KB
testcase_02 AC 209 ms
47,272 KB
testcase_03 AC 420 ms
64,568 KB
testcase_04 AC 373 ms
61,672 KB
testcase_05 AC 2,674 ms
66,184 KB
testcase_06 AC 2,109 ms
63,584 KB
testcase_07 AC 44 ms
34,128 KB
testcase_08 AC 35 ms
27,336 KB
testcase_09 AC 2,333 ms
68,076 KB
testcase_10 AC 2,441 ms
63,720 KB
testcase_11 AC 118 ms
53,904 KB
testcase_12 AC 1,443 ms
63,472 KB
testcase_13 AC 28 ms
24,088 KB
testcase_14 AC 28 ms
26,132 KB
testcase_15 AC 1,660 ms
60,020 KB
testcase_16 AC 87 ms
27,480 KB
testcase_17 AC 1,488 ms
61,976 KB
testcase_18 AC 1,101 ms
50,792 KB
testcase_19 AC 1,256 ms
55,096 KB
testcase_20 AC 2,559 ms
66,992 KB
testcase_21 AC 1,036 ms
50,596 KB
testcase_22 AC 413 ms
40,872 KB
testcase_23 AC 73 ms
27,252 KB
testcase_24 AC 108 ms
26,756 KB
testcase_25 AC 328 ms
40,944 KB
testcase_26 AC 2,070 ms
63,816 KB
testcase_27 AC 1,812 ms
59,592 KB
testcase_28 AC 1,201 ms
57,596 KB
testcase_29 AC 241 ms
37,316 KB
testcase_30 AC 1,601 ms
58,672 KB
testcase_31 AC 1,379 ms
54,684 KB
testcase_32 AC 536 ms
45,388 KB
testcase_33 AC 1,651 ms
63,268 KB
testcase_34 AC 97 ms
29,836 KB
testcase_35 AC 245 ms
35,180 KB
testcase_36 AC 112 ms
27,388 KB
testcase_37 AC 773 ms
42,588 KB
testcase_38 AC 53 ms
38,000 KB
testcase_39 AC 869 ms
44,900 KB
testcase_40 AC 149 ms
34,876 KB
testcase_41 AC 48 ms
36,484 KB
testcase_42 AC 1,883 ms
56,816 KB
testcase_43 AC 189 ms
48,048 KB
testcase_44 AC 1,234 ms
47,436 KB
testcase_45 AC 115 ms
27,520 KB
testcase_46 AC 165 ms
47,264 KB
testcase_47 AC 1,212 ms
47,552 KB
testcase_48 AC 1,192 ms
48,528 KB
testcase_49 AC 330 ms
36,820 KB
testcase_50 AC 37 ms
22,068 KB
testcase_51 AC 254 ms
38,312 KB
testcase_52 AC 531 ms
46,488 KB
testcase_53 AC 191 ms
32,692 KB
testcase_54 AC 173 ms
34,472 KB
testcase_55 AC 408 ms
62,272 KB
testcase_56 AC 527 ms
62,080 KB
testcase_57 AC 550 ms
67,704 KB
testcase_58 AC 280 ms
53,828 KB
testcase_59 AC 617 ms
66,452 KB
testcase_60 AC 29 ms
26,140 KB
testcase_61 AC 28 ms
24,012 KB
testcase_62 AC 29 ms
26,052 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Console;
using static System.Math;

namespace AtTest.yukicoder
{
    class _812
    {
        static void Main(string[] args)
        {
            Method(args);
        }

        static void Method(string[] args)
        {
            int[] nm = ReadInts();
            int n = nm[0];
            int m = nm[1];
            List<Edge>[] graph = new List<Edge>[n];
            for (int i = 0; i < n; i++) graph[i] = new List<Edge>();
            for(int i = 0; i < m; i++)
            {
                int[] pq = ReadInts();
                int a = pq[0] - 1;
                int b = pq[1] - 1;
                graph[a].Add(new Edge(b));
                graph[b].Add(new Edge(a));
            }
            int q = ReadInt();
            long[][] vals = new long[q][];
            for(int i = 0; i < q; i++)
            {
                long dist = 0;
                long[] distances = Dijkstra(
                        ReadInt() - 1, graph, out dist);

                vals[i] = new long[2];
                long div = 1;
                while (dist > div)
                {
                    vals[i][1]++;
                    div *= 2;
                }
                for (int j = 0; j < n; j++)
                {
                    if (distances[j] < long.MaxValue) vals[i][0]++;
                }
                vals[i][0]--;
            }
            for (int i = 0; i < q; i++)
            {
                WriteLine(vals[i][0] + " " + vals[i][1]);
            }
        }

        static long[] Dijkstra(int startIndex,
            List<Edge>[] graph, out long maxDistance)
        {
            List<Edge> startNode = graph[startIndex];
            var pQueue = new PriorityQueue<int>();
            var visitFlags = new bool[graph.Length];
            var distances = new long[graph.Length];
            maxDistance = 0;

            //Initialize nodes
            for (int i = 0; i < distances.Length; i++)
            {
                distances[i] = long.MaxValue;
                visitFlags[i] = false;
            }
            pQueue.Enqueue(0, startIndex);
            distances[startIndex] = 0;

            while (pQueue.Exist())
            {
                KeyValuePair<long, int> pair = pQueue.Dequeue();
                long distance = pair.Key;
                int index = pair.Value;

                if (visitFlags[index]) continue;

                //Confirm distances
                List<Edge> nowNode = graph[index];
                visitFlags[index] = true;
                maxDistance = Math.Max(maxDistance, distance);

                //Update priority queue
                for (int i = 0; i < nowNode.Count; i++)
                {
                    int nextIndex = nowNode[i].toIndex;
                    if (visitFlags[nextIndex]) continue;

                    long nextDistance = distance + nowNode[i].distance;
                    if (nextDistance < distances[nextIndex])
                    {
                        distances[nextIndex] = nextDistance;
                        pQueue.Enqueue(nextDistance, nextIndex);
                    }
                }
            }
            return distances;
        }

        class Edge
        {
            public int toIndex;
            public long distance;

            public Edge(int toIndex, long distance = 1)
            {
                this.toIndex = toIndex;
                this.distance = distance;
            }
        }

        class PriorityQueue<T>
        {
            private readonly List<KeyValuePair<long, T>> list;
            private int count;

            public PriorityQueue()
            {
                list = new List<KeyValuePair<long, T>>();
                count = 0;
            }

            private void Add(KeyValuePair<long, T> pair)
            {
                if (count == list.Count)
                {
                    list.Add(pair);
                }
                else
                {
                    list[count] = pair;
                }
                count++;
            }

            private void Swap(int a, int b)
            {
                KeyValuePair<long, T> tmp = list[a];
                list[a] = list[b];
                list[b] = tmp;
            }

            public void Enqueue(long key, T value)
            {
                Add(new KeyValuePair<long, T>(key, value));
                int c = count - 1;
                while (c > 0)
                {
                    int p = (c - 1) / 2;
                    if (list[c].Key >= list[p].Key) break;

                    Swap(p, c);
                    c = p;
                }
            }

            public KeyValuePair<long, T> Dequeue()
            {
                KeyValuePair<long, T> pair = list[0];
                count--;
                if (count == 0) return pair;

                list[0] = list[count];
                int p = 0;
                while (true)
                {
                    int c1 = p * 2 + 1;
                    int c2 = p * 2 + 2;
                    if (c1 >= count) break;

                    int c = (c2 >= count || list[c1].Key < list[c2].Key)
                        ? c1 : c2;
                    if (list[c].Key >= list[p].Key) break;

                    Swap(p, c);
                    p = c;
                }
                return pair;
            }

            public bool Exist() { return count > 0; }
        }

        private static string Read() { return ReadLine(); }
        private static char[] ReadChars() { return Array.ConvertAll(Read().Split(), a => a[0]); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }
    }
}
0