poj3107(dfs,树形dp)
和poj1655的方法完全一样,但是这道题的n的范围大了,用vector存图会TLE(poj没有O2编译优化),所以改用前向星来存图就可以了。、
有关树的重心,看这里:poj1655
这里解释一下前向星存图的方法:
其实就是用静态链表来实现邻接链表,这样可以避免使用指针。
head[i]数组来记录每个节点的第一条边;每条边用结构体e[i]来存,e[i].v表示这条边指向的点,e[i].next表示这条边连向的下一条边。
它的巧妙之处在于每次插入到链表的首部而不是尾部,这样就避免了对链表的遍历。同一起点的各条边在邻接表中的顺序和读入顺序正好相反。
贴个模板:
- struct node
- {
- int v,next;
- }e[m];//m是总边数
- int head[n],cnt;//n是总节点数,cnt记录边数
- void init()
- {
- memset(head,-,sizeof(head));
- cnt=;
- }
- void add(int u,int v)
- {
- e[cnt].v=v;
- e[cnt].next=head[u];
- head[u]=cnt++;
- }
本题AC代码:
- #include<iostream>
- #include<cstdio>
- #include<cstdlib>
- #include<cstring>
- #include<cmath>
- #include<map>
- #include<set>
- #include<vector>
- #include<algorithm>
- #include<stack>
- #include<queue>
- using namespace std;
- #define INF 100000000
- #define eps 1e-8
- #define pii pair<int,int>
- #define LL long long int
- struct node
- {
- int v,next;
- }e[];
- int n,a,b,head[],mi=;
- int num[],bal[],cnt=;
- void add(int aa,int bb);
- int dfs1(int x,int fa);
- void dfs2(int x,int fa);
- int main()
- {
- //freopen("in1.txt","r",stdin);
- //freopen("out.txt","w",stdout);
- scanf("%d",&n);
- memset(head,-,sizeof(int)*(n+));
- for(int i=;i<=n;i++) num[i]=;
- for(int i=;i<=n-;i++)
- {
- scanf("%d%d",&a,&b);
- add(a,b);
- add(b,a);
- }
- dfs1(,-);
- dfs2(,-);
- for(int i=;i<=n;i++)
- {
- if(bal[i]<bal[mi])
- {
- mi=i;
- }
- }
- printf("%d",mi);
- for(int i=mi+;i<=n;i++)
- {
- if(bal[i]==bal[mi])
- {
- printf(" %d",i);
- }
- }
- printf("\n");
- //fclose(stdin);
- //fclose(stdout);
- return ;
- }
- void add(int aa,int bb)
- {
- e[cnt].v=bb;
- e[cnt].next=head[aa];
- head[aa]=cnt++;
- }
- int dfs1(int x,int fa)
- {
- for(int i=head[x];i!=-;i=e[i].next)
- {
- if(e[i].v==fa)
- continue;
- else
- {
- num[x]+=dfs1(e[i].v,x);
- }
- }
- return num[x];
- }
- void dfs2(int x,int fa)
- {
- for(int i=head[x];i!=-;i=e[i].next)
- {
- if(e[i].v==fa)
- {
- bal[x]=max(bal[x],n-num[x]);
- }
- else
- {
- bal[x]=max(bal[x],num[e[i].v]);
- dfs2(e[i].v,x);
- }
- }
- }
poj3107(dfs,树形dp)的更多相关文章
- 杭电OJ——1011 Starship Troopers(dfs + 树形dp)
Starship Troopers Problem Description You, the leader of Starship Troopers, are sent to destroy a ba ...
- POJ 1849 - Two - [DFS][树形DP]
Time Limit: 1000MS Memory Limit: 30000K Description The city consists of intersections and streets t ...
- POJ3107 Godfather (树形DP)
题意:求树的重心 题解:先跑一遍dfs 预处理出这种遍历方式每个节点的儿子(含自己)的数 再跑一遍 每个点的值就是他所有儿子中取一个最大值 再和它父亲这个方向比较一下 又被卡常了 vector一直tl ...
- 图论--树的直径--DFS+树形DP模板
#include <iostream> #include <cstring> using namespace std; //maxv:源点能到的最远点,maxdis:最远点对应 ...
- Codeforces 765E. Tree Folding [dfs][树形dp]
题解:先从节点1开始dfs.对于每一个节点,用一个set记录:以该点为根的子树的深度. a) 如果此节点的某个子节点打出了GG,则此节点直接打出GG. b) 若set的元素个数<=1,那么,以该 ...
- poj2378(dfs,树形dp)
和poj3107,poj1655一样的方法 #include<iostream> #include<cstdio> #include<cstdlib> #inclu ...
- poj1655(dfs,树形dp,树的重心)
这是找树的重心的经典题目. 树的重心有下面几条常见性质: 定义1:找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心.定义2:以这个点为根,那么所有的子树(不算整个树自身)的大 ...
- poj1655(dfs,树形dp,树的重心)(点分治基础)
题意:就是裸的求树的重心. #include<cstring> #include<algorithm> #include<cmath> #include<cs ...
- CF979C Kuro and Walking Route(简单的dfs/树形dp)
题意:给出一个$n$个点,$n-1$条边的无向连通图,给出两个点$x,y$,经过$x$后的路径上就不能经过$y$,问可以走的路径$(u,v)$有多少条,($(u,v)$和$(v,u)$考虑为两条不同的 ...
- 【bzoj2435】[NOI2011]道路修建 树形dp
题目描述 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿意修建恰好 n – 1条双向道路. 每条道路的修 ...
随机推荐
- rails timeout 异常
发现经常有”超时“的错误信息,如/usr/lib/ruby/1.8/timeout.rb:54:in `rbuf_fill': execution expired (Timeout::Error),恩 ...
- UVALive 6906 A - Cluster Analysis
思路:排个序,依次选就好了. #include <bits/stdc++.h> #define PB push_back #define MP make_pair using namesp ...
- Python3.x:Linux下安装python3.6
Python3.x:Linux下安装python3.6 下载 #先进入download文件夹 cd /home/download #输入命令(下载到当前目录) wget https://www.pyt ...
- SQuirrel-GUI工具安装手册-基于phoenix驱动
背景描述: SQuirrel sql client 官方地址:http://www.squirrelsql.org/index.php?page=screenshots 一个图形界面的管理工具 安装手 ...
- 20145229吴姗珊逆向BOF实践
20145229吴姗珊逆向BOF实践 实践 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. ...
- MapReduce:输出是一个文本文件,每一行第一个数字式行标,第二个数字是输入文件中每一行除行标外数字的平均值,且整数不保留小数,小数保留两位小数点
有时候你会遇到这样的问题:你有一个表格,给出了每个人在十二月,一月和二月的收入. 表格如下: 姓名 一月 二月 三月 楚乔 200 314 3500 宇文玥 2000 332 ...
- idea setting
input
- Apache配置的5个技巧
AcceptMutex Apache 1.3.21和Apache 2.0中引入了AcceptMutex 指示符,该指示符给调节服务器的性能带来了一个难得的机会.该指示符配置Apache的accept( ...
- Google maps api demo
demo: <!DOCTYPE html> <html> <head> <meta name="viewport" content=&qu ...
- 记一次Configured Capacity: 0 (0 B)的解决
场景 最近hadoop集群新加了一个节点N,通过Ambari管理 一切正常. 过了两天发现,虽然集群每天要进几个G的数据(共8个节点),但节点N占用空间丝毫没有变化,显然没有进数据啊 日志 查看该节点 ...