当前位置:网站首页>Codeworks round 801 (Div. 2) and epic Institute of technology round D. tree queries (tree DP)
Codeworks round 801 (Div. 2) and epic Institute of technology round D. tree queries (tree DP)
2022-07-28 17:10:00 【Code92007】
subject
t(t<=1e4) Group example , Give one at a time n(n<=2e5) A rootless tree with two dots ,
The author thinks of a tree point , Record as point x,
Find the minimum number of points k, Make no matter x What point is it ,
You ask this k A point and x Distance of , After the questioner answers one by one ,
You can find out what the person who made the question thought x What's the point ,
You don't need to actually give a dot , Just output k The minimum value of
Source of ideas
dls Code 、jiangly Code
Answer key
I was defeated in the game , After the game, I did find a counterexample of the code I wrote , Maybe it would be much better to write a match in the game
First in the race The special sentence was dropped n=1 and n=2, Then I played with my hands and found ,
If Fix two points on the tree u、v, It is equivalent to marking the tree u To v This chain , The points on this chain can be uniquely determined ,
To the point on this chain , If there is only one with the same depth , It can also be uniquely identified , If it is larger than one, it cannot be marked ,
There is no obvious greedy strategy , So consider tree dp,dp[i] It means that only i At least a few points should be marked on this subtree ,
If marked u、v None of them are leaves , Actually, it's not as good as marking u、v They are all excellent leaves , because It can be extended to both ends
And found that , If there is a chain in the subtree , There is a chain that can be marked without points , There is a chance of exemption
Like trees (1-2,2-3,2-4), If it's marked 1 Number point and 3 Number point , actual 4 The number point is not marked , Because it can be uniquely determined
So the race starts with a leaf dfs, Then pair the subtree dp,
Finally, I decided whether the root needs to be marked , And then I hung up , There are still two tips I didn't think of it
Use leaves as roots dfs The problem is , When this root is directly connected by a degree =2 At the time of ,
I have to record this degree =2 In the subtree of the point , Has this exemption opportunity been used ,
If degree =2 The point of is connected by another degree =2 The point of , You have to think recursively ,
therefore , actually 2 The degree point is to be shrunk , This determines the writing method of this problem :
1. One chain , In fact, only one point at the end of the chain can be determined , Special judgment
2. From a certain degree >=3 It's the beginning of dfs( If from the leaves / degree =2 It's the beginning of dfs There will be the above recursive consideration , It will be difficult to write )
Experience
Codeforces Global Round 19 F.Towers( Tree form dp) And this tree dp It might be similar to
At that time, I didn't expect to put the biggest h Point as root to make a tree dp,wa A few rounds and then got through
I think the difficulty may be 2100-2400 Between ,dp We still need to play with more hands and find more properties , And practice shooting to find counterexamples
Code 1
Code 1 According to the competition wa The code changed , Actually, it can be written more succinctly , Such as code 2 Shown
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long ll;
const int N=2e5+10,INF=0x3f3f3f3f;
int t,n,m,x,y,dp[N],col[N],rt,mx;
bool link[N];
vector<int>e[N];
void dfs(int u,int fa){
dp[u]=0;
link[u]=1;
int son=0;
bool haslinkson=0;
for(auto &v:e[u]){
if(v==fa)continue;
dfs(v,u);
dp[u]+=dp[v];
son++;
link[u]&=link[v];
haslinkson|=link[v];
}
if(son>1)link[u]=0;
if(!son)dp[u]=1;
if(son>1 && haslinkson)dp[u]--;
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=1;i<=n;++i){
e[i].clear();
}
mx=0;
for(int i=1;i<n;++i){
scanf("%d%d",&x,&y);
e[x].push_back(y);
e[y].push_back(x);
mx=max(mx,(int)e[x].size());
mx=max(mx,(int)e[y].size());
}
if(n==1 || n==2){
printf("%d\n",n-1);
continue;
}
if(mx<=2){
puts("1");
continue;
}
int ans=n;
for(int i=1;i<=n;++i){
if(((int)e[i].size())==3){
rt=i;
dfs(rt,-1);
ans=min(ans,dp[rt]);
break;
}
}
printf("%d\n",ans);
}
return 0;
}
/*
888
8
1 2
2 3
2 4
3 5
3 6
4 7
4 8
*/Code 2
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long ll;
const int N=2e5+10,INF=0x3f3f3f3f;
int t,n,m,x,y,dp[N],mx;
vector<int>e[N];
void dfs(int u,int fa){
dp[u]=0;
int link=0;
for(auto &v:e[u]){
if(v==fa)continue;
dfs(v,u);
dp[u]+=dp[v];
if(!dp[v])link++;
}
dp[u]+=max(link-1,0);
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=1;i<=n;++i){
e[i].clear();
}
mx=0;
for(int i=1;i<n;++i){
scanf("%d%d",&x,&y);
e[x].push_back(y);
e[y].push_back(x);
mx=max(mx,(int)e[x].size());
mx=max(mx,(int)e[y].size());
}
if(n==1 || n==2){
printf("%d\n",n-1);
continue;
}
if(mx<=2){
puts("1");
continue;
}
for(int i=1;i<=n;++i){
if(((int)e[i].size())>=3){
dfs(i,-1);
printf("%d\n",dp[i]);
break;
}
}
}
return 0;
}
/*
888
8
1 2
2 3
2 4
3 5
3 6
4 7
4 8
*/边栏推荐
- 做题笔记3(二分查找)
- Best cow fences solution
- 深入理解 DeepSea 和 Salt 部署工具 – Storage6
- How to use fail2ban to protect WordPress login page
- Educational codeforces round 126 (rated for Div. 2) f.teleporters (two sets and two points)
- 概率论与数理统计第一章
- PostgreSQL weekly news - July 20, 2022
- MySQL安装教程
- Binary representation of negative integers and floating point numbers
- Unity shader transparent effect
猜你喜欢

