当前位置: 首页 > news >正文

【颜色平衡树 / E】

题目

思路

  • DFS暴力 60分

代码

#include <bits/stdc++.h>
using namespace std;
const int N = 5010;
const int M = 5010;
int h[N], e[M], ne[M], idx;
int c[N], f;
int ans;
void add(int a, int b)  // 添加一条边a->b
{e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}
bool check(int tmp[])
{int last = 0;for(int i = 1; i <= 5000; i++){if(!last && tmp[i]) last = tmp[i];else if(last && tmp[i]){if(last != tmp[i]) return false;last = tmp[i];}}return true;
}
void dfs(int u, int f[])
{int tmp[N] = {0};//根节点颜色算进子树tmp[c[u]] += 1;bool leaf = true;for(int i = h[u]; ~i; i = ne[i]){int j = e[i];dfs(j, tmp);leaf = false;}//判断平衡性if(leaf) ans++;else{if(check(tmp)) ans++;}//呈递颜色for(int i = 1; i <= 5000; i++)f[i] += tmp[i];
}
int main()
{int n;cin >> n;memset(h, -1, sizeof h);for(int i = 1; i <= n; i++){cin >> c[i] >> f;add(f, i);}int tmp[N] = {0};dfs(1, tmp);cout << ans;
}


http://www.mrgr.cn/news/44255.html

相关文章:

  • Java之二叉树的基本操作实现
  • LCD屏入门(基于ESP-IDF、SPI屏)
  • D29【python 接口自动化学习】- python基础之输入输出与文件操作
  • 菜鸟笔记004 获取目标对象的渐变颜色值
  • LeetCode讲解篇之139. 单词拆分
  • 设计模式之装饰器模式(Decorator)
  • history的pushState/replaceState理解
  • vSAN05:vSAN延伸集群简介与创建、资源要求与计算、高级功能配置、维护、故障处理
  • 突触可塑性与STDP:神经网络中的自我调整机制
  • 电子信息类专业技术学习及比赛路线总结(大一到大三)
  • LeetCode hot100---栈专题(C++语言)
  • 10月5日刷题记录
  • 数据结构与算法篇(树 - 常见术语)
  • vue.js组建开发
  • 数据结构与算法篇(图)(持续更新迭代)
  • 【LeetCode-热题100-128题】官方题解好像有误
  • 【重学 MySQL】五十八、文本字符串(包括 enum set)类型
  • 如 有 任 何 问 题 ,请 及 时 联 系 我 们 反 馈 !
  • 一个值得关注的3D生成新算法:速度和图像生成平齐,能生成合理的展开贴图和高质量mesh
  • 19年408数据结构