hdu5044 Tree 上海网络赛1003 LCA+输入输出外挂

HUMAnN数据库国内备份下载源(转自刘永鑫博士github) 百度云链接 R语言常用包合集 Windows 10版4.0.x:链接:https://pan.baidu.com/s/1JFwLP2SonK1howD5MNgNYg 提取码:l08k Windows 10版3.6.x:链接:https://pan.baidu.com/s/1h-qR2lmlaMgR4M7VbgtAFg 提取码:s5fi 下载后解压至当前目录,可获得3.6/4.0目录,剪切并替换文档 - R - win-library - 3.6/4.0 目录即可 扩增子数据库 宏基因组数据库 HUM. 阅读详情

Tree

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 668    Accepted Submission(s): 141


Problem Description
You are given a tree (an acyclic undirected connected graph) with N nodes. The tree nodes are numbered from 1 to N

There are N - 1 edges numbered from 1 to N - 1.

Each node has a value and each edge has a value. The initial value is 0.

There are two kind of operation as follows:

● ADD1 u v k: for nodes on the path from u to v, the value of these nodes increase by k.

● ADD2 u v k: for edges on the path from u to v, the value of these edges increase by k.

After finished M operation on the tree, please output the value of each node and edge.
 

Input
The first line of the input is T (1 ≤ T ≤ 20), which stands for the number of test cases you need to solve.

The first line of each case contains two integers N ,M (1 ≤ N, M ≤105),denoting the number of nodes and operations, respectively.

The next N - 1 lines, each lines contains two integers u, v(1 ≤ u, v ≤ N ), denote there is an edge between u,v and its initial value is 0.

For the next M line, contain instructions “ADD1 u v k” or “ADD2 u v k”. (1 ≤ u, v ≤ N, -105 ≤ k ≤ 105)
 

Output
For each test case, print a line “Case #t:”(without quotes, t means the index of the test case) at the beginning.

The second line contains N integer which means the value of each node.

The third line contains N - 1 integer which means the value of each edge according to the input order.
 

Sample Input
2 4 2 1 2 2 3 2 4 ADD1 1 4 1 ADD2 3 4 2 4 2 1 2 2 3 1 4 ADD1 1 4 5 ADD2 3 2 4
 

Sample Output
Case #1: 1 1 0 1 0 2 2 Case #2: 5 0 0 5 0 4 0

  之前也就做了一道类似LCA的题,对LCA还不是很熟悉,比赛的时候感觉是LCA但是不会写。

  看了题解,仔细想想这题其实真不难。。就是记录每个操作,最后从叶子到根网上遍历一遍,每次把要修改的值传上去就行了。。

  对点u,v加w就是:add1[u]+=w,add1[v]+=w,x=lca(u,v),add[x]-=w(因为传上来两个w),因为到了x之后不能再往上传,设sub[x]+=x,每个点的add值减去sub值就是要往上传的值。

  设add2[u]为u和fa[u]的边要增加的值,那么对边操作add2[u]+=w,add2[v]+=w,add2[x]-=2*w。

  因为这题要按输入的顺序输出边的权值,那么还要记录u和u的父节点之间的边的编号。

  注意要输入输出外挂才能过。。。。。。。

