-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
68 lines (59 loc) · 1.52 KB
/
Solution.cs
File metadata and controls
68 lines (59 loc) · 1.52 KB
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
public class Solution
{
public int MinimumScore(int[] nums, int[][] edges)
{
int totalXor = 0;
int ans = int.MaxValue;
foreach (int num in nums)
{
totalXor ^= num;
}
int n = nums.Length;
List<int>[] adj = new List<int>[n];
for (int i = 0; i < n; i++)
{
adj[i] = [];
}
foreach (int[] edge in edges)
{
int u = edge[0], v = edge[1];
adj[u].Add(v);
adj[v].Add(u);
}
int DFS(int u, int p)
{
int xor = nums[u];
foreach (int v in adj[u])
{
if (v == p) continue;
xor ^= DFS(v, u);
}
foreach (int v in adj[u])
{
if (v == p)
{
DFS2(v, u, u, xor);
}
}
return xor;
}
int DFS2(int u, int p, int r, int otherXor)
{
int xor = nums[u];
foreach (int v in adj[u])
{
if (v == p) continue;
xor ^= DFS2(v, u, r, otherXor);
}
if (p == r) return xor;
ans = Math.Min(ans, Calc(otherXor, xor, totalXor ^ otherXor ^ xor));
return xor;
}
DFS(0, -1);
return ans;
}
int Calc(int part1, int part2, int part3)
{
return Math.Max(part1, Math.Max(part2, part3)) - Math.Min(part1, Math.Min(part2, part3));
}
}