虽然官方解释是这题目里的树看作无向无环图,从答案来看还是在“以1作为根节点”这一前提下进行的,这棵树搭建好以后,从叶节点开始访问,一直推到根节点即可——很像动态规划的“自底向上”。

但这棵树的搭建堪忧:给出的边不知道哪边更接近根节点。所以我给出的方案干脆在两个顶点都将对方加成孩子,等到访问的时候再作处理,根据从1这个根节点开始访问这个特性,额外加一个“isVisited"来做区分。

然后利用栈对树进行非递归访问

/**
* For best-coder problem 3
*/
#include <iostream>
using namespace std; #include <set>
#include <stack> struct Node
{
public:
Node() :mIsVisited(false) {} bool mIsVisited;
set< int > mChilds;
set< int > mColorSet;
}; int main()
{
int nNode, nCounting;
while( cin >> nNode >> nCounting )
{
Node node[50001]; for( int i=1; i<nNode; i++ )
{
int a, b;
cin >> a >> b;
node[a].mChilds.insert(b);
node[b].mChilds.insert(a);
} for( int i=0; i<nCounting; i++ )
{
int n, color;
cin >> n >> color;
node[n].mColorSet.insert(color);
} stack<int> nodeStack; node[1].mIsVisited = true;
nodeStack.push(1); do{
int currentTop = nodeStack.top();
Node& topNode = node[currentTop]; set<int> & topChilds = topNode.mChilds;
set<int> & topColors = topNode.mColorSet; for( set<int>::iterator ci = topChilds.begin();
ci != topChilds.end();
ci++ )
{
int child = *ci;
if( node[child].mIsVisited )
{
topChilds.erase(child);
continue;
} node[child].mIsVisited = true;
nodeStack.push(child);
break;
} // it's a leaf child
if( topChilds.empty() )
{
nodeStack.pop(); if( nodeStack.empty() ) continue; Node& topNode = node[ nodeStack.top() ];
topNode.mColorSet.insert(topColors.begin(),topColors.end());
topNode.mChilds.erase(currentTop);
continue;
}
}while(!nodeStack.empty()); // output
for( int i=1; i<=nNode; i++ )
{
cout << node[i].mColorSet.size();
if( i != nNode )
{
cout << " ";
}else{
cout << endl;
}
}
}
}

  

Best Coder Round#25 1003 树的非递归访问的更多相关文章

  1. Best Coder Round#25 1001 依赖检测

    原题大致上就是检测一系列进程之间是否存在循环依赖的问题,形如: a->b->c->a,  a->a ,都行成了循环依赖,事实上可以视为“检测链表中是否存在环” AC代码: #i ...

  2. SplayTree伸展树的非递归实现(自底向上)

    Splay Tree 是二叉查找树的一种,它与平衡二叉树.红黑树不同的是,Splay Tree从不强制地保持自身的平衡,每当查找到某个节点n的时候,在返回节点n的同时,Splay Tree会将节点n旋 ...

  3. 从lca到树链剖分 bestcoder round#45 1003

    bestcoder round#45 1003 题,给定两个点,要我们求这两个点的树上路径所经过的点的权值是否出现过奇数次.如果是一般人,那么就是用lca求树上路径,然后判断是否出现过奇数次(用异或) ...

  4. Codeforces Round #270 1003

    Codeforces Round #270 1003 C. Design Tutorial: Make It Nondeterministic time limit per test 2 second ...

  5. 二叉树之AVL树的平衡实现(递归与非递归)

    这篇文章用来复习AVL的平衡操作,分别会介绍其旋转操作的递归与非递归实现,但是最终带有插入示例的版本会以递归呈现. 下面这张图绘制了需要旋转操作的8种情况.(我要给做这张图的兄弟一个赞)后面会给出这八 ...

  6. hdu5044 Tree 树链拆分,点细分,刚,非递归版本

    hdu5044 Tree 树链拆分.点细分.刚,非递归版本 //#pragma warning (disable: 4786) //#pragma comment (linker, "/ST ...

  7. 树的广度优先遍历和深度优先遍历(递归非递归、Java实现)

    在编程生活中,我们总会遇见树性结构,这几天刚好需要对树形结构操作,就记录下自己的操作方式以及过程.现在假设有一颗这样树,(是不是二叉树都没关系,原理都是一样的) 1.广度优先遍历 英文缩写为BFS即B ...

  8. Codeforces Beta Round #25 (Div. 2 Only)

    Codeforces Beta Round #25 (Div. 2 Only) http://codeforces.com/contest/25 A #include<bits/stdc++.h ...

  9. Educational Codeforces Round 25 E. Minimal Labels&&hdu1258

    这两道题都需要用到拓扑排序,所以先介绍一下什么叫做拓扑排序. 这里说一下我是怎么理解的,拓扑排序实在DAG中进行的,根据图中的有向边的方向决定大小关系,具体可以下面的题目中理解其含义 Educatio ...

随机推荐

  1. swift_属性观察者

    //: Playground - noun: a place where people can play import Cocoa var str = "Hello, playground& ...

  2. angular-ui-router中的$stateProvider设置

    $stateProvider .state('contacts.list', { url: '', templateUrl: 'contacts.list.html' }) .state('conta ...

  3. css样式大全

    字体属性:(font) 大小 {font-size: x-large;}(特大) xx-small;(极小) 一般中文用不到,只要用数值就可以,单位:PX.PD 样式 {font-style: obl ...

  4. virtualbox安装增强功能(centos6.5)

    vitualbox安装增强功能(centos 6.5) 1. 安装依赖包 #yum install kernel-headers-$(uname -r) #yum install kernel-dev ...

  5. 关于maven

    主要涉及的配置文件是setting.xml与pom.xml 其中setting配置文件主要负责加载jar包路径设置, pom.xml文件主要负责jar包配置. 包含jar包的版本. maven打包:之 ...

  6. MySql学习(二) —— where / having / group by / order by / limit 简单查询

    注:该MySql系列博客仅为个人学习笔记. 这篇博客主要记录sql的五种子句查询语法! 一个重要的概念:将字段当做变量看,无论是条件,还是函数,或者查出来的字段. select五种子句 where 条 ...

  7. 程设大作业xjb写——魔方复原

    鸽了那么久总算期中过[爆]去[炸]了...该是时候写写大作业了 [总不能丢给他们不会写的来做吧 一.三阶魔方的几个基本定义 ↑就像这样,可以定义面的称呼:上U下D左L右R前F后B UD之间的叫E,LR ...

  8. blade and soul races guide

    Race Four races are available for those who wish to choose the path of martial arts: the careful Gon ...

  9. HDU 1513 最长子序列

    Palindrome Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  10. POJ 3176 Cow Bowling

    Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13016   Accepted: 8598 Desc ...