結果

問題 No.2677 Minmax Independent Set
ユーザー 👑 kakel-sankakel-san
提出日時 2024-03-15 23:44:01
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 625 ms / 2,000 ms
コード長 2,446 bytes
コンパイル時間 9,126 ms
コンパイル使用メモリ 158,200 KB
実行使用メモリ 239,232 KB
最終ジャッジ日時 2024-03-15 23:44:34
合計ジャッジ時間 31,080 ms
ジャッジサーバーID
(参考情報)
judge10 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
33,060 KB
testcase_01 AC 82 ms
33,060 KB
testcase_02 AC 83 ms
33,060 KB
testcase_03 AC 82 ms
33,060 KB
testcase_04 AC 81 ms
33,060 KB
testcase_05 AC 560 ms
77,196 KB
testcase_06 AC 527 ms
77,452 KB
testcase_07 AC 563 ms
77,968 KB
testcase_08 AC 527 ms
78,604 KB
testcase_09 AC 526 ms
79,884 KB
testcase_10 AC 518 ms
79,224 KB
testcase_11 AC 509 ms
79,228 KB
testcase_12 AC 573 ms
105,112 KB
testcase_13 AC 585 ms
88,912 KB
testcase_14 AC 580 ms
90,676 KB
testcase_15 AC 617 ms
94,804 KB
testcase_16 AC 625 ms
74,616 KB
testcase_17 AC 620 ms
74,612 KB
testcase_18 AC 407 ms
64,944 KB
testcase_19 AC 474 ms
67,640 KB
testcase_20 AC 186 ms
54,564 KB
testcase_21 AC 521 ms
70,180 KB
testcase_22 AC 109 ms
39,332 KB
testcase_23 AC 83 ms
33,316 KB
testcase_24 AC 82 ms
33,060 KB
testcase_25 AC 82 ms
33,060 KB
testcase_26 AC 83 ms
33,060 KB
testcase_27 AC 83 ms
33,188 KB
testcase_28 AC 507 ms
79,100 KB
testcase_29 AC 549 ms
93,704 KB
testcase_30 AC 82 ms
33,060 KB
testcase_31 AC 83 ms
33,060 KB
testcase_32 AC 84 ms
33,060 KB
testcase_33 AC 83 ms
32,932 KB
testcase_34 AC 82 ms
33,060 KB
testcase_35 AC 83 ms
33,060 KB
testcase_36 AC 82 ms
33,060 KB
testcase_37 AC 83 ms
33,188 KB
testcase_38 AC 82 ms
33,316 KB
testcase_39 AC 83 ms
33,572 KB
testcase_40 AC 85 ms
34,212 KB
testcase_41 AC 91 ms
35,748 KB
testcase_42 AC 103 ms
38,180 KB
testcase_43 AC 124 ms
43,172 KB
testcase_44 AC 174 ms
52,900 KB
testcase_45 AC 301 ms
62,072 KB
testcase_46 AC 588 ms
73,100 KB
testcase_47 AC 620 ms
74,376 KB
testcase_48 AC 591 ms
74,376 KB
testcase_49 AC 606 ms
74,504 KB
testcase_50 AC 230 ms
63,484 KB
testcase_51 AC 90 ms
36,004 KB
testcase_52 AC 441 ms
74,928 KB
testcase_53 AC 389 ms
69,408 KB
testcase_54 AC 88 ms
35,492 KB
testcase_55 AC 538 ms
80,140 KB
testcase_56 AC 538 ms
80,316 KB
testcase_57 AC 529 ms
80,328 KB
testcase_58 AC 520 ms
80,404 KB
testcase_59 AC 516 ms
80,440 KB
testcase_60 AC 360 ms
78,148 KB
testcase_61 AC 363 ms
75,260 KB
testcase_62 AC 367 ms
80,004 KB
testcase_63 AC 382 ms
239,232 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (107 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 #

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

class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[] NMi => ReadLine().Split().Select(c => int.Parse(c) - 1).ToArray();
    static int[][] NMap(int n) => Enumerable.Repeat(0, n).Select(_ => NMi).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var n = NN;
        var map = NMap(n - 1);
        var tree = new List<Pair>[n];
        for (var i = 0; i < tree.Length; ++i) tree[i] = new List<Pair>();
        foreach (var edge in map)
        {
            tree[edge[0]].Add(new Pair(edge[1]));
            tree[edge[1]].Add(new Pair(edge[0]));
        }
        DFS1(-1, 0, tree);
        DFS2(-1, 0, 0, 0, tree);
        var ans = int.MaxValue;
        for (var i = 0; i < n; ++i)
        {
            var sub = 1;
            foreach (var next in tree[i]) sub += next.W;
            ans = Math.Min(ans, sub);
        }
        WriteLine(ans);
    }
    static (int b, int w) DFS1(int prev, int cur, List<Pair>[] tree)
    {
        var b = 1;
        var w = 0;
        foreach (var next in tree[cur])
        {
            if (next.To == prev) continue;
            var nr = DFS1(cur, next.To, tree);
            next.B = nr.b;
            next.W = nr.w;
            b += nr.w;
            w += Math.Max(nr.w, nr.b);
        }
        return (b, w);
    }
    static void DFS2(int prev, int cur, int b, int w, List<Pair>[] tree)
    {
        var bsum = 1;
        var wsum = 0;
        foreach (var next in tree[cur])
        {
            if (next.To == prev) 
            {
                next.B = b;
                next.W = w;
            }
            bsum += next.W;
            wsum += Math.Max(next.B, next.W);
        }
        foreach (var next in tree[cur])
        {
            if (next.To == prev) continue;
            DFS2(cur, next.To, bsum - next.W, wsum - Math.Max(next.B, next.W), tree);
        }
    }
    class Pair
    {
        public int To;
        public int B;
        public int W;
        public Pair(int to)
        {
            To = to;
        }
        public override string ToString()
        {
            return $"To: {To}, B: {B}, W: {W}";
        }
    }
}
 
0