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

【颜色平衡树 / 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.kler.cn/news/335519.html

相关文章:

  • 【ubuntu】Ubuntu20.04安装中文百度输入法
  • 力扣刷题 | 两数之和
  • web网页项目--用户登录,注册页面代码
  • Flink源码剖析
  • 统计方形(暴力枚举)
  • sql-server【bcp工具】
  • 20.1 分析pull模型在k8s中的应用,对比push模型
  • redis+mysql数据一致性+缓存穿透解决方案
  • Python知识点:如何使用SpaCy进行文本预处理与分析
  • Python知识点:如何使用Multiprocessing进行并行任务管理
  • Java | Leetcode Java题解之第457题环形数组是否存在循环
  • Golang | Leetcode Golang题解之第455题分发饼干
  • L1415 【哈工大_操作系统】CPU调度策略一个实际的schedule函数
  • [Offsec Lab] ICMP Monitorr-RCE+hping3权限提升
  • Kotlin真·全平台——Kotlin Compose Multiplatform Mobile(kotlin跨平台方案、KMP、KMM)
  • 4款专业电脑数据恢复软件,帮你保障数据安全。
  • CUDA与TensorRT学习四:模型部署基础知识、模型部署的几大误区、模型量化、模型剪枝、层融合
  • Oracle RAC中停止has、crs、cluster的区别
  • WooCommerce与wordpress是什么关系
  • 遥感影像-实例分割数据集:iSAID 从切图到YOLO格式数据集制作详细介绍