当前位置:网站首页>【codeforces Round#801 Div2 D题 Tree Queries】树形贪心结论
【codeforces Round#801 Div2 D题 Tree Queries】树形贪心结论
2022-07-17 22:57:00 【宇智波一打七~】
题目链接
题意:
给你一棵树,你需要执行多次询问来确定一个节点x的位置,对于每一次询问,你需要选择一个节点,能得到这个节点与x节点的距离是多少,问至少需要多少次询问才能确定x的位置。
分析:
有一个结论,就是选所有叶节点一定能表示出所有的节点,现在咱们来证明一下。选所有叶节点的话,叶节点的直接父节点就能唯一确定,那么咱们就把这些直接父节点作为新的叶节点来再唯一确定父节点的父节点,那么就能唯一确定所有的节点了,那么很显然选叶子节点是一定比选父节点优的,因为叶节点只有一个分支,不确定性更小,所以说如果最优解中有非叶子节点的话咱们可以替换成叶子节点而不影响最优性的,采用了交换论证的思想。当然咱们不能选所有的叶子节点,因为多余了,做法就是当遇到分叉的时候,这个分叉下面的叶子节点只删一个,这样子就能保证所有的节点都能唯一确定了。下面请看代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 200010,M = N+N;
int n,cnt;
int du[N],h[N],e[M],ne[M],idx;
void add(int a,int b){
du[a]++;
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
bool st[N];
void dfs(int u,int fa){
for(int i=h[u];i!=-1;i=ne[i]){
int j = e[i];
if(j == fa) continue;
if(!st[j] && du[j] > 2){
cnt++;
st[j] = true;
return;
}
else if(st[j] && du[j] > 2) return;
else dfs(j,u);
}
}
void solve(){
scanf("%d",&n);
for(int i=1;i<=n;i++) h[i] = -1,st[i] = false,du[i] = 0;
idx=0;cnt=0;
int ans = 0;
for(int i=1;i<n;i++){
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
int t1 = 0;
for(int i=1;i<=n;i++){
if(du[i] == 1) dfs(i,0),ans++;
else if(du[i] == 2) t1++;
}
if(ans + t1 == n) printf("1\n");
else
printf("%d\n",ans-cnt);
}
int main(){
int _;
scanf("%d",&_);
while(_--){
solve();
}
return 0;
}
边栏推荐
- MySQL 安装
- Icml2022 | géométrie multimodale Contrastive Representation Learning
- Maximum heap and heap sort and priority queue
- 天勤第九章课后习题代码
- 2036: [蓝桥杯2022初赛] 统计子矩阵(二维前缀和,一维前缀和)
- Tips for using setup
- [LeetCode]-动态规划-4
- kibana 使用json文档数据
- C - Matrix Chain Multiplication(栈的应用)
- Code runner for vs code, with more than 40million downloads! Support more than 50 languages
猜你喜欢

GYM103660H. Distance

Leetcode 1296. Divide the array into a set of consecutive numbers (solved)

【微服务】 微服务学习笔记三:利用Feign替换RestTemplate完成远程调用

模块1 作业

Achieve the effect of software login account by authorizing wechat ~ ~ unfinished

ICML2022 | 幾何多模態對比錶示學習

Google Earth engine - Classification and processing of UAV images

FMC sub card: 4-channel 12bit 3.2g, 2-channel 12bit, 6.4g AD acquisition / 5G acquisition card /6g acquisition card

3U VPX cooling conduction high performance srio/ Ethernet data exchange board

Natural language processing model of bigscience open source bloom
随机推荐
Several points to be analyzed in the domestic fpga/dsp/zynq scheme
Leetcode 1275. 找出井字棋的获胜者
JVM common tuning configuration parameters
MMRotate从零开始训练自己的数据集
Natural language processing model of bigscience open source bloom
微信小程序8-云函数
5-21 interceptor
2036: [蓝桥杯2022初赛] 统计子矩阵(二维前缀和,一维前缀和)
Li Hongyi machine learning 2022.07.15 -- error
Is it safe to buy funds in a securities account? I want to make a fixed investment in the fund
kibana 使用json文档数据
LeetCode 912.排序
GYM103660H.Distance
揭开服务网格~Istio Service Mesh神秘的面纱
Leetcode 1275. 找出井字棋的獲勝者
P1004 [noip2000 improvement group] grid access
2022/7/17
Wechat applet 7 cloud storage
Code Runner for VS Code,下载量突破 4000 万!支持超过50种语言
A - Play on Words