标签:正整数 script rom main 应该 name class style col
(其实换根也是可做滴!!)
1 #include<cstdio> 2 #include<iostream> 3 using namespace std; 4 const int inf=1000000000; 5 int n,m,c[100010],f[100010][2],to[200010],from[200010],head[100010],cnt; 6 void insert(int x,int y) { to[++cnt]=y; from[cnt]=head[x]; head[x]=cnt; } 7 void dp(int root,int fa) 8 { 9 f[root][0]=f[root][1]=1; 10 if (root<=m) if (c[root]) f[root][0]=inf; else f[root][1]=inf; 11 for (int i=head[root];i;i=from[i]) 12 if (to[i]!=fa) 13 { 14 dp(to[i],root); 15 f[root][0]+=min(f[to[i]][0]-1,f[to[i]][1]); 16 f[root][1]+=min(f[to[i]][1]-1,f[to[i]][0]); 17 } 18 } 19 int main() 20 { 21 scanf("%d%d",&n,&m); 22 for (int i=1;i<=m;i++) scanf("%d",&c[i]); 23 for (int i=1;i<=n-1;i++) 24 { 25 int x,y; 26 scanf("%d%d",&x,&y); 27 insert(x,y); 28 insert(y,x); 29 } 30 dp(m+1,-1); 31 printf("%d",min(f[m+1][1],f[m+1][0])); 32 return 0; 33 }
标签:正整数 script rom main 应该 name class style col
原文地址:https://www.cnblogs.com/Comfortable/p/9277406.html