题目
https://www.lydsy.com/JudgeOnline/problem.php?id=3257
解题思路
设 f [ x ] f[x] f[x]表示以 x x x为根的子树中,满足有0个黑色点,若干个白色点的最少切割次数; g [ x ] g[x] g[x]表示以 x x x为根的子树中,满足有若干个黑色点,0个白色点的最少切割次数; h [ x ] h[x] h[x]表示以 x x x为根的子树中,满足有若干个黑色点,1个白色点的最少切割次数。
如果该点是黑色的:
有
{
g
[
x
]
+
=
m
i
n
(
g
[
a
[
i
]
.
y
]
,
m
i
n
(
h
[
a
[
i
]
.
y
]
,
f
[
a
[
i
]
.
y
]
)
+
a
[
i
]
.
z
)
;
h
[
x
]
=
m
i
n
(
h
[
x
]
,
h
[
a
[
i
]
.
y
]
+
g
[
x
]
−
m
i
n
(
g
[
a
[
i
]
.
y
]
,
m
i
n
(
h
[
a
[
i
]
.
y
]
,
f
[
a
[
i
]
.
y
]
)
+
a
[
i
]
.
z
)
)
;
\left\{\begin{matrix} g[x]+=min(g[a[i].y],min(h[a[i].y],f[a[i].y])+a[i].z); \\ h[x]=min(h[x],h[a[i].y]+g[x]-min(g[a[i].y],min(h[a[i].y],f[a[i].y])+a[i].z)); \end{matrix}\right.
{g[x]+=min(g[a[i].y],min(h[a[i].y],f[a[i].y])+a[i].z);h[x]=min(h[x],h[a[i].y]+g[x]−min(g[a[i].y],min(h[a[i].y],f[a[i].y])+a[i].z));
如果该点是白色的:
有
{
f
[
x
]
+
=
m
i
n
(
f
[
a
[
i
]
.
y
]
,
a
[
i
]
.
z
+
m
i
n
(
g
[
a
[
i
]
.
y
]
,
h
[
a
[
i
]
.
y
]
)
)
;
h
[
x
]
+
=
m
i
n
(
g
[
a
[
i
]
.
y
]
,
a
[
i
]
.
z
+
m
i
n
(
f
[
a
[
i
]
.
y
]
,
h
[
a
[
i
]
.
y
]
)
)
;
\left\{\begin{matrix} f[x]+=min(f[a[i].y],a[i].z+min(g[a[i].y],h[a[i].y])); \\ h[x]+=min(g[a[i].y],a[i].z+min(f[a[i].y],h[a[i].y])); \end{matrix}\right.
{f[x]+=min(f[a[i].y],a[i].z+min(g[a[i].y],h[a[i].y]));h[x]+=min(g[a[i].y],a[i].z+min(f[a[i].y],h[a[i].y]));
如果该点是灰色的:
有
{
f
[
x
]
+
=
m
i
n
(
f
[
a
[
i
]
.
y
]
,
a
[
i
]
.
z
+
m
i
n
(
g
[
a
[
i
]
.
y
]
,
h
[
a
[
i
]
.
y
]
)
)
;
g
[
x
]
+
=
m
i
n
(
g
[
a
[
i
]
.
y
]
,
a
[
i
]
.
z
+
m
i
n
(
f
[
a
[
i
]
.
y
]
,
h
[
a
[
i
]
.
y
]
)
)
;
h
[
x
]
=
m
i
n
(
h
[
x
]
,
h
[
a
[
i
]
.
y
]
+
g
[
x
]
−
m
i
n
(
g
[
a
[
i
]
.
y
]
,
a
[
i
]
.
z
+
m
i
n
(
f
[
a
[
i
]
.
y
]
,
h
[
a
[
i
]
.
y
]
)
)
)
;
\left\{\begin{matrix} f[x]+=min(f[a[i].y],a[i].z+min(g[a[i].y],h[a[i].y])); \\ g[x]+=min(g[a[i].y],a[i].z+min(f[a[i].y],h[a[i].y])); \\ h[x]=min(h[x],h[a[i].y]+g[x]-min(g[a[i].y],a[i].z+min(f[a[i].y],h[a[i].y]))); \end{matrix}\right.
⎩⎨⎧f[x]+=min(f[a[i].y],a[i].z+min(g[a[i].y],h[a[i].y]));g[x]+=min(g[a[i].y],a[i].z+min(f[a[i].y],h[a[i].y]));h[x]=min(h[x],h[a[i].y]+g[x]−min(g[a[i].y],a[i].z+min(f[a[i].y],h[a[i].y])));
代码
#include<cstdio>
#include<string>
#include<cstring>
#define rr register
#define cle(x) memset(x,0,sizeof(x));
#define min(a,b) (a<b?a:b)
using namespace std;
const int N=300005;
long long f[N],g[N],h[N];
int n,T,b[N],tot,head[N],q[N],fa[N];
struct node{int y,z,next;}a[N<<1];
inline int read(){
int p=0,f=1; char c=getchar();
while(!isdigit(c)) {if (c=='-') f=-1;c=getchar();}
while(isdigit(c)) p=(p*10)+c-48,c=getchar();
return p*f;
}
void add(int x,int y,int z){
a[++tot]=(node){y,z,head[x]}; head[x]=tot;
a[++tot]=(node){x,z,head[y]}; head[y]=tot;
}
inline void dfs(int s){
rr int l=0,r=1,x,i; q[1]=s;fa[s]=0;
while(l<r){
x=q[++l];
for(i=head[x];i;i=a[i].next)
if(a[i].y!=fa[x]) fa[a[i].y]=x,q[++r]=a[i].y;
}
}
int main(){
T=read();
while (T--){
cle(head);cle(fa);cle(a);tot=0;
n=read(); int x,y,z;
for(rr int i=1;i<=n;i++) b[i]=read();
for(rr int i=1;i<n;i++) x=read(),y=read(),z=read(),add(x,y,z);
dfs(1);
for(rr int j=n;j>=1;j--){
x=q[j];
if(!b[x]){
f[x]=h[x]=1e18; g[x]=0;
for(rr int i=head[x];i;i=a[i].next)
if(a[i].y!=fa[x])
g[x]+=min(g[a[i].y],min(h[a[i].y],f[a[i].y])+a[i].z);
for(rr int i=head[x];i;i=a[i].next)
if(a[i].y!=fa[x])
h[x]=min(h[x],h[a[i].y]+g[x]-min(g[a[i].y],min(h[a[i].y],f[a[i].y])+a[i].z));
} else if(b[x]==1){
g[x]=1e18; f[x]=h[x]=0;
for(rr int i=head[x];i;i=a[i].next)
if(a[i].y!=fa[x]){
f[x]+=min(f[a[i].y],a[i].z+min(g[a[i].y],h[a[i].y]));
h[x]+=min(g[a[i].y],a[i].z+min(f[a[i].y],h[a[i].y]));
}
} else {
h[x]=1e18; f[x]=g[x]=0;
for(rr int i=head[x];i;i=a[i].next)
if(a[i].y!=fa[x]) {
f[x]+=min(f[a[i].y],a[i].z+min(g[a[i].y],h[a[i].y]));
g[x]+=min(g[a[i].y],a[i].z+min(f[a[i].y],h[a[i].y]));
}
for(rr int i=head[x];i;i=a[i].next)
if(a[i].y!=fa[x])
h[x]=min(h[x],h[a[i].y]+g[x]-min(g[a[i].y],a[i].z+min(f[a[i].y],h[a[i].y])));
}
}
printf("%lld\n",min(f[1],min(h[1],g[1])));
}
}