当前位置:网站首页>[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 ;
}
边栏推荐
- Inside and outside: flow chart drawing elementary: six common mistakes
- C# 实现 FFT 正反变换 和 频域滤波
- Hair growth shampoo industry Research Report - market status analysis and development prospect forecast
- 安信证券手机版下载 网上开户安全吗
- NoSQL之readis配置与优化(终章)
- 电子元器件B2B商城系统开发:赋能企业构建进销存标准化流程实例
- Median and order statistics
- Research Report on market supply and demand and strategy of tetramethylpyrazine industry in China
- [Acwing] 58周赛 4489. 最长子序列
- Go语言循环语句(第10课下)
猜你喜欢
电子元器件B2B商城系统开发:赋能企业构建进销存标准化流程实例
祝贺Artefact首席数据科学家张鹏飞先生荣获 Campaign Asia Tech MVP 2022
Why do you say that the maximum single table of MySQL database is 20million? Based on what?
Object.keys()的用法
Can you really use MySQL explain?
【Go ~ 0到1 】 第六天 文件的读写与创建
Principle and general steps of SQL injection
Solution du système de gestion de la chaîne d'approvisionnement du parc logistique intelligent
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
51 single chip microcomputer temperature alarm based on WiFi control
随机推荐
DC-2靶场搭建及渗透实战详细过程(DC靶场系列)
[Chongqing Guangdong education] National Open University spring 2019 1248 public sector human resource management reference questions
手里10万元存款买什么理财产品收益最高?
Understand asp Net core - Authentication Based on jwtbearer
矿产行业商业供应链协同系统解决方案:构建数智化供应链平台,保障矿产资源安全供应
Lv166 turned over
egg. JS learning notes
【云原生】服务网格是什么“格”?
线程池的使用和原理
2022PMP考试基本情况详情了解
Research Report of exoskeleton robot industry - market status analysis and development prospect prediction
Inside and outside: flow chart drawing elementary: six common mistakes
Hair growth shampoo industry Research Report - market status analysis and development prospect forecast
Array filter fliter in JS
Jump table instance
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?
Statistical learning: logistic regression and cross entropy loss (pytoch Implementation)
Solution du système de gestion de la chaîne d'approvisionnement du parc logistique intelligent
Position encoding practice in transformer
Principle and general steps of SQL injection