题意:给出一个深度优先遍历树的up down顺序,求这棵树以及这棵树变为”左子右兄”树的高度

思路:直接dfs,x代表树1的高度,y代表树2的高度

  1. #include<cstdio>
  2. #include<cmath>
  3. #include<cstring>
  4. #include<iostream>
  5. #include<algorithm>
  6. int h1=,h2=,u=;
  7. char s[];
  8. int read(){
  9. char ch=getchar();int f=,t=;
  10. while (ch<''||ch>'') {if (ch=='-') f=-;ch=getchar();}
  11. while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
  12. return t*f;
  13. }
  14. void dfs(int x,int y){
  15. int son=;
  16. while (s[u]=='d'){
  17. u++;son++;
  18. dfs(x+,y+son);
  19. }
  20. u++;
  21. h1=std::max(h1,x);
  22. h2=std::max(h2,y);
  23. }
  24. int main(){
  25. int T=;
  26. scanf("%s",s);
  27. while (s[]!='#'){
  28. h1=h2=u=;
  29. dfs(,);
  30. printf("Tree %d: %d => %d\n",++T,h1,h2);
  31. scanf("%s",s);
  32. }
  33. }

POJ 3437 Tree Grafting的更多相关文章

  1. poj 3237 Tree [LCA] (树链剖分)

    poj 3237 tree inline : 1. inline 定义的类的内联函数,函数的代码被放入符号表中,在使用时直接进行替换,(像宏一样展开),没有了调用的开销,效率也很高. 2. 很明显,类 ...

  2. poj 3237 Tree(树链拆分)

    题目链接:poj 3237 Tree 题目大意:给定一棵树,三种操作: CHANGE i v:将i节点权值变为v NEGATE a b:将ab路径上全部节点的权值变为相反数 QUERY a b:查询a ...

  3. POJ 1741 Tree 求树上路径小于k的点对个数)

                                                                                                 POJ 174 ...

  4. POJ 2378 Tree Cutting 3140 Contestants Division (简单树形dp)

    POJ 2378 Tree Cutting:题意 求删除哪些单点后产生的森林中的每一棵树的大小都小于等于原树大小的一半 #include<cstdio> #include<cstri ...

  5. poj 1741 Tree(树的点分治)

    poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...

  6. POJ 3723 Tree(树链剖分)

    POJ 3237 Tree 题目链接 就多一个取负操作,所以线段树结点就把最大和最小值存下来,每次取负的时候,最大和最小值取负后.交换就可以 代码: #include <cstdio> # ...

  7. POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<=K的点对数量

    POJ 1741. Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 34141   Accepted: 11420 ...

  8. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

  9. poj 3237 Tree 树链剖分

    题目链接:http://poj.org/problem?id=3237 You are given a tree with N nodes. The tree’s nodes are numbered ...

随机推荐

  1. typeof的用法

    typeof可以返回变量的类型,返回值为字符串,其值有 "undefined" "boolean" "string" "numbe ...

  2. php中类的static变量使用

    <?php #访问静态变量 #类外部: 类名::$类变量名 #类内部: 娄名::$类变量名或self::$类变量名 class Char{ public static $number = 0; ...

  3. Linux信号函数

    1. signal函数: #include <signal.h> void (*signal(int signo, void (*func)(int)))(int); ret-成功返回信号 ...

  4. Lodash使用示例(比较全)

    <html> <head> <meta name="viewport" content="width=device-width" ...

  5. 【bzoj3223】文艺平衡树

    #include<bits/stdc++.h> #define N 300005 #define rat 4 using namespace std; struct Node{ int s ...

  6. 2015多校第6场 HDU 5355 Cake 贪心,暴力DFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5355 题意:给你n个尺寸大小分别为1,2,3,…,n的蛋糕,要求你分成m份,要求每份中所有蛋糕的大小之 ...

  7. Name Disambiguation in AMiner-Clustering, Maintenance, and Human in the Loop

    Name Disambiguation in AMiner: Clustering, Maintenance, and Human in the Loop paper:http://keg.cs.ts ...

  8. Hibernate检索策略与检索方式

    hibernate的Session在加载Java对象时,一般都会把鱼这个对象相关联的其他Java对象也都加载到缓存中,以方便程序的调用.但很多情况下,我们不需要加载太多无用的对象到缓存中,一来会占用大 ...

  9. Word Search——经典题(还没细看)

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  10. [转载]Python命令行参数学习

    转载自: http://blog.163.com/weak_time/blog/static/25852809120169333247925/ Python的命令行参数,提供了很多有用的功能,可以方便 ...