当前位置:网站首页>[Acwing] 58周赛 4490. 染色
[Acwing] 58周赛 4490. 染色
2022-07-04 15:05:00 【*DDL_GzmBlog】
前言
周六的时候忙,听说这题挺难的,什么没见过的dp
结果感觉难度 C< B
t a g : tag : tag:思维树形结构
传送门 :
题意 :
给定一棵树和每个节点需要的颜色,你可以进行多次操作,询问最少的操作使得每个节点的颜色温和
操作定义如下 :
选择一个节点 v 和一种颜色 x。
将以节点 v 为根节点的子树中的全部节点(包括节点 v)都染成颜色 x。
思路 :
看完操作,很明显的就提示你了,是 自下而上的进行 d f s dfs dfs
然后我们再考虑怎么进行染色,显然我们可以贪心的考虑, 如果子树和本身颜色不同,显然是需要染色的
反之不需要染色, 这肯定是花费最小的(证明不知道,显然)
Mycode :
#include <iostream>
#include <vector>
#include <map>
#include <cstring>
#include <queue>
#include <math.h>
#include <set>
#include <stack>
#include <algorithm>
using namespace std;
#define IOS ios::sync_with_stdio(false);
#define CIT cin.tie(0);
#define COT cout.tie(0);
#define ll long long
#define x first
#define y second
#define pb push_back
#define endl '\n'
#define all(x) (x).begin(),x.end()
#define Fup(i,a,b) for(int i=a;i<=b;i++)
#define Fde(i,a,b) for(int i=a;i>=b;i--)
#define cer(a) cerr<<#a<<'='<<(a)<<" @ line "<<__LINE__<<" "<<endl
typedef priority_queue<int,vector<int>,greater<int>> Pri_m;
typedef pair<int,int> pii;
typedef vector<int> VI;
map<int,int> mp;
const int N = 2e5+10,INF = 0x3f3f3f3f;
const double eps = 1e-5;
struct node{
int to,val;
};
vector<int> g[N];
int col[N];
int n,i;
int ans = 1;
void dfs(int u,int fa){
for(auto x : g[u]){
if(x == fa)continue;
dfs(x,u);
if(col[x] != col[u]) ++ ans;
}
}
void solve(){
cin>>n;
Fup(i,2,n){
int x;cin>>x;
g[x].pb(i);
}
Fup(i,1,n) cin>>col[i];
dfs(1,0);
cout<<ans<<endl;
}
int main(){
//int t;cin>>t;while(t--)
solve();
return 0 ;
}
边栏推荐
- Accounting regulations and professional ethics [10]
- Visual studio 2019 (localdb) mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports 782
- Vscode setting outline shortcut keys to improve efficiency
- 周大福践行「百周年承诺」,真诚服务推动绿色环保
- 基于check-point机制的任务状态回滚和数据分块任务
- Accounting regulations and professional ethics [7]
- 最大子数组与矩阵乘法
- 时序图数据建模与产业链分析
- China's plastic processing machinery market trend report, technological innovation and market forecast
- Research Report of exoskeleton robot industry - market status analysis and development prospect prediction
猜你喜欢

世界环境日 | 周大福用心服务推动减碳环保

对人胜率84%,DeepMind AI首次在西洋陆军棋中达到人类专家水平

Capvision Rongying's prospectus in Hong Kong was "invalid": it was strictly questioned by the CSRC and required supplementary disclosure

Interface fonctionnelle, référence de méthode, Widget de tri de liste implémenté par lambda

嵌入式软件架构设计-函数调用
Can you really use MySQL explain?

overflow:auto与felx结合的用法

Understand ThreadLocal in one picture

基于wifi控制的51单片机温度报警器

The vscode waveform curve prompts that the header file cannot be found (an error is reported if the header file exists)
随机推荐
Understand ThreadLocal in one picture
How can programmers improve the speed of code writing?
Hair growth shampoo industry Research Report - market status analysis and development prospect forecast
Model fusion -- stacking principle and Implementation
函數式接口,方法引用,Lambda實現的List集合排序小工具
照明行业S2B2B解决方案:高效赋能产业供应链,提升企业经济效益
散列表
祝贺Artefact首席数据科学家张鹏飞先生荣获 Campaign Asia Tech MVP 2022
Unity interview questions (continuously updated)
Integration of ongdb graph database and spark
Hair and fuzz interceptor Industry Research Report - market status analysis and development prospect forecast
安信证券排名 网上开户安全吗
《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下
Software Engineer vs Hardware Engineer
Use and principle of thread pool
Go development: how to use go singleton mode to ensure the security of high concurrency of streaming media?
基于wifi控制的51单片机温度报警器
基于check-point机制的任务状态回滚和数据分块任务
FIREBIRD使用经验总结
. Net applications consider x64 generation