当前位置:网站首页>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;
}
边栏推荐
- C语言的内置宏(定义日志宏)
- 运放-运算放大器经典应用电路大全-应用电路大全
- The working principle of the transformer (illustration, schematic explanation, understand at a glance)
- Explain the wait() function and waitpid() function in C language in detail
- 代码目录结构
- A test engineer with an annual salary of 35W was laid off. Personal experience: advice that you have to listen to
- 阿里巴巴官方技术号
- APP product source data interface (taobao, jingdong/spelling/suning/trill platform details a lot data analysis interface) code and docking tutorial
- Service
- workbench 数据导出
猜你喜欢

leetcode 之 70 爬楼梯问题 (斐波那契数)

无重复的字符的最长子串

Altium designer software commonly used the most complete package library, including schematic library, PCB library and 3D model library

CalBioreagents超全Id 蛋白兔单克隆抗体,助力科研

Distributed id generator implementation

Zero shift of leetcode

中英文说明书丨CalBioreagents 醛固酮单克隆抗体

pdf加密、找回密码

DevNet: Deviation Aware Networkfor Lane Detection

使用百度EasyDL实现智能垃圾箱
随机推荐
Altium designer软件常用最全封装库,包含原理图库、PCB库和3D模型库
Error: flask: TypeError: 'function' object is not iterable
【Wwise】ArgumentException: The specified path is not of a legal form (empty). About the path reading error in WwiseGlobal
6 states of a thread
XxlJobConfig分布式定时器任务管理XxlJob配置类,替代
一道很简答但是没答对的SQL题
我入职阿里后,才知道原来简历这么写
XxlJobConfig distributed timer task management XxlJob configuration class, replace
CMake中INSTALL_RPATH与BUILD_RPATH问题
Simple Factory Pattern
像天才一样思考:如何培养自己的创造力?
单例模式
Gao Zelong, a famous digital collection expert and founder of the Digital Collection Conference, was interviewed by China Entrepreneur Magazine
String.toLowerCase(Locale.ROOT)
线程池总结
SIGINT,SIGKILL,SIGTERM信号区别,各类信号总结
crc计算
集合内之部原理总结
单例 DCL(double check lock) 饱汉模式和饿汉模式
常用Oracle命令