当前位置:网站首页>[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 ;
}
边栏推荐
- ONgDB图数据库与Spark的集成
- .Net 应用考虑x64生成
- [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
- GO开发:如何利用Go单例模式保障流媒体高并发的安全性?
- Daily notes~
- Research Report on market supply and demand and strategy of surgical stapler industry in China
- 手里10万元存款买什么理财产品收益最高?
- 线程池的使用和原理
- 基于check-point实现图数据构建任务
- Accounting regulations and professional ethics [10]
猜你喜欢
L1-072 scratch lottery
嵌入式软件架构设计-函数调用
Years of training, towards Kata 3.0! Enter the safe container experience out of the box | dragon lizard Technology
MFC implementation of ACM basic questions encoded by the number of characters
How to decrypt worksheet protection password in Excel file
D3D11_ Chili_ Tutorial (2): draw a triangle
如何实现一个延时队列 ?
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
Visual Studio 2019 (LocalDB)MSSQLLocalDB SQL Server 2014 数据库版本为852无法打开,此服务器支持782
Principle and general steps of SQL injection
随机推荐
Four point probe Industry Research Report - market status analysis and development prospect prediction
Oracle监听器Server端与Client端配置实例
【云原生】服务网格是什么“格”?
FIREBIRD使用经验总结
中位数与次序统计量
Market trend report, technical innovation and market forecast of taillight components in China
SQL implements split
NoSQL之readis配置与优化(终章)
Hair and fuzz interceptor Industry Research Report - market status analysis and development prospect forecast
APOC custom functions and procedures
Capvision Rongying's prospectus in Hong Kong was "invalid": it was strictly questioned by the CSRC and required supplementary disclosure
Daily notes~
c# 实现定义一套中间SQL可以跨库执行的SQL语句
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
C implementation defines a set of intermediate SQL statements that can be executed across libraries
力扣今日题-1200. 最小绝对差
周大福践行「百周年承诺」,真诚服务推动绿色环保
Start by counting
2022PMP考试基本情况详情了解
Lv166 turned over