题目描述

You are given a tree with $ n $ nodes labelled $ 1,2,\dots,n $ . The $ i $ -th edge connects nodes $ u_i $ and $ v_i $ and has an unknown positive integer weight $ w_i $ . To help you figure out these weights, you are also given the distance $ d_i $ between the nodes $ i $ and $ i+1 $ for all $ 1 \le i \le n-1 $ (the sum of the weights of the edges on the simple path between the nodes $ i $ and $ i+1 $ in the tree).

Find the weight of each edge. If there are multiple solutions, print any of them. If there are no weights $ w_i $ consistent with the information, print a single integer $ -1 $ .

输入格式

The first line contains a single integer $ n $ ( $ 2 \le n \le 10^5 $ ).

The $ i $ -th of the next $ n-1 $ lines contains two integers $ u_i $ and $ v_i $ ( $ 1 \le u_i,v_i \le n $ , $ u_i \ne v_i $ ).

The last line contains $ n-1 $ integers $ d_1,\dots,d_{n-1} $ ( $ 1 \le d_i \le 10^{12} $ ).

It is guaranteed that the given edges form a tree.

有 \(n-1\) 条边, \(n-1\) 个等式,理论上是可以暴力解方程的。复杂度 \(O(n^3)\)

钦定 1 为根的话,考虑从 \(1\) 到点 \(x\) 的距离入手,设为 \(a_x\)。

方程形如 \(a_i+a_{i+1}-2a_d=s_{i}\),发现 \(a_d\) 很烦,想办法把他消掉。

由于 \(a_1\) 一定为 \(0\),所以将整个方程 \(\mod 2\) 可以得到 \(a_i\mod 2\) 的值。

然后发现 \(a_i mod 4=s_{i-1}-a_{i-1}+2(a_d\mod 2)\),一次类推,就可以得出 \(a_i\) 的值。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL INF=1e18;
const int N=1e5+5;;
int n,fa[N][20],hd[N],u[N],v[N],dep[N],e_num,lc[N];
struct edge{
int v,nxt;
}e[N<<1];
void add_edge(int u,int v)
{
e[++e_num]=(edge){v,hd[u]};
hd[u]=e_num;
}
LL d[N],ans[N],ls[N];
LL read()
{
LL s=0;
char ch=getchar();
while(ch<'0'||ch>'9')
ch=getchar();
while(ch>='0'&&ch<='9')
s=s*10+ch-48,ch=getchar();
return s;
}
void dfs(int x,int y)
{
dep[x]=dep[y]+1;
fa[x][0]=y;
for(int i=1;i<=18;i++)
fa[x][i]=fa[fa[x][i-1]][i-1];
for(int i=hd[x];i;i=e[i].nxt)
if(e[i].v^y)
dfs(e[i].v,x);
}
int lca(int x,int y)
{
if(dep[x]<dep[y])
swap(x,y);
for(int i=18;~i;i--)
if(dep[fa[x][i]]>=dep[y])
x=fa[x][i];
if(x==y)
return x;
for(int i=18;~i;--i)
if(fa[x][i]^fa[y][i])
x=fa[x][i],y=fa[y][i];
return fa[x][0];
}
int main()
{
n=read();
for(int i=1;i<n;i++)
add_edge(u[i]=read(),v[i]=read()),add_edge(v[i],u[i]);
for(int i=2;i<=n;i++)
d[i]=read();
dfs(1,0);
for(int i=2;i<=n;i++)
lc[i]=lca(i,i-1);
for(LL i=2;i<=INF;i<<=1)
{
for(int j=2;j<=n;j++)
ans[j]=(d[j]-ans[j-1]+2*ls[lc[j]]+i)%i;
memcpy(ls,ans,sizeof(ls));
}
for(int i=2;i<=n;i++)
if(ans[fa[i][0]]>=ans[i]||ans[i]<0||ans[i]+ans[i-1]-2*ans[lc[i]]^d[i])
return puts("-1"),0;
for(int i=1;i<n;i++)
printf("%lld\n",abs(ans[u[i]]-ans[v[i]]));
}

