結果
| 問題 | No.439 チワワのなる木 |
| コンテスト | |
| ユーザー |
👑 |
| 提出日時 | 2026-12-01 00:07:23 |
| 言語 | C# (.NET 10.0.403 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 140 ms / 5,000 ms |
| + 86µs | |
| コード長 | 1,834 bytes |
| 記録 | |
| コンパイル時間 | 12,435 ms |
| コンパイル使用メモリ | 171,588 KB |
| 実行使用メモリ | 71,592 KB |
| 最終ジャッジ日時 | 2026-12-01 00:07:46 |
| 合計ジャッジ時間 | 17,075 ms |
|
ジャッジサーバーID (参考情報) |
judge2_0 / judge5_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 28 |
コンパイルメッセージ
復元対象のプロジェクトを決定しています... /home/judge/data/code/main.csproj を復元しました (262 ミリ秒)。 main -> /home/judge/data/code/bin/Release/net10.0/main.dll main -> /home/judge/data/code/bin/Release/net10.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 s = ReadLine();
var map = NMap(n - 1);
WriteLine(Cww(n, s, map));
}
static long Cww(int n, string s, int[][] map)
{
var tree = new List<int>[n];
for (var i = 0; i < n; ++i) tree[i] = new List<int>();
foreach (var edge in map)
{
tree[edge[0]].Add(edge[1]);
tree[edge[1]].Add(edge[0]);
}
var ans = 0L;
call = s.Count(c => c == 'c');
wall = n - call;
DFS(0, -1, tree, s, ref ans);
return ans;
}
static int call = 0;
static int wall = 0;
static (int ccount, int wcount) DFS(int cur, int prev, List<int>[] tree, string s, ref long ans)
{
var ccount = 0;
var wcount = 0;
if (s[cur] == 'c') ++ccount;
else ++wcount;
foreach (var next in tree[cur])
{
if (prev == next) continue;
var (ci, wi) = DFS(next, cur, tree, s, ref ans);
if (s[cur] == 'w')
{
ans += ci * (wall - wi - 1L);
}
ccount += ci;
wcount += wi;
}
if (s[cur] == 'w')
{
ans += (call - ccount) * (wcount - 1L);
}
return (ccount, wcount);
}
}