当前位置:网站首页>[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 ;
}
边栏推荐
- Research Report on market supply and demand and strategy of China's plastics and polymer industry
- 函數式接口,方法引用,Lambda實現的List集合排序小工具
- APOC custom functions and procedures
- 照明行业S2B2B解决方案:高效赋能产业供应链,提升企业经济效益
- Accounting regulations and professional ethics [11]
- Object.keys()的用法
- Years of training, towards Kata 3.0! Enter the safe container experience out of the box | dragon lizard Technology
- 安信证券网上开户安全吗 开户收费吗
- 力扣今日题-1200. 最小绝对差
- Li Kou today's question -1200 Minimum absolute difference
猜你喜欢

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

Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding

~89 deformation translation

TypeError: list indices must be integers or slices, not str

Principle and general steps of SQL injection

D3D11_ Chili_ Tutorial (2): draw a triangle
Can you really use MySQL explain?

昆明三环闭合工程将经过这些地方,有在你家附近的吗?
随机推荐
Research Report on plastic recycling machine industry - market status analysis and development prospect forecast
多年锤炼,迈向Kata 3.0 !走进开箱即用的安全容器体验之旅| 龙蜥技术
What is torch NN?
Detailed process of DC-2 range construction and penetration practice (DC range Series)
散列表
基于check-point机制的任务状态回滚和数据分块任务
ECCV 2022放榜了:1629篇论文中选,录用率不到20%
APOC custom functions and procedures
Practice: fabric user certificate revocation operation process
Overflow: the combination of auto and Felx
System. Currenttimemillis() and system Nanotime (), which is faster? Don't use it wrong!
Market trend report, technical innovation and market forecast of tetrabromophthalate (pht4 diol) in China
c# 实现定义一套中间SQL可以跨库执行的SQL语句
同构图与异构图CYPHER-TASK设计与TASK锁机制
Accounting regulations and professional ethics [10]
智慧物流園區供應鏈管理系統解决方案:數智化供應鏈賦能物流運輸行業供應鏈新模式
How to "use" Perl modules in directories that are not in @inc- How do I 'use' a Perl module in a directory not in @INC?
tp配置多数据库
Daily notes~
表单传递时,如何隐式将值传过去