1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int point[303],next[603],v[603],c[603],cnt=0,N,M,K,f[303][303][2]; 6 bool p[303]; 7 void insect(int x,int y,int z){next[cnt]=point[x];point[x]=cnt;v[cnt]=y;c[cnt]=z;cnt++;} 8 void dfs(int x) 9 { 10 int i,j; 11 for (i=point[x];i!=-1;i=next[i]) 12 { 13 if (l[x]==0) l[x]=v[i],p[v[i]]=1; else 14 { 15 j=l[x]; 16 while (r[j]!=0) j=r[j]; 17 r[j]=v[i]; p[v[i]]=1; 18 } 19 } 20 i=l[x]; 21 while (i!=0) dfs(i),i=r[i]; 22 } 23 void 24 int main() 25 { 26 memset(point,-1,sizeof(point)); 27 memset(next,-1,sizeof(next)); 28 memset(v,0,sizeof(v)); 29 memset(c,0,sizeof(c)); 30 memset(p,0,sizeof(p)); 31 scanf("%d %d %d\n",&N,&M,&K); 32 if (K+M-1>N) {printf("-1\n");return 0;} 33 int i,a,b,c; 34 for (i=1;i<N;++i) 35 { 36 scanf("%d %d %d\n",&a,&b,&c); 37 insect(a,b,c);insect(b,a,c); 38 }p[1]=1; dfs(1); 39 printf("%d\n",dp(1,K,1)); 40 }
时间: 2024-10-11 23:07:33