poj1655的方法完全一样,但是这道题的n的范围大了,用vector存图会TLE(poj没有O2编译优化),所以改用前向星来存图就可以了。、

有关树的重心,看这里:poj1655

这里解释一下前向星存图的方法:

其实就是用静态链表来实现邻接链表,这样可以避免使用指针。

head[i]数组来记录每个节点的第一条边;每条边用结构体e[i]来存,e[i].v表示这条边指向的点,e[i].next表示这条边连向的下一条边。

它的巧妙之处在于每次插入到链表的首部而不是尾部,这样就避免了对链表的遍历。同一起点的各条边在邻接表中的顺序和读入顺序正好相反。

贴个模板:

  1. struct node
  2. {
  3. int v,next;
  4. }e[m];//m是总边数
  5. int head[n],cnt;//n是总节点数,cnt记录边数
  6. void init()
  7. {
  8. memset(head,-,sizeof(head));
  9. cnt=;
  10. }
  11. void add(int u,int v)
  12. {
  13. e[cnt].v=v;
  14. e[cnt].next=head[u];
  15. head[u]=cnt++;
  16. }

本题AC代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<map>
  7. #include<set>
  8. #include<vector>
  9. #include<algorithm>
  10. #include<stack>
  11. #include<queue>
  12. using namespace std;
  13. #define INF 100000000
  14. #define eps 1e-8
  15. #define pii pair<int,int>
  16. #define LL long long int
  17. struct node
  18. {
  19. int v,next;
  20. }e[];
  21. int n,a,b,head[],mi=;
  22. int num[],bal[],cnt=;
  23. void add(int aa,int bb);
  24. int dfs1(int x,int fa);
  25. void dfs2(int x,int fa);
  26. int main()
  27. {
  28. //freopen("in1.txt","r",stdin);
  29. //freopen("out.txt","w",stdout);
  30. scanf("%d",&n);
  31. memset(head,-,sizeof(int)*(n+));
  32. for(int i=;i<=n;i++) num[i]=;
  33. for(int i=;i<=n-;i++)
  34. {
  35. scanf("%d%d",&a,&b);
  36. add(a,b);
  37. add(b,a);
  38. }
  39. dfs1(,-);
  40. dfs2(,-);
  41. for(int i=;i<=n;i++)
  42. {
  43. if(bal[i]<bal[mi])
  44. {
  45. mi=i;
  46. }
  47. }
  48. printf("%d",mi);
  49. for(int i=mi+;i<=n;i++)
  50. {
  51. if(bal[i]==bal[mi])
  52. {
  53. printf(" %d",i);
  54. }
  55. }
  56. printf("\n");
  57. //fclose(stdin);
  58. //fclose(stdout);
  59. return ;
  60. }
  61. void add(int aa,int bb)
  62. {
  63. e[cnt].v=bb;
  64. e[cnt].next=head[aa];
  65. head[aa]=cnt++;
  66. }
  67. int dfs1(int x,int fa)
  68. {
  69. for(int i=head[x];i!=-;i=e[i].next)
  70. {
  71. if(e[i].v==fa)
  72. continue;
  73. else
  74. {
  75. num[x]+=dfs1(e[i].v,x);
  76. }
  77. }
  78. return num[x];
  79. }
  80. void dfs2(int x,int fa)
  81. {
  82. for(int i=head[x];i!=-;i=e[i].next)
  83. {
  84. if(e[i].v==fa)
  85. {
  86. bal[x]=max(bal[x],n-num[x]);
  87. }
  88. else
  89. {
  90. bal[x]=max(bal[x],num[e[i].v]);
  91. dfs2(e[i].v,x);
  92. }
  93. }
  94. }

