結果

問題 No.2677 Minmax Independent Set
ユーザー tobisatistobisatis
提出日時 2024-03-15 23:29:31
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 795 ms / 2,000 ms
コード長 6,683 bytes
コンパイル時間 9,970 ms
コンパイル使用メモリ 158,764 KB
実行使用メモリ 303,884 KB
最終ジャッジ日時 2024-03-15 23:30:10
合計ジャッジ時間 35,172 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
33,188 KB
testcase_01 AC 95 ms
33,188 KB
testcase_02 AC 100 ms
33,188 KB
testcase_03 AC 83 ms
32,932 KB
testcase_04 AC 94 ms
33,188 KB
testcase_05 AC 654 ms
106,880 KB
testcase_06 AC 670 ms
108,016 KB
testcase_07 AC 640 ms
108,892 KB
testcase_08 AC 695 ms
111,028 KB
testcase_09 AC 681 ms
114,724 KB
testcase_10 AC 614 ms
109,456 KB
testcase_11 AC 648 ms
109,460 KB
testcase_12 AC 786 ms
192,652 KB
testcase_13 AC 795 ms
133,288 KB
testcase_14 AC 703 ms
135,360 KB
testcase_15 AC 766 ms
151,864 KB
testcase_16 AC 712 ms
98,216 KB
testcase_17 AC 652 ms
98,216 KB
testcase_18 AC 454 ms
83,180 KB
testcase_19 AC 553 ms
95,648 KB
testcase_20 AC 222 ms
55,436 KB
testcase_21 AC 610 ms
95,908 KB
testcase_22 AC 128 ms
41,124 KB
testcase_23 AC 96 ms
33,572 KB
testcase_24 AC 93 ms
33,316 KB
testcase_25 AC 97 ms
33,316 KB
testcase_26 AC 93 ms
33,316 KB
testcase_27 AC 94 ms
33,444 KB
testcase_28 AC 622 ms
111,108 KB
testcase_29 AC 731 ms
153,428 KB
testcase_30 AC 104 ms
33,188 KB
testcase_31 AC 92 ms
33,188 KB
testcase_32 AC 94 ms
33,188 KB
testcase_33 AC 93 ms
33,188 KB
testcase_34 AC 92 ms
33,188 KB
testcase_35 AC 93 ms
33,316 KB
testcase_36 AC 94 ms
33,316 KB
testcase_37 AC 94 ms
33,444 KB
testcase_38 AC 95 ms
33,572 KB
testcase_39 AC 95 ms
33,956 KB
testcase_40 AC 101 ms
34,852 KB
testcase_41 AC 105 ms
36,388 KB
testcase_42 AC 114 ms
39,972 KB
testcase_43 AC 143 ms
46,244 KB
testcase_44 AC 208 ms
54,308 KB
testcase_45 AC 331 ms
72,444 KB
testcase_46 AC 693 ms
107,608 KB
testcase_47 AC 667 ms
101,700 KB
testcase_48 AC 683 ms
98,380 KB
testcase_49 AC 667 ms
106,088 KB
testcase_50 AC 273 ms
60,580 KB
testcase_51 AC 108 ms
36,516 KB
testcase_52 AC 478 ms
99,980 KB
testcase_53 AC 470 ms
92,464 KB
testcase_54 AC 101 ms
35,876 KB
testcase_55 AC 613 ms
108,860 KB
testcase_56 AC 614 ms
108,272 KB
testcase_57 AC 633 ms
109,224 KB
testcase_58 AC 624 ms
108,392 KB
testcase_59 AC 643 ms
108,636 KB
testcase_60 AC 364 ms
101,924 KB
testcase_61 AC 388 ms
98,848 KB
testcase_62 AC 413 ms
115,628 KB
testcase_63 AC 500 ms
303,884 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (103 ms)。
MSBuild のバージョン 17.7.3+8ec440e68 (.NET)
  main -> /home/judge/data/code/bin/Release/net7.0/main.dll
  main -> /home/judge/data/code/bin/Release/net7.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