結果

問題 No.2677 Minmax Independent Set
ユーザー tobisatistobisatis
提出日時 2024-03-15 23:29:31
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 839 ms / 2,000 ms
コード長 6,683 bytes
コンパイル時間 9,072 ms
コンパイル使用メモリ 169,528 KB
実行使用メモリ 319,608 KB
最終ジャッジ日時 2024-09-30 03:05:01
合計ジャッジ時間 27,306 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
30,720 KB
testcase_01 AC 59 ms
30,976 KB
testcase_02 AC 59 ms
31,104 KB
testcase_03 AC 49 ms
30,208 KB
testcase_04 AC 76 ms
31,340 KB
testcase_05 AC 518 ms
107,476 KB
testcase_06 AC 527 ms
108,240 KB
testcase_07 AC 536 ms
109,040 KB
testcase_08 AC 496 ms
110,456 KB
testcase_09 AC 530 ms
115,072 KB
testcase_10 AC 438 ms
107,520 KB
testcase_11 AC 446 ms
108,020 KB
testcase_12 AC 839 ms
200,316 KB
testcase_13 AC 647 ms
132,736 KB
testcase_14 AC 515 ms
134,836 KB
testcase_15 AC 679 ms
152,784 KB
testcase_16 AC 502 ms
96,888 KB
testcase_17 AC 480 ms
96,876 KB
testcase_18 AC 292 ms
81,280 KB
testcase_19 AC 381 ms
93,440 KB
testcase_20 AC 156 ms
53,888 KB
testcase_21 AC 483 ms
93,940 KB
testcase_22 AC 88 ms
39,424 KB
testcase_23 AC 61 ms
31,488 KB
testcase_24 AC 59 ms
31,488 KB
testcase_25 AC 59 ms
31,104 KB
testcase_26 AC 59 ms
31,488 KB
testcase_27 AC 60 ms
31,232 KB
testcase_28 AC 414 ms
108,672 KB
testcase_29 AC 603 ms
154,676 KB
testcase_30 AC 59 ms
31,472 KB
testcase_31 AC 58 ms
30,848 KB
testcase_32 AC 57 ms
31,088 KB
testcase_33 AC 58 ms
31,360 KB
testcase_34 AC 58 ms
31,348 KB
testcase_35 AC 59 ms
31,104 KB
testcase_36 AC 59 ms
30,848 KB
testcase_37 AC 60 ms
31,616 KB
testcase_38 AC 61 ms
31,616 KB
testcase_39 AC 62 ms
32,384 KB
testcase_40 AC 64 ms
32,384 KB
testcase_41 AC 71 ms
34,288 KB
testcase_42 AC 83 ms
38,144 KB
testcase_43 AC 100 ms
44,660 KB
testcase_44 AC 151 ms
52,608 KB
testcase_45 AC 226 ms
70,784 KB
testcase_46 AC 508 ms
105,728 KB
testcase_47 AC 499 ms
99,584 KB
testcase_48 AC 489 ms
97,056 KB
testcase_49 AC 499 ms
104,064 KB
testcase_50 AC 164 ms
59,136 KB
testcase_51 AC 70 ms
34,176 KB
testcase_52 AC 310 ms
97,536 KB
testcase_53 AC 294 ms
89,728 KB
testcase_54 AC 68 ms
33,792 KB
testcase_55 AC 426 ms
107,264 KB
testcase_56 AC 434 ms
106,364 KB
testcase_57 AC 423 ms
107,392 KB
testcase_58 AC 437 ms
106,820 KB
testcase_59 AC 429 ms
107,648 KB
testcase_60 AC 323 ms
96,740 KB
testcase_61 AC 334 ms
97,272 KB
testcase_62 AC 395 ms
115,856 KB
testcase_63 AC 569 ms
319,608 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (81 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;

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

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);
    }

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

class GraphBase<T> { public required Graph<T> Graph { get; init; } }

