[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 ...
随机推荐
- java与es8实战之三:Java API Client有关的知识点串讲
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本篇是<java与es8实战>系 ...
- 《SQL与数据库基础》11. 索引
目录 索引 概述 结构 B-Tree B+Tree Hash 思考 分类 语法 SQL性能分析 SQL执行频率 慢查询日志 profile详情 explain执行计划 索引失效情况 范围查询 索引列运 ...
- 一个.NET 7 + DDD + CQRS +React+Vite的实战项目
项目简介 基于SignalR实现聊天通信,支持横向扩展,可支撑上万用户同时在线聊天 后端架构 后端技术栈采用 .NET 7 PostgreSQL (业务数据库) Redis(用于存放热点数据,和支持S ...
- word2010中统一调整表格格式
word中统一调整表格格式基本思路是: 1.选中所有的表格. 2.再对表格格式调整. 选中所有表格需要用到宏,操作很简单,具体操作如下: (1)工具栏"视图"下右下角&quo ...
- 如何解决IOS 15提示“此App的开发者需要更新APP以在此IOS版本上正常工作”, 无法打开安装的APP的问题
在苹果手机最新的IOS 15 beta的系统上安装自签名或者企业签名的APP时,可能会遇到如下的错误提示: 此App的开发者需要更新APP以在此IOS版本上正常工作 The developer of ...
- nginx配置kibana访问用户名和密码认证、及无认证访问配置
转载请注明出处: 在nginx上配置kibana页面访问时,默认是采用kibana的认证,一般直接安装kibana后,是没有用户名和密码认证的. 如果要在负载均衡上配置反向代理和用户认证,可按以下步骤 ...
- destoon关于archiver归档的性能优化
今天在处理一个项目时候发现archiver单个模块归档超过百万数据,打开速度就特慢,所以打开archiver下index.php文件进行分析,发现有句sql作怪 1 $result = $db-> ...
- 传统的MVC开发模式和前后端分离开发模式
1. 引言 在当今互联网时代,移动应用和网页应用的发展极大地推动了前后端分离开发模式的兴起.传统的后端渲染方式已经不能满足用户对高性能和优质用户体验的需求,于是前后端分离逐渐成为了一种主流的开发模式. ...
- c语言代码练习--函数
函数: 一,概念: 1,在计算科学中,子程序(英语:Subroutione,procedure,function,rotine,method.subprogram,callable unit),是一个 ...
- FFMPEG+SDL简单视频播放器——视频快进
之前写过一篇关于视频播放器的文章.播放器只简单实现了视频播放的功能,在此功能的基础上,给它加上一个视频快进的功能. 实现 添加参数 // video play control bool do_seek ...