Qiuly @ 2019-05-11 16:15:37
哪里写错了TAT,为什么会爆炸啊。
第一个样例过了,链的数据就变成负数了,loj上的大样例已经死掉了。
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N=3e5+2;
const ll inf=1e18+9;
int n,k,head[N],cnt;
struct date {
ll x;int y;
bool operator < (const date&var) const {return x==var.x?y>var.y:x<var.x;}
date operator + (const date&var) {return (date){x+var.x,y+var.y};}
date operator + (const ll&var) {return (date){x+var,y};}
date operator + (const int&var) {return (date){x+var,y};}
}dp[3][N];
ll number(date var) {return var.x;}
struct Edge{int nxt,to,val;} G[N<<1];
void add(int u,int v,int w) {
G[++cnt]=(Edge){head[u],v,w},head[u]=cnt;
G[++cnt]=(Edge){head[v],u,w},head[v]=cnt;
}
template <typename _Tp> inline void IN(_Tp&x) {
char ch;bool flag=0;x=0;
while(ch=getchar(),!isdigit(ch)) if(ch=='-') flag=1;
while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
if(flag) x=-x;
}
void check(ll add,int u,int fa) {
dp[0][u]=(date){0,0},
dp[1][u]=(date){-inf,0},
dp[2][u]=(date){add,1};
for(int e=head[u];e;e=G[e].nxt) {
int v=G[e].to,w=G[e].val;
if(v==fa) continue;
check(add,v,u);
date num=max(dp[0][v],max(dp[1][v],dp[2][v]));
dp[2][u]=max(dp[2][u],dp[2][u]+num);
dp[2][u]=max(dp[2][u],dp[1][u]+w+max(dp[0][v],dp[1][v]+(date){-add,-1}));
dp[1][u]=max(dp[1][u],dp[1][u]+num);
dp[1][u]=max(dp[1][u],dp[0][u]+w+max(dp[1][v],dp[0][v]+(date){add,1}));
dp[0][u]=dp[0][u]+num;
}return;
}
ll wqs(ll sum) {
ll l=-sum,r=sum,mid;
date now;
while(l<r) {
mid=(l+r)>>1,check(mid,1,0);
now=max(dp[0][1],max(dp[1][1],dp[2][1]));
if(!(now.y^k)) break;
now.y<k?l=mid:r=mid;
}
check(mid,1,0);
now=max(dp[0][1],max(dp[1][1],dp[2][1]));
return now.x-mid*k;
}
int main() {
freopen("P4383.in","r",stdin);
freopen("P4383.out","w",stdout);
IN(n),IN(k);++k;
ll sum=0;
for(int i=1;i<n;++i) {
int x,y,v;IN(x),IN(y),IN(v);
add(x,y,v),sum+=abs(v);
}
printf("%lld\n",wqs(sum));
return 0;
}
by Luban @ 2019-05-11 16:21:54
by Luban @ 2019-05-11 16:22:40
by Qiuly @ 2019-05-11 16:22:53
...