Tree(未解决。。。)
Tree
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2968 Accepted Submission(s): 507
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.
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)
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.
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
1 1 0 1
0 2 2
Case #2:
5 0 0 5
0 4 0
题意:
先给你一颗树,然后有两种操作:
● 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.
add1把从u到v的点所经过的点的权值都增加k;
add2把从u到v所经过的边都增加k;
大婶们都说,这是树链剖分的水题。。。
我用邻接表搞了很久很久,却还是wa。。。。
邻接表代码先贴着,有空学学数链剖分。。。
wa代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
const int MAXN=1e5+10;
int head[MAXN<<1];
int edgnum;
struct Edge{
int from,to,next,val;
};
Edge edg[MAXN<<1];
struct Node{
int u,v;
};
Node dt[MAXN];
int dis[MAXN];
void initial(){
mem(head,-1);edgnum=0;mem(dis,0);
}
void add(int u,int v,int w){
Edge E={u,v,head[u],w};
edg[edgnum]=E;
head[u]=edgnum++;
}
/*void adw(int u,int v,int w){
for(int i=head[u];i!=-1;i=edg[i].next){ }
}*/
void dfs1(int i,int v,int w){
// printf("%d\n",i);
if(i==-1)return;
edg[i].val+=w;
for(int j=head[v];j!=-1;j=edg[j].next){
if(edg[j].to==edg[i].from){
edg[j].val+=w;break;
}
}
// printf("%d %d\n",edg[i].from,edg[i].to);
if(edg[i].to==v)return;
dfs1(head[edg[i].to],v,w);
}
void dfs2(int i,int v,int w){
if(i==-1)return;
int to=edg[i].to;
dis[to]+=w;
// printf("%d\n",to);
if(edg[i].to==v)return;
dfs2(head[edg[i].to],v,w);
}
int main()
{
int t,n,m,kase=0;
char s[5];
scanf("%d",&t);
while(t--){
int u,v,w;
initial();
scanf("%d%d",&n,&m);
for(int i=1;i<n;i++){
scanf("%d%d",&dt[i].u,&dt[i].v);
add(dt[i].u,dt[i].v,0);
add(dt[i].v,dt[i].u,0);
}
while(m--){
scanf("%s",s);
scanf("%d%d%d",&u,&v,&w);
if(strcmp(s,"ADD1")==0){
dis[u]+=w;
if(u!=v)dfs2(head[u],v,w);
}
else{
if(u!=v)dfs1(head[u],v,w);
}
}
printf("Case #%d:\n",++kase);
for(int i=1;i<=n;i++){
if(i!=1)printf(" ");
printf("%d",dis[i]);
}puts("");
for(int i=1;i<n;i++){
u=dt[i].u;v=dt[i].v;
int k=head[u];
if(i!=1)printf(" ");
printf("%d",edg[k].val);
}puts("");
}
return 0;
}
Tree(未解决。。。)的更多相关文章
- C/C++编译和链接过程详解 (重定向表,导出符号表,未解决符号表)
详解link 有 些人写C/C++(以下假定为C++)程序,对unresolved external link或者duplicated external simbol的错误信息不知所措(因为这样的错 ...
- 直接请求json文件爬取天眼查企业信息(未解决验证码问题)——python3实现
几个月前...省略一堆剧情...直接请求json文件爬取企业信息未成功,在知乎提问后,得到解决,有大佬说带上全部headers和cookie是可以的,我就又去试了下,果然可以(之前自己试的时候不行,没 ...
- 记一次未解决的异常:java.lang.NoClassDefFoundError: net/sf/json/JSONObject
原因:Jetty会导致这个问题,Tomcat可以正常启动 一.异常产生现象 使用json-lib转换实体类/字符串,跑单元测试没问题,但是启动jetty后调用JSONArray.fromObjec ...
- Ajax返回中文乱码问题(未解决)
(未解决) 暂时使用办法:改用返回Map<String,String>形式的返回值,在ajax中获取json形式的数据.
- openerp学习笔记 计算字段、关联字段(7.0中非计算字段、关联字段只读时无法修改保存的问题暂未解决)
计算字段.关联字段,对象修改时自动变更保存(当 store=True 时),当 store=False 时,默认不支持过滤和分组7.0中非计算字段.关联字段只读时无法修改保存的问题暂未解决 示例代码: ...
- oracle,wamp,FZ突然出现问题,求解决方案(未解决,最终系统还原)
-----背景------- 系统:win7 64位oracle 11g(11.1)每天都用oracle.用toad for oracle .电脑固定IP.未更改任何配置信息.用了几个月,突然出现了 ...
- (转载) C/C++编译和链接过程详解 (重定向表,导出符号表,未解决符号表)
转载http://blog.csdn.net/neo_ustc/article/details/9024839 有 些人写C/C++(以下假定为C++)程序,对unresolved external ...
- tomcat中显示本地图片①(未解决)
<本模块文仅作为学习过程中的自我总结,有需要可参看,欢迎指导与提出建议,很多地方可能断章取义,理解不到位,虚心求学.谢谢!> 资料查阅原因:2018/7/10(做项目中显示详情页面,从数据 ...
- JIRA状态为任务结束,但是解决结果为未解决相关配置
1.JIRA状态为任务结束,但是解决结果为未解决,如下图所示: 2.在工作流->界面->结果处理中进行解决结果的配置(首先确保界面配置中有“解决结果”字段). 3.点击“结果处理”,进行结 ...
- [未解决]Exception in thread "main" java.lang.IllegalArgumentException: offset (0) + length (8) exceed the capacity of the array: 6
调用这个方法 是报错,未解决 binfo.setTradeAmount(Double.parseDouble(new String(result.getValue(Bytes.toBytes(fami ...
随机推荐
- vs2012C#编程环境设置智能提示
vs2012 智能提示和 显示行号的问题 路径为 菜单里 工具-->选项-->文本编辑器-->C# 如图所示 自动列出成员就是 vs里面的智能提示 行号就会显示所写代码的行 ...
- js处理json的方法
var json = "{id:"myid", url:"http://www.myurl.com"}"; var js= (new Fun ...
- 使用PHP把下划线分隔命名的字符串 转换成驼峰式命名方式 , 把下划线后面的第一个字母变成大写
最近项目使用symfony框架,这个框架对数据库的操作在这个团队里使用的是ORM进行操作,说实话使用ORM的开发效率和运行效率不一定高多少,到是它的实体命名和现有数据库字段的命名不太一样,ORM实体属 ...
- java调用C++ DLL库方法
最近一个项目要开发网页端人脸识别项目,人脸识别的算法已经写好,是C++版,但是网页端要求使用Java后台,这就涉及到Java调用DLL的问题.经过查找,实现了一个简单的例子. 1.第一步,先在Java ...
- Mysql 笔记:
1:可以查看information_schema.index_statistics 来查看索引的使用信息.还可以使用pt-index-usage 这个工具来分析日志再结合explain 来分析使用的索 ...
- 《转》JAVA并发编程:volatile关键字解析
volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在Java 5之后,volatile关键字才得以 ...
- es5 api
ES5 Object.create(prototype, descriptors) //创建对象 var o1 = {foo:'bar'}; var o2 = new Object(); //Obje ...
- 转: html5 history api详解~很好的文章
从Ajax翻页的问题说起 请想象你正在看一个视频下面的评论,在翻到十几页的时候,你发现一个写得稍长,但非常有趣的评论.正当你想要停下滚轮细看的时候,手残按到了F5.然后,页面刷新了,评论又回到了第一页 ...
- 转: mysql create view 创建视图
以下的文章主要是对MySQL视图的描述,其中包括MySQ视图L概述,以及创建MySQL视图-create view与修改MySQL视图--alter view等相关内容的具体描述,以下就是文章的具体内 ...
- Struts2 一张图片引发的bug
今天如常的打开项目开放.写了一会保存测试.在登录时出了个错误当不影响正常使用.丫的昨天还好好的.行下手上的工作 开始找bug 错误核心代码如下: 10:34:46,442 WARN OgnlValu ...