我今天上午做了这道题,很久之前就想做了,但是题面劝退我,之后才知道有个消防的题和本题一样,就去翻了消防的题面来做,码完代码测了样例,都过了,然后看消防一题的讨论里面有wqy大佬留下的hack数据,自测了一下结果不对,但还是在本题里交了,结果A了?然后去交消防,应该AC的<=300的两个点都WA掉了。。说明代码不对,求指正emmm
#include<iostream>
#include<cstring>
#include<cstdio>
#define N 301
const int inf=0x7fffffff;
using namespace std;
int head[N],cnt,maxdis,point,start,end;
int ans[N],n,s,allans=inf;
bool dian[N];
struct Edge{
int next,to,worth;
}e[N*2];
void add(int from,int to,int worth)
{
e[++cnt].next=head[from];
e[cnt].to=to;
head[from]=cnt;
e[cnt].worth=worth;
}
void dfs(int x,int fa,int dis)
{
point=x;
for(int i=head[x];i;i=e[i].next)
{
int to=e[i].to;
if(to==fa) continue;
if(dis+e[i].worth<=s) dfs(to,x,dis+e[i].worth);
else if(maxdis<dis) maxdis=dis,point=x;
}
}
bool dfs_mark(int x,int fa)
{
if(x==point)
{
dian[x]=true;
return true;
}
for(int i=head[x];i;i=e[i].next)
{
int to=e[i].to;
if(to==fa) continue;
if(dfs_mark(to,x))
{
dian[x]=true;
return true;
}
}
return false;
}
void dfs_dis(int x,int fa)
{
for(int i=head[x];i;i=e[i].next)
{
int to=e[i].to;
if(to==fa) continue;
if(dian[to]) continue;
// if(!e[i].cancel)
ans[to]=ans[x]+e[i].worth;
dfs_dis(to,x);
}
}
int main()
{
scanf("%d %d",&n,&s);
for(int i=1;i<n;i++)
{
int x,y,z;
scanf("%d %d %d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
for(int i=1;i<=n;i++)
{
point=0;
maxdis=0;
dfs(i,0,0);
memset(dian,0,sizeof(dian));
dfs_mark(i,0);
// cout<<point<<endl;
// for(int j=1;j<=n*2;j++) if(e[j].worth==2) cout<<e[j].cancel<<" ";
// cout<<endl;
memset(ans,0,sizeof(ans));
for(int j=1;j<=n;j++)
{
if(!dian[j]) continue;
dfs_dis(j,0);
}
// for(int j=1;j<=n;j++) cout<<ans[j]<<" ";
// cout<<endl;
int maxans=0;
for(int j=1;j<=n;j++) if(ans[j]>maxans) maxans=ans[j];
allans=min(maxans,allans);
}
printf("%d",allans);
return 0;
}