Tree

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

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
 

题意:

先给你一颗树,然后有两种操作:

● 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代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<vector>
  6. #include<algorithm>
  7. using namespace std;
  8. #define mem(x,y) memset(x,y,sizeof(x))
  9. const int MAXN=1e5+10;
  10. int head[MAXN<<1];
  11. int edgnum;
  12. struct Edge{
  13. int from,to,next,val;
  14. };
  15. Edge edg[MAXN<<1];
  16. struct Node{
  17. int u,v;
  18. };
  19. Node dt[MAXN];
  20. int dis[MAXN];
  21. void initial(){
  22. mem(head,-1);edgnum=0;mem(dis,0);
  23. }
  24. void add(int u,int v,int w){
  25. Edge E={u,v,head[u],w};
  26. edg[edgnum]=E;
  27. head[u]=edgnum++;
  28. }
  29. /*void adw(int u,int v,int w){
  30. for(int i=head[u];i!=-1;i=edg[i].next){
  31.  
  32. }
  33. }*/
  34. void dfs1(int i,int v,int w){
  35. // printf("%d\n",i);
  36. if(i==-1)return;
  37. edg[i].val+=w;
  38. for(int j=head[v];j!=-1;j=edg[j].next){
  39. if(edg[j].to==edg[i].from){
  40. edg[j].val+=w;break;
  41. }
  42. }
  43. // printf("%d %d\n",edg[i].from,edg[i].to);
  44. if(edg[i].to==v)return;
  45. dfs1(head[edg[i].to],v,w);
  46. }
  47. void dfs2(int i,int v,int w){
  48. if(i==-1)return;
  49. int to=edg[i].to;
  50. dis[to]+=w;
  51. // printf("%d\n",to);
  52. if(edg[i].to==v)return;
  53. dfs2(head[edg[i].to],v,w);
  54. }
  55. int main()
  56. {
  57. int t,n,m,kase=0;
  58. char s[5];
  59. scanf("%d",&t);
  60. while(t--){
  61. int u,v,w;
  62. initial();
  63. scanf("%d%d",&n,&m);
  64. for(int i=1;i<n;i++){
  65. scanf("%d%d",&dt[i].u,&dt[i].v);
  66. add(dt[i].u,dt[i].v,0);
  67. add(dt[i].v,dt[i].u,0);
  68. }
  69. while(m--){
  70. scanf("%s",s);
  71. scanf("%d%d%d",&u,&v,&w);
  72. if(strcmp(s,"ADD1")==0){
  73. dis[u]+=w;
  74. if(u!=v)dfs2(head[u],v,w);
  75. }
  76. else{
  77. if(u!=v)dfs1(head[u],v,w);
  78. }
  79. }
  80. printf("Case #%d:\n",++kase);
  81. for(int i=1;i<=n;i++){
  82. if(i!=1)printf(" ");
  83. printf("%d",dis[i]);
  84. }puts("");
  85. for(int i=1;i<n;i++){
  86. u=dt[i].u;v=dt[i].v;
  87. int k=head[u];
  88. if(i!=1)printf(" ");
  89. printf("%d",edg[k].val);
  90. }puts("");
  91. }
  92. return 0;
  93. }

  

