当前位置:网站首页>dp学习笔记
dp学习笔记
2022-08-09 06:36:00 【先求一个导】
题目
题意: n个点的树,每个节点具有一种颜色。任选一个点作为根,使得所有以非根为节点的子树满足颜色一致。
思路: 找规律,如果存在某个点满足所有特殊边都连过他,即满足。特殊边是指连接不同颜色的边。
或者树形dp,但是难度有些高。先一遍dfs求出以1为根是否满足条件,之后再dfs。如果某个点的所有儿子均满足条件,可;否则,只有当有且仅有一个儿子不满足条件且当前节点与父节点同色,且其他儿子与当前节点同色且满足条件才行。日,打死俺也想不出来的换根dp.
时间复杂度: O(n)
代码:
// Problem: A. Timofey and a tree
// Contest: Codeforces - Codeforces Round #395 (Div. 1)
// URL: https://codeforces.com/problemset/problem/763/A
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int n,m,k,T;
int a[N];
vector<int> va[N];
bool f[N]; //以i为根是否同色
void dfs(int cur,int fa)
{
f[cur] = 1;
for(int j:va[cur])
{
if(j==fa) continue;
dfs(j,cur);
if(!f[j]||a[cur]!=a[j]) f[cur]=0;
}
}
int dfs2(int cur,int fa)
{
// cout<<cur<<":"<<fa<<"\n";
int cnt = 0; //有几个儿子满足条件
int cnt2 = 0; //有几个儿子满足条件且与父节点同色,这样才能换根之后以父节点连的其他分支满足条件
int wh = 0;
for(int j:va[cur])
{
if(j==fa) continue;
if(f[j])
{
cnt++;
if(a[cur]==a[j]) cnt2++;
}
else wh = j;
}
int tot = va[cur].size();
if(cur!=1) tot--;
if(cnt == tot) return cur;
if(cnt2 == tot-1 && a[cur]==a[fa])
{
int t = dfs2(wh,cur);
if(t!=-1) return t;
}
return -1;
}
void solve()
{
cin>>n;
for(int i=0;i<n-1;++i)
{
int x,y; cin>>x>>y;
va[x].push_back(y),va[y].push_back(x);
}
for(int i=1;i<=n;++i) cin>>a[i]; a[0] = a[1];
dfs(1,0);
// for(int i=1;i<=n;++i) cout<<i<<":"<<f[i]<<"\n";
int ans = dfs2(1,0);
if(ans==-1) cout<<"NO";
else cout<<"YES\n"<<ans;
}
signed main(void)
{
T = 1;
// cin>>T;
while(T--)
solve();
return 0;
}
边栏推荐
- 字节跳动笔试题2020 (抖音电商)
- Word文件的只读模式没有密码怎么退出?
- Program Performance Analysis - Complexity Analysis
- ByteDance Written Exam 2020 (Douyin E-commerce)
- Thread Pool Summary
- C language implements sequential stack and chain queue
- Integer 线程安全的
- 事务总结
- 图论,二叉树,dfs,bfs,dp,最短路专题
- The working principle of the transformer (illustration, schematic explanation, understand at a glance)
猜你喜欢
IQ Products CMV Brite Turbo试剂盒的原理
中英文说明书丨CalBioreagents 山羊抗人白蛋白,IgG组分
sql problem solving statement to create a table
longest substring without repeating characters
Redis 2 - 高级
Import the pycharm environment package into another environment
工控设备的系统如何进行加固
A test engineer with an annual salary of 35W was laid off. Personal experience: advice that you have to listen to
Teach you how to make the Tanabata meteor shower in C language - elegant and timeless (detailed tutorial)
Use baidu EasyDL intelligent bin
随机推荐
mmdetection源码解析--ResNet18
P7阿里面试题2020.07 之滑动窗算法(阿里云面试)
逆向工程
抗菌药物丨Toronto Research Chemicals 天冬酰胺D
中英文说明书丨CalBioreagents 山羊抗人白蛋白,IgG组分
INSTALL_RPATH and BUILD_RPATH problem in CMake
pycharm环境包导入到另外一个环境
Leetcode 70 stairs issues (Fibonacci number)
Introduction and use of BeautifulSoup4
什么是excel文件保护
电学知识的疑问
XxlJobConfig分布式定时器任务管理XxlJob配置类,替代
AD画PCB板教程 20分钟讲清楚操作流程 铺铜 网络标号
单例 DCL(double check lock) 饱汉模式和饿汉模式
e-learning summary
运放-运算放大器经典应用电路大全-应用电路大全
2022-08-08: Given an array arr, it represents the height of the missiles that will appear in order from morning to night.When the cannon shoots missiles, once the cannon is set to shoot at a certain h
单例模式
APP商品详情源数据接口(淘宝/京东/拼多多/苏宁/抖音等平台详情数据分析接口)代码对接教程
C# 利用iTextSharp画PDF