POJ/obc - Step Traversing a Tree

即黑书里的“隔三遍历”,具体分析见黑书,我只是想了下证明没啥好说的。


#include <cstdio>

#define MAXV 5005
#define MAXE ((MAXV << 1) - 2)

int N;
int Vefw[MAXE], Vt[MAXE], Veh[MAXV], Veptr;
int V_b8[MAXV];

int leafrch(int s)
{
int ret = s;
V_b8[s] = 1;
for(int e = Veh[s]; e; e = Vefw[e]) {
int t = Vt[--e];
if (!V_b8[t]) {
ret = leafrch(t);
break;
}
}
V_b8[s] = 0;
return ret;
}

void solve(int c, int s)
{
V_b8[s] = 1;
if (c & 1) printf("%d\n", s+1);
for(int e=Veh[s]; e; e = Vefw[e]) {
int t = Vt[--e];
if (!V_b8[t])
solve(c+1, t);
}
if (!(c & 1)) printf("%d\n", s+1);
V_b8[s] = 0;
}

#define addedge(j,k) ({\
Vefw[Veptr] = Veh[j], Vt[Veptr] = k; Veh[j] = ++Veptr; })

int main(void)
{

scanf("%d", &N);
int fakeroot = 0;
for(int i=0; i<N-1; ++i) {
int j, k;
scanf("%d%d", &j, &k); --j, --k;
addedge(j, k), addedge(k, j);
if (k == fakeroot) fakeroot = j;
}
solve(1, leafrch(fakeroot));
return 0;
}

测试结果:



































































Test case Status Time/Limit Points
 0   OK 0.00s/0.10s 0/0
 1   OK 0.00s/0.10s 9/9
 2   OK 0.00s/0.10s 9/9
 3   OK 0.00s/0.10s 9/9
 4   OK 0.00s/0.10s 9/9
 5   OK 0.01s/0.10s 9/9
 6   OK 0.01s/0.10s 9/9
 7   OK 0.00s/0.10s 9/9
 8   OK 0.00s/0.10s 9/9
 9   OK 0.01s/0.10s 9/9
 10   OK 0.01s/0.10s 9/9
 11   OK 0.01s/0.10s 10/10

时间: 2024-11-02 13:51:32

POJ/obc - Step Traversing a Tree的相关文章

遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化

遍历二叉树   traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R分别表示遍历左子树.访问根节点.遍历右子树 可能的情况6种 排列A3 2 LDR LRD DLR DRL RLD RDL 若限定先左后右 LDR LRD  中根序遍历  后根序遍历 DLR  先根序遍历 先/中/后 序遍历 原文地址:https://www.cnblogs.com/yuanjiangw/p

HDU 1325 POJ 1308 Is It A Tree? (并查集)

这道题就是裸并查集,关键在于对不是树几种的判断 1. 空树是树 2. 森林不是树 3. 无环 或者从入度来看:1,无环:2,除了根,所有的入度为1,根入度为0:3,这个结构只有一个根,不然是森林了. 这道题本来暑假做的POJ 1308 但是HDU没有过.在于空树没有考虑. 用并查集判断有多少个森林注意编号是随机的,不是次序.... /* input: 0 0 1 1 0 0 1 2 1 2 0 0 1 2 2 3 4 5 0 0 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9

【POJ】【1741】Tree

点分治 怎么又一道叫Tree的题目……真是醉了. 本题为漆子超论文<分治算法在树的路径问题中的应用>例一 题解 : http://blog.csdn.net/sdj222555/article/details/7893862      http://blog.csdn.net/yang_7_46/article/details/9966455 既然是点分治嘛,为了保证复杂度不退化,必然要找树的重心,这一步可以通过一次dfs实现:以任意节点为根(方便起见就选1号节点了)求出每个 节点子树大小,那

HDU 1325,POJ 1308 Is It A Tree

HDU认为1>2,3>2不是树,POJ认为是,而Virtual Judge上引用的是POJ数据这就是唯一的区别....(因为这个瞎折腾了半天) 此题因为是为了熟悉并查集而刷,其实想了下其实好好利用sort应该能更简单A掉,下次有空再去试试... 题目大意:判断是否为树,so: 1,无环: 2,除了根,所有的入度为1,根入度为0: 3,这个结构只有一个根,不然是森林了:4.空树也是树,即第一次输入的两个数字为0 0则是树,其他时候输入只是结束条件 因为POJ和HDU题面一样,要求不一样,所以这题

[POJ 1308]Is It A Tree?(并查集判断图是否为一棵有根树)

Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties. There is exactly one node, called the root, t

POJ 1308 Is It A Tree? &amp;&amp; NYOJ 129 (树的判定+并查集)

[题目链接]click here~~ [题目大意]给定多对节点,判断所有节点能否组成一棵树 [解题思路]并查集的基本操作,定义node,edge,统计node和edge的数目,如果(edge==node-1||node==0)则可以成树 树的判定:n个节点,最多n-1条环,只有一个入度为边,不成0 的点,其他入度不大于1,不过要注意poj数据里如果1 1 0 0也会不符合要求,也就是不能自己指向自己 代码: /* Author:HRW 树的判定: n个节点,最多n-1条边 不成环 只有一个入度为

POJ 1308 Is It A Tree?

Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24299   Accepted: 8339 Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edge

POJ 1308 Is It A Tree?--题解报告

Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31092   Accepted: 10549 Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edg

POJ 1308 Is It A Tree? (并查集)

Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23006   Accepted: 7898 Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edge