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
| #include<algorithm> #include<bitset> #include<complex> #include<deque> #include<exception> #include<fstream> #include<functional> #include<iomanip> #include<ios> #include<iosfwd> #include<iostream> #include<istream> #include<iterator> #include<limits> #include<list> #include<locale> #include<map> #include<memory> #include<new> #include<numeric> #include<ostream> #include<queue> #include<set> #include<sstream> #include<stack> #include<stdexcept> #include<streambuf> #include<string> #include<typeinfo> #include<utility> #include<valarray> #include<vector> #include<cctype> #include<cerrno> #include<cfloat> #include<ciso646> #include<climits> #include<clocale> #include<cmath> #include<csetjmp> #include<csignal> #include<cstdarg> #include<cstddef> #include<cstdio> #include<cstdlib> #include<cstring> #include<ctime> #define ll long long using namespace std; inline ll read(){ ll res=0,f=1;char ch=getchar(); while(ch<'0'ch>'9'){if(ch=='-') f=-1;ch=getchar();} while(ch>='0'&&ch<='9') res=res*10+ch-'0',ch=getchar(); return res*f; } inline void write(ll x){ if(x<0) putchar('-'),x=-x; if(x<10) putchar(x+'0'); else{ write(x/10); putchar(x%10+'0'); } } ll m,n,c[10010],in[10010],rt,f[10010][5]; vector<ll> v[10010]; void TreeDp(ll x,ll fa){ if(x<=n){ if(c[x]) f[x][1]=1,f[x][0]=2e9,f[x][2]=1; else f[x][0]=1,f[x][1]=2e9,f[x][2]=1; }else f[x][1]=1,f[x][0]=1; for(ll i=0;i<v[x].size();i++){ ll to=v[x][i]; if(to==fa) continue ; TreeDp(to,x); f[x][0]+=min(f[to][0]-1,min(f[to][1],f[to][2])); f[x][1]+=min(f[to][0],min(f[to][1]-1,f[to][2])); f[x][2]+=min(f[to][0],min(f[to][1],f[to][2])); } } int main(){ m=read();n=read(); for(ll i=1;i<=n;i++) c[i]=read(); for(ll i=1;i<=m-1;i++){ ll x=read(),y=read(); v[x].push_back(y); v[y].push_back(x); in[x]++;in[y]++; if(rt==0){ if(in[x]>1) rt=x; else if(in[y]>1) rt=y; } } TreeDp(rt,0); write(min(f[rt][0],f[rt][1]));putchar('\n'); return 0; }
|