結果
問題 | No.2677 Minmax Independent Set |
ユーザー |
|
提出日時 | 2024-03-15 23:44:01 |
言語 | C# (.NET 8.0.404) |
結果 |
AC
|
実行時間 | 586 ms / 2,000 ms |
コード長 | 2,446 bytes |
コンパイル時間 | 9,070 ms |
コンパイル使用メモリ | 167,656 KB |
実行使用メモリ | 247,028 KB |
最終ジャッジ日時 | 2024-09-30 03:14:01 |
合計ジャッジ時間 | 29,978 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 61 |
コンパイルメッセージ
復元対象のプロジェクトを決定しています... /home/judge/data/code/main.csproj を復元しました (92 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/
ソースコード
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}";}}}