結果

問題 No.2888 Mamehinata
ユーザー tobisatis
提出日時 2024-09-13 22:07:42
言語 C#
(.NET 8.0.404)
結果
AC  
実行時間 319 ms / 2,000 ms
コード長 3,576 bytes
コンパイル時間 8,384 ms
コンパイル使用メモリ 167,636 KB
実行使用メモリ 189,780 KB
最終ジャッジ日時 2024-09-13 22:08:11
合計ジャッジ時間 20,470 ms
ジャッジサーバーID
(参考情報)
judge6 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 52
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (95 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

namespace AtCoder;

#nullable enable

using System.Numerics;

class Graph<T>
{
    public struct Edge
    {
        public int Ab { get; set; }
        public int Ad { get; set; }
        public T Distance { get; set; }
    }

    public int N { get; private init; }
    public bool Directed { get; private init; }
    public List<List<(int next, int edgeIndex)>> AdjacencyList { get; private init; }
    public List<Edge> Edges { get; private init; }

    public Graph(int verticals, IReadOnlyList<Edge> edges, bool directed)
    {
        N = verticals;
        Directed = directed;
        AdjacencyList = new List<List<(int, int)>>();
        for (var i = 0; i < N; i++) AdjacencyList.Add(new List<(int, int)>());
        Edges = new List<Edge>();
        for (var i = 0; i < edges.Count; i++)
        {
            var edge = edges[i];
            Edges.Add(edge);
            AdjacencyList[edge.Ab].Add((edge.Ad, i));
            if (!Directed) AdjacencyList[edge.Ad].Add((edge.Ab, i));
        }
    }
}

class GraphBuilder
{
    public static Graph<int> SimpleGraph(int verticals, (int ab, int ad)[] edges, bool directed)
    {
        var e = edges.Select(e => new Graph<int>.Edge { Ab = e.ab, Ad = e.ad, Distance = 1 }).ToList();
        return new(verticals, e, directed);
    }
}

static class Extensions
{
    public static T[] Repeat<T>(this int time, Func<T> F) => Enumerable.Range(0, time).Select(_ => F()).ToArray();
}

class AtCoder
{
    object? Solve()
    {
        var n = Int();
        var m = Int();
        var edges = m.Repeat(() => (Int() - 1, Int() - 1));
        var g = GraphBuilder.SimpleGraph(n, edges, false).AdjacencyList;
        if (g[0].Count == 0)
        {
            Out(new int[n]);
            return null;
        }
        var xz = n.Repeat(() => n + 1);
        var q = new Queue<int>();
        xz[0] = 0;
        q.Enqueue(0);
        while (q.Count > 0)
        {
            var v = q.Dequeue();
            var w = xz[v];
            foreach (var (next, _) in g[v])
            {
                if (xz[next] <= w + 1) continue;
                xz[next] = w + 1;
                q.Enqueue(next);
            }
        }
        var az = new int[n + 1];
        for (var i = 0; i < n; i++)
        {
            var j = xz[i];
            if (j <= n) az[j]++;
        }
        for (var i = 2; i <= n; i++) az[i] += az[i - 2];
        Out(az.Skip(1));
        return null;
    }

    public static void Main() => new AtCoder().Run();
    public void Run()
    {
        var res = Solve();
        if (res != null)
        {
            if (res is bool yes) res = yes ? "Yes" : "No";
            sw.WriteLine(res);
        }
        sw.Flush();
    }

    string[] input = Array.Empty<string>();
    int iter = 0;
    readonly StreamWriter sw = new(Console.OpenStandardOutput()) { AutoFlush = false };

    string String()
    {
        while (iter >= input.Length) (input, iter) = (Console.ReadLine()!.Trim().Split(' '), 0);
        return input[iter++];
    }
    T Input<T>() where T : IParsable<T> => T.Parse(String(), null);
    int Int() => Input<int>();
    void Out(object? x, string? separator = null)
    {
        separator ??= Environment.NewLine;
        if (x is System.Collections.IEnumerable obj and not string)
        {
            var firstLine = true;
            foreach (var item in obj)
            {
                if (!firstLine) sw.Write(separator);
                firstLine = false;
                sw.Write(item);
            }
        }
        else sw.Write(x);
        sw.WriteLine();
    }
}
0