当前位置:网站首页>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;
}
边栏推荐
- AD picture PCB tutorial 20 minutes clear label shop operation process, copper network
- 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
- install flask
- XxlJobConfig distributed timer task management XxlJob configuration class, replace
- 运算放大器(OPA)超详细参数讲解-运放---以及8个型号的运算放大器分析对比
- String.toLowerCase(Locale.ROOT)
- The working principle of the transformer (illustration, schematic explanation, understand at a glance)
- SIGINT,SIGKILL,SIGTERM信号区别,各类信号总结
- Error jinja2.exceptions.UndefinedError: 'form' is undefined
- AD画PCB板教程 20分钟讲清楚操作流程 铺铜 网络标号
猜你喜欢
字节跳动笔试题2020 (抖音电商)
workbench 数据导出
C语言实现顺序栈和链队列
ZIP压缩包文件删除密码的方法
Built-in macros in C language (define log macros)
CMake中INSTALL_RPATH与BUILD_RPATH问题
05 多线程与高并发 - ThreadPoolExecutor 源码解析
6 states of a thread
The solution that does not work and does not take effect after VScode installs ESlint
Output method of list string print(*a) print(““.join(str(c) for c in a) )
随机推荐
Error: flask: TypeError: 'function' object is not iterable
workbench 数据导出
Excel受保护的工作表怎么操作?
pycharm环境包导入到另外一个环境
报错:FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disab
VS2019常用快捷键
[HNOI2002]营业额统计
运算放大器(OPA)超详细参数讲解-运放---以及8个型号的运算放大器分析对比
The singleton pattern
Program Performance Analysis - Complexity Analysis
代码目录结构
2022.8.8DAY628
cut命令的使用实例
分布式id 生成器实现
longest substring without repeating characters
bzoj 5333 [Sdoi2018]荣誉称号
逆向工程
线程的6种状态
一道很简答但是没答对的SQL题
The division principle summary within the collection