poj3107(dfs,树形dp)的更多相关文章

  1. 杭电OJ——1011 Starship Troopers(dfs + 树形dp)

    Starship Troopers Problem Description You, the leader of Starship Troopers, are sent to destroy a ba ...

  2. POJ 1849 - Two - [DFS][树形DP]

    Time Limit: 1000MS Memory Limit: 30000K Description The city consists of intersections and streets t ...

  3. POJ3107 Godfather (树形DP)

    题意:求树的重心 题解:先跑一遍dfs 预处理出这种遍历方式每个节点的儿子(含自己)的数 再跑一遍 每个点的值就是他所有儿子中取一个最大值 再和它父亲这个方向比较一下 又被卡常了 vector一直tl ...

  4. 图论--树的直径--DFS+树形DP模板

    #include <iostream> #include <cstring> using namespace std; //maxv:源点能到的最远点,maxdis:最远点对应 ...

  5. Codeforces 765E. Tree Folding [dfs][树形dp]

    题解:先从节点1开始dfs.对于每一个节点,用一个set记录:以该点为根的子树的深度. a) 如果此节点的某个子节点打出了GG,则此节点直接打出GG. b) 若set的元素个数<=1,那么,以该 ...

  6. poj2378(dfs,树形dp)

    和poj3107,poj1655一样的方法 #include<iostream> #include<cstdio> #include<cstdlib> #inclu ...

  7. poj1655(dfs,树形dp,树的重心)

    这是找树的重心的经典题目. 树的重心有下面几条常见性质: 定义1:找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心.定义2:以这个点为根,那么所有的子树(不算整个树自身)的大 ...

  8. poj1655(dfs,树形dp,树的重心)(点分治基础)

    题意:就是裸的求树的重心. #include<cstring> #include<algorithm> #include<cmath> #include<cs ...

  9. CF979C Kuro and Walking Route(简单的dfs/树形dp)

    题意:给出一个$n$个点,$n-1$条边的无向连通图,给出两个点$x,y$,经过$x$后的路径上就不能经过$y$,问可以走的路径$(u,v)$有多少条,($(u,v)$和$(v,u)$考虑为两条不同的 ...

  10. 【bzoj2435】[NOI2011]道路修建 树形dp

    题目描述 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿意修建恰好 n – 1条双向道路. 每条道路的修 ...

随机推荐

  1. rails timeout 异常

    发现经常有”超时“的错误信息,如/usr/lib/ruby/1.8/timeout.rb:54:in `rbuf_fill': execution expired (Timeout::Error),恩 ...

  2. UVALive 6906 A - Cluster Analysis

    思路:排个序,依次选就好了. #include <bits/stdc++.h> #define PB push_back #define MP make_pair using namesp ...

  3. Python3.x:Linux下安装python3.6

    Python3.x:Linux下安装python3.6 下载 #先进入download文件夹 cd /home/download #输入命令(下载到当前目录) wget https://www.pyt ...

  4. SQuirrel-GUI工具安装手册-基于phoenix驱动

    背景描述: SQuirrel sql client 官方地址:http://www.squirrelsql.org/index.php?page=screenshots 一个图形界面的管理工具 安装手 ...

  5. 20145229吴姗珊逆向BOF实践

    20145229吴姗珊逆向BOF实践 实践 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. ...

  6. MapReduce:输出是一个文本文件,每一行第一个数字式行标,第二个数字是输入文件中每一行除行标外数字的平均值,且整数不保留小数,小数保留两位小数点

    有时候你会遇到这样的问题:你有一个表格,给出了每个人在十二月,一月和二月的收入. 表格如下: 姓名 一月 二月 三月 楚乔     200   314   3500 宇文玥     2000  332 ...

  7. idea setting

    input

  8. Apache配置的5个技巧

    AcceptMutex Apache 1.3.21和Apache 2.0中引入了AcceptMutex 指示符,该指示符给调节服务器的性能带来了一个难得的机会.该指示符配置Apache的accept( ...

  9. Google maps api demo

    demo: <!DOCTYPE html> <html> <head> <meta name="viewport" content=&qu ...

  10. 记一次Configured Capacity: 0 (0 B)的解决

    场景 最近hadoop集群新加了一个节点N,通过Ambari管理 一切正常. 过了两天发现,虽然集群每天要进几个G的数据(共8个节点),但节点N占用空间丝毫没有变化,显然没有进数据啊 日志 查看该节点 ...