#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<set>
#include<map>
#include<stack>
#define INF 0x3f3f3f3f
#define MAXN 100010
#define LOGMAXN 20
#define MAXM 1500
#define MOD 1000000007
#define MAXNODE 8*MAXN
#define eps 1e-10
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
int T,N,M,L[MAXN],anc[MAXN][LOGMAXN],fa[MAXN],e[MAXN],deg[MAXN];
LL add1[MAXN],add2[MAXN],sub[MAXN],node[MAXN],edge[MAXN];
char str[10];
struct Edge{
    int to,id;
    Edge(){}
    Edge(int to,int id):to(to),id(id){}
};
vector<Edge> G[MAXN];
void DFS(int u,int f,int level){
    L[u]=level;
    int len=G[u].size();
    for(int i=0;i<len;i++){
        int v=G[u][i].to;
        if(v!=f){
            deg[u]++;
            fa[v]=u;
            e[v]=G[u][i].id;
            DFS(v,u,level+1);
        }
    }
}
void lca_init(){
    for(int i=1;i<=N;i++){
        anc[i][0]=fa[i];
        for(int j=1;(1<<j)<=N;j++) anc[i][j]=-1;
    }
    for(int j=1;(1<<j)<=N;j++)
        for(int i=1;i<=N;i++) if(anc[i][j-1]!=-1){
            int a=anc[i][j-1];
            anc[i][j]=anc[a][j-1];
        }
}
int lca(int p,int q){
    if(L[p]<L[q]) swap(p,q);
    int log;
    for(log=0;(1<<log)<=L[p];log++);
    log--;
    for(int i=log;i>=0;i--) if(L[p]-(1<<i)>=L[q]) p=anc[p][i];
    if(p==q) return p;
    for(int i=log;i>=0;i--) if(anc[p][i]!=-1&&anc[p][i]!=anc[q][i]){
        p=anc[p][i];
        q=anc[q][i];
    }
    return fa[p];
}
void solve(){
    queue<int> q;
    for(int i=1;i<=N;i++) if(!deg[i]) q.push(i);    //叶子节点
    while(!q.empty()){
        int x=q.front();
        q.pop();
        node[x]=add1[x];
        add1[x]-=sub[x];
        edge[e[x]]=add2[x];
        int f=fa[x];
        add1[f]+=add1[x];
        add2[f]+=add2[x];
        if(!(--deg[f])) q.push(f);  //子节点都处理完了,成为叶子节点
    }
}
template <class T>
inline bool scan_d(T &ret) {
    char c; int sgn;
    if(c=getchar(),c==EOF) return 0; //EOF
    while(c!='-'&&(c<'0'||c>'9')) c=getchar();
    sgn=(c=='-')?-1:1;
    ret=(c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
    ret*=sgn;
    return 1;
}
inline void out(long long x) {
    if(x>9) out(x/10);
    putchar(x%10+'0');
}
int main(){
    freopen("in.txt","r",stdin);
    int cas=0;
    scan_d(T);
    while(T--){
        scan_d(N);
        scan_d(M);
        for(int i=1;i<=N;i++) G[i].clear();
        int u,v;
        for(int i=1;i<=N-1;i++){
            scan_d(u);
            scan_d(v);
            G[u].push_back(Edge(v,i));
            G[v].push_back(Edge(u,i));
        }
        memset(fa,0,sizeof(fa));
        memset(deg,0,sizeof(deg));
        DFS(1,-1,0);
        lca_init();
        memset(add1,0,sizeof(add1));
        memset(add2,0,sizeof(add2));
        memset(sub,0,sizeof(sub));
        while(M--){
            int u,v,w;
            scanf("%s",str);
            scan_d(u);
            scan_d(v);
            scan_d(w);
            int x=lca(u,v);
            if(str[3]=='1'){
                add1[u]+=w;
                add1[v]+=w;
                add1[x]-=w;
                sub[x]+=w;
            }
            else{
                add2[u]+=w;
                add2[v]+=w;
                add2[x]-=w*2;
            }
        }
        solve();
        printf("Case #%d:\n",++cas);
        for(int i=1;i<=N;i++){
            if(i>1) printf(" ");
            out(node[i]);
        }
        puts("");
        if(N==1) puts("");
        else{
            for(int i=1;i<=N-1;i++){
                if(i>1) printf(" ");
                out(edge[i]);
            }
            puts("");
        }
    }
    return 0;
}


ESP32 STA AP模式下,设备连接 ESP的AP 后无法上网,无法访问域名上网(或者Failed to enable NAPT) ESP32 STA AP模式下,设备连接 ESP的AP 后无法上网,无法访问域名上网(或者Failed to enable NAPT)1.可能是ESP STA模式未成功连接到路由器,确保 STA 成功连接到外部网络。2.检查 STA 和 AP 的网络配置是否正确,避免子网冲突。3.ESP32的NAPT功能未正确启用,确保 menuconfig 中已启用 IP_NAPT。4.DNS服务器未正确配置。 阅读详情

相关推荐

[V-10]ARMv8/v9-CPU Virtualization-CPU虚拟化架构简介(vCPU/vPE)

本文简要介绍了CPU虚拟化架构的基础知识。

基层架构师的分享 1272

HDU 5044 Tree(LCA)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5044 题意:对于一棵树,可以进行两种操作: ADD1 U V K:将U到V结点路上的结点的权值都增加K; ADD2 U V K:将U到V结点路上的边的权值都增加K; 最后输出所有点和所有边的权值。 思路:思路出自http://blog.csdn.net/hongrock/article...

Tongqi 798

NVIDIA DRIVE SDK学习(二)——NvMedia

可以通过使用NvSciBuf API通过NvMedia Tensor属性分配NvSciBuf来创建NvMedia张量。由于NvSciBuf API有助于NvMedia和NVIDIA®CUDA®之间的数据共享,这使得分配的张量可以在NvSciBuf API允许的情况下重复使用。它支持图像聚合操作。NvSciSync组件使得NvMedia组件可以与其他组件保持同步,比如CUDA组件、基于NvSciSync的组件等。立体视差模式:在这种模式下,该模块通过左图像和右图像来产生立体视差,从而进行物体深度测量。

weixin_39592381的博客 3058

HDU5044【LCA+差分】

思路: 树以1为根节点,然后向下DFS 两个差分 ADD1 操作,对节点 (u, v) 就是 他们的两个各自节点+w, 然后节点LCA(u, v) - w,LCA(u, v)的父节点-w,然后DFS一遍就能求出来 ADD2 操作就更简单了,边的权值都放在节点上,然后直接在LCA(u, v)上 - 2*w,另外u, v都+w. 但是很卡常数。//#pragma comment(li

ONE MORE TRY 519

Windows环境下Elasticsearch与JDK版本兼容性实战指南

本文详细解析了Windows环境下Elasticsearch与JDK版本的兼容性问题,提供了从版本匹配检查到安装部署的完整实战指南。重点介绍了Elasticsearch 8.x需JDK 17+等关键版本对应关系,并分享多版本JDK共存方案及性能优化技巧,帮助开发者高效避坑。

weixin_29250403的博客 428

基于BP神经网络的语音识别实现(附带Matlab代码)

在语音识别中,常用的网络结构是前馈神经网络(Feedforward Neural Network),它由输入层、隐藏层和输出层组成。输入层接收语音样本的特征向量,隐藏层负责处理输入信息,输出层给出对应的识别结果。标签可以是文本表示的语音内容,或者是语音的特征向量。以上是一个简单的语音识别实现示例,实际的语音识别系统可能会更加复杂。但是通过BP神经网络的训练和预测过程,我们可以逐步提高语音识别的准确性和性能。需要注意的是,输入的特征向量需要进行预处理,以便更好地适应神经网络的训练。希望这个示例对你有帮助!

PixelLoom的博客 554

hdu 5044 树区间操作最后输出/ lca+dfs

题意:一棵树,俩种操作:1 有路径上的所有点加vi,2所有边加vi。  先离线求出所有询问的lca,再遍历询问一次,点+vi,lca-2*vi ,最后dfs从叶子扫上来一次,最后再祖先点补上即可。用了输入挂。复杂度(n+m). #pragma comment(linker,"/STACK:10240000000000,10240000000000") #include #include #in

天羽屠龙舞 1399

汇川Easy系列PLC系统PID指令编程应用(增量型)

增量式PIDS7-1200/1500增量式PID(输出归一化、支持PWM输出)_西门子1200pid算法公式-CSDN博客文章浏览阅读1k次。本文介绍了S7-1200/1500 PLC中增量式PID算法的应用,包括输出归一化和PWM输出。通过详细解析,阐述了PID输出如何限制在0.0-1.0之间,并且如何将PID输出与PWM占空比关联,适用于温度等控制场景。

RXXW_Dor的博客 1457

HDU 5044 Tree(LCA ,2014上海网络赛1003)

题目:Tree 题意:又是yi'g

hongrock的专栏 1110

Monorepo与pnpm:前端项目管理的完美搭档

pnpm又称 performant npm,翻译过来就是高性能的npm。Monorepo是一种项目开发与管理的策略模式,它代表"单一代码仓库"(Monolithic Repository)。在Monorepo模式中,所有相关的项目和组件都被存储在一个统一的代码仓库中,而不是分散在多个独立的代码仓库中,这些项目之间还可能会有依赖关系。

m0_68271787的博客 2567

Google Earth Engine(GEE)——全球影像数据正确下载方式和注意事项

今天看到有人担心全球影像能不能下载的问题? 有时候我们会用到全球的数据,但是如何下载全球的数据呢? 首先如果没有全球的矢量边界我们会不会无法下载全球的数据,答案当然是可以的,因为我们在边界筛选的过程中filterBounds(),就是为了筛选边界,当我们不进行边界筛选的时候,自然而然默认的边界范围就是全球的,但是紧接着会面临其他两个问题:1如何下载?2如何在不缩小分辨率的情况下下载成功? 对于第一个问题我们如何下载?还是按照正常的逻辑利用export直接倒出到Google硬盘当中, 代码:

此星光明博客 3782

HDU5044 2014上海网络赛1003 tree

Tree Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1024    Accepted Submission(s): 188 Problem Description You are given a tre

、Poppy 882

Dify 本地部署教程

到这里,Dify 就安装完成了,更多操作设置可以参考官方文档,Dify 和 FastGPT 基本上差不多,也可以同时安装一下 FastGPT 进行体验~Docker Compose 部署 | Dify。

Muti-Agent 6733

uva11996 - Jewel Magic 伸展树

Problem J Jewel Magic I am a magician. I have a string of emeralds and pearls. I may insert new jewels in the string, or remove old ones. I may even reverse a consecutive part of the string. At

7529

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

堆 优先队列

首先看完全二叉树的定义: 若设二叉树的深度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树。   如果一个完全二叉树的某个结点值都不大于或小于他的父结点,那就叫做堆。根结点是所有结点里最大的叫大根堆,最小的叫小根堆。   比如:  9-1 a是大根堆,9-2 a是小根堆,剩下的都不是堆(因为不是完全二叉树)

2236

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

uva10410 - Tree Reconstruction BFS和DFS重建树

You have just finished a compiler design homework question whereyou had to find the parse tree of an expression. Unfortunately youleft your assignment in the library, but luckily your friend picked it

1223

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

uva12206 - Stammering Aliens 哈希LCP和后缀数组两种做法

Dr. Ellie Arroway has established contact with an extraterrestrialcivilization. However, all efforts to decode their messages have failed so farbecause, as luck would have it, they have stumbled upon

1034

uva12003 - Array Transformer 分块结构

Write a program to transform an array A[1], A[2], . . . , A[n] according to m instructions. Each instruction(L, R, v, p) means: First, calculate how many numbers from A[L] to A[R] (inclusive) are stri

929

并查集

在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将属于同一组的元素所在的集合合并,其间要反复查找一个元素在哪个集合中。   有n个元素,一开始每个元素都是一个集合,我们用p[x]表示x的父亲结点,当p[x]=x时是根结点,那么一开始p[i]=i(i=1...n)。如果想把两个集合合并,只需要把一个集合的根结点接到另一个集合的根结点下就行了(我们只

920

uva12232 - Exclusive-OR 加权并查集

You are not given n non-negative integers X0, X1,..., Xn-1 less than 220, but they do exist, and their values never change. I'll gradually provide you some facts about them, and ask you some ques

884

uva10829 - L-Gap Substrings 后缀数组+RMQ

Problem J L-Gap Substrings Input:StandardInput Output: Standard Output If a string is in the form UVU, where Uis not empty, and V has exactly L characters, we say UVUis an L-Gap string. For exa

877

哈夫曼树

给定n个权值作为n个叶子结点,构造一棵二叉树,若带权路径长度达到最小,称这样的二叉树为最优二叉树,也称为哈夫曼树(Huffman tree)。结点的带权路径长度为:从根结点到该结点之间的路径长度与该结点的权的乘积。树的带权路径长度规定为所有叶子结点的带权路径长度之和。   1*3+2*3+3*2+4*1  = (1+2)+(1+2+3)+(1+2+3+4) =19   整个树的带权

869

uva11992 - Fast Matrix Operations 区间修改 二维线段树

//修改、查询区间均为[y1,y2] struct IntervalTree{ //同一结点先考虑set,再考虑add //sumv[o],minv[o],maxv[o]保存的是以o为根结点的树内修改后的和,最大,最小值,o的父节点及以上结点修改的情况没有包括在里面 int setv[MAXNODE],addv[MAXNODE],sumv[MAXNODE],minv[MAX

864

hdu 1542 upcoj 2540 矩形面积并 线段树

几天没AC了 。。。太爷跟我说什么有个线段树的题很简单,可以做一下。。于是被坑了。。   那个题是upcoj 2540,求一堆矩形重叠2次部分的面积,现在我还只会线段树简单的点修改区间修改,呵呵呵一看这题我果断不会做。。百度了一下,看了太爷给的一个不错的线段树汇总http://www.notonlysuccess.com/index.php/segment-tree-complete/,算是对这

798

离散化+树状数组 求逆序对数

除了归并排序,也可以用树状数组求逆序对数。先看 POJ 2299 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 32606   Accepted: 11633 Description In this problem, you have

774

uva11525 - Permutation 线段树加速康托展开

Problem F Permutation Input: Standard Input Output: StandardOutput   Given N and K find the N’thpermutation of the integers from 1 to K when those permutations arelexicographically ordered.  N s

757
上一篇: 最小树形图 朱刘算法
下一篇: Dinic+当前弧优化
小小小小葱
博客等级 码龄14年 22粉丝 266原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址