-
Notifications
You must be signed in to change notification settings - Fork 79
/
BalancedForest.cpp
148 lines (118 loc) · 2.92 KB
/
BalancedForest.cpp
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
#include<bits/stdc++.h>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
const int sz=1<<21+1;
struct node
{
long long s; //c[i].s - sum of values in subtree rooted in i
int u; //time when dfs arrived in i-th node
int o; //time when dfs leave i-th node ( all children are marked)
};
long long mini,sum;
node c[sz];
long long a[sz];
int tim,x,y,n;
vector <int> v[sz];
map <long long, int> mi,ma,mi1,ma1;
void clear_everything()
{
sum=0;
tim=0;
for (int i=1;i<=n;i++)
v[i].clear();
ma.clear();
ma1.clear();
mi.clear();
mi1.clear();
}
void dfs(int x, int pred)
{
tim++;
c[x].u=tim;
c[x].s=a[x];
for (int i=0;i<v[x].size();i++)
if (v[x][i]!=pred)
{
dfs(v[x][i],x);
c[x].s+=c[v[x][i]].s;
}
c[x].o=tim;
return;
}
void do_it(int x)
{
long long add=3*c[x].s-sum;
if (mi[c[x].s]!=ma[c[x].s]) mini=min(mini,add); else
if (mi[c[x].s-add] && (c[mi1[c[x].s-add]].u<c[x].u || c[mi1[c[x].s-add]].u>c[x].o)) mini=min(mini,add); else
if (ma[c[x].s-add] && (c[ma1[c[x].s-add]].u<c[x].u || c[ma1[c[x].s-add]].u>c[x].o)) mini=min(mini,add); else
if (mi[2*c[x].s-add]) mini=min(mini,add); else
if (mi[2*c[x].s]) mini=min(mini,add);
return ;
}
void uradi(int x)
{
if ((sum-c[x].s)%2==0)
{
long long p=(sum-c[x].s)/2;
if (ma[p] && (c[ma1[p]].u>c[x].u || c[ma1[p]].o<c[x].u)) mini=min(mini,p-c[x].s); else
if (mi[p] && (c[mi1[p]].u>c[x].u || c[mi1[p]].o<c[x].u)) mini=min(mini,p-c[x].s); else
if (mi[p+c[x].s] && c[mi1[p+c[x].s]].u<c[x].u && c[mi1[p+c[x].s]].o>=c[x].u) mini=min(mini,p-c[x].s); else
if (ma[p+c[x].s] && c[ma1[p+c[x].s]].u<c[x].u && c[ma1[p+c[x].s]].o>=c[x].u) mini=min(mini,p-c[x].s);
return ;
}
}
void solve()
{
scanf("%d",&n);
clear_everything();
for (int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
sum+=a[i];
}
for (int i=1;i<n;i++)
{
scanf("%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1,0);
for (int i=1;i<=n;i++)
if (!mi[c[i].s])
{
mi[c[i].s]=c[i].u;
ma[c[i].s]=c[i].u;
mi1[c[i].s]=i;
ma1[c[i].s]=i;
} else
{
if (mi[c[i].s]>c[i].u)
{
mi[c[i].s]=c[i].u;
mi1[c[i].s]=i;
}
if (ma[c[i].s]<c[i].u)
{
ma[c[i].s]=c[i].u;
ma1[c[i].s]=i;
}
}
mini=1e15;
if (sum%2==0 && ma[sum/2]) mini=sum/2;
for (int i=1;i<=n;i++)
if (3*c[i].s<=sum) uradi(i); else
if (2*c[i].s<sum) do_it(i);
if (mini==1e15) printf("-1\n"); else
printf("%lld\n",mini);
return ;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
solve();
return 0;
}