当前位置:网站首页>[acwing] 58 weeks 4490 dyeing
[acwing] 58 weeks 4490 dyeing
2022-07-04 17:01:00 【*DDL_ GzmBlog】
Preface
Busy on Saturday , It is said that this question is very difficult , What I haven't seen dp
The result feels difficult C< B
t a g : tag : tag: thinking A tree structure
Portal :
The question :
Given a tree and the color required for each node , You can do it many times , inquiry Minimal operation Make the color of each node mild
The operation is defined as follows :
Select a node v And a color x.
Will be in the form of nodes v All nodes in the subtree of the root node ( Including nodes v) All dyed in color x.
Ideas :
Read the operation , It clearly reminds you , yes Bottom up In the process of d f s dfs dfs
Then we will consider how to dye , Obviously we can Greedy consideration , If the subtree is different in color from itself , Obviously, it needs dyeing
On the contrary, there is no need to dye , It must be The least expensive ( Prove that you don't know , obviously )
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 ;
}
边栏推荐
- FIREBIRD使用经验总结
- China tall oil fatty acid market trend report, technical dynamic innovation and market forecast
- 线性时间排序
- Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
- Application and Optimization Practice of redis in vivo push platform
- World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
- 电子元器件B2B商城系统开发:赋能企业构建进销存标准化流程实例
- APOC自定义函数和过程
- Software Engineer vs Hardware Engineer
- ECCV 2022放榜了:1629篇论文中选,录用率不到20%
猜你喜欢

Object.keys()的用法

VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题

Embedded software architecture design - function call

GO开发:如何利用Go单例模式保障流媒体高并发的安全性?

Visual studio 2019 (localdb) mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports 782

Yanwen logistics plans to be listed on Shenzhen Stock Exchange: it is mainly engaged in international express business, and its gross profit margin is far lower than the industry level

The winning rate against people is 84%, and deepmind AI has reached the level of human experts in army chess for the first time

《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下

电子元器件B2B商城系统开发:赋能企业构建进销存标准化流程实例

对人胜率84%,DeepMind AI首次在西洋陆军棋中达到人类专家水平
随机推荐
安信证券手机版下载 网上开户安全吗
[North Asia data recovery] a database data recovery case where the disk on which the database is located is unrecognized due to the RAID disk failure of HP DL380 server
Inside and outside: flow chart drawing elementary: six common mistakes
.Net 应用考虑x64生成
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?
Hair growth shampoo industry Research Report - market status analysis and development prospect forecast
Firebird experience summary
Task state rollback and data blocking tasks based on check point mechanism
Application of clock wheel in RPC
Redis: SDS source code analysis
Object. Usage of keys()
DC-2靶场搭建及渗透实战详细过程(DC靶场系列)
SQL implements split
Market trend report, technical innovation and market forecast of electrochromic glass and devices in China and Indonesia
Statistical learning: logistic regression and cross entropy loss (pytoch Implementation)
Is it safe to open an account online
How to implicitly pass values when transferring forms
tp配置多数据库
Years of training, towards Kata 3.0! Enter the safe container experience out of the box | dragon lizard Technology
Can you really use MySQL explain?