結果
| 問題 |
No.1817 Reversed Edges
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-12-21 00:02:17 |
| 言語 | C# (.NET 8.0.404) |
| 結果 |
AC
|
| 実行時間 | 334 ms / 2,000 ms |
| コード長 | 1,733 bytes |
| コンパイル時間 | 9,699 ms |
| コンパイル使用メモリ | 172,120 KB |
| 実行使用メモリ | 238,596 KB |
| 最終ジャッジ日時 | 2024-09-27 10:18:33 |
| 合計ジャッジ時間 | 17,565 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 23 |
コンパイルメッセージ
復元対象のプロジェクトを決定しています... /home/judge/data/code/main.csproj を復元しました (98 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[] 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);
}
}
}