[CF1844G] Tree Weights的更多相关文章

  1. Weights Assignment For Tree Edges

    题目: (我的题目很长,你忍一下--) 题目分析: 这道题目的体面比较复杂,先是讲了一下树是怎样的一个结构,并且告诉我们在这里,他是以什么样的一种方式描述一棵树的,就是通过描述每个节点的父节点是哪个( ...

  2. POJ3013 Big Christmas Tree[转换 最短路]

    Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 23387   Accepted: 5 ...

  3. ZOJ 3201 Tree of Tree

    树形DP.... Tree of Tree Time Limit: 1 Second      Memory Limit: 32768 KB You're given a tree with weig ...

  4. Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA/(树链剖分+数据结构) + MST

    E. Minimum spanning tree for each edge   Connected undirected weighted graph without self-loops and ...

  5. Codeforces Edu3 E. Minimum spanning tree for each edge

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  6. CF# Educational Codeforces Round 3 E. Minimum spanning tree for each edge

    E. Minimum spanning tree for each edge time limit per test 2 seconds memory limit per test 256 megab ...

  7. HDU 5416 CRB and Tree(前缀思想+DFS)

    CRB and Tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  8. HDU 5002 Tree(动态树LCT)(2014 ACM/ICPC Asia Regional Anshan Online)

    Problem Description You are given a tree with N nodes which are numbered by integers 1..N. Each node ...

  9. A Complete Tutorial on Tree Based Modeling from Scratch (in R & Python)

    A Complete Tutorial on Tree Based Modeling from Scratch (in R & Python) MACHINE LEARNING PYTHON  ...

  10. Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA链上最大值

    E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...

随机推荐

  1. 9.1 C++ STL 排序、算数与集合

    C++ STL(Standard Template Library)是C++标准库中的一个重要组成部分,提供了丰富的模板函数和容器,用于处理各种数据结构和算法.在STL中,排序.算数和集合算法是常用的 ...

  2. MyBatis-Plus和PageHelper冲突导致Factory method sqlSessionFactory threw exception,并且如何分页显示全部

    springboot开始引入了mybaits-plus.后来想引入pagehelper进行分页,引入之后报错 Error starting ApplicationContext. To display ...

  3. 【火坑】一切从TimeSpan说起

    小编在编写WPF程序时,需要做一个判断:定时使用Modbus协议使用Quartz.net 定时任务读取设备中的数据,同时也使用定时任务判断是否长时间获取不到数据的情况,如果程序中超过一分钟没有获取到数 ...

  4. 万字长文教你实现华为云IoT+OpenHarmony智能家居开发

    本文分享自华为云社区<华为云IoT+OpenHarmony的智能家居开发>,作者:袁睿. 一.选题说明 1. 选题为基于OpenHarmony的智能家居,应用场景为户用,受益人群为住户. ...

  5. Vue源码学习(七):合并生命周期(混入Vue.Mixin)

    好家伙,   1.使用场景 现在来,来想一下,作为一个使用Vue的开发者,假设现在我们要使用created(),我们会如何使用 1.1.  .vue文件中使用 <template> < ...

  6. Teamcenter RAC 开发之《AbstractRendering》

    背景 关于Teamcenter RAC 客制化渲染表单,做一两个有时间做还是可以的,问题是大批量做的时候就会存在很多重复的代码 例如: 1.定义很多 TCProperty,JTextFiled,ite ...

  7. Linux平台Oracle 23c单实例 安装部署配置 快速参考

    转眼间已经2023年,再有一周就要过年了,在这里先给大家拜个早年,祝大家新的一年万事顺利. Oracle如今版本号也和年份挂钩,在前段时间的OCW上也宣布发布了beta版本的23c,因为23c是继19 ...

  8. MySQL——MySQL面试题

    文章目录 数据库基础知识 为什么要使用数据库 什么是SQL? 什么是MySQL? 数据库三大范式是什么 mysql有关权限的表都有哪几个 MySQL的binlog有有几种录入格式?分别有什么区别? 数 ...

  9. 如何优雅重启 kubernetes 的 Pod

    最近在升级服务网格 Istio,升级后有个必要的流程就是需要重启数据面的所有的 Pod,也就是业务的 Pod,这样才能将这些 Pod 的 sidecar 更新为新版本. 方案 1 因为我们不同环境的 ...

  10. webpack配置打包

    一.webpack基本安装 1.创建webpack项目目录如webpackDemo,并进入webpackDemo; 2. 在node已经安装的前提下,打开命令行控制器,输入如下命令: npm init ...