結果
| 問題 |
No.912 赤黒木
|
| コンテスト | |
| ユーザー |
ei1333333
|
| 提出日時 | 2019-10-18 12:59:14 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 877 ms / 3,000 ms |
| コード長 | 2,066 bytes |
| コンパイル時間 | 2,850 ms |
| コンパイル使用メモリ | 114,088 KB |
| 実行使用メモリ | 97,144 KB |
| 最終ジャッジ日時 | 2024-06-25 14:19:49 |
| 合計ジャッジ時間 | 18,092 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 30 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System;
using System.Collections.Generic;
class Program
{
Scanner cin;
int N;
List<int>[] g;
int[] sub;
int[,] dp;
void rec(int idx, int par)
{
int[] dp2 = { 0, 0, 0 };
foreach(var to in g[idx])
{
if (to == par) continue;
rec(to, idx);
sub[idx] += sub[to];
int[] dp3 = { 10101010, 10101010, 10101010 };
for (int i = 0; i < 3; i++)
{
for(int j = 0; i + j < 3; j++)
{
dp3[i + j] = Math.Min(dp3[i + j], dp2[i] + dp[to, j] + (sub[to] + j) % 2);
}
}
for (int i = 0; i < 3; i++)
{
dp2[i] = dp3[i];
}
}
for(int i = 0; i < 3; i++)
{
dp[idx, i] = dp2[i];
}
}
void myon()
{
cin = new Scanner();
N = cin.nextInt();
g = new List<int>[N];
for(int i = 0; i < N; i++)
{
g[i] = new List<int>();
}
for(int i = 1; i < N; i++)
{
int a = cin.nextInt() - 1;
int b = cin.nextInt() - 1;
g[a].Add(b);
g[b].Add(a);
}
sub = new int[N];
for(int i = 1; i < N; i++)
{
int a = cin.nextInt() - 1;
int b = cin.nextInt() - 1;
sub[a] = 1 - sub[a];
sub[b] = 1 - sub[b];
}
dp = new int[N, 3];
rec(0, -1);
Console.WriteLine(dp[0, 2] + (N - 1));
}
static void Main(string[] args)
{
new Program().myon();
}
}
class Scanner
{
string[] s;
int i;
readonly char[] cs = new char[] { ' ' };
public Scanner()
{
s = new string[0];
i = 0;
}
public string next()
{
if (i < s.Length) return s[i++];
string st = Console.ReadLine();
while (st == "") st = Console.ReadLine();
s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
if (s.Length == 0) return next();
i = 0;
return s[i++];
}
public string nextLine()
{
return Console.ReadLine();
}
public int nextInt()
{
return int.Parse(next());
}
public long nextLong()
{
return long.Parse(next());
}
public double nextDouble()
{
return double.Parse(next());
}
}
ei1333333