Skip to content

Commit

Permalink
Merge pull request #197 from HirokiYoshida837/BCH027
Browse files Browse the repository at this point in the history
add : solved Bootcamp Hard 027
  • Loading branch information
HirokiYoshida837 committed Sep 19, 2022
2 parents d658be0 + e2305fa commit 3e56dcf
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
8 changes: 8 additions & 0 deletions BootCampForBeginners/Hard/Hard_027/Hard_027.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

</Project>
118 changes: 118 additions & 0 deletions BootCampForBeginners/Hard/Hard_027/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static System.Math;

namespace Hard_027
{
public static class Program
{
public static void Main(string[] args)
{
var (n, q) = ReadValue<int, int>();

var abList = Enumerable.Range(0, n-1)
.Select(_ => ReadValue<int, int>())
.Select(x => (x.Item1 - 1, x.Item2 - 1))
.ToArray();
var pxList = Enumerable.Range(0, q)
.Select(_ => ReadValue<int, long>())
.Select(x => (x.Item1 - 1, x.Item2))
.ToArray();

var pDic = pxList.GroupBy(x => x.Item1)
.ToDictionary(x => x.Key, x => x.Select(x => x.Item2).Sum());

var graph = Enumerable.Range(0, n)
.ToDictionary(x => x, x => new HashSet<int>());

foreach (var (u, v) in abList)
{
graph[u].Add(v);
graph[v].Add(u);
}

// DFSしながら足していく
var ansList = new long[n + 1];

void DFS(int current, int parent)
{
if (parent != -1)
{
ansList[current] += ansList[parent];
}

if (pDic.ContainsKey(current))
{
ansList[current] += pDic[current];
}


foreach (var i in graph[current])
{
if (i == parent) continue;
DFS(i, current);
}
}

DFS(0, -1);

var sb = new StringBuilder();
foreach (var l in ansList.Take(n))
{
sb.Append(l);
sb.Append(" ");
}

Console.WriteLine(sb.ToString());
}


public static T ReadValue<T>()
{
var input = Console.ReadLine();
return (T) Convert.ChangeType(input, typeof(T));
}

public static (T1, T2) ReadValue<T1, T2>()
{
var input = Console.ReadLine().Split();
return (
(T1) Convert.ChangeType(input[0], typeof(T1)),
(T2) Convert.ChangeType(input[1], typeof(T2))
);
}

public static (T1, T2, T3) ReadValue<T1, T2, T3>()
{
var input = Console.ReadLine().Split();
return (
(T1) Convert.ChangeType(input[0], typeof(T1)),
(T2) Convert.ChangeType(input[1], typeof(T2)),
(T3) Convert.ChangeType(input[2], typeof(T3))
);
}

/// <summary>
/// 指定した型として、一行読み込む。
/// </summary>
/// <param name="separator"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
#nullable enable
public static IEnumerable<T> ReadList<T>(params char[]? separator)
{
return Console.ReadLine()
.Split(separator)
.Select(x => (T) Convert.ChangeType(x, typeof(T)));
}
#nullable disable
}
}

0 comments on commit 3e56dcf

Please sign in to comment.