【深度学习】:《PyTorch入门到项目实战》第九天:Dropout实现(含源码)

Re13: read the paper gender and racial stereotype detection in legal opinion word embeddings

【深度学习】:《PyTorch入门到项目实战》第五天:从0到1实现Softmax回归(含源码)

【深度学习】:《PyTorch入门到项目实战》第七天之模型评估和选择(上):欠拟合和过拟合(含源码)

Atcoder regular contest 133 d.range XOR (digital dp+ classification discussion)

【深度学习】:《PyTorch入门到项目实战》第二天:从零实现线性回归(含详细代码)
![[deep learning]: day 9 of pytorch introduction to project practice: dropout implementation (including source code)](/img/19/18d6e94a1e0fa4a75b66cf8cd99595.png)
[deep learning]: day 9 of pytorch introduction to project practice: dropout implementation (including source code)

Comprehensively design an oppe homepage -- page service part

Easypoi --- excel file export

System clock failure of database fault tolerance
随机推荐
[deep learning]: introduction to pytorch to project practice: simple code to realize linear neural network (with code)
Leetcode70 suppose you are climbing stairs. You need n steps to reach the roof. You can climb one or two steps at a time. How many different ways can you climb to the roof?
概率论与数理统计第一章
Round 1A 2022 - Code jam 2022 c.weightlifting (interval DP)
Hikvision responded to the impact of the 'US ban': there are options for the components currently used
做题笔记2(两数相加)
Some notes on how unity objects move
Time complexity
做题笔记3(二分查找)
Round 1C 2022 - Code jam 2022 b.square (Mathematics, thinking)
阿里云 MSE 支持 Go 语言流量防护
The 2021 ICPC ASIA Taipei Regional programming contest L. leadfoot (combinatorics /2-adic assignment function +kummer theorem)
Unity3d shader achieves ablation effect
Semtech推出物联网地理定位解决方案LoRa Edge,首款芯片LR1110现已上市
Semtech launched Lora edge, a geolocation solution for the Internet of things, and the first chip lr1110 is now on the market
MD5 encryption verification
Unity shader procedural texture
Binary representation of negative integers and floating point numbers
在AD中添加差分对及连线
向高通支付18亿美元专利费之后,传华为向联发科订购了1.2亿颗芯片!官方回应