[CF1844G] Tree Weights
题目描述
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的更多相关文章
- Weights Assignment For Tree Edges
题目: (我的题目很长,你忍一下--) 题目分析: 这道题目的体面比较复杂,先是讲了一下树是怎样的一个结构,并且告诉我们在这里,他是以什么样的一种方式描述一棵树的,就是通过描述每个节点的父节点是哪个( ...
- POJ3013 Big Christmas Tree[转换 最短路]
Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 23387 Accepted: 5 ...
- ZOJ 3201 Tree of Tree
树形DP.... Tree of Tree Time Limit: 1 Second Memory Limit: 32768 KB You're given a tree with weig ...
- 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 ...
- 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 ...
- 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 ...
- HDU 5416 CRB and Tree(前缀思想+DFS)
CRB and Tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 形象谈JVM-第四章-JVM内存结构
给我一个CPU,给我一块内存,我来执行一段代码. 我要如何分配呢? new User(); 这里有一个有一个User类,如果我要new出来User对象,必须先知道它长什么样子,我先搞一块区域出来,把U ...
- 手把手教你使用Vite构建第一个Vue3项目
写在前面 在之前的文章中写过"如何创建第一个vue项目",但那篇文章写的是创建vue2的 项目. 传送门如何创建第一个vue项目 打开Vue.js官网:https://cn.vue ...
- 解决SVN死锁问题
svn执行clean up后出现提示:svn cleanup failed–previous operation has not finished; run cleanup if it was int ...
- 「shoi 2012」随机数
link. 对于 pass 1, 你把他考虑成 \(\frac{\sum x}{i}\) 的形式, 于是每次操作的贡献就是 \(\frac{2}{i}\), 那么答案就是 \(\sum_{i=2}^n ...
- Docker Swarm + Harbor + Portainer 打造高可用,高伸缩,集群自动化部署,更新。
Docker Swarm是Docker官方自带的容器编排工具,Swarm,Compose,Machine合称Docker三剑客.Docker Swarm对于中小型应用来说,还是比较方便,灵活,当然K8 ...
- Vue2系列(lqz)——6-Vue-cli、7-Vue插件、8-Vue第三方框架之ElementUi
文章目录 6 Vue-CLI 项目搭建 1 单文件组件 2 Vue-CLI 项目搭建 2.1 环境搭建 2.2 项目的创建 创建项目 启动/停止项目 打包项目 package.json中 2.3 认识 ...
- 一场3天前的cf
啊 这次的cf其实水的(指前4题) 题面就不给了awaT1其实就是一个贪心,其实手模一下就好了.可以发现,先让小的那个变大,然后在后面一直让小的加上大的统计一下次数就是答案了.因为如果是这样算的话,两 ...
- 图形学、02 推导证明 | 任意一点经过透视投影后 z 坐标相对于之前有什么变化
齐次坐标知识点: \(\begin{bmatrix} x \\ y \\ z \\ 1 \\\end{bmatrix} \Rightarrow\begin{bmatrix} nx \\ ny \\ n ...
- MPL协议原文
MPL协议原文 原文参考链接 中文翻译有一个,但是只翻译了两条 原文 Mozilla Public License Version 2.0 1. Definitions 1.1. "Cont ...
- 【前端开发】基于vue+elemnt-ui流程图设计器解决方案
前言 越来越多的企业都在研发低代码平台,其中流程引擎是核心之一,拥有一个可以拖拽设计审批流程的设计器是相当重要的. 介绍 审批流程设计器是一种工具,用于创建和设计审批流程.它通常是一个可视化的设计器界 ...