当前位置:网站首页>[codeworks round 801 div2 D tree queries] tree greedy conclusion
[codeworks round 801 div2 D tree queries] tree greedy conclusion
2022-07-27 05:30:00 【Yuzhibo one dozen seven~】
Topic link
The question :
Give you a tree , You need to perform multiple queries to determine a node x The location of , For every inquiry , You need to select a node , You can get this node and x What is the distance between nodes , Ask at least how many times it takes to be sure x The location of .
analysis :
There is a conclusion , That is, selecting all leaf nodes must represent all nodes , Now let's prove it . If all leaf nodes are selected , The direct parent node of the leaf node can be uniquely determined , Then let's take these direct parent nodes as new leaf nodes to uniquely determine the parent node of the parent node , Then we can uniquely determine all nodes , Then it is obvious that selecting leaf nodes is better than selecting parent nodes , Because the leaf node has only one branch , Less uncertainty , So if there are non leaf nodes in the optimal solution, we can replace them with leaf nodes without affecting the optimality , The idea of exchange argument is adopted . Of course, we can't choose all leaf nodes , Because it's redundant , The way to do it is when you encounter bifurcation , Only one leaf node under this bifurcation is deleted , This will ensure that all nodes can be uniquely identified . Now look at the code :
#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;
}
边栏推荐
猜你喜欢

Utility gadget: kotlin code snippet

Li Hongyi machine learning team learning punch in activity day03 --- error and gradient decline

Scientific Computing Library - numpy

Shell course summary

Graph cuts learning

上传七牛云的方法

Multiplication sorting in torch, * & torch. Mul () & torch. MV () & torch. Mm () & torch. Dot () & @ & torch. Mutmal ()

Integrate SSM

flask项目配置

李宏毅机器学习组队学习打卡活动day04---深度学习介绍和反向传播机制
随机推荐
Notes series k8s orchestration MySQL container - stateful container creation process
Redis transaction
Derivation and explanation of PBR physical illumination calculation formula
创建项目 实现登录注册,生成jwt,发送验证码
B1030 perfect sequence
Differences and examples between internal classes and static internal classes
p7 day1 初识Flask框架
异步数据-短信验证码
封装JWT
登录到主页功能实现
Flask登录实现
简化JDBC的MyBits框架
Shell course summary
Differences among bio, NiO and AIO
redis事务
Find the number of combinations (the strongest optimization)
Set static IP for raspberry pie
图片上传的逻辑
Redis lock
李宏毅机器学习组队学习打卡活动day02---回归