結果

問題 No.1817 Reversed Edges
ユーザー 👑 kakel-sankakel-san
提出日時 2023-12-21 00:02:17
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 321 ms / 2,000 ms
コード長 1,733 bytes
コンパイル時間 15,596 ms
コンパイル使用メモリ 157,744 KB
実行使用メモリ 227,488 KB
最終ジャッジ日時 2023-12-21 00:02:42
合計ジャッジ時間 19,387 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
33,060 KB
testcase_01 AC 84 ms
33,060 KB
testcase_02 AC 83 ms
33,060 KB
testcase_03 AC 81 ms
33,060 KB
testcase_04 AC 85 ms
33,060 KB
testcase_05 AC 87 ms
33,060 KB
testcase_06 AC 83 ms
33,060 KB
testcase_07 AC 274 ms
65,872 KB
testcase_08 AC 147 ms
52,900 KB
testcase_09 AC 269 ms
64,644 KB
testcase_10 AC 163 ms
55,972 KB
testcase_11 AC 255 ms
64,252 KB
testcase_12 AC 295 ms
65,588 KB
testcase_13 AC 291 ms
65,584 KB
testcase_14 AC 293 ms
65,588 KB
testcase_15 AC 305 ms
65,584 KB
testcase_16 AC 288 ms
65,588 KB
testcase_17 AC 289 ms
65,592 KB
testcase_18 AC 321 ms
65,592 KB
testcase_19 AC 303 ms
65,588 KB
testcase_20 AC 308 ms
65,588 KB
testcase_21 AC 307 ms
65,588 KB
testcase_22 AC 225 ms
64,440 KB
testcase_23 AC 214 ms
64,312 KB
testcase_24 AC 236 ms
227,488 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (121 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[] 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 < n; ++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]));
        }
        var ans = new int[n];
        DFS1(-1, 0, tree, ans);
        DFS2(-1, 0, 0, tree, ans);
        WriteLine(string.Join("\n", ans));
    }
    class Pair
    {
        public int To;
        public int Val;
        public Pair(int to)
        {
            To = to;
        }
    }
    static void DFS1(int prev, int cur, List<Pair>[] tree, int[] ans)
    {
        foreach (var next in tree[cur])
        {
            if (prev == next.To) continue;
            DFS1(cur, next.To, tree, ans);
            next.Val = ans[next.To] + (cur > next.To ? 1 : 0);
            ans[cur] += next.Val;
        }
    }
    static void DFS2(int prev, int cur, int take, List<Pair>[] tree, int[] ans)
    {
        ans[cur] += take;
        if (prev >= 0 && prev < cur) ++ans[cur];
        foreach (var next in tree[cur])
        {
            if (prev == next.To) continue;
            DFS2(cur, next.To, ans[cur] - next.Val, tree, ans);
        }
    }
}
0