Even Tree 小议
原题链接: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 小议的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- SAP CRM 树视图(TREE VIEW)
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...
- 无限分级和tree结构数据增删改【提供Demo下载】
无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...
- 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- Vuforia开发完全指南---License Manager和Target Manager详解
License Manager和Target Manager License Manager 对于每一个用Vuforia开发的AR程序来说,都有一个唯一的license key,在Unity中必须首先 ...
- 第4章 同步控制 Synchronization ----互斥器(Mutexes)
Win32 的 Mutex 用途和 critical section 非常类似,但是它牺牲速度以增加弹性.或许你已经猜到了,mutex 是 MUTual EXclusion 的缩写.一个时间内只能够有 ...
- 非常有用的css使用总结
积小流以成江海,很多东西你不总结就不是你的东西 常用css总结: /*设置字体*/ @font-face { font-family: 'myFont'; src: url('../font/myFo ...
- [js高手之路] html5 canvas动画教程 - 实时获取鼠标的当前坐标
有了前面的canvas基础之后,现在开始就精彩了,后面写的canvas教程都是属于综合应用,前面已经写了常用的canvas基础知识,参考链接如下: [js高手之路] html5 canvas系列教程 ...
- 洗礼灵魂,修炼python(3)--从一个简单的print代码揭露编码问题,运行原理和语法习惯
前期工作已经准备好后,可以打开IDE编辑器了,你可以选择python自带的IDLE,也可以选择第三方的,这里我使用pycharm--一个专门为python而生的IDE 按照惯例,第一个python代码 ...
- 再起航,我的学习笔记之JavaScript设计模式28(委托模式)
## 委托模式 ### 概念介绍 **委托模式(Entrust): **多个对象接收并处理同一请求,他们将请求委托给另一个对象统一处理请求. ### 利用委托优化循环 如果我们有一个需求需要让用户点击 ...
- PHP通过URL获取文件大小
function getFileSize($url){ $url = parse_url($url); if($fp = @fsockopen($url['host'],empty($url['por ...
- 关于xamarin.forms 中 list 的loadmore
前言 最近几天在研究上拉加载啊,下拉刷新啊什么的.然而坑爹的事情总是那么多.在xamarin.forms中,list自带的,并没有上拉加载的这个属性(难道当初他们封装方法时,就不会想到数据多了会咋整吗 ...
- install xdebug
安装准备 安排php的xdebug扩展,在php.ini上配置xdebug.通过phpinfo或者php-m 查看 [Xdebug] zend_extension ="D:\upupw7\P ...
- Jquery 使用Ajax获取后台返回的Json数据后,页面处理
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...