原题链接:https://www.hackerrank.com/challenges/even-tree/problem

思路:把树还原出来,对每个结点,计算以该结点为根的子树结点数。子树结点数为偶数的结点数量就是所求森林中树木的数量。结点数量减去一就是所求结果。

 #include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std; struct node{
int index, num_subtree;
vector<node*> children;
node(int i=,int n=):
index(i),num_subtree(n), children(vector<node*>()){}
}; node* construct_tree(int parent, int index, const vector<vector<int>>& adjLsit);
void count_even(node* root, int& cnt); int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int N, M; cin >> N >> M;
vector<vector<int>> adjList(N + );
for(int i = ; i < M; i++){
int n1, n2; cin >> n1 >> n2;
adjList[n1].push_back(n2);
adjList[n2].push_back(n1);
}
node* root = construct_tree(, , adjList);
int cnt = ;
count_even(root, cnt);
cout << cnt - << endl;
return ;
} node* construct_tree(int parent, int index, const vector<vector<int>>& adjList){
node* tmp = new node(index);
tmp->num_subtree = ;
for(int i = ; i < adjList[index].size(); i++){
if(adjList[index][i] != parent){
tmp->children.push_back(construct_tree(index, adjList[index][i], adjList));
tmp->num_subtree += tmp->children.back()->num_subtree;
}
}
return tmp;
} void count_even(node* root, int& cnt){
for(auto vit: root->children){
count_even(vit, cnt);
}
if(root->num_subtree%==){
cnt++;
}
}

Even Tree 小议的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. SAP CRM 树视图(TREE VIEW)

    树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...

  3. 无限分级和tree结构数据增删改【提供Demo下载】

    无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...

  4. 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>

    在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...

  5. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  6. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  7. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  8. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  9. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. Valgrind检测内存泄露简介

    原文地址: Valgrind 概述 体系结构 Valgrind是一套Linux下,开放源代码(GPL V2)的仿真调试工具的集合.Valgrind由内核(core)以及基于内核的其他调试工具组成.内核 ...

  2. xcode7.3 iTunes Store operation failed解决

    使用apploader上传程序 提示:如果您安装了XCode开发环境.在/Applications/XCode.app/Contents/Applications目录中可以找到Application ...

  3. 一个完整的Node.js RESTful API

    前言 这篇文章算是对Building APIs with Node.js这本书的一个总结.用Node.js写接口对我来说是很有用的,比如在项目初始阶段,可以快速的模拟网络请求.正因为它用js写的,跟i ...

  4. Html语言,使用<a>标签发送电子邮件

    <a>标签有一个功能是可以链接Email地址,使用mailto能让访问者便捷向网站管理者发送电子邮件. 使用mailto 属性时请参考下表: 如果mailto里同时有多个参数,第一个参数必 ...

  5. 洗礼灵魂,修炼python(1)--python简介

    首先,本人也是刚接触python短短几个月,没有老鸟的经验和技能,大佬勿喷,以下所有皆是本人对python的理解 python,是一种解释型(高级)的,面向对象的,带有动态语义的高级程序设计的开源语言 ...

  6. 自适应 Tab 宽度可以滑动文字逐渐变色的 TabLayout(仿今日头条顶部导航)

    TabLayout相信大家都用过,2015年Google大会上发布了新的Android Support Design库里面包含了很多新的控件,其中就包含TabLayout,它可以配合ViewPager ...

  7. windowsxp_电脑桌面显示不出来。

    问题:在工作的时候遇到电脑桌面显示不出来 解决方案: 1.结束explorer.exe进程 2.新建一个explorer.exe进程

  8. Hadoop(六)之HDFS的存储原理(运行原理)

    前言 其实说到HDFS的存储原理,无非就是读操作和写操作,那接下来我们详细的看一下HDFS是怎么实现读写操作的! 一.HDFS读取过程 1)客户端通过调用FileSystem对象的open()来读取希 ...

  9. 详解变量声明加 var 和不加 var 的区别

    在全局作用域中声明变量加 var 关键字和不加 var ,js 引擎都会将这个变量声明为全局变量,在实际运行时,两种声明方式的变量的行为也是几乎一致的.但是在全局作用域下是否声明一个变量的 时候加va ...

  10. Hibernate的一对多查询及去掉重复的对象distinct

    问:sql 中 select * from A left join B on A.id=B.id where A.id=? 如果在Hibernate 中 用HQL 怎么表达呢 ?答:from A le ...