【60分】

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=1e5+;
const int maxm=maxn;
const ll mod=1e18;
int fa[maxn];
ll dep[maxn];
ll dp[maxn];
struct node{
ll v;
ll f;
}g[maxn];
struct edge{
int to;
int nxt;
ll w;
}e[*maxm];
int tot;
int n; int head[maxn];
void init(){
memset(head,-,sizeof(head));
tot=;
memset(dp,-,sizeof(dp));
memset(dep,,sizeof(dep));
}
void add(int u,int v,ll w){
e[tot].to=v;
e[tot].w=w;
e[tot].nxt=head[u];
head[u]=tot++;
}
ll dis(int u,int v){
ll tmp=g[u].f-(dep[v]-dep[u]);
tmp=tmp*tmp;
tmp=g[u].v-tmp;
return tmp;
}
void update(int son,int u){
if(u==) return;
dp[u]=max(dp[u],dp[son]+dis(u,son));
update(son,fa[u]);
}
void dfs(int u){
for(int i=head[u];i!=-;i=e[i].nxt){
int v=e[i].to;
if(v<u) continue;
dfs(v);
}
if(dp[u]==-) dp[u]=;
update(u,fa[u]);
}
void getdepth(int u){
for(int i=head[u];i!=-;i=e[i].nxt){
int v=e[i].to;
ll w=e[i].w;
if(v<u) continue;
dep[v]=dep[u]+w;
getdepth(v);
}
}
ll work(){
getdepth();
dfs();
ll ans=;
for(int i=;i<=n;i++){
ans=(ans+dp[i])%mod;
}
return ans;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
init();
scanf("%d",&n);
int u;
ll d,v,f;
for(int i=;i<=n;i++){
scanf("%d%lld%lld%lld",&u,&d,&v,&f);
g[i].v=v;
g[i].f=f;
add(u,i,d);
add(i,u,d);
fa[i]=u;
}
ll ans=work();
printf("%lld\n",ans);
}
return ;
}

最新文章

  1. 在计算机 . 上没有找到服务 WAS
  2. css中filter:alpha透明度使用
  3. sql和access中截取字符串的区别
  4. php solr 查询
  5. BZOJ 2768: [JLOI2010]冠军调查 最小割
  6. 212. Word Search II
  7. CFUUIDRef和CFStringRef-生成唯一标识符
  8. ARM编译空间属性(转)
  9. JS时间戳与日期类型格式相互转换
  10. 20181218 - PostgreSQL Auto Commit Guide(自动提交)
  11. 【Golang】如何统一处理HTTP请求中的异常捕获
  12. ‘params’一个奇妙的东西
  13. LeetCode Weekly Contest 32
  14. ngxinx 配置
  15. IE 8 浏览器 F12 调试功能无法使用
  16. Ubuntu Remove Mysql.service in Systemctl
  17. 关于RSA加密算法的工具类
  18. 【总结】前端框架:react还是vue?
  19. [转]激活函数ReLU、Leaky ReLU、PReLU和RReLU
  20. Mybatis的mapper代理开发方法

热门文章

  1. 2018.2.09 php学习(二)
  2. volatile引发的一系列血案
  3. apropos linux
  4. cocos2dx for lua A*寻路算法实现2
  5. 括号匹配性检测C语言实现
  6. ARC中__weak;__strong;__unsafe_unretained;修饰词
  7. 前端应该如何去认识http
  8. C++ 学习笔记(三)string 类
  9. knn算法之预测数字
  10. 【整理】虚拟机和主机ping不通解决办法,虚拟机ping不通外网的解决方法