Tree(未解决。。。)的更多相关文章

  1. C/C++编译和链接过程详解 (重定向表,导出符号表,未解决符号表)

    详解link  有 些人写C/C++(以下假定为C++)程序,对unresolved external link或者duplicated external simbol的错误信息不知所措(因为这样的错 ...

  2. 直接请求json文件爬取天眼查企业信息(未解决验证码问题)——python3实现

    几个月前...省略一堆剧情...直接请求json文件爬取企业信息未成功,在知乎提问后,得到解决,有大佬说带上全部headers和cookie是可以的,我就又去试了下,果然可以(之前自己试的时候不行,没 ...

  3. 记一次未解决的异常:java.lang.NoClassDefFoundError: net/sf/json/JSONObject

    原因:Jetty会导致这个问题,Tomcat可以正常启动   一.异常产生现象 使用json-lib转换实体类/字符串,跑单元测试没问题,但是启动jetty后调用JSONArray.fromObjec ...

  4. Ajax返回中文乱码问题(未解决)

    (未解决) 暂时使用办法:改用返回Map<String,String>形式的返回值,在ajax中获取json形式的数据.

  5. openerp学习笔记 计算字段、关联字段(7.0中非计算字段、关联字段只读时无法修改保存的问题暂未解决)

    计算字段.关联字段,对象修改时自动变更保存(当 store=True 时),当 store=False 时,默认不支持过滤和分组7.0中非计算字段.关联字段只读时无法修改保存的问题暂未解决 示例代码: ...

  6. oracle,wamp,FZ突然出现问题,求解决方案(未解决,最终系统还原)

    -----背景------- 系统:win7  64位oracle 11g(11.1)每天都用oracle.用toad for oracle .电脑固定IP.未更改任何配置信息.用了几个月,突然出现了 ...

  7. (转载) C/C++编译和链接过程详解 (重定向表,导出符号表,未解决符号表)

    转载http://blog.csdn.net/neo_ustc/article/details/9024839 有 些人写C/C++(以下假定为C++)程序,对unresolved external ...

  8. tomcat中显示本地图片①(未解决)

    <本模块文仅作为学习过程中的自我总结,有需要可参看,欢迎指导与提出建议,很多地方可能断章取义,理解不到位,虚心求学.谢谢!> 资料查阅原因:2018/7/10(做项目中显示详情页面,从数据 ...

  9. JIRA状态为任务结束,但是解决结果为未解决相关配置

    1.JIRA状态为任务结束,但是解决结果为未解决,如下图所示: 2.在工作流->界面->结果处理中进行解决结果的配置(首先确保界面配置中有“解决结果”字段). 3.点击“结果处理”,进行结 ...

  10. [未解决]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 ...

随机推荐

  1. ​c++ 调用DLL函数,出现错误

    ​c++ 调用DLL函数,出现错误 Run-Time Check Failure #0 - The value of ESP was not properly saved across a funct ...

  2. shell检测interface是否已分配ip,qt调用shell脚本

    #include <QCoreApplication>#include <QDebug>#include <QTextStream>#include <QDi ...

  3. 成为 Linux 内核高手的四个方法

    (之前我在 CUSEC 网站发表了关于内核并不可怕的一篇文章,本文是后续.) 我曾经问别人如何开始内核编程的学习,他们基本上都说:1. 如果你不需要了解内核是如何为你工作的,你为何要尝试呢?2. 你应 ...

  4. javascript简单对象创建

    由于javascript中定义了一个函数就相当于定义了一个类,我们当然可以创建一个这个类的对象. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 ...

  5. CCNA实验(10) -- Access List

    使用包过滤技术在路由器上读取三层及四层报头的信息如源地址.目的地址.源端口.目的端口根据预先定义好的规则对包进行过滤 三种类型:1.标准ACL:表号范围1-99或1300-1999.仅对源IP地址进行 ...

  6. JqGrid使用经验

    一.更改分页组件的样式 分页组件中

  7. CNZZ公告:近期无法获取百度关键词

    今天登录cnzz网站统计,出现一条公告,说是“关于近期无法获取百度关键词的公告”,内容如下: 近日部分用户反馈百度搜索词流量出现不同程度的下降.经排查,是由于百度搜索引擎调整了URL规则,取消了来源U ...

  8. 解决Agent admitted failure to sign using the kye with ssh

    之前如果建立 ssh 连接,只要將公匙复制到~/.ssh/authorized_keys就可以直接登录而不需要建立密碼. 如果在使用时候出现如下信息: Agent admitted failure t ...

  9. if...else if...else

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace prog ...

  10. ASP.NET之电子商务系统开发-1(数据列表)

    一.前言 首先声明的是,这是我第一个与别人合作的.net项目,另一个人做的是后台管理,我做的前台,这是一个电子商务的系统,主要实现的功能是查看商品以及购物功能. 二.开始 首先看一下我截取的项目部分商 ...