結果

問題 No.2677 Minmax Independent Set
ユーザー tobisatistobisatis
提出日時 2024-03-15 23:28:06
言語 C#
(.NET 8.0.203)
結果
RE  
実行時間 -
コード長 6,653 bytes
コンパイル時間 8,530 ms
コンパイル使用メモリ 170,028 KB
実行使用メモリ 320,472 KB
最終ジャッジ日時 2024-09-30 03:03:24
合計ジャッジ時間 29,295 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
30,848 KB
testcase_01 AC 64 ms
30,844 KB
testcase_02 AC 60 ms
31,360 KB
testcase_03 RE -
testcase_04 AC 63 ms
31,356 KB
testcase_05 AC 502 ms
106,860 KB
testcase_06 AC 507 ms
108,160 KB
testcase_07 AC 513 ms
109,184 KB
testcase_08 AC 488 ms
110,592 KB
testcase_09 AC 525 ms
114,936 KB
testcase_10 AC 429 ms
107,792 KB
testcase_11 AC 433 ms
107,408 KB
testcase_12 AC 845 ms
199,804 KB
testcase_13 AC 643 ms
133,432 KB
testcase_14 AC 521 ms
134,912 KB
testcase_15 AC 693 ms
152,560 KB
testcase_16 AC 492 ms
96,616 KB
testcase_17 AC 500 ms
96,884 KB
testcase_18 AC 295 ms
81,740 KB
testcase_19 AC 377 ms
93,440 KB
testcase_20 AC 153 ms
53,760 KB
testcase_21 AC 443 ms
93,564 KB
testcase_22 AC 88 ms
39,552 KB
testcase_23 AC 63 ms
31,464 KB
testcase_24 AC 63 ms
30,848 KB
testcase_25 AC 60 ms
31,104 KB
testcase_26 AC 60 ms
31,104 KB
testcase_27 AC 63 ms
31,872 KB
testcase_28 AC 414 ms
108,520 KB
testcase_29 AC 609 ms
154,540 KB
testcase_30 AC 61 ms
31,104 KB
testcase_31 AC 62 ms
31,080 KB
testcase_32 AC 62 ms
31,088 KB
testcase_33 AC 61 ms
30,848 KB
testcase_34 AC 61 ms
31,360 KB
testcase_35 AC 59 ms
31,080 KB
testcase_36 AC 62 ms
31,104 KB
testcase_37 AC 62 ms
31,468 KB
testcase_38 AC 63 ms
31,488 KB
testcase_39 AC 63 ms
32,000 KB
testcase_40 AC 65 ms
32,488 KB
testcase_41 AC 72 ms
34,044 KB
testcase_42 AC 87 ms
38,272 KB
testcase_43 AC 113 ms
44,412 KB
testcase_44 AC 152 ms
52,836 KB
testcase_45 AC 224 ms
70,912 KB
testcase_46 AC 505 ms
105,320 KB
testcase_47 AC 495 ms
99,468 KB
testcase_48 AC 493 ms
97,280 KB
testcase_49 AC 525 ms
104,060 KB
testcase_50 AC 168 ms
58,792 KB
testcase_51 AC 77 ms
34,432 KB
testcase_52 AC 312 ms
97,408 KB
testcase_53 AC 294 ms
89,472 KB
testcase_54 AC 67 ms
33,920 KB
testcase_55 AC 432 ms
106,636 KB
testcase_56 AC 436 ms
107,440 KB
testcase_57 AC 455 ms
106,856 KB
testcase_58 AC 427 ms
107,436 KB
testcase_59 AC 427 ms
107,284 KB
testcase_60 AC 307 ms
96,384 KB
testcase_61 AC 332 ms
97,592 KB
testcase_62 AC 389 ms
115,728 KB
testcase_63 AC 562 ms
320,472 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (80 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));
        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