class Tree<T> : GraphBase<T>
{
    public static Tree<T>? TryConstruct(Graph<T> graph)
    {
        if (graph.Directed || graph.Edges.Count + 1 != graph.N) return null;
        var visited = new bool[graph.N];
        int S(int v, int prev)
        {
            visited[v] = true;
            var res = 1;
            foreach (var (next, _) in graph.AdjacencyList[v])
            {
                if (next == prev || visited[next]) continue;
                res += S(next, v);
            }
            return res;
        }
        if (S(0, -1) < graph.N) return null;
        return new Tree<T> { Graph = graph };
    }

    public interface IRerootingOperator<TResult>
    {
        TResult Identity { get; }
        TResult Merge(TResult l, TResult r);
        TResult Finalize(TResult e, int v);
    }
    public TResult[] Rerooting<TResult, TOperator>(TOperator op = default) where TOperator : struct, IRerootingOperator<TResult>
    {
        var n = Graph.N;
        var res = new TResult[n];
        var adjacencyList = Graph.AdjacencyList;
        var subTreeValues = new (TResult, int)[n][];
        TResult SubTreeValue(int v, int prev)
        {
            var merged = op.Identity;
            var l = adjacencyList[v].Count;
            subTreeValues[v] = new (TResult, int)[l];
            for (var i = 0; i < l; i++)
            {
                var (next, index) = adjacencyList[v][i];
                if (next == prev) continue;
                var subTreeValue = SubTreeValue(next, v);
                subTreeValues[v][i] = (subTreeValue, index);
                merged = op.Merge(merged, subTreeValue);
            }
            return op.Finalize(merged, v);
        }
        void Evaluate(int v, int prev, TResult prevValue)
        {
            var adjacencies = adjacencyList[v];
            var l = adjacencies.Count;
            for (var i = 0; i < l; i++) if (adjacencies[i].next == prev)
            {
                subTreeValues[v][i].Item1 = prevValue;
                break;
            }
            var sumL = new TResult[l];
            var sumR = new TResult[l];
            sumL[0] = op.Identity;
            sumR[l - 1] = op.Identity;
            for (var i = 1; i < l; i++) sumL[i] = op.Merge(sumL[i - 1], subTreeValues[v][i - 1].Item1);
            for (var i = l - 2; i >= 0; i--) sumR[i] = op.Merge(sumR[i + 1], subTreeValues[v][i + 1].Item1);
            res[v] = op.Finalize(op.Merge(sumR[0], subTreeValues[v][0].Item1), v);
            for (var i = 0; i < l; i++)
            {
                var next = adjacencies[i].next;
                if (next == prev) continue;
                Evaluate(next, v, op.Finalize(op.Merge(sumL[i], sumR[i]), next));
            }
        }
        SubTreeValue(0, -1);
        Evaluate(0, -1, op.Identity);
        return res;
    }
}

readonly struct Op : Tree<int>.IRerootingOperator<(int, int, int)>
{
    public (int, int, int) Identity => (0, 0, 0);
    public (int, int, int) Finalize((int, int, int) e, int v)
    {
        var (e0, _, em) = e;
        e0++;
        return (em, e0, Math.Max(e0, em));
    }
    public (int, int, int) Merge((int, int, int) l, (int, int, int) r)
    {
        var (l0, l1, lm) = l;
        var (r0, r1, rm) = r;
        return (l0 + r0, l1 + r1, lm + rm);
    }
}

class AtCoder
{
    object? Solve()
    {
        var n = Int();
        var edges = (n - 1).Repeat(() => (Int() - 1, Int() - 1));
        if (n == 1) return 1;
        var graph = GraphBuilder.SimpleGraph(n, edges, false);
        var tree = Tree<int>.TryConstruct(graph)!;
        var dp = tree.Rerooting<(int, int, int), Op>();
        var ans = dp.Select(e => e.Item2).Min();
        return ans;
    }

    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 };

    #pragma warning disable IDE0051
    string String()
    {
        while (iter >= input.Length) (input, iter) = (Console.ReadLine()!.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();
    }
    #pragma warning restore IDE0051
}
0