|
|
вернуться в форумAC coad .wa many times only because the input do not say which node is father #include<iostream> using namespace std; const int maxn=105; int count[maxn]; int g[maxn][maxn]; int n;int q; int cost[maxn][maxn]; int f[maxn][maxn]; int visit[maxn]; void init( ) { cin>>n>>q; for(int i=1;i<n;i++) { int a,b,c; scanf("%d %d %d",&a,&b,&c); g[a][++count[a]]=b; cost[a][b]=c; g[b][++count[b]]=a; cost[b][a]=c; } //memset(f,-10000,sizeof(f)); } void dfs(int x) { visit[x]=1; if(count[x]==0) {
return ; } for(int i=1;i<=count[x];i++) { int child=g[x][i]; if(visit[child]==0){ dfs(child); for(int j=q;j>=0;j--) for(int k=q-j-1;k>=0;k--) { f[x][k+j+1]=max(f[child][k]+f[x][j]+cost[x][child],f[x][k+j+1]);
} } } } void print( ) { cout<<f[1][q]<<endl; } int main( ) { freopen("in.txt","r",stdin); init( ); dfs(1); print( ); return 0; } Re: AC coad .wa many times only because the input do not say which node is father Please explain your solution. Thanks |
|
|