-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cut the Tree.cs
175 lines (147 loc) · 3.86 KB
/
Cut the Tree.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
public class Solver
{
private int n;
private List<int>[] neighbours;
private int[] a;
private int ans = int.MaxValue;
private int[] sums;
void ClearParent(int x, int p)
{
for (int i = 0; i < neighbours[x].Count; i++)
if (neighbours[x][i] == p)
{
neighbours[x].RemoveAt(i);
i--;
}
else
ClearParent(neighbours[x][i], x);
}
int CalcSums(int x)
{
sums[x] = a[x];
foreach (int c in neighbours[x])
sums[x] += CalcSums(c);
return sums[x];
}
void TryRemove(int x)
{
foreach (int c in neighbours[x])
{
ans = Math.Min(ans, Math.Abs(sums[0] - 2 * sums[c]));
TryRemove(c);
}
}
public object Solve()
{
n = ReadInt();
a = ReadIntArray();
neighbours = Enumerable.Repeat(0, n).Select(x => new List<int>()).ToArray();
for (int i = 0; i < n - 1; i++)
{
int x = ReadInt() - 1;
int y = ReadInt() - 1;
neighbours[x].Add(y);
neighbours[y].Add(x);
}
ClearParent(0, -1);
sums = new int[n];
CalcSums(0);
TryRemove(0);
return ans;
}
#region Main
protected static TextReader reader;
protected static TextWriter writer;
static void Main()
{
#if DEBUG
reader = new StreamReader("..\\..\\input.txt");
writer = Console.Out;
//writer = new StreamWriter("..\\..\\output.txt");
#else
reader = Console.In;
writer = Console.Out;
#endif
try
{
object result = new Solver().Solve();
if (result != null)
{
writer.WriteLine(result);
}
}
catch (Exception ex)
{
#if DEBUG
Console.WriteLine(ex);
#else
throw;
#endif
}
reader.Close();
writer.Close();
}
#endregion
#region Read/Write
private static Queue<string> currentLineTokens = new Queue<string>();
private static string[] ReadAndSplitLine()
{
return reader.ReadLine().Split(new[] {' ', '\t'}, StringSplitOptions.RemoveEmptyEntries);
}
public static string ReadToken()
{
if (currentLineTokens.Count == 0)
currentLineTokens = new Queue<string>(ReadAndSplitLine());
return currentLineTokens.Dequeue();
}
public static int ReadInt()
{
return int.Parse(ReadToken());
}
public static long ReadLong()
{
return long.Parse(ReadToken());
}
public static double ReadDouble()
{
return double.Parse(ReadToken(), CultureInfo.InvariantCulture);
}
public static int[] ReadIntArray()
{
return ReadAndSplitLine().Select(int.Parse).ToArray();
}
public static long[] ReadLongArray()
{
return ReadAndSplitLine().Select(long.Parse).ToArray();
}
public static double[] ReadDoubleArray()
{
return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray();
}
public static int[][] ReadIntMatrix(int numberOfRows)
{
int[][] matrix = new int[numberOfRows][];
for (int i = 0; i < numberOfRows; i++)
matrix[i] = ReadIntArray();
return matrix;
}
public static string[] ReadLines(int quantity)
{
string[] lines = new string[quantity];
for (int i = 0; i < quantity; i++)
lines[i] = reader.ReadLine().Trim();
return lines;
}
public static void WriteArray<T>(IEnumerable<T> array)
{
writer.WriteLine(string.Join(" ", array));
}
